var xmlHttp;
var searchstring;


function check_availabilityemail(obj){
	
	if(check_email(obj))
		return false;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	 var url="check_availability_email.php";
	 url=url+"?email="+obj.value;
	 xmlHttp.onreadystatechange=stateChangedemail
	 xmlHttp.open("GET",url,true)
	 xmlHttp.send(null)	 
}

function check_availability(obj){
	
	if(validate_username(obj))
		return false;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	 var url="check_username_availability.php";
	 url=url+"?username="+obj.value;
	 xmlHttp.onreadystatechange=stateChanged
	 xmlHttp.open("GET",url,true)
	 xmlHttp.send(null)	 
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 searchstring=xmlHttp.responseText;
 html=xmlHttp.responseText;
 document.getElementById("display_availability").innerHTML=html;
 
 if(html.indexOf('cross.gif')==-1)
 	document.getElementById("username").style.border='1px solid #A0BABB';
 else
 	document.getElementById("username").style.border='1px solid #FF0000';
 } 
}

function stateChangedemail() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 searchstring=xmlHttp.responseText;
 html=xmlHttp.responseText;
 document.getElementById("check_email").innerHTML=html;
 
 if(html.indexOf('cross.gif')==-1)
 	document.getElementById("email").style.border='1px solid #A0BABB';
 else
 	document.getElementById("email").style.border='1px solid #FF0000';
 } 
}



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_field(obj,div){
	if(is_empty(obj.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Can not be empty</font>'; 
		document.getElementById(div).innerHTML=html;
		obj.style.border="1px solid #FF0000";
		return true;
	}
	
	if(!is_number(obj.value) && div=="zipdiv"){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must be a number</font>'; 
		document.getElementById(div).innerHTML=html;
		obj.style.border="1px solid #FF0000";
		return true;
	}
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById(div).innerHTML=html;
	obj.style.border="1px solid #A0BABB";
	return false;
}




function check_repassword(repassword){
	password=document.getElementById("password");
	
	if(has_only_spaces(repassword.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Spaces not allowed.</font>'; 
		document.getElementById("check_repassword").innerHTML=html;
		repassword.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(is_empty(repassword.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Enter password again.</font>'; 
		document.getElementById("check_repassword").innerHTML=html;
		repassword.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(has_spaces(repassword.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Can not contain spaces</font>'; 
		document.getElementById("check_repassword").innerHTML=html;
		repassword.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(repassword.value.length<6){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must contain 6 chars</font>'; 
		document.getElementById("check_repassword").innerHTML=html;
		repassword.style.border="1px solid #FF0000";
		return true;	
	}
	
	if(password.value!=repassword.value){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must be as password field</font>'; 
		document.getElementById("check_repassword").innerHTML=html;
		repassword.style.border="1px solid #FF0000";
		return true;
	}
	
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById("check_repassword").innerHTML=html;
	repassword.style.border="1px solid #A0BABB";
	return false;
}

function check_password(password){
	
	
	if(has_only_spaces(password.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Spaces not allowed.</font>'; 
		document.getElementById("check_password").innerHTML=html;
		password.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(is_empty(password.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Enter password.</font>'; 
		document.getElementById("check_password").innerHTML=html;
		password.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(has_spaces(password.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Can not contain spaces</font>'; 
		document.getElementById("check_password").innerHTML=html;
		password.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(password.value.length<6){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must contain 6 chars</font>'; 
		document.getElementById("check_password").innerHTML=html;
		password.style.border="1px solid #FF0000";
		return true;	
	}
	
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById("check_password").innerHTML=html;
	password.style.border='1px solid #A0BABB';
	return false;
}

function validate_username(user_name){
	
	
	if(is_empty(user_name.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Enter user name.</font>'; 
		document.getElementById("display_availability").innerHTML=html;
		user_name.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(has_spaces(user_name.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Can not contain spaces.</font>'; 
		document.getElementById("display_availability").innerHTML=html;
		user_name.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(!is_alpha_numeric(user_name.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Allowed chars A-Z,a-z,0-9</font>'; 
		document.getElementById("display_availability").innerHTML=html;
		user_name.style.border="1px solid #FF0000";
		return true;
		
	}
	
	if(user_name.value.length<6){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must contain 6 chars</font>'; 
		document.getElementById("display_availability").innerHTML=html;
		user_name.style.border="1px solid #FF0000";
		return true;	
	}
	

	
	
}

function check_email(email){
	if(!is_email(email.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Invalid email address</font>'; 
		document.getElementById("check_email").innerHTML=html;
		email.style.border="1px solid #FF0000";
		return true;
	}
	

}
	

function check_option_selected(obj,div){
	if(obj[obj.selectedIndex].value=="0"){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Please select a value</font>'; 
		document.getElementById(div).innerHTML=html;
		obj.style.border="1px solid #FF0000";
		return true;	
	}
	
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById(div).innerHTML=html;
	obj.style.border="1px solid #A0BABB";
	return false;
	
}

function check_oldpassword(password){
	
	
	if(is_empty(password.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Enter existing password.</font>'; 
		document.getElementById("check_oldpassword").innerHTML=html;
		password.style.border="1px solid #FF0000";
		return true;
		
	}
	
	
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById("check_oldpassword").innerHTML=html;
	password.style.border='1px solid #A0BABB';
	return false;
}

function validate_phone(obj,div){
	
		
	if(is_empty(obj.value)){
		document.getElementById(div).innerHTML='';
		obj.style.border="1px solid #A0BABB";
		return false;
	}
	
	if(!is_number(obj.value)){
		html='<font color="RED" size=1"><img src="images/cross.gif" height="15" width="15">Must be a number</font>'; 
		document.getElementById(div).innerHTML=html;
		obj.style.border="1px solid #FF0000";
		return true;
	}
	html='<img	src="images/tick.gif" height="15" width="15" alt="ok">';
	document.getElementById(div).innerHTML=html;
	obj.style.border="1px solid #A0BABB";	
	return false;
}





function validateform(){

	var error=0;
	
	if(validate_username(document.getElementById("username")))
		error=1;
	if(check_password(document.getElementById("password")))
		error=1;											  											  
	
	if(check_repassword(document.getElementById("repassword")))
		error=1;
	
	if(validate_field(document.getElementById("firstname"),"firstnamediv"))
		error=1;
	if(validate_field(document.getElementById("lastname"),"lastnamediv"))
		error=1;
	if(validate_field(document.getElementById("city"),"citydiv"))
		error=1;	
	if(validate_field(document.getElementById("zip"),"zipdiv"))
		error=1;		
	if(check_email(document.getElementById("email")))
		error=1;
	if(check_option_selected(document.getElementById("state"),"statediv"))
		error=1;
	if(check_option_selected(document.getElementById("usertype"),"usertypediv"))
		error=1;
		
	if(validate_phone(document.getElementById("homephone"),"homephonediv"))
		error=1	;	
		
	if(validate_phone(document.getElementById("cellphone"),"cellphonediv"))
		error=1	;
		
	if(validate_phone(document.getElementById("workphone"),"workphonediv"))
		error=1	;	
		
	if(error)
		return false;
	else
		return true;
}

function validate_form_change_password(){
 var error=0;
	if(check_oldpassword(document.getElementById("oldpassword")))
		error=1;
		
	if(check_password(document.getElementById("password")))
		error=1;											  											  
	
	if(check_repassword(document.getElementById("repassword")))
		error=1;
	
	if(error)
		return false;
	else
		return true;
}

function clear_all_change_password(){
	
	document.getElementById("check_oldpassword").innerHTML='';
	document.getElementById("oldpassword").style.border='1px solid #A0BABB';
	document.getElementById("check_password").innerHTML='';
	document.getElementById("password").style.border='1px solid #A0BABB';
	document.getElementById("check_repassword").innerHTML='';
	document.getElementById("repassword").style.border='1px solid #A0BABB';
}
function clear_all(){
	
	
	
	document.getElementById("firstname").style.border='1px solid #A0BABB';
	document.getElementById("firstnamediv").innerHTML='';
	//document.getElementById("firstname").style.border='1px solid #A0BABB';
	
	document.getElementById("lastname").style.border='1px solid #A0BABB';
	document.getElementById("lastnamediv").innerHTML='';
	
	document.getElementById("city").style.border='1px solid #A0BABB';
	document.getElementById("citydiv").innerHTML='';
	
	document.getElementById("zip").style.border='1px solid #A0BABB';
	document.getElementById("zipdiv").innerHTML='';	

	//document.getElementById("phone").style.border='';
	//document.getElementById("phonediv").innerHTML='';
	
	document.getElementById("email").style.border='1px solid #A0BABB';
	document.getElementById("check_email").innerHTML='';	
	
	document.getElementById("username").style.border='1px solid #A0BABB';
	document.getElementById("display_availability").innerHTML='';	
	
	document.getElementById("password").style.border='1px solid #A0BABB';
	document.getElementById("check_password").innerHTML='';
	
	document.getElementById("repassword").style.border='1px solid #A0BABB';
	document.getElementById("check_repassword").innerHTML='';
	
	document.getElementById("workphone").style.border='1px solid #A0BABB';
	document.getElementById("workphonediv").innerHTML='';
	
	document.getElementById("cellphone").style.border='1px solid #A0BABB';
	document.getElementById("cellphonediv").innerHTML='';
	
	document.getElementById("homephone").style.border='1px solid #A0BABB';
	document.getElementById("homephonediv").innerHTML='';
	
	document.getElementById("usertype").style.border='1px solid #A0BABB';
	document.getElementById("usertypediv").innerHTML='';
	
	document.getElementById("state").style.border='1px solid #A0BABB';
	document.getElementById("statediv").innerHTML='';
	
				
	//document.getElementById("password").style.border='';
}
