function Validate_Input(val){
 	if(val.length==0){ 
		return false; 
 	}
	return true;
}
function Validate_EmailInput(res){
	if(res.length==0){ 
	  return false; 
	}else{
		var str = res;
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		if (!reg1.test(str) && reg2.test(str)) {
			return true;
		}else{
			return false; 
		}
	  return false;
	 }
}
function RegisterUser(){
	document.getElementById("td_err").innerHTML='';
	document.getElementById("td_username").style.color='005291';
	document.getElementById("td_password").style.color='005291';
	document.getElementById("td_vpassword").style.color='005291';
	document.getElementById("td_firstname").style.color='005291';
	document.getElementById("td_lastname").style.color='005291';
	document.getElementById("td_gender").style.color='005291';
	document.getElementById("td_email").style.color='005291';
	document.getElementById("td_phone").style.color='005291';
	document.getElementById("td_country").style.color='005291';
	document.getElementById("td_state").style.color='005291';
	document.getElementById("td_city").style.color='005291';
	document.getElementById("td_degrees").style.color='005291';
	document.getElementById("td_institution").style.color='005291';
	
	var username=document.getElementById("username").value;
	var npassword=document.getElementById("password").value;
	var vpassword=document.getElementById("vpassword").value;
	var firstname=document.getElementById("firstname").value;
	var lastname=document.getElementById("lastname").value;
	var gender=document.getElementById("gender").value;
	var email=document.getElementById("email").value;
	var phone=document.getElementById("phone").value;
	var country=document.getElementById("country").value;
	var state=document.getElementById("state").value;
	var city=document.getElementById("city").value;
	var degrees=document.getElementById("degrees").value;
	var institution=document.getElementById("institution").value;
	var x=0;
	if(!Validate_Input(username)){
		document.getElementById("td_username").style.color='red';
		x=1;
	}
	if(!Validate_Input(npassword)){
		document.getElementById("td_password").style.color='red';
		x=2;
	}
	if(!Validate_Input(vpassword)){
		document.getElementById("td_vpassword").style.color='red';
		x=3;
	}
	if(!Validate_Input(firstname)){
		document.getElementById("td_firstname").style.color='red';
		x=4;
	}
	if(!Validate_Input(lastname)){
		document.getElementById("td_lastname").style.color='red';
		x=5;
	}
	if(!Validate_Input(gender)){
		document.getElementById("td_gender").style.color='red';
		x=6;
	}
	if(!Validate_EmailInput(email)){
		document.getElementById("td_email").style.color='red';
		x=7;
	}
	if(!Validate_Input(phone)){
		document.getElementById("td_phone").style.color='red';
		x=8;
	}
	if(!Validate_Input(country)){
		document.getElementById("td_country").style.color='red';
		x=9;
	}
	if(!Validate_Input(city)){
		document.getElementById("td_city").style.color='red';
		x=11;
	}
	if(!Validate_Input(degrees)){
		document.getElementById("td_degrees").style.color='red';
		x=12;
	}
	if(!Validate_Input(institution)){
		document.getElementById("td_institution").style.color='red';
		x=13;
	}
	if(npassword!=vpassword){
		document.getElementById("td_password").style.color='red';
		document.getElementById("td_vpassword").style.color='red';
		x=14;
	}
	if(x>0){
		document.getElementById("td_err").style.color='red';
		document.getElementById("td_err").innerHTML='<b>Please Fill Missing Data</b>';
		return false;
	}
	x_users_CheckUser(username,RegisterUser_res);
	return false;
}
function RegisterUser_res(res){
	if(res==true){
		document.getElementById("td_err").style.color='red';
		document.getElementById("td_err").innerHTML='<b>Username Exists Please Select An Other One</b>';
		return false;
	}else{
		document.getElementById("reg_input").value='Please Wait';
		document.getElementById("reg_input").disabled=true;
	var username=document.getElementById("username").value;
	var npassword=document.getElementById("password").value;
	var vpassword=document.getElementById("vpassword").value;
	var firstname=document.getElementById("firstname").value;
	var lastname=document.getElementById("lastname").value;
	var gender=document.getElementById("gender").value;
	var email=document.getElementById("email").value;
	var phone=document.getElementById("phone").value;
	var country=document.getElementById("country").value;
	var state=document.getElementById("state").value;
	var city=document.getElementById("city").value;
	var degrees=document.getElementById("degrees").value;
	var institution=document.getElementById("institution").value;
		x_RegisterUser(username,npassword,firstname,lastname,email,phone,state,'','','',country,city,gender,degrees,institution,RegisterUser_Display);
	}
}
function RegisterUser_Display(res){
	if(res==false){
		document.getElementById("td_err").style.color='red';
		document.getElementById("td_err").innerHTML='<b>Registration Error Please Try Again Later</b>';
		return false;
	}else{
		document.location='reg-thanks.php';
		return false;
	}
}
function Validate_Username(NForm){
 	NForm.username.focus();
 	if(NForm.username.value.length==0){ 
		alert("You must enter Username"); 
		return false; 
 	}
	return true;
}
function Validate_Password(NForm){
 	NForm.password.focus();
 	if(NForm.password.value.length==0){ 
		alert("You must enter Password"); 
		return false; 
 	}
	return true;
}
function Validate_Login(NForm){
	return Validate_Username(NForm) &&
			Validate_Password(NForm) &&
			true;
}
function Validate_ChangePassword(NForm){
	return Validate_OldPassword(NForm) &&
			Validate_Password(NForm) &&
			Validate_VerifyPassword(NForm) &&
			true;
}
function Validate_OldPassword(NForm){
 	NForm.opassword.focus();
 	if(NForm.opassword.value.length==0){ 
	  	alert("You must enter Old Password"); 
  		return false; 
 	}
	  return true;
}
function Validate_VerifyPassword(NForm){
	NForm.vpassword.focus();
	if(NForm.password.value!=NForm.vpassword.value){
		alert("Please Verify your Password.");
		return false;
	}
	return true;
}
