/*Checks for New User*/

var xmlHttp

			function newUserCheck()
			{
				//document.getElementById("newUser").submit();
				//alert(document.getElementById("newUserName").value);
//				var i = document.getElementById("newUserName").value;
//				var ii = document.getElementById("newEmail").value;
				
				var salutation = document.getElementById("salutation").selectedIndex;
				// 0-mr,1-mrs,2-ms,3-miss,4-select
				
				var firstName = document.getElementById("firstName").value;
				var lastName = document.getElementById("lastName").value;
				var newUserName = document.getElementById("newUserName").value;
				var newEmail = document.getElementById("newEmail").value;
				var state = document.getElementById("state").selectedIndex;
				var errCheck = 0;
				
				if (salutation==4) {
					document.getElementById("salutationErr").innerHTML=("*");
					errCheck = 1;
				} else {
					document.getElementById("salutationErr").innerHTML=("");
				}

				if (state==7) {
					document.getElementById("stateErr").innerHTML=("*");
					errCheck = 1;
				} else {
					document.getElementById("stateErr").innerHTML=("");
				}

				
				if (firstName=="") {
					document.getElementById("firstNameErr").innerHTML=("*");
					errCheck = 1;
				} else {
					document.getElementById("firstNameErr").innerHTML=("");
				}

				if (lastName=="") {
					document.getElementById("lastNameErr").innerHTML=("*");
					errCheck = 1;
				} else {
					document.getElementById("lastNameErr").innerHTML=("");
				}

				if (newUserName=="") {
					document.getElementById("newUserNameErr").innerHTML=("*");
					errCheck = 1;
				} else {
					document.getElementById("newUserNameErr").innerHTML=("");
				}				

				//newEmail = validate_email(newEmail,"Invalid Email")
				
				if (newEmail=="") {
					document.getElementById("newEmailErr").innerHTML=("*");
					errCheck = 1;
				} else if (validate_email(newEmail,"Sorry that is not a valid e-mail address.")==false) {
					errCheck = 1;
					document.getElementById("newEmailErr").innerHTML=("*");
				}
				
				else {
					document.getElementById("newEmailErr").innerHTML=("");
				}		

				//if (validate_email(newEmail,"Not a valid e-mail address!")==false) {
				//	alert("good");
				//}
				

				
				if (errCheck==0) {
					//alert(newUserName+firstName+lastName+newEmail+state+salutation);
					checkAll(newUserName,firstName,lastName,newEmail,state,salutation);
					
					//document.getElementById("newUser").innerHTML=("<br><br>User Request Submitted.<br><br>Please Check your email for your password");
				} else {
					//alert("Error with new login details");
				}

			}


function checkUser(str,email)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="NewUser.asp";
url=url+"?u="+str;
url=url+"&e="+email;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function checkAll(uname,fname,lname,email,state,sal)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="NewUser.asp";
url=url+"?uname="+uname;
url=url+"&fname="+fname;
url=url+"&lname="+lname;
url=url+"&e="+email;
url=url+"&sta="+state;
url=url+"&sal="+sal;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function loginCheck() {
	usr = document.getElementById("log").value;
	pwd = document.getElementById("pwd").value;
	//if (login <> "" && pwd <> "") {
		doLogin(usr,pwd);
	//}
		
}

var xmlHttpLogin

function doLogin(usr,pwd)
{ 
xmlHttpLogin=GetXmlHttpObject();
if (xmlHttpLogin==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="login.asp";
url=url+"?u="+usr;
url=url+"&p="+pwd;
url=url+"&sid="+Math.random();
xmlHttpLogin.onreadystatechange=loginChecked;
xmlHttpLogin.open("GET",url,true);
xmlHttpLogin.send(null);
}

function doLogout()
{ 
xmlHttpLogin=GetXmlHttpObject();
if (xmlHttpLogin==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="login.asp";
url=url+"?logout=1";
url=url+"&sid="+Math.random();
xmlHttpLogin.onreadystatechange=loginChecked;
xmlHttpLogin.open("GET",url,true);
xmlHttpLogin.send(null);
}

function loginChecked() {
	if (xmlHttpLogin.readyState==4) { 
	
		if (xmlHttpLogin.responseText == "Login OK") {
		//alert("You are Now Logged In");
		//document.getElementById("UserNameTab").innerHTML=("u"+u);
		window.location.reload();
		} 
		
		if (xmlHttpLogin.responseText == "Invalid Login") {
		alert("Login Failed");
		} 

		if (xmlHttpLogin.responseText == "Logged Out") {
		//alert("You have been logged Out.");
		window.location.reload();
		} 

		
		//else {
		//alert("Login Failed");
		//}
	}
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
//enable this for debug output
//document.getElementById("userExist").innerHTML=xmlHttp.responseText;

if (xmlHttp.responseText == "Invalid Character") {
	alert("There may be an Invalid Character in one of the fields.");
	//document.getElementById("newUser").innerHTML=("<br><br>User Request Submitted.<br><br>Please Check your email for your password");
	//$('#HideMe').hide();
	}

// If the response is : "New User Added" then, disable the form.
if (xmlHttp.responseText == "Exists or Invalid") {
	alert("The username you have selected may already exist, please try another one.");
	//document.getElementById("newUser").innerHTML=("<br><br>User Request Submitted.<br><br>Please Check your email for your password");
	//$('#HideMe').hide();
	} 

if (xmlHttp.responseText == "New User Added") {
	document.getElementById("newUser").innerHTML=("<br><span style='color:#ffffff;font-weight:bold;'>Thanks!</span><br><br>Your registration was successful.<br><br>Please Check your email for your password");	
	} 
	
/* for stock & company lookup results */	
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	
}
}

/*Checks for New User*/



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 validate_email(field,alerttxt)
{
with (field)
{
apos=field.indexOf("@");
dotpos=field.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}

function submitonEnter(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == "13") {
		loginCheck();
	}
}