var xmlObj=null;
var quesid=0;
var useid=0;
var optype="";
var lastpage="";
var opinid=0;

function validate()
{
	var frm=document.registration;
    if(frm.upass.value!=frm.repass.value)
    {
    	msg+=">> Passwords entered by you do not match! Please Re-enter.\n";
        frm.upass.value="";
        frm.repass.value="";
        frm.upass.focus();
    }
    if(frm.umail.value!=frm.remail.value)
    {
    	msg+=">> Re-entered Email ID do not match! Please Re-enter.\n";
        frm.remail.value="";
        frm.remail.focus();
    }
	else if(frm.umail.value.indexOf("@")<0 || frm.umail.value.indexOf(".com")<0)
		msg+=">> Invalid mail-id!! It should be in 'abc@xyz.com' format.\n";
    msg+=">> You need to select your-\n";
    if(frm.umast.selectedIndex==0)
    	msg+="     * Marital Status\n";
    if(frm.useor.selectedIndex==0)
    	msg+="     * Sexual Orientation\n";
	if(frm.ureli.selectedIndex==0)
    	msg+="     * Religion\n";
    if(frm.uedu.selectedIndex==0)
    	msg+="     * Education\n";
    if(frm.unati.selectedIndex==0)
    	msg+="     * Nationality\n";
    if(frm.ucoun.selectedIndex==0)
    	msg+="     * Country\n";
    if(msg.length>28)
	    return msg;
    else
    	return "";
}

function trim_spaces(val){
if(val=="")return "";
  str=val.split("");
  str2=val.split("");
  for(mj=0;mj<str2.length;mj++){
   if(str2[mj].charCodeAt(0)!=32){
     break;
   }
   else{
    str.shift();
   }
  }
  if(str.length==0||str==null)return "";
  str.reverse();
  str2=str.join("");
  str2=str2.split("");
  for(mj=0;mj<str2.length;mj++){
   if(str2[mj].charCodeAt(0)!=32){
     break;
   }
   else{
    str.shift();
   }
  }
   str.reverse();
   str=str.join("");
    return str;
}

function validateopinion(f)
{
	if(trim_spaces(f.opinion.value).length==0)
	{
		alert('Please enter your opinion in the text area to proceed!');
		f.opinion.focus();
		return false;
	}
}

function disablearea()
{
document.getElementById('opi').disabled=true;
document.getElementById('sub').disabled=true;
}

function enablearea()
{
document.getElementById('opi').disabled=false;
document.getElementById('sub').disabled=false;
}

function giveopinion(oid, qid, uid)
{
	if(uid==null || uid.length==0)
		location.href="index.php?type=errmessage";
	else
	{
		quesid=qid;
		useid=uid;
		opinid=oid;
		xmlObj=getXMLObject();
		xmlObj.onreadystatechange=opstatechange;
		xmlObj.open("GET", "include/process.php?action=isOpinionGiven&uid="+uid+"&qid="+quesid, true);
		xmlObj.send(null);
		//var cnt=document.getElementById('c'+qid);
	}
}

function opstatechange()
{
	if(xmlObj.readyState==4 || xmlObj.readyState=="complete")
	{
		var result=xmlObj.responseText;
		if(result=="yes")
			alert("You have already given an opinion for this question");
		else
		{			var container=document.getElementById('c'+opinid);
			if(container==null)
				container=document.getElementById('q'+quesid);
  			container.innerHTML='<form action="include/process.php" method="post" onSubmit="return validateopinion(this)">&nbsp;&nbsp;<textarea cols=50 rows=5 name="opinion"></textarea><input type="hidden" name="para" value="'+location.href.substring(location.href.lastIndexOf("/")+1)+'"><input type="hidden" name="action" value="saveopinion"><input type="hidden" name="quesid" value="'+quesid+'"><input type="submit" value="Done" class="btn"></form>';
  		}
	}
}

function saveop(type, oid, uid)
{
	if(uid.length==0)
		location.href="index.php?type=errmessage";
	else
	{
  		optype=type;
  		xmlObj=getXMLObject();
  		xmlObj.onreadystatechange=comstatechange;
  		xmlObj.open("GET", "include/process.php?action=isCommentGiven&opt="+type+"&oid="+oid+"&uid="+uid, true);
  		xmlObj.send(null);
	}
}

function comstatechange()
{
	if(xmlObj.readyState==4 || xmlObj.readyState=="complete")
	{
		var result=xmlObj.responseText;
		if(result=="done")
			alert("Thanks, your comment '"+optype+"' has successfully been recorded.");
		else if(result=="opinion")
			alert("You can't comment on your own opinion!");
		else
			alert("You have already commented '"+result+"' on this question!");
		//alert(result);

	}
}

function setOrder(valu)
{
	var url=location.href.substring(location.href.lastIndexOf("/")+1);
	if(url.length==0 || url=="index.php")
		url="?type=home";
	if(url.indexOf("&order")==-1)
		url+="&order="+valu;
	else
	{
		url=url.substring(0, url.indexOf("&order"));
		url+="&order="+valu;
	}
	location.href=url;
}

function getXMLObject()
{
	var xml=null;
	try
	{
		xml=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xml=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xml=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xml;
}
