var xmlHttp;
var secure_id = randomString();
var block = '<p><strong>Error: C.R.I.S. does not recognize that email address.<br />If you believe this is in error, please contact the Body Buster Office.</strong></p>';
var not_found = '<p><strong>Error: That email address was not found in our system.<br />If you believe this is an error, please contact the Body Buster Office.</strong></p>';
var not_set = '<p><strong>You have not set a password.<br /><a href="./cris_reset_password.php?sid='+secure_id+'">Click here to set your password.</a></strong></p>';
var found_email = '<p><strong>Please enter your password.</strong><br />Forgot your password? <a href="./cris_reset_password.php?sid='+secure_id+'">Reset it!</a></p></p>';
var password_match = '<p><strong>PASSWORD MATCH</strong></p>';
var password_nomatch = '<p><strong>ERROR: EMAIL/PASSWORD are not correct.</strong><br />Forgot your password? <a href="./cris_reset_password.php?sid='+secure_id+'">Reset it!</a></p>';
var login_success = 'http://www.bodybusterfitness.com/cris/cris_start.php?sid='+secure_id;

function validateEmail(str,pass)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="./x_mysql/validate_email.php";
	url=url+"?q="+str;
	url=url+"&p="+pass;
	url=url+"&sid="+secure_id;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		if (xmlHttp.responseText == "notfound")
			{
			document.getElementById("txtError").innerHTML=not_found;
			}
		else if (xmlHttp.responseText == "notset")
			{
			document.getElementById("txtError").innerHTML=not_set;
			}
		else if (xmlHttp.responseText == "found")
			{
			document.getElementById("txtError").innerHTML=found_email;
	 		document.getElementById("row_password").style.display='';
			}
		else if (xmlHttp.responseText == "match")
			{
			window.location=login_success;
			}
		else if (xmlHttp.responseText == "nomatch")
			{
			document.getElementById("txtError").innerHTML=password_nomatch;
			}
		else if (xmlHttp.responseText == "block")
			{
			document.getElementById("txtError").innerHTML=block;
			}
	 } 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 32;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

