// Ajax.js
function postForm(userid,form,action,targetDiv){
  if(zXmlHttp.isSupported()){
	  //alert("Supported");
	  sendRequest(userid,form,action,targetDiv); 
	  return false;
  } else {
	  //Use $PHP_SELF post
	  //alert("Not Supported");
	  return true;
  }
}
function sendRequest(userid,form,action,targetDiv) {
  //Message while user is waiting  
  changeMessage("Working...",targetDiv);
  var oForm = document.getElementById(form);
  var sBody = getRequestBody(oForm,userid);
  //alert("Body is:"+sBody);  
  var oXmlHttp = zXmlHttp.createRequest();
  oXmlHttp.open("post",action,true);
  oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
  oXmlHttp.onreadystatechange = function () {
    if (oXmlHttp.readyState == 4) {
      if (oXmlHttp.status == 200) {
		//responseText includes ERROR if something goes wrong (or in some cases the sql attempt dies at source)
		if(oXmlHttp.responseText.indexOf("ERROR")>-1){//Error
		  changeMessage(oXmlHttp.responseText,targetDiv);	  
	    } else {//No Error
		  changeMessage(oXmlHttp.responseText,targetDiv);
		  if (oForm.name=="secdmailremoveform"){
		    //Update Current Second Email Div Text
			var current = document.getElementById("currentsecdemail");
            current.innerHTML = "Current Secondary Email address is None.";            
		  } else if (oForm.name=="secdmailchangeform"){
		    //Update Current Second Email Div Text
		    var responseSplit=oXmlHttp.responseText.split(" at ");
		    var current = document.getElementById("currentsecdemail");
            current.innerHTML = "Current Secondary Email address is "+responseSplit[1]+" which is not currently activated.";           
		  } else if (oForm.name=="primmailchangeform"){
		    //Update Current Primary Email Div Text
		    var responseSplit=oXmlHttp.responseText.split(" at ");
		    var current = document.getElementById("currentprimemail");
            current.innerHTML = "Current Primary Email address is "+responseSplit[1]+" which is not currently confirmed.";           
          }
		}
      } else {
        changeMessage("ERROR - " + oXmlHttp.statusText,targetDiv);
      }
    }            
  };
  oXmlHttp.send(sBody);        
}
function getRequestBody(oForm,userid) {
  var aParams = new Array();
  var skipNext = false;
  //var sValue = new String();
  aParams.push("userid="+userid);//Add userid to Post because not in all Forms (not always needed by handler)
  for (var i=0;i<oForm.elements.length;i++) {
  //for (var i=0;i<9;i++) {
	if (skipNext){
      skipNext=false;
	  continue;
	}
    //sValue=oForm.elements[i].value;
	var elementi=oForm.elements[i];
	
	//Radio Button Version
	if((oForm.id=="predictform") && !((i%4)==0)){//Mod 4 screens out deadlines and submit from checked test
      //alert("if "+oForm.elements[i].name+" "+oForm.elements[i].value+" "+oForm.elements[i].checked);
      if (elementi.checked==true){
        //var element = oForm.elements[i];
        var sParam = encodeURIComponent(elementi.name);
        sParam += "=";
        sParam += encodeURIComponent(elementi.value);
        aParams.push(sParam);
	    skipNext=false;//skipNext not needed for radio version 
	  }
	}else{//Dont forget used by other forms too
      //var element = oForm.elements[i];
      var sParam = encodeURIComponent(elementi.name);
      sParam += "=";
      sParam += encodeURIComponent(elementi.value);
      aParams.push(sParam);
	  skipNext=false;//skipNext not needed for radio version 
	}
	
	/*
	//Select Button Version
	if (oForm.name=="predictform" && sValue.length == 2 && (sValue.charAt(0)==sValue.charAt(1))){//Result hasn't changed
	  //skip this one. which is the result which hasn't changed
	  //and skip next one. which is the corresponding deadline	
	  skipNext = true;
	  continue;
	} else {
	  var element = oForm.elements[i];
      var sParam = encodeURIComponent(element.name);
      sParam += "=";
      sParam += encodeURIComponent(element.value);
      aParams.push(sParam);
	  skipNext=false;
    }
	*/
  }           
  return aParams.join("&");        
}
function changeMessage(sMessage,targetDiv) {
  var feedback = document.getElementById(targetDiv);
  //feedback.innerHTML = ": " + sMessage;            
  feedback.innerHTML = sMessage;            
}

/* Deleted Stuff


*/

