// JavaScript Document


function validate2()
{
	
	if(!login_validate()) 		return false;
	if(!password_validate()) 	return false;
	if(!repass_validate())  	return false;
	if(!email_validate())  		return false;
	
	if(!address1_validate())  	return false;
	if(!city_validate())  		return false;
	//if(!state_validate())  		return false;
	if(!country_validate())  	return false;
	if(!postcode_validate())  	return false;
	if(!terms_validate())  		return false;	
	//if(!chk_dup_email(document.getElementById("email").value))  		return false;
	
}



var ajax_ret2 = new sack();

function chk_bl_username2(usrname)
{
	var re = /^[A-Z,a-z,_,.,0-9]*$/;
	if(document.getElementById("login_id").value=='')
	{
		document.getElementById('div_username2').style.height="15px";
		document.getElementById('div_username2').style.color="#FF0000";
		document.getElementById('div_username2').innerHTML = "Please specify username";
	}
	else if(!re.test(document.getElementById("login_id").value)){				
			//alert("Name field should be alphabet only!");
			document.getElementById('div_username2').style.height="15px";
			document.getElementById('div_username2').style.color="#FF0000";
			document.getElementById('div_username2').innerHTML = "Please don't use any special character";
			document.getElementById("login_id").value = document.getElementById("login_id").value.replace(/[^A-Z,a-z,_,.,0-9]/g,"");		
	}
	else
	{
		validate_login2();
		
	}
}




function validate_login2()
{
		var username=document.getElementById("login_id").value;
		ajax_ret2.setVar("usrname", username);
		ajax_ret2.method= 'POST';
		ajax_ret2.requestFile = "chk_user.php";	// Specifying which file to get
		ajax_ret2.onCompletion = SHOW_INSERT2;	// Specify function that will be executed after file has been found
		ajax_ret2.runAJAX();			
}
function SHOW_INSERT2()
{
	var content2 = ajax_ret2.response;
	if(content2 == 0)
	{
		document.formRegistration.login.focus();
		document.getElementById('div_username2').style.height="15px";
		document.getElementById("div_username2").innerHTML = "<font color='#FF0000'>Username <b>"+document.getElementById("login_id").value+"</b> is already in use.</font>";	
		document.formRegistration.login.focus();
	}
	else
	{
		document.getElementById('div_username2').style.height="15px";
		document.getElementById("div_username2").innerHTML = "<font color='#009900'>Username <b>"+document.getElementById("login_id").value+"</b> is available.</font>";	
	}
		
}

//#################################
function chk_dup_email(email)
{
	
		var email=document.getElementById("email").value;
		ajax_ret2.setVar("email", email);
		ajax_ret2.method= 'POST';
		ajax_ret2.requestFile = "chk_email.php";	// Specifying which file to get
		ajax_ret2.onCompletion = SHOW_EMAIL;	// Specify function that will be executed after file has been found
		ajax_ret2.runAJAX();			
}
function SHOW_EMAIL()
{
	
	var content2 = ajax_ret2.response;
	if(content2 == 0)
	{		
		document.getElementById('div_email').style.height="15px";
		document.getElementById("div_email").innerHTML = "<font color='#FF0000'>This email address is already in use.</font>";
		document.formRegistration.email.value ="";
		document.formRegistration.email.focus();
		return false;
	}
	else
	{
		document.getElementById('div_email').style.height="0px";
		document.getElementById("div_email").innerHTML = "";
		return true;
	}
		
}
//################################



function chk_bl_password2()
{
	if(document.formRegistration.password.value=='')
	{
		document.getElementById('div_password2').style.height="15px";
		document.getElementById('div_password2').style.color="#FF0000";
		document.getElementById('div_password2').innerHTML = "Please specify password";
		document.formRegistration.password.focus();	
		alert();
	}
	else
	{
		document.getElementById('div_password2').style.height="0px";
		document.getElementById('div_password2').innerHTML = "";
	}
}

function chk_bl_repassword()
{
	if(document.formRegistration.password.value !=document.formRegistration.repassword.value)
	{
		document.getElementById('div_repassword').style.height="15px";
		document.getElementById('div_repassword').style.color="#FF0000";
		document.getElementById('div_repassword').innerHTML = "password and re-type password are not same";
	}
	else
	{
		document.getElementById('div_repassword').style.height="0px";
		document.getElementById('div_repassword').innerHTML = "";
	}
}


function chk_bl_email()
{
	if(document.formRegistration.email.value =='')
	{
		document.getElementById('div_email').style.height="15px";
		document.getElementById('div_email').style.color="#FF0000";
		document.getElementById('div_email').innerHTML = "email address can't be blank";
	}
	if(!ChkEmail(document.formRegistration.email.value))
	{
		document.getElementById('div_email').style.height="15px";
		document.getElementById('div_email').style.color="#FF0000";
		document.getElementById('div_email').innerHTML = "please specify proper email address";
	}
	else
	{
		document.getElementById('div_email').style.height="0px";
		document.getElementById('div_email').innerHTML = "";
	}
}





function login_validate()
{
	if(document.formRegistration.login.value=='')
	{
		document.getElementById('div_username2').style.height="15px";
		document.getElementById('div_username2').style.color="#FF0000";
		document.getElementById('div_username2').innerHTML = "Please specify username";
		document.formRegistration.login.focus();
		return false;
	}
	else
	{
		document.getElementById('div_username2').style.height="0px";
		document.getElementById('div_username2').innerHTML = "";
		document.formRegistration.password.focus();
		return true;
	}
}

function address1_validate()
{
	if(document.formRegistration.address1.value=='')
	{		
		document.getElementById('div_address1').style.height="15px";
		document.getElementById('div_address1').style.color="#FF0000";
		document.getElementById('div_address1').innerHTML = "Please specify address.";
		document.formRegistration.address1.focus();
		return false;
	}
	else
	{
		document.getElementById('div_address1').style.height="0px";
		document.getElementById('div_address1').innerHTML = "";
		document.formRegistration.country.focus();
		return true;
	}
}


function city_validate()
{
	if(document.formRegistration.city.value=='')
	{		
		document.getElementById('div_city').style.height="15px";
		document.getElementById('div_city').style.color="#FF0000";
		document.getElementById('div_city').innerHTML = "Please specify city.";
		document.formRegistration.city.focus();
		return false;
	}
	else
	{
		document.getElementById('div_city').style.height="0px";
		document.getElementById('div_city').innerHTML = "";
		document.formRegistration.state.focus();
		return true;
	}
}

/*function state_validate()
{
	if(document.formRegistration.state.value=='')
	{		
		document.getElementById('div_state').style.height="15px";
		document.getElementById('div_state').style.color="#FF0000";
		document.getElementById('div_state').innerHTML = "Please specify county.";
		document.formRegistration.state.focus();
		return false;
	}
	else
	{
		document.getElementById('div_state').style.height="0px";
		document.getElementById('div_state').innerHTML = "";
		document.formRegistration.country.focus();
		return true;
	}
}
*/

function country_validate()
{
	if(document.formRegistration.country.value=='')
	{		
		document.getElementById('div_country').style.height="15px";
		document.getElementById('div_country').style.color="#FF0000";
		document.getElementById('div_country').innerHTML = "Please specify country.";
		document.formRegistration.country.focus();
		return false;
	}
	else
	{
		document.getElementById('div_country').style.height="0px";
		document.getElementById('div_country').innerHTML = "";
		document.formRegistration.post_code.focus();
		return true;
	}
}

function terms_validate()
{
	if(document.formRegistration.accept.checked == 1)
	{
		return true;
	}
	else
	{
		alert("Please tick the box for User Agreement");
		document.formRegistration.accept.focus();
		return false; 
	}	

}


function password_validate()
{
	if(document.formRegistration.password.value=='')
	{
		document.getElementById('div_password2').style.height="15px";
		document.getElementById('div_password2').style.color="#FF0000";
		document.getElementById('div_password2').innerHTML = "Please specify password";
		document.formRegistration.password.focus();
		return false;
	}
	else if((document.formRegistration.password.value.length > 10) || (document.formRegistration.password.value.length < 6))
	{			
		document.getElementById('div_password2').style.height="15px";
		document.getElementById('div_password2').style.color="#FF0000";
		document.getElementById('div_password2').innerHTML = "password must be within 6 to 10 characters";
		document.formRegistration.password.focus();
		return false;
	}
	else
	{
		document.getElementById('div_password2').style.height="0px";
		document.getElementById('div_password2').innerHTML = "";
		document.formRegistration.repassword.focus();
		return true;
	}
}


function repass_validate()
{
	if(document.formRegistration.password.value !=document.formRegistration.repassword.value)
	{
		document.getElementById('div_repassword').style.height="15px";
		document.getElementById('div_repassword').style.color="#FF0000";
		document.getElementById('div_repassword').innerHTML = "password and re-type password are not same";
		document.formRegistration.repassword.focus();
		return false;
	}
	else
	{
		document.getElementById('div_repassword').style.height="0px";
		document.getElementById('div_repassword').innerHTML = "";
		document.formRegistration.email.focus();
		return true;
	}
}




function email_validate()
{
	if(document.formRegistration.email.value =='')
	{
		document.getElementById('div_email').style.height="15px";
		document.getElementById('div_email').style.color="#FF0000";
		document.getElementById('div_email').innerHTML = "email address can't be blank";
		document.formRegistration.email.focus();
		return false;
	}
	else if(!ChkEmail(document.formRegistration.email.value))
	{
		document.getElementById('div_email').style.height="15px";
		document.getElementById('div_email').style.color="#FF0000";
		document.getElementById('div_email').innerHTML = "please specify proper email address ";
		document.formRegistration.email.focus();
		return false;
	}
	else
	{
		document.getElementById('div_email').style.height="0px";
		document.getElementById('div_email').innerHTML = "";
		document.formRegistration.email.focus();
		return true;
	}
}


function postcode_validate()
{
	if(document.formRegistration.post_code.value=='')
	{		
		document.getElementById('div_postcode').style.height="15px";
		document.getElementById('div_postcode').style.color="#FF0000";
		document.getElementById('div_postcode').innerHTML = "Please specify postcode.";
		document.formRegistration.post_code.focus();
		return false;
	}
	else
	{
		document.getElementById('div_postcode').style.height="0px";
		document.getElementById('div_postcode').innerHTML = "";
		document.formRegistration.accept.focus();
		return true;
	}
}



/* CHECKS WETHER THE EMAIL ENTERED IS VALID OR NOT*/
function ChkEmail(mail)
{
		var str=mail;
		if (!str=="")
		{
			if (str.indexOf("@",1) == -1)
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			if (str.indexOf("@",1)== 0)
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			if (str.indexOf(".")== 0)
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			if (str.indexOf(".",1) == -1)
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
		
			// extra validation
			var posat=str.indexOf("@");
			var posdot=str.indexOf(".");
			var rposdot=str.lastIndexOf(".");
			if(rposdot==posdot)
			if((posdot < posat) || (posdot-posat < 3))
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			if(str.charAt(str.length-1)==".")
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			if(str.charAt(str.length-1)=="@")
			{
				//alert("That is not a valid Email address. Please enter again.");
				return false;
			}
			var j=0;
			for( var i=0;i<str.length;i++)
			{
				if(str.charAt(i) == "@")
				j++;
			}
			if(j > 1)
			{
			//alert("That is not a valid Email address. Please enter again.");
			return false;
			}
		}
		return true;
}



