// Validation details
invalidChars = "/:,;!@#1234567890$%^&*(){}[]<>?'~`+=|\:";
invalidTitle = "/;$^*{}[]<>~`+=|"; 
invalidUsername = " /:,;!@#$%^&*(){}[]<>?'~`+=|\:";
invalidPassword = " /:,;@#$%^&*(){}[]<>?'~`+=|\:";
invalidPhone = "/:,;!@abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%^&{}[]<>?'~`+=|\:";
invalidAddress = "/:,;!@$%^&*(){}[]<>?'~`+=|\:";
invalidPostalCode = "/:,;!@#$%^&*(){}[]<>?'~`+=|\:";
invalidZip = " /:,;!@#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%^&*{}()[]<>?'~`+=|\:";
invalidCard = " /:,;!@#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%^&*{}()[]<>?'~`+-=|\:";
InvalidQuantity = " /:,;!@#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%^&*{}()[]<>?'~`+-=|\:"


// Check user input (credit card number) for Check Out Page
function submitform (thisform) 
{	
	if ((thisform.cardNumber.length == 5) && !validate(thisform.cardNumber.value,invalidCard,5)) {
		alert("Invalid card number.");
		thisform.cardNumber.focus();
		thisform.cardNumber.select();
		return false;
	}
	else
		return true
}


// Reset form for Member Sign Up page
function resetform () {
  if (confirm('Are you sure you want to reset ALL the fields?') )
	return true;
  else 
	return false;
}
	
		
/*	Check user input (first name, last name, phone, email, username, password, 
	address description, addressline1, addressline2, city, province, postal code) 
	for Member Sign Up and Member Update pages
*/
function submitmemberform (personalform) {
if (confirm('Are you sure you have filled out ALL the fields?') ) {

	if (!validchar(personalform.fname.value)) {
		alert("Invalid First Name.");
		personalform.fname.focus();
		personalform.fname.select();
		return false;
	}
	
	if (!validchar(personalform.lname.value)) {
		alert("Invalid Last Name.");
		personalform.lname.focus();
		personalform.lname.select();
		return false;
	}
	
	if (!validphone3(personalform.phoneA.value)) {
		alert("Invalid Phone.");
		personalform.phoneA.focus();
		personalform.phoneA.select();
		return false;
	}
	
	if (!validphone3(personalform.phoneP.value)) {
		alert("Invalid Phone.");
		personalform.phoneP.focus();
		personalform.phoneP.select();
		return false;
	}
	
	if (!validphone4(personalform.phoneS.value)) {
		alert("Invalid Phone.");
		personalform.phoneS.focus();
		personalform.phoneS.select();
		return false;
	}
	
	if (!validEmail(personalform.email.value)) {
		alert("Invalid email address.");
		personalform.email.focus();
		personalform.email.select();
		return false;
	}
	
	if (!validuserName(personalform.userName.value)) {
		alert("Invalid user name.");
		personalform.userName.focus();
		personalform.userName.select();
		return false;
	}
	
	if (!validpassword(personalform.password.value)) {
		alert("Invalid password.");
		personalform.password.focus();
		personalform.password.select();
		return false;
	}
	
	
	if (!validaddress(personalform.addressD.value)) {
		alert("Invalid Address Description.");
		personalform.addressD.focus();
		personalform.addressD.select();
		return false;
	}
	
	if (!validaddress(personalform.address1.value)) {
		alert("Invalid Address Line1.");
		personalform.address1.focus();
		personalform.address1.select();
		return false;
	}
	
	if (!validaddress(personalform.address2.value)) {
		alert("Invalid Address Line2.");
		personalform.address2.focus();
		personalform.address2.select();
		return false;
	}
	
	if (!validchar(personalform.city.value)) {
		alert("Invalid City.");
		personalform.city.focus();
		personalform.city.select();
		return false;
	}
	
	if (!validpostalcode(personalform.postal.value)) {
		alert("Invalid PostalCode.");
		personalform.postal.focus();
		personalform.postal.select();
		return false;
	}
	
	if ((personalform.province.value == "")||(personalform.province.value == 0)){
		alert("Select province.");
		return false;
	}

	return true;
	
}
else 
	return false;
}

function validchar(field) {

	invalidChars = "/:,;!@#1234567890$%^&*(){}[]<>?'~`+=|\:";
	if (field == "") {
		return false;
	}

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (field.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	return true;
}

function validuserName(field) {

	if ((field == "") || (field.length > 10)) {
		return false;
	}
	return true;
}

function validpassword(field) {

	if ((field == "") || (field.length > 10)) {
		return false;
	}
	return true;
}

function validphone3(field) {
	OKChars = "1234567890";
	if ((field == "") || (field.length != 3)) {
		return false;
	}

	for (i=0; i<field.length; i++) {
		Char = field.charAt(i);
		if (OKChars.indexOf(Char,0) < 0) {
			return false;
		}
	}
	
	return true;
}
		
function validphone4(field) {

	OKChars = "1234567890";
	if ((field == "") || (field.length != 4)) {
		return false;
	}

	for (i=0; i<field.length; i++) {
		Char = field.charAt(i);
		if (OKChars.indexOf(Char,0) < 0) {
			return false;
		}
	}
	
	return true;
}

function validaddress(field) {

	invalidChars = "/:,;!@#$%^&*(){}[]<>?'~`+=|\:";
	if (field == "") {
		return false;
	}

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (field.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	return true;
}

function validpostalcode(field) {

	invalidChars = "/:,;!@#$%^&*(){}[]<>?'~`+=|\:";
	if ((field == "") || (field.length != 7)) {
		return false;
	}

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (field.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	return true;
}


function validEmail(email) {

	invalidChars = "/:,;";
	if (email == "") {
		return false;
	}

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}

	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@", atPos+1)>-1) {
		return false;
	}
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+2 > email.length) {
		return false;
	}
	return true;
	}
	

// Check user input (quantity) for Product Details Page 
function quantityform (thisform) 
{	
	if (!validate(thisform.txtQuantity.value,InvalidQuantity,1)) {
		alert("Please enter quantity!");
		thisform.txtQuantity.focus();
		thisform.txtQuantity.select();
		return false;
	} 
	if (thisform.txtQuantity.value <= 0) {
	   alert("Please enter quantity!");
	   thisform.txtQuantity.focus();
	   thisform.txtQuantity.select();
	   return false;
	}
	else
		return true
}


// Check user input (username and password) for Login Page
function loginform (thisform) 
{	
	if (!validate(thisform.username.value,invalidUsername,1)) {
		alert("Invalid Username");
		thisform.username.focus();
		thisform.username.select();
		return false;
	} else 	if (!validate(thisform.password.value,invalidPassword,1)) {
		alert("Invalid Password");
		thisform.password.focus();
		thisform.password.select();
		return false;
	} 
	else
		return true
}

function validate(field, invalidChars, length) 
{
	if(field.length < length)
		return false
		
	for (i=0; i<invalidChars.length; i++) 
	{
		badChar = invalidChars.charAt(i);
		if (field.indexOf(badChar,0) > -1) 
		{
			return false;
		}
	}
	return true;
}


