// JavaScript Document
var sleepSec = 0;
var df= document.page1;
var Images_Path = 'images';
var ErrorDetails = new Array();
var alreadyFocussed = false;
var fd_tout = 10000;
var arr_opacThread = {}; // prevent multiple parallel threads on "correct" img
var exp_fresher = '99';
var not_pursuing_grad = '1';
var requester = null;
//var iframe = rm_getFrame();
var max_email_disp_len = 25;
var spamChkEmailAjaxObj = null;
var arrayChangedElements = Array();



String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function gbi(eleId)
{
	
	try { return document.getElementById(eleId); } catch(e) { return null; }
}

//Email address checking
function v_EMAIL(objid,errordiv,statusimage,lableid,headname,ob) {
 var err = true;
        var email = gbi(objid);
        var error_email = gbi(errordiv);
        var status_email = gbi(statusimage);
        var params = new Array();
        
        email.value = email.value.trim();
        if ( email.value.length == 0 ) {
                params = {'ErrDivObj': error_email, "ErrorMsg": "Please Specify Your Email Id.", "EleToFocus":email, "StatusObj":status_email, "ob": ob, "HName": "Email", "label":lableid };
                showErrMsg(params);
                err = false;
        } else if ( email.value.length < 6 || !isValidEmail(email.value)) {
                params = {'ErrDivObj': error_email, "EleToFocus":email, "StatusObj":status_email, "ob": ob, "HName": "Email" , "label":lableid};
                if ( email.value.length > max_email_disp_len ) {
                        params['ErrorMsg'] = "< " + email.value.substring(0, max_email_disp_len ) + "... > is Not a Valid Email Id. Please Enter a Valid Email Id.";
                } else {
                        params['ErrorMsg'] = "< " + email.value + " > is Not a Valid Email Id. Please Enter a Valid Email Id.";
                }
                showErrMsg(params);
                err = false;
        }
        if ( ob && err ) {
                 hideErrorCSS(objid,errordiv,statusimage,lableid);
                /*CheckEmailAvailability('email',ob);*/
				status_email.src = Images_Path+"/correct.gif";
                status_email.style.display = '';
                opacity(statusimage, 100, 0, fd_tout);
				
        }
		return err;
}
function isValidEmail(Email)
{
        var pattern = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/;

        return  pattern.test(Email);
}



//combo fld validation function.
function v_combofld(objid,errordiv,statusimage,lableid,headname,ob,errormsg) {
        var err = true;
        var combofld = gbi(objid);
        var error_combofld = gbi(errordiv);
        var status_combofld = gbi(statusimage);
        error_combofld.style.display = "none";
        status_combofld.style.display = "none";

        if(combofld.value == "0") 
		{
                err = false;
                showErrMsg({"ErrDivObj":error_combofld, "ErrorMsg":errormsg, "EleToFocus":combofld, "StatusObj":status_combofld, "ob":ob, "HName":headname,"label":lableid});
				
        }

        if (ob && err) 
		{
                hideErrorCSS(objid,errordiv,statusimage,lableid);
				status_combofld.src = Images_Path+"/correct.gif";
                status_combofld.style.display = "";
                opacity(statusimage, 100, 0, fd_tout);
        }
		return err;
}

// txt fld validation function.
function v_txtfld(objid,errordiv,statusimage,lableid,headname,ob,errormsg,minlength,charallow) {
        var err = true;
        var txtfld= gbi(objid);
		//alert(txtfld.value)
		var error_txtfld = gbi(errordiv);
        var status_txtfld = gbi(statusimage);
        error_txtfld.style.display = "none";
        status_txtfld.style.display = "none";
      	//alert(txtfld.value.substr(0,1));
        
	//var fullMphone = txtfld.value;
	if (txtfld.value.length < minlength)
	 {
					err = false;
        	        showErrMsg({"ErrDivObj":error_txtfld, "ErrorMsg":headname+' Must be Minimum '+minlength+' Characters Long.', "EleToFocus":txtfld, "StatusObj":status_txtfld, "ob":ob, "HName":headname, "label":lableid});
					
    }
	
     else if ( isValidChar(txtfld.value,charallow) == false  ) 
	 {
            params = { "ErrDivObj": error_txtfld, "ErrorMsg": errormsg, "EleToFocus":txtfld, "StatusObj": status_txtfld, "ob":ob, "HName":headname, "label":lableid }
            showErrMsg(params);
            err = false;
			
	 }  
	 //alert(txtfld.value)
        if (ob && err) {
                txtfld.className = "";
				txtfld.value=txtfld.value.substr(0,1).toUpperCase()+txtfld.value.substr(1,txtfld.value.length);
				if (txtfld.value == "") // don't show tick in case phone no. is empty
                        status_txtfld.src = "";//Images_Path+"/zero.gif";
                else {
                        status_txtfld.src = Images_Path+"/correct.gif";
                        status_txtfld.style.display = "";
                        opacity(statusimage, 100, 0, fd_tout);
                }
        }
		return err;
}
//char validation for txtfld function.
function isValidChar(Name,allochar)
{
		var pattern = allochar;
		return ! pattern.test ( Name );
}

// checking Availibility in database function variables

var txtfld;
var error_txtfld;
var status_txtfld;
var label_txtfld;
var hdname;
var txtfldname;
var error_txtfldname;
var status_txtfldname;
//checking Availibility in database function.
function ChecknameAvailability(objid,errordiv,statusimage,lableid,headname,ob,errormsg,minlength,charallow,sqlstr,calledFromValidate)
{
		//alert(sqlstr);
        txtfld = gbi(objid);
        error_txtfld = gbi(errordiv);
        status_txtfld = gbi(statusimage);
		label_txtfld=lableid;
		hdname=headname;
		txtfldname=objid;
		error_txtfldname=errordiv;
		status_txtfldname=statusimage;
        var params = new Array();
        var err = false;
        error_txtfld.innerHTML = "";
      
        if ( txtfld.value.length < minlength )
		 { 
               err = true;
        	   showErrMsg({"ErrDivObj":error_txtfld, "ErrorMsg":headname+' Must be Minimum '+minlength+' Characters Long.', "EleToFocus":txtfld, "StatusObj":status_txtfld, "ob":ob, "HName":headname, "label":lableid});
                
        }
        else if ( isValidChar(txtfld.value,charallow) == false  ) {
              params = { "ErrDivObj": error_txtfld, "ErrorMsg": errormsg, "EleToFocus":txtfld, "StatusObj": status_txtfld, "ob":ob, "HName":headname, "label":lableid }
            showErrMsg(params);
            err = true;
	} else {
	
		status_txtfld.src = Images_Path + '/progressbar.gif';
			status_txtfld.style.display = "none";
	}

	if ( calledFromValidate)
	{
		if ( err)
		{
			alreadyFocussed = true;
			txtfld.focus();
		}
	}
	else
	{
		if ( ob && !err ) {
			 txtfld.className = "";
			 txtfld.value=txtfld.value.substr(0,1).toUpperCase()+txtfld.value.substr(1,txtfld.value.length);
			if ( ! this.verified )  {
				error_txtfld.innerHTML = "Checking availability of name.....";
				error_txtfld.style.display = "none";
				if ( requester != null && requester.readyState != 0 && requester.readyState != 4 )
					requester.abort();
				try  {
					requester = new XMLHttpRequest();
				}catch (error)  {
					try  {
						requester = new ActiveXObject("Microsoft.XMLHTTP");
					}catch (error)  {
						requester = null;
						return ;
					}
				}
				var str=sqlstr;
				requester.onreadystatechange = onReadyStateChangeUsername;
				requester.open("GET", "checkdata.asp?selqry=" + str);
				requester.send(null);
			}
		}
	}
        return true;
}
function onReadyStateChangeUsername()
{
if (requester.readyState==4)
  {
	//alert(requester.readyState);  
  	if(requester.responseText!="Name Available.")
		 {
    	error_txtfld.innerHTML = "";
		 //error_txtfld.style.display = "";
		  params = { "ErrDivObj": error_txtfld, "ErrorMsg": requester.responseText, "EleToFocus":txtfld, "StatusObj": status_txtfld, "ob":1, "HName":hdname, "label":label_txtfld }
                
                showErrMsg(params);
  }
   else
		 {
			
		 	error_txtfld.innerHTML=requester.responseText;
			hideErrorCSS(txtfldname,error_txtfldname,status_txtfldname,label_txtfld);
		 	status_txtfld.src = Images_Path+"/correct.gif";
            status_txtfld.style.display = "";
            opacity(status_txtfldname, 100, 0, fd_tout);
		 }
	}
}

function hideErrorCSS(objId, ErrMsgDivId, StatusObjId, labelID) {
//try{

        var obj = gbi(objId)
        obj.className = '';
        gbi(ErrMsgDivId).style.display = 'none';
        gbi(StatusObjId).style.display = 'none';
	//alert(obj.parentNode.name);
        if (obj.parentNode && obj.parentNode.name && obj.parentNode.name == "borderele") {
                obj.parentNode.className = "";
        }
	gbi(labelID).className = ''; 
        return true;
//} catch(e) { }
}
function showErrMsg(arr_params) {
//try{
        var i;
        arr_params['ErrDivObj'].style.display = "";
        arr_params['ErrDivObj'].innerHTML ="<img src='images/error.gif'>"+ 	arr_params['ErrorMsg'];
	arr_params['ErrDivObj'].className = "subCont errortxt";

        if (arr_params['EleToFocus']) {
                if (arr_params['EleToFocus']["isArray"]) {
                        for (i=0; i<arr_params['EleToFocus'].length; ++i) {
                                arr_params['EleToFocus'][i].className = "error";
				gbi(arr_params['label'][i]).className = 'lerror';
				if ( arr_params['EleToFocus'][i].parentNode.name == 'borderele')
					arr_params['EleToFocus'][i].parentNode.className = 'error';
                        }
                } else {
                        arr_params['EleToFocus'].className = "error";
						
			arr_params['label'].className = 'lerror';
			if ( arr_params['EleToFocus'].parentNode.name == 'borderele')
			{
				arr_params['EleToFocus'].parentNode.className = 'error';
			}
		}
        }
        if (arr_params['StatusObj']) {
                if (arr_params['StatusObj']["isArray"]) {
                        for (i=0; i<arr_params['StatusObj'].length; ++i) {
                                arr_params['StatusObj'][i].style.display = "";
				arr_params['StatusObj'][i].src = Images_Path+"/spacer.gif";	
                                opacity(arr_params['StatusObj'][i].id, 99, 100, 1);
                        }
                }
                else {
                        arr_params['StatusObj'].style.display = "";
                        arr_params['StatusObj'].src = Images_Path+"/spacer.gif";		
                        opacity(arr_params['StatusObj'].id, 99, 100, 1);
                }
        }
        if (!arr_params['ob']) {
                ErrorDetails[arr_params["HName"]] = 1;
                if (arr_params['EleToFocus']) {
                        if (!alreadyFocussed) {
                                alreadyFocussed = true;
                                if (arr_params['EleToFocus']["isArray"]) {
                                        arr_params['EleToFocus'][0].focus();
                                }
                                else {
                                        arr_params['EleToFocus'].focus();
                                }
                        }
                }
        }
//} catch(e) {alert(e.description + "\n" + arr_params['HName'])}
}
function opacity(id, opacStart, opacEnd, millisec, idx) 
{
        if (idx == undefined) {
                ele = gbi(id);
                idx = "";
        }
        else {
                tmpele = document.getElementsByName(id);
                ele = tmpele[idx];
        }


    if (arr_opacThread[id+idx])
        return;
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
    var i=0; // very imp line - don't remove this
    var processingCorrect = (ele.src.search("correct") != -1);
    if (processingCorrect)
        arr_opacThread[id+idx] = 1;
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i-=2) {
            setTimeout("changeOpac(" + i + ",'" + id + "','" + idx + "')",(timer * speed));
            timer++;
        }
            if (processingCorrect)
                setTimeout("arr_opacThread['"+id+idx+"'] = 0",(timer * speed))
    } else if(opacStart < opacEnd) {
      for(i = opacStart; i <= opacEnd; i+=2)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "','" + idx + "')",(timer * speed));
            timer++;
        }
            if (processingCorrect)
                setTimeout("arr_opacThread['"+id+idx+"'] = 0",(timer * speed))
    }
}
function changeOpac(opacity, id, idx) {
        if (idx == "")
                object = gbi(id);
        else {
                tmpobject = document.getElementsByName(id);
                object = tmpobject[idx];
        }
        if (String(object.src).indexOf("erroricon") != -1) // stop the fade out threads in case of error
                opacity = 100;
        object.style.opacity = (opacity / 100);
        object.style.MozOpacity = (opacity / 100);
        object.style.KhtmlOpacity = (opacity / 100);
        object.style.filter = "alpha(opacity=" + opacity + ")";
}

function testing()
{
	savepolling(0);
}

//setting value on hidden feild to set answer.
function setvalue(eleid)
{
	
	if(eleid.checked==true)
	{
		document.getElementById('hidans').value=eleid.value;
	}
}


function loadXMLDoc(dname)
{
	if (window.XMLHttpRequest)
  	{
 		 xhttp=new XMLHttpRequest();
  	}
	else
  	{
 		 xhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	}
		xhttp.open("GET",dname,false);
		xhttp.send();
	return xhttp.responseXML;
} 

function fillpolling(qval,state)
{
	//changing value in xml.
	//alert("hi");
	var xmlDoc=loadXMLDoc("polling.xml");
	
	//x=xmlDoc.getElementsByTagName("super")[qval].childNodes[0];
	//x.nodeValue=parseInt(x.nodeValue)+1;
	
	
	//fetching value.
	//xmlDoc=loadXMLDoc("polling.xml");
	x=xmlDoc.getElementsByTagName("question");
	if(x[qval].childNodes[0].nodeValue==document.getElementById("hidpollques").value)
	{
		t=xmlDoc.getElementsByTagName("total");
		x=xmlDoc.getElementsByTagName("super");
		
		document.getElementById('super').innerHTML=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		document.getElementById('superbar').style.width=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		
		x=xmlDoc.getElementsByTagName("good");
		document.getElementById('good').innerHTML=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		document.getElementById('goodbar').style.width=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		
		x=xmlDoc.getElementsByTagName("normal");
		document.getElementById('normal').innerHTML=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		document.getElementById('normalbar').style.width=parseInt(parseInt(x[qval].childNodes[0].nodeValue)*100/parseInt(t[qval].childNodes[0].nodeValue))+"%";
		
		if(state==0)
		{
		x=xmlDoc.getElementsByTagName("total");
		document.getElementById('tot').innerHTML=x[qval].childNodes[0].nodeValue;
		}
	}
}

//saving selected poll value on click of save button
function savepolling(qval)
{

xmlDoc=loadXMLDoc("polling.xml");
		
	//fetching value.
	//xmlDoc=loadXMLDoc("polling.xml");
	
	x=xmlDoc.getElementsByTagName("question");
	
	if(x[0].childNodes[0].nodeValue==document.getElementById("hidpollques").value)
	{
		x=xmlDoc.getElementsByTagName("super");
		var superstr=x[0].childNodes[0].nodeValue;
				
		x=xmlDoc.getElementsByTagName("good");
		var good=x[0].childNodes[0].nodeValue;
		
		x=xmlDoc.getElementsByTagName("normal");
		var normal=x[0].childNodes[0].nodeValue;
			
		x=xmlDoc.getElementsByTagName("total");
		var total=x[0].childNodes[0].nodeValue;
		//alert(x[0].childNodes[0].nodeValue);
	}
	//alert('hi');
	document.getElementById('pollsave').style.display="none"
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
 	{
  		alert ("Your browser does not support XMLHTTP!");
  		return;
 	} 
	if(document.getElementById('hidans').value=="1")
	{
		superstr=parseInt(superstr)+1;
		total=parseInt(total)+1;
	}
	else if(document.getElementById('hidans').value=="2")
	{
		good=parseInt(good)+1;
		total=parseInt(total)+1;
	}
	else if(document.getElementById('hidans').value=="3")
	{
		normal=parseInt(normal)+1;
		total=parseInt(total)+1;
	}

	document.getElementById('tot').innerHTML=total;
	//alert(document.getElementById('tot').innerHTML);
	var url="pollsave.php?s="+superstr+"&g="+good+"&n="+normal+"&t="+total;
	//alert(url);
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
function stateChanged()
{
if (xmlhttp.readyState==4)
  {
    	//alert(xmlhttp.responseText);
	 	fillpolling(0,1);
  }  
}