// JavaScript Document
/*
	File Title		: JavaScript for the homefront E-Commerce Application
	Created by		: Melanie Decle
	Created on		: October 23, 2003
*/

// function to make sure that quantity is entered as an integer
function validateQTY(f)
{
	if(!parseInt(f.quantity.value))
	{
		alert("Please enter a valid quantity \(numbers only\)!");
		f.quantity.value = 1;
		f.quantity.focus();
		return false;
	}
}

// function to validate check out form
function validateForm(f)
{
	var credit_card_number = /[\D]/; // test for any non-numbers
	if(f.firstName.value=="")
	{
		alert("Please enter your first name!");
		f.firstName.focus();
		return false;		
	}
	else if(f.lastName.value=="")
	{
		alert("Please enter your last name!");
		f.lastName.focus();
		return false;
	}
	else if(f.address.value=="")
	{
		alert("Please enter your address!");
		f.address.focus();
		return false;
	}
	else if((f.email.value=="") || (f.email.value.indexOf('@')< 2) || (f.email.value.indexOf('.')< 2))
	{
		alert("Please enter your email address!");
		f.email.focus();
		return false;
	}
	else if((f.card.value.length<15) || (credit_card_number.test(f.card.value)))
	{
		alert("Please enter a valid credit card!");
		f.card.value='';
		f.card.focus();
		return false;
	}
}
//login validation for the registry users
function RegistryValidate(f)
{
	if(f.customerEmail.value=="")
	{
		alert("Please enter your username!");
		f.customerEmail.focus();
		return false;
	}
	else if(f.customerPassword.value=="")
	{
		alert("Please enter your password!");
		f.customerPassword.focus();
		return false;
	}
}

// admin login form validation
function validateLogin(f)
{
	if(f.customerEmail.value=="")
	{
		alert("Please enter your user e-mail address!");
		f.customerEmail.focus();
		return false;
	}
	else if(f.customerPassword.value=="")
	{
		alert("Please enter your user password!");
		f.customerPassword.focus();
		return false;
	}
}

// function to validate admin products page
function validateProducts(f)
{
	var testNums = /[^0-9\.]/
	if(f.title.value=="")
	{
		alert("Please enter a title!");
		f.title.focus();
		return false;
	}
	else if((f.price.value=="") || (testNums.test(f.price.value)))
	{
		alert("Please enter a price!");
		f.price.focus();
		return false;
	}
	else if(f.description.value=="")
	{
		alert("Please enter a description!");
		f.description.focus();
		return false;
	}  
}

// function to validate admin customers
function validateCustomer(f)
{
	if(f.firstName.value=="")
	{
		alert("Please enter a first name!");
		f.firstName.focus();
		return false;
	}
	else if(f.lastName.value=="")
	{
		alert("Please enter a last name!");
		f.lastName.focus();
		return false;
	}
	else if(f.address.value=="")
	{
		alert("Please enter an address!");
		f.address.focus();
		return false;
	}
	else if((f.email.value=="") || (f.email.value.indexOf('@')<2) || (f.email.value.indexOf('.')<2))
	{
		alert("Please enter an email address!");
		f.email.focus();
		return false;
	}
}

// function to validate newsletter subscribers
function validateNewsletter(f)
{
	if(f.firstName.value=="")
	{
		alert("Please enter a first name!");
		f.firstName.focus();
		return false;
	}
	else if(f.lastName.value=="")
	{
		alert("Please enter a last name!");
		f.lastName.focus();
		return false;
	}
	else if((f.email.value=="") || (f.email.value.indexOf('@')<2) || (f.email.value.indexOf('.')<2))
	{
		alert("Please enter an email address!");
		f.email.focus();
		return false;
	}
	
	else if(f.city.value=="")
	{
		alert("Please enter a city!");
		f.city.focus();
		return false;
	}
	else if(f.phone.value=="")
	{
		alert("Please enter a phone Number!");
		f.phone.focus();
		return false;
	}
	else if(f.postal.value=="")
	{
		alert("Please enter a postal code!");
		f.postal.focus();
		return false;
	}
	else if(f.terms.checked==false)
	{
		alert("Please read our Privacy Policy and Terms of Use and check box accordingly!");
		f.terms.focus();
		return false;
	}

}

// delete warnings for shopping cart
var confirmEmpty = "Are you sure you want to empty all items from your shopping bag?"; 
var msg = "Are you sure that you want to delete this item?";

// delete warnings for Manage Products in Admin section (will delete from entire database)
var deleteProdFromDB = "Would you like to delete this product from the store?";
// delete warnings for Manage Products2 in Admin section (will delete from selected category)
var deleteProdFromCat = "Would you like to delete this product from this category listing?";

// add quantity before hitting add to bag warning
//function validateAddQty(f)
{
//	if(f.quantity.value=="")
	{
//		alert("Please enter a quantity before adding an item to your shopping bag!");
//		f.quantity.focus();
//		return false;
	}
//	for (i=0; i < f.quantity.value.length; i++) 
	{ 
//		if ((f.quantity.value.charAt(i) < "1") || (f.quantity.value.charAt(i) > "9")) 
		{ 
//		alert("Please enter numeric characters only!"); 	
//		f.quantity.focus();
//		return false; 
		} 
	} 
//return true; 
} 

//==========================================================================
// Validate the forgot password email inputted
//==========================================================================
function validate_email(f)
{
	// EMAIL CHECK  ====================================================
	// BOI, followed by one or more characters, followed by @,
	// followed by one or more characters, followed by ., 
	// followed by one or more characters, followed by EOI.
	var reEmail = /^.+\@.+\..+$/
	var emailRegExp = new RegExp(reEmail);
	if(f.customerEmail.value=="")
	{
		alert("Please enter an email address to send your password to.");
		f.customerEmail.focus();
		return false;
	}
	// check email for billing info
	if (emailRegExp.test(f.customerEmail.value) == false )
	{
		alert("Please enter a valid email address.");
		f.customerEmail.focus();
		return false;
	}	
}

function validateAddQty(f)
{
	// check to see if quantity is empty and return alert if it is
	if(f.quantity.value=="")
	{
		alert("Please enter a quantity before adding an item to your shopping bag!");
		f.quantity.focus();
		return false;
	}
	// check to see if quantity is empty and return alert if it is
	if(f.quantity.value=="0")
	{
		alert("Quantity must be more than 0 to add to shopping bag!");
		f.quantity.focus();
		return false;
	}
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 	// check to make sure that quantity entered is a numeric value and return msg if not
   for (i = 0; i < f.quantity.value.length && IsNumber == true; i++) 
      { 
      Char = f.quantity.value.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
		 alert("Please enter numeric characters only!"); 
         }
      }
   return IsNumber;   
}

//==========================================================================
// Validation for the Checkout form begins here 
//==========================================================================
// initialize variables to use on the shipping form
var customerFirstName1 = "";
var customerLastName1 = "";
var customerPhone1 = "";
var customerEmail1 = "";
var customerAddress11 = "";
var customerAddress21 = "";
var customerCity1 = "";
var customerProvStateIndex1 = 0;
var customerProvState1  = "";
var customerCountryIndex1 = 0;
var customerCountry1  = "";
var customerPostal1 = "";

// function to copy the values from the shipping info to local variables  ==
function InitSaveVariables(form) 
{
	var customerFirstName1 = "";
	var customerLastName1 = "";
	var customerPhone1 = "";
	var customerEmail1 = "";
	var customerAddress11 = "";
	var customerAddress21 = "";
	var customerCity1 = "";
	var customerProvStateIndex1 = 0;
	var customerProvState1  = "";
	var customerCountryIndex1 = 0;
	var customerCountry1  = "";
	var customerPostal1 = "";
}

//Function to copy the billing info to the shipping info
//===========================================================================
function useBillInfo(form) 
{
	// if the checkbox is checked, copy the billing info to shipping info and disable the input tags
	if (form.ship_useBill.checked)
	{
		InitSaveVariables(form);
		// shipping values get billing values
		form.customerFirstName1.value = form.customerFirstName.value;
		form.customerLastName1.value = form.customerLastName.value;
		form.customerPhone1.value = form.customerPhone.value;
		form.customerEmail1.value = form.customerEmail.value;
		form.customerAddress11.value = form.customerAddress1.value;
		form.customerAddress21.value = form.customerAddress2.value;
		form.customerCity1.value = form.customerCity.value;
		form.customerProvState1.selectedIndex = form.customerProvState.selectedIndex;
		form.customerProvState1.value = form.customerProvState.value;		
		form.customerCountry1.selectedIndex = form.customerCountry.selectedIndex;
		form.customerPostal1.value = form.customerPostal.value;
		// set value to true
		form.ship_bill_equal.value = "true";
   }
   // if the checkbox is not checked, enable the input tags and set then to initial value
   else
   {
		// assign initialized variables to the form values
   		form.customerFirstName1.value = customerFirstName1;
		form.customerLastName1.value = customerLastName1;
		form.customerPhone1.value = customerPhone1;
		form.customerEmail1.value = customerEmail1;
		form.customerAddress11.value = customerAddress11;
		form.customerAddress21.value = customerAddress21;
		form.customerCity1.value = customerCity1;
		form.customerProvState1.selectedIndex = customerProvStateIndex1;
		form.customerCountry1.selectedIndex = customerCountryIndex1;
		form.customerPostal1.value = customerPostal1;
		form.ship_bill_equal.value = "false";								
   }
}

//function to get the shipping values
function getShipValues(form)
{
	if (form.customerFirstName1.value != form.customerFirstName.value)
	{
		// initialize variable to hold ship values
		// initialize variables to use on the shipping form
		customerFirstName1 = form.customerFirstName1.value;
		customerLastName1 = form.customerLastName1.value;
		customerPhone1 = form.customerPhone1.value;
		customerEmail1 = form.customerEmail1.value;
		customerAddress11 = form.customerAddress11.value;
		customerAddress21 = form.customerAddress21.value;
		customerCity1 = form.customerCity1.value;
		customerProvStateIndex1 = form.customerProvState1.selectedIndex;
		customerCountryIndex1 = form.customerCountry1.selectedIndex;
		customerPostal1 = form.customerPostal1.value; 
		
		// allow newly entered values to be captured
		// assign initialized variables to the form values
		form.customerFirstName1.value = customerFirstName1;
		form.customerLastName1.value = customerLastName1;
		form.customerPhone1.value = customerPhone1;
		form.customerEmail1.value = customerEmail1;
		form.customerAddress11.value = customerAddress11;
		form.customerAddress21.value = customerAddress21;
		form.customerCity1.value = customerCity1;
		form.customerProvState1.selectedIndex = customerProvStateIndex1;
		form.customerCountry1.selectedIndex = customerCountryIndex1;
		form.customerPostal1.value = customerPostal1;   
	}
}

//=========================================================================
// validate shipping/billing info for returning customers
//=========================================================================
function validateShipBill(f)
{	
	if(f.returningCustomer.value=="true")
	{
		//==================================================================
		//Phone Number validation
		//================================================================== 
		// Declaring required variables
		var digits = "0123456789";
		// non-digit characters which are allowed in phone numbers
		var phoneNumberDelimiters = "()- ";
		// characters which are allowed in international phone numbers
		// (a leading + is OK)
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		// Minimum no of digits in an international phone no.
		var minDigitsInIPhoneNumber = 10;

		function isInteger(s)
		{   var i;
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character is number.
				var c = s.charAt(i);
				if (((c < "0") || (c > "9"))) return false;
			}
			// All characters are numbers.
			return true;
		}

		function stripCharsInBag(s, bag)
		{   var i;
			var returnString = "";
			// Search through string's characters one by one.
			// If character is not in bag, append to returnString.
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character isn't whitespace.
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) returnString += c;
			}
			return returnString;
		}

		function checkInternationalPhone(strPhone)
		{
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
		}
		
		//===================================================================
		// END OF PHONE NUMBER FUNCTIONS
		//===================================================================
			
		// Check first Name ================================================
		if(f.customerFirstName.value=="")
		{
			alert("Please enter the first name of the person being billed.");
			f.customerFirstName.focus();
			return false;
		}
		// Check last name =================================================
		if(f.customerLastName.value=="")
		{
			alert("Please enter the last name of the person being billed.");
			f.customerLastName.focus();
			return false;
		}
		
		// Check Billing Phone Number=======================================
		var billPhone= f.customerPhone;
		
		if ((billPhone.value==null)||(billPhone.value==""))
		{
			alert("Please Enter the billing Phone Number");
			billPhone.focus();
			return false;
		}
		if (checkInternationalPhone(billPhone.value)==false)
		{
			alert("Please Enter a valid billing Phone Number");
			billPhone.value="";
			billPhone.focus();
			return false;
		}		
			
		// EMAIL CHECK  ====================================================
		var emailChars = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var emailRegExp = new RegExp(emailChars);
		if(f.customerEmail.value=="")
		{
			alert("Please enter an email address in the billing information!");
			f.customerEmail.value = "";
			f.customerEmail.focus();
			return false;
		}
		if(emailRegExp.test(f.customerEmail.value) == "false")
		{
			alert("Please enter a valid email address in the billing information!");
			f.customerEmail.value = "";		
			f.customerEmail.focus();
			return false;		
		}

		// check address ===================================================
		if(f.customerAddress1.value=="")
		{
			alert("Please enter the address being billed!");
			f.customerAddress1.focus();
			return false;
		}
		// check city ======================================================
		if(f.customerCity.value=="")
		{
			alert("Please Enter a City for the person being billed.");
			f.customerCity.focus();
			return false;
		}
		// check prov/state ================================================	
		if(f.customerProvState.value=="Select One")
		{
			alert("Please Select a Province for the person being billed.");
			f.customerProvState.focus();
			return false;
		}	
		// check country ===================================================	
		if(f.customerCountry.value=="Select One")
		{
			alert("Please Select a Country for the person being billed.");
			f.customerCountry.focus();
			return false;
		}
		
		//=======================================================================
		// POSTAL CODE BILLING VALIDATION
		//=======================================================================
		// validate postal code for billing info ===============================
		if (f.customerPostal.value =="")
		{
			alert("Please Enter a Vaild Postal Code for the person being billed.");
			f.customerPostal.focus();
			return false;
		}
		// validate postal/zip code based on country chosen
		if(f.customerCountry.value=="Canada")
		{
			// validate postal code for billing info ===============================
			var postalPattern = /^[a-cehj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d$/i;
			var postalRegExp = new RegExp(postalPattern);
			
			if (postalRegExp.test(f.customerPostal.value) == false )
			{
				alert("Please Enter a Vaild Postal Code for the person being billed.");
				f.customerPostal.focus();
				f.customerPostal.value = "";		
				return false;	
			}			
		}
		// validate zip code based on country chosen
		if(f.customerCountry.value=="US")
		{
			// validate postal code for billing info ===============================
			var valid = "0123456789-";
			var hyphencount = 0;
			
			if (f.customerPostal.value.length!=5 && f.customerPostal.value.length!=10) 
			{
				alert("Please enter your 5 digit or 5 digit+4 shipping zip code.");
				f.customerPostal.value = "";
				f.customerPostal.focus();				
				return false;
			}
			for (var i=0; i < f.customerPostal.value.length; i++) 
			{
				temp = "" + f.customerPostal.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") 
				{
					alert("Invalid characters in your shipping zip code. Please try again.");
					f.customerPostal.value = "";
					f.customerPostal.focus();					
					return false;
				}
				if ((hyphencount > 1) || ((f.customerPostal.value.length==10) && ""+f.customerPostal.value.charAt(5)!="-")) 
				{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
					f.customerPostal.value = "";
					f.customerPostal.focus();
					return false;
			   }
			}	
		}						
		//=========================================================================
		// END OF POSTAL CODE VALIDATION 
		//=========================================================================

		// Check shipping first Name ========================================					
		if(f.customerFirstName1.value=="")
		{
			alert("Please enter the first name of the person receiving the order.");
			f.customerFirstName1.focus();
			return false;
		}	
		// Check shipping last Name ==========================================						
		if(f.customerLastName1.value=="")
		{
			alert("Please enter the last name of the person receiving the order.");
			f.customerLastName1.focus();
			return false;
		}
		
		// Check shipping ph number ==========================================
		var shipPhone= f.customerPhone1;
		
		if ((shipPhone.value==null)||(shipPhone.value==""))
		{
			alert("Please Enter the shipping Phone Number");
			shipPhone.focus();
			return false;
		}
		if (checkInternationalPhone(shipPhone.value)==false)
		{
			alert("Please Enter a valid shipping Phone Number");
			shipPhone.value="";
			shipPhone.focus();
			return false;
		}		
	
		// check email for shipping info
		if(f.customerEmail1.value=="")
		{
			alert("Please enter an email address in the shipping information!");
			f.customerEmail1.focus();
			return false;
		}
		// test email for shipping info
		if (emailRegExp.test(f.customerEmail1.value) == false )
		{
			alert("Please enter a valid email address in the shipping information!");
			f.customerEmail1.focus();
			return false;
		}
		// Check shipping address =============================================							
		if(f.customerAddress11.value=="")
		{
			alert("Please enter the address of the person receiving the order.");
			f.customerAddress11.focus();
			return false;
		}
		// Check shipping city ================================================						
		if(f.customerCity1.value=="")
		{
			alert("Please Enter a City for the person receiving the order.");
			f.customerCity1.focus();
			return false;
		}	
		// Check shipping province ============================================						
		if(f.customerProvState1.value=="Select One")
		{
			alert("Please Select a Province for the person receiving the order.");
			f.customerProvState1.focus();
			return false;
		} 	
		// Check shipping country =============================================						
		if(f.customerCountry1.value=="Select One")
		{
			alert("Please Select a Country for the person receiving the order.");
			f.customerCountry1.focus();
			return false;
		}
		//=======================================================================
		// POSTAL CODE SHIPPING VALIDATION
		//=======================================================================
		// validate postal code for shipping info ================================
		if (f.customerPostal1.value =="")
		{
			alert("Please Enter a Postal Code in the shipping information.");
			f.customerPostal1.value = "";
			f.customerPostal1.focus();
			return false;
		}
		
		// validate postal/zip code based on country chosen
		if(f.customerCountry1.value=="Canada")
		{
			// validate postal code for billing info ===============================
			var postalPattern = /^[a-cehj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d$/i;
			var postalRegExp = new RegExp(postalPattern);
			
			if (postalRegExp.test(f.customerPostal1.value) == false )
			{
				alert("Please Enter a Vaild Postal Code in the shipping information.");
				f.customerPostal1.focus();
				f.customerPostal1.value = "";		
				return false;	
			}			
		}
		// validate postal/zip code based on country chosen
		if(f.customerCountry1.value=="US")
		{
			// validate postal code for billing info ===============================
			var valid = "0123456789-";
			var hyphencount = 0;
			
			if (f.customerPostal1.value.length!=5 && f.customerPostal1.value.length!=10) 
			{
				alert("Please enter your 5 digit or 5 digit+4 shipping zip code.");
				f.customerPostal1.value = "";
				f.customerPostal1.focus();				
				return false;
			}
			for (var i=0; i < f.customerPostal1.value.length; i++) 
			{
				temp = "" + f.customerPostal1.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") 
				{
					alert("Invalid characters in your shipping zip code. Please try again.");
					f.customerPostal1.value = "";
					f.customerPostal1.focus();					
					return false;
				}
				if ((hyphencount > 1) || ((f.customerPostal1.value.length==10) && ""+f.customerPostal1.value.charAt(5)!="-")) 
				{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
					f.customerPostal1.value = "";
					f.customerPostal1.focus();
					return false;
			   }
			}	
		}				
		//========================================================================
		// END OF POSTAL CODE VALIDATION
		//========================================================================
		
		// call funtion to grab newly entered values
		getShipValues(form);
	}
	else
	{
		//==================================================================
		//Phone Number validation
		//================================================================== 
		// Declaring required variables
		var digits = "0123456789";
		// non-digit characters which are allowed in phone numbers
		var phoneNumberDelimiters = "()- ";
		// characters which are allowed in international phone numbers
		// (a leading + is OK)
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		// Minimum no of digits in an international phone no.
		var minDigitsInIPhoneNumber = 10;

		function isInteger(s)
		{   var i;
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character is number.
				var c = s.charAt(i);
				if (((c < "0") || (c > "9"))) return false;
			}
			// All characters are numbers.
			return true;
		}

		function stripCharsInBag(s, bag)
		{   var i;
			var returnString = "";
			// Search through string's characters one by one.
			// If character is not in bag, append to returnString.
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character isn't whitespace.
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) returnString += c;
			}
			return returnString;
		}

		function checkInternationalPhone(strPhone)
		{
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
		}
		
		//===================================================================
		// END OF PHONE NUMBER FUNCTIONS
		//===================================================================
		
		// call funtion to grab newly entered values
		getShipValues(form);
		// Check first Name ===================================================
		if(f.customerFirstName.value=="")
		{
			alert("Please enter the first name of the person being billed.");
			f.customerFirstName.focus();
			return false;
		}
		// Check last name ====================================================
		if(f.customerLastName.value=="")
		{
			alert("Please enter the last name of the person being billed.");
			f.customerLastName.focus();
			return false;
		}
		
		// Check Billing Phone Number=======================================
		var billPhone= f.customerPhone;
		
		if ((billPhone.value==null)||(billPhone.value==""))
		{
			alert("Please Enter the billing Phone Number");
			billPhone.focus();
			return false;
		}
		if (checkInternationalPhone(billPhone.value)==false)
		{
			alert("Please Enter a valid billing Phone Number");
			billPhone.value="";
			billPhone.focus();
			return false;
		}

		// EMAIL CHECK  =======================================================
		var emailChars = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var emailRegExp = new RegExp(emailChars);
		if(f.customerEmail.value=="")
		{
			alert("Please enter an email address in the billing information!");
			f.customerEmail.focus();
			return false;
		}
		// check email for billing info
		if (emailRegExp.test(f.customerEmail.value) == false )
		{
			alert("Please enter a valid email address in the billing information!");
			f.customerEmail.focus();
			return false;
		}
		// check address =======================================================
		if(f.customerAddress1.value=="")
		{
			alert("Please enter the address being billed!");
			f.customerAddress1.focus();
			return false;
		}
		// check city ==========================================================
		if(f.customerCity.value=="")
		{
			alert("Please Enter a City for the person being billed.");
			f.customerCity.focus();
			return false;
		}
		// check prov/state ====================================================	
		if(f.customerProvState.value=="Select One")
		{
			alert("Please Select a Province for the person being billed.");
			f.customerProvState.focus();
			return false;
		}	
		// check country =======================================================	
		if(f.customerCountry.value=="Select One")
		{
			alert("Please Select a Country for the person being billed.");
			f.customerCountry.focus();
			return false;
		}
		//=======================================================================
		// POSTAL CODE BILLING VALIDATION
		//=======================================================================
		// validate postal code for billing info ===============================
		if (f.customerPostal.value =="")
		{
			alert("Please Enter a Vaild Postal Code for the person being billed.");
			f.customerPostal.focus();
			return false;
		}
		// validate postal/zip code based on country chosen
		if(f.customerCountry.value=="Canada")
		{
			// validate postal code for billing info ===============================
			var postalPattern = /^[a-cehj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d$/i;
			var postalRegExp = new RegExp(postalPattern);
			
			if (postalRegExp.test(f.customerPostal.value) == false )
			{
				alert("Please Enter a Vaild Postal Code for the person being billed.");
				f.customerPostal.focus();
				f.customerPostal.value = "";		
				return false;	
			}			
		}
		// validate zip code based on country chosen
		if(f.customerCountry.value=="US")
		{
			// validate postal code for billing info ===============================
			var valid = "0123456789-";
			var hyphencount = 0;
			
			if (f.customerPostal.value.length!=5 && f.customerPostal.value.length!=10) 
			{
				alert("Please enter your 5 digit or 5 digit+4 shipping zip code.");
				f.customerPostal.value = "";
				f.customerPostal.focus();				
				return false;
			}
			for (var i=0; i < f.customerPostal.value.length; i++) 
			{
				temp = "" + f.customerPostal.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") 
				{
					alert("Invalid characters in your shipping zip code. Please try again.");
					f.customerPostal.value = "";
					f.customerPostal.focus();					
					return false;
				}
				if ((hyphencount > 1) || ((f.customerPostal.value.length==10) && ""+f.customerPostal.value.charAt(5)!="-")) 
				{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
					f.customerPostal.value = "";
					f.customerPostal.focus();
					return false;
			   }
			}	
		}				
		//=========================================================================
		// END OF POSTAL CODE VALIDATION 
		//=========================================================================
		
		// Check shipping first Name ===========================================					
		if(f.customerFirstName1.value=="")
		{
			alert("Please enter the first name of the person receiving the order.");
			f.customerFirstName1.focus();
			return false;
		}	
		// Check shipping last Name =============================================						
		if(f.customerLastName1.value=="")
		{
			alert("Please enter the last name of the person receiving the order.");
			f.customerLastName1.focus();
			return false;
		}

		// check shipping phone number =========================================
		var shipPhone= f.customerPhone1;
		
		if ((shipPhone.value==null)||(shipPhone.value==""))
		{
			alert("Please Enter the shipping Phone Number");
			shipPhone.focus();
			return false;
		}
		if (checkInternationalPhone(shipPhone.value)==false)
		{
			alert("Please Enter a valid shipping Phone Number");
			shipPhone.value="";
			shipPhone.focus();
			return false;
		}
		
		//======================================================================		
		// check email for shipping info
		if(f.customerEmail1.value=="")
		{
			alert("Please enter an email address in the shipping information!");
			f.customerEmail1.focus();
			return false;
		}
		// test email for shipping info
		if (emailRegExp.test(f.customerEmail1.value) == false )
		{
			alert("Please enter a valid email address in the shipping information!");
			f.customerEmail1.focus();
			return false;
		}
		// Check shipping address ===============================================							
		if(f.customerAddress11.value=="")
		{
			alert("Please enter the address of the person receiving the order.");
			f.customerAddress11.focus();
			return false;
		}
		// Check shipping city ==================================================						
		if(f.customerCity1.value=="")
		{
			alert("Please Enter a City for the person receiving the order.");
			f.customerCity1.focus();
			return false;
		}	
		// Check shipping province ==============================================						
		if(f.customerProvState1.value=="Select One")
		{
			alert("Please Select a Province for the person receiving the order.");
			f.customerProvState1.focus();
			return false;
		} 	
		// Check shipping country ===============================================						
		if(f.customerCountry1.value=="Select One")
		{
			alert("Please Select a Country for the person receiving the order.");
			f.customerCountry1.focus();
			return false;
		}
		//=======================================================================
		// POSTAL CODE SHIPPING VALIDATION
		//=======================================================================
		// validate postal code for shipping info ================================
		if (f.customerPostal1.value =="")
		{
			alert("Please Enter a Postal Code in the shipping information.");
			f.customerPostal1.value = "";
			f.customerPostal1.focus();
			return false;
		}
		
		// validate postal code based on country chosen
		if(f.customerCountry1.value=="Canada")
		{
			// validate postal code for billing info ===============================
			var postalPattern = /^[a-cehj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d$/i;
			var postalRegExp = new RegExp(postalPattern);
			
			if (postalRegExp.test(f.customerPostal1.value) == false )
			{
				alert("Please Enter a Vaild Postal Code in the shipping information.");
				f.customerPostal1.focus();
				f.customerPostal1.value = "";		
				return false;	
			}			
		}
		// validate zip code based on country chosen
		if(f.customerCountry1.value=="US")
		{
			// validate postal code for billing info ===============================
			var valid = "0123456789-";
			var hyphencount = 0;
			
			if (f.customerPostal1.value.length!=5 && f.customerPostal1.value.length!=10) 
			{
				alert("Please enter your 5 digit or 5 digit+4 shipping zip code.");
				f.customerPostal1.value = "";
				f.customerPostal1.focus();				
				return false;
			}
			for (var i=0; i < f.customerPostal1.value.length; i++) 
			{
				temp = "" + f.customerPostal1.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") 
				{
					alert("Invalid characters in your shipping zip code. Please try again.");
					f.customerPostal1.value = "";
					f.customerPostal1.focus();					
					return false;
				}
				if ((hyphencount > 1) || ((f.customerPostal1.value.length==10) && ""+f.customerPostal1.value.charAt(5)!="-")) 
				{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
					f.customerPostal1.value = "";
					f.customerPostal1.focus();
					return false;
			   }
			}	
		}		
		
		//========================================================================
		// END OF POSTAL CODE VALIDATION
		//========================================================================
		
		if (f.validCustomer.value == "false")
		{									
			// declare password variables for testing
			var customerPassword = f.customerPassword.value;
			var reenterPassword = f.reenterPassword.value;
			if(f.customerPassword.value=="")
			{
				alert("Please enter a password!");
				f.customerPassword.focus();
				return false;
			// only allow password length between 6-10 characters
				var illegalChars = /[\W_]/; // allow only letters and numbers
				if ((f.customerPassword.length < 6) || (f.customerPassword.length > 10)) 
				{
					alert("Please enter a password having 6-10 character.\n");
					f.customerPassword.focus();
					return false;
				}
				else if (illegalChars.test(customerPassword)) 
				{
					alert("The password contains illegal characters.\n");
					f.customerPassword.focus(); 
					return false;
				}
			}
			if(f.reenterPassword.value=="")
			{
				alert("Please confirm your password!");
				f.reenterPassword.focus();
				return false;
			}
			// test password verification entry
			if (customerPassword != reenterPassword) 
			{
				alert ("\nThe password's entered do not match. Please try again.");
				f.reenterPassword.value = "";
				f.reenterPassword.focus();
				return false;
			}
		}
	}
}
//=========================================================================
////////////////////////////////////////////////////////////////////////////////////
//END of Fuctions to validate the registry forms.
////////////////////////////////////////////////////////////////////////////////////
