/*********************************************************************
File:			suValidation.js
Description:	Realtime validation code for Sign-up form
*********************************************************************/

var xmlHttp;
var sURL;
var bEMCheck = false;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function handleEmailStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			handleEmailCheck();
		}
		else {
			alert ("AJAX error!");
		}
	}
}

function checkEmail() {
	var sEmail = document.getElementById("txtEmail").value;
	bEMCheck = false;
	
	if ( echeck(sEmail) ) {
		//email address is valid, check against server
		sURL = "suValidation.php?email=" + sEmail;
		sURL = sURL + "&ts=" + new Date().getTime();
			
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleEmailStateChange;
		xmlHttp.open("GET", sURL, true);
		xmlHttp.send(null);
	} else {
		while (document.getElementById("errEmail").hasChildNodes())
		{
		  document.getElementById("errEmail").removeChild(document.getElementById("errEmail").firstChild);
		}
		document.getElementById("errEmail").appendChild(document.createTextNode("Invalid e-mail address!")); 
	}
}

function handleEmailCheck() {
	var responseXML = xmlHttp.responseXML;
	var sStatus = responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;
	while (document.getElementById("errEmail").hasChildNodes())
	{
	  document.getElementById("errEmail").removeChild(document.getElementById("errEmail").firstChild);
	}
	if (sStatus == "DUPE") {
		document.getElementById("errEmail").appendChild(document.createTextNode("This E-mail address is already in the system, please enter a different one"));
		bEMCheck = false;
	} else {
		bEMCheck = true;
	}
	vFinalCheck();
}

function checkPasswd() {
	var sPass1 = document.getElementById("txtPass1").value;
	var sPass2 = document.getElementById("txtPass2").value;
	
	if (sPass1 != "" && sPass2 != "") {
		while (document.getElementById("errPass").hasChildNodes())
		{
		  document.getElementById("errPass").removeChild(document.getElementById("errPass").firstChild);
		}
		if (sPass1 == sPass2) {
			return true;
		} else {
			document.getElementById("errPass").appendChild(document.createTextNode("Password and Verify Password must match!")); 
			return false;
		}
	} else {
		return false;
	}
}

function vFinalCheck() {
	var sFName = document.getElementById("txtFName").value;
	var sLName = document.getElementById("txtLName").value;
	var sSName = document.getElementById("txtSName").value;
	var bPass = checkPasswd();
		
	//alert(bEMCheck + " -- " + bPass + " -- " + sFName + " -- " + sLName + " -- " + sSName);
	
	if (bEMCheck && bPass && sFName != "" && sLName != "" && sSName != "") {
		vEnableSubmit (true);
	} else {
		vEnableSubmit (false);
	}
}


function vEnableSubmit (bGo) {
	if (bGo) {
		document.getElementById("btnSubmit").removeAttribute("disabled");
	} else {
		document.getElementById("btnSubmit").setAttribute("disabled","true");
	}
}


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}


