// common javascript validation file

// check valid email
function isValidateEmail( str ) {
    var strRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    return strRE.test( str );
}

// check empty validation
function isEmpty( str ){
    var strRE = /^[\s ]*$/gi;
    return strRE.test( str );
}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

//  add to cart page validation
function checkfrmAddCart()
{
	var txtQty = document.getElementById("txtQuantity").value;
	var txtError = document.getElementById("spanError");
	if(txtQty == 0 && txtQty == 00)
	{
		txtError.style.display="";
		return false;
	}
	else
	{
		txtError.style.display="none";
	}
	//return false;
}

function locate()
{ 
	document.shopcart.action="add_cart.php?update=1";		
	document.shopcart.submit();
}

// login page validation
function checkfrmLogin()
{
	var txtEmail = document.getElementById("txtEmail");
	var txtPassword = document.getElementById("txtPassword");
	
	
	if(isEmpty(txtEmail.value))
	{
		alert("Please enter email");
		txtEmail.focus();
		return false;
	}
	if(!isValidateEmail(txtEmail.value))
	{
		alert("Please enter valid email");
		txtEmail.focus();
		return false;
	}
	if(isEmpty(txtPassword.value))
	{
		alert("Please enter password");
		txtPassword.focus();
		return false;
	}
	//return false;
}

// registration page validation
function checkfrmReg()
{
	var txtFname = document.getElementById("fname");
	var txtLname = document.getElementById("lname");
	var txtEmail = document.getElementById("email");
	var txtPass = document.getElementById("pass");
	var txtCPass = document.getElementById("cpass");
	var txtPhone = document.getElementById("phone");
	var txtAddress1 = document.getElementById("address1");
	var txtCity = document.getElementById("city");
	var txtPostal = document.getElementById("postal_code");
	
	var d=document.regForm;

	if(isEmpty(txtFname.value))
	{
		alert("Please enter first name");
		txtFname.focus();
		return false;
	}
	if(isEmpty(txtLname.value))
	{
		alert("Please enter last name");
		txtLname.focus();
		return false;
	}
	if(isEmpty(txtEmail.value))
	{
		alert("Please enter email");
		txtEmail.focus();
		return false;
	}
	if(!isValidateEmail(txtEmail.value))
	{
		alert("Please enter valid email");
		txtEmail.focus();
		return false;
	}
	if(isEmpty(txtPass.value))
	{
		alert("Please enter password");
		txtPass.focus();
		return false;
	}
	if(isEmpty(txtCPass.value))
	{
		alert("Please enter confirm password");
		txtCPass.focus();
		return false;
	}
	if(txtPass.value != txtCPass.value)
	{
		alert("Passwords do not match. Please type carefully.");
		txtCPass.focus();
		return false;
	}
	if(isEmpty(txtPhone.value))
	{
		alert("Please enter phone");
		txtPhone.focus();
		return false;
	}
	if(isEmpty(txtAddress1.value))
	{
		alert("Please enter address1");
		txtAddress1.focus();
		return false;
	}
	if(isEmpty(txtCity.value))
	{
		alert("Please enter city");
		txtCity.focus();
		return false;
	}
	
	if(d.country.value=='0')
	{
		alert("Country field cannot be left blank");
		d.country.focus();
		return false;
	}
	if((d.country.value=='51') || (d.country.value=='308'))
	{
		if(d.province.selectedIndex=='0')
		{
			alert("Please select province");
			d.province.focus();
			return false;
		}
	}
	if((d.country.value!='51') || (d.country.value!='308'))
	{
		if(isEmpty(d.province.value))
		{
			alert('Province / State field cannot be left blank');
			d.province.focus();
			return false;
		}
	}
	if(isEmpty(txtPostal.value))
	{
		alert("Please enter postal / zip code.");
		txtPostal.focus();
		return false;
	}
	
	//return false;
}

// forgot password validation

function checkfrmForgotPass()
{
	var txtEmail = document.getElementById("txtEmail");
	
	if(isEmpty(txtEmail.value))
	{
		alert("Please enter email");
		txtEmail.focus();
		return false;
	}
	if(!isValidateEmail(txtEmail.value))
	{
		alert("Please enter valid email");
		txtEmail.focus();
		return false;
	}

}

// edit billing page validation
function checkfrmBilling()
{
	var txtFname = document.getElementById("fname");
	var txtLname = document.getElementById("lname");
	var txtAddress1 = document.getElementById("address1");
	var txtCity = document.getElementById("city");
	var txtPostal = document.getElementById("postal_code");
	
	var d=document.frmBilling;

	if(isEmpty(txtFname.value))
	{
		alert("Please enter first name");
		txtFname.focus();
		return false;
	}
	if(isEmpty(txtLname.value))
	{
		alert("Please enter last name");
		txtLname.focus();
		return false;
	}
	if(isEmpty(txtAddress1.value))
	{
		alert("Please enter address1");
		txtAddress1.focus();
		return false;
	}
	if(isEmpty(txtCity.value))
	{
		alert("Please enter city");
		txtCity.focus();
		return false;
	}
	
	if(d.country.value=='0')
	{
		alert("Country field cannot be left blank");
		d.country.focus();
		return false;
	}
	if((d.country.value=='51') || (d.country.value=='308'))
	{
		if(d.province.selectedIndex=='0')
		{
			alert("Please select province");
			d.province.focus();
			return false;
		}
	}
	if((d.country.value!='51') || (d.country.value!='308'))
	{
		if(isEmpty(d.province.value))
		{
			alert('Province / State field cannot be left blank');
			d.province.focus();
			return false;
		}
	}
	if(isEmpty(txtPostal.value))
	{
		alert("Please enter postal / zip code.");
		txtPostal.focus();
		return false;
	}
	
	//return false;
}

// edit shipping page validation
function checkfrmShipping()
{
	var txtFname = document.getElementById("fname");
	var txtLname = document.getElementById("lname");
	var txtAddress1 = document.getElementById("address1");
	var txtCity = document.getElementById("city");
	var txtPostal = document.getElementById("postal_code");
	
	var d=document.frmShipping;

	if(isEmpty(txtFname.value))
	{
		alert("Please enter first name");
		txtFname.focus();
		return false;
	}
	if(isEmpty(txtLname.value))
	{
		alert("Please enter last name");
		txtLname.focus();
		return false;
	}
	if(isEmpty(txtAddress1.value))
	{
		alert("Please enter address1");
		txtAddress1.focus();
		return false;
	}
	if(isEmpty(txtCity.value))
	{
		alert("Please enter city");
		txtCity.focus();
		return false;
	}
	
	if(d.country.value=='0')
	{
		alert("Country field cannot be left blank");
		d.country.focus();
		return false;
	}
	if((d.country.value=='51') || (d.country.value=='308'))
	{
		if(d.province.selectedIndex=='0')
		{
			alert("Please select province");
			d.province.focus();
			return false;
		}
	}
	if((d.country.value!='51') || (d.country.value!='308'))
	{
		if(isEmpty(d.province.value))
		{
			alert('Province / State field cannot be left blank');
			d.province.focus();
			return false;
		}
	}
	if(isEmpty(txtPostal.value))
	{
		alert("Please enter postal / zip code.");
		txtPostal.focus();
		return false;
	}
	
	//return false;
}
