<!--
//** generic functions *****************************************************************************

function toggleMainImage(imgSrc){
	if(document.all || document.layers){
		if (imgSrc != ""){
			eval("document.mainImage.src=\"" + imgSrc + "\"");
		}
	}
}

function setColourOption(theForm, idxProduct){
	var fDropdownBox = eval("document." + theForm + ".colourOptionsDropdown" + idxProduct);
	var fHiddenField = eval("document." + theForm + ".idxProductColour");
	fHiddenField.value = fDropdownBox.value;
}

function addToCart(theForm, idxProduct, mode, qty){
	var f = eval("document." + theForm);
	f.idxProduct.value = idxProduct;
	f.mode.value = mode;
	f.qty.value = qty;
	//set colour options in case setColourOption() has not been called
	var fDropdownBox = eval("document." + theForm + ".colourOptionsDropdown" + idxProduct);
	var fHiddenField = eval("document." + theForm + ".idxProductColour");
	fHiddenField.value = fDropdownBox.value;
	//submit the form
	f.action = "Cart.asp";
	f.submit();
}

function addToCartPotLuck(theForm, idxProduct, mode, qty){
	var f = eval("document." + theForm);
	f.idxProduct.value = idxProduct;
	f.mode.value = mode;
	f.qty.value = qty;
	//submit the form
	f.action = "Cart.asp";
	f.submit();
}

function addToCartVive(theForm, idxProduct, mode, qty){
	var f = eval("document." + theForm);
	f.idxProduct.value = idxProduct;
	f.mode.value = mode;
	f.qty.value = qty;
	//submit the form
	f.action = "Cart.asp";
	f.submit();
}

function addToCartDetailsPage(textbox1, textbox2){
	textbox1.value = parseInt(textbox1.value);
	if(textbox1.value == 'NaN')
		textbox1.value = 1;
	if (textbox1.value < 1)
		textbox1.value = 1;
	var f = document.fProductDetails;
	f.idxProductColour.value = textbox2.value;
	f.action = "Cart.asp";
	f.submit();
}

function removeCartItem(mode, idxProduct, idxProductColour){
	if(confirm("Are you sure you wish to remove this product from your cart?")){
		var f = document.fCart;
		f.mode.value = mode;
		f.idxProduct.value = idxProduct;
		//var fDropdownBox = eval("document.fCart.colourOptionsDropdown" + idxProduct);
		f.idxProductColour.value = idxProductColour;
		f.action = "Cart.asp";
		f.submit();
	}
}

function storeCartItem(mode, idxProduct){
	if(confirm("Are you sure you wish to store this product?")){
		var f = document.fCart;
		f.mode.value = mode;
		f.idxProduct.value = idxProduct;
		f.action = "Cart.asp";
		f.submit();
	}
}

function addStoredItemToCart(mode, idxProduct){
	if(confirm("Are you sure you wish to add this item to your cart?")){
		var f = document.fCartStoredItems;
		f.mode.value = mode;
		f.idxProduct.value = idxProduct;
		//set colour options in case setColourOption() has not been called
		var fDropdownBox = eval("document.fCartStoredItems.colourOptionsDropdown" + idxProduct);
		var fHiddenField = eval("document.fCartStoredItems.idxProductColour");
		fHiddenField.value = fDropdownBox.value;
		f.action = "Cart.asp";
		f.submit();
	}
}

function updateCart(mode, iUpperBound){
	//iUpperBound is a looping variable so we loop forms collection the correct
	//number of times
	var f = document.fCart;
	f.mode.value = mode;
	var i = 0;
	
	var iFormLength = f.length;				//form length
	var element;							//represents current form element
	var tmpElementValue;					//temp variable to hold current element value i.e the quantity
	var tmpElementName;						//temp variable to hold current element name
	var arrQty = new Array();				//array
	var arrIdxProduct = new Array();		//array
	var arrIdxProductColours = new Array();	//array
	var strQty = "";						//string variable for quantity
	var strIdxProduct = "";					//string variable for idxProduct
	var strIdxProductColour = "";			//string variable for idxProductColour
	
	//loop through forms collection and set values of hidden fields for qty
	for(i = 0; i < iFormLength; i++){
		element = f[i];
		if(element.type == "text"){
			if(element.name.indexOf("qty-") == 0){						//we have found a quantity field
				strQty = strQty + element.value + ",";
			}
		}
		if(element.type == "hidden"){
			if(element.name.indexOf("idxProduct-") == 0){				//we have found idxProduct field
				strIdxProduct = strIdxProduct + element.value + ",";
			}
		}
		if(element.type == "hidden"){
			if(element.name.indexOf("idxProductColour-") == 0){			//we have found idxProductColour field
				strIdxProductColour = strIdxProductColour + element.value + ",";
			}
		}
	}
	
	f.arrQty.value = strQty;
	f.arrIdxProduct.value = strIdxProduct;
	f.arrIdxProductColours.value = strIdxProductColour;
	f.action = "Cart.asp";
	f.submit();
}

function addToStoredItems(){
	var f = document.fProductDetails;
	if(confirm("Are you sure you want to add this product to your Stored Items?")){
		f.action = "Account_StoredItems.asp";
		f.submit();
	}
}

function pageThruCategory(idxCategory, iCurrentPage, iCategoryFilter){
	document.fProductList.iCurrentPage.value = iCurrentPage;
	document.fProductList.action = "Product_List.asp?idxCategory=" + idxCategory + "&categoryFilter=" + iCategoryFilter;
	document.fProductList.submit();
}

function pageThruSearchResults(iCurrentPage){
	document.fSearchResults.iCurrentPage.value = iCurrentPage;
	document.fSearchResults.action = "Search_Results.asp";
	document.fSearchResults.submit();
}

function pageThruStoredItems(iCurrentPage){
	document.fStoredItems.iCurrentPage.value = iCurrentPage;
	document.fStoredItems.action = "Account_StoredItems.asp";
	document.fStoredItems.submit();
}

function pageThruPriceBeaters(iCurrentPage){
	document.fPriceBeaterList.iCurrentPage.value = iCurrentPage;
	document.fPriceBeaterList.action = "Price_Beaters_List.asp";
	document.fPriceBeaterList.submit();
}

function pageThruViveLaDifference(iCurrentPage){
	document.fViveLaDifferenceList.iCurrentPage.value = iCurrentPage;
	document.fViveLaDifferenceList.action = "Vive_La_Difference_List.asp";
	document.fViveLaDifferenceList.submit();
}

function update(textbox){
	textbox.value = parseInt(textbox.value);
	if(textbox.value == 'NaN')
		textbox.value = 1;
	if (textbox.value < 1)
		textbox.value = 1;
}

function updateStoredItems(){
	var f = document.fStoredItems;
	if(confirm("Are you sure you wish to update your Stored Items?")){
		f.action = "Account_StoredItems_Delete.asp";
		f.submit();
	}
}

var enlargedWindow;
var blnIsEnlargedWindowOpen = false;
function enlarge(idxProduct){
	if(blnIsEnlargedWindowOpen){
		enlargedWindow.close();
	}
	enlargedWindow = window.open("Product_Enlarged.asp?idxProduct=" + idxProduct,"EnlargedImage","width=240,height=270,resizable=no,status=no,scrollbars=no,menubar=no,toolbar=no,location=no");
	blnIsEnlargedWindowOpen = true;	
}

var cantFindWindow;
var blnIsCantFindWindowOpen = false;
function openCantFind(idxProduct){
	if(blnIsCantFindWindowOpen){
		cantFindWindow.close();
	}
	cantFindWindow = window.open("Cant_Find.asp?","CantFindWindow","width=370,height=240,resizable=yes,status=no,scrollbars=no,menubar=no,toolbar=no,location=no");
	blnIsCantFindWindowOpen = true;	
}

function submitForm(){
	document.searchForm.action = "searchResults.asp";
	document.searchForm.submit();
}

function pageThruList(href, currentPage){
	var f = document.fList;
	f.action = href + "?currentPage=" + currentPage;
	f.submit();
}

function goToPage(href, theForm){
	var f = eval("document." + theForm);
	f.action = href;
	f.submit();
}

function validateIfBlankField(field){
	if(field == ""){
		return false;
	}
	return true;
}

function validateEmail(email){
	if(email == ""){
		return false;
	}
	else{
  		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  		if (!reg1.test(email) && reg2.test(email)){ // if syntax is valid
			return true;
  		}
		else{
			return false;
		}
	}
	return true;
}

function validateIfSelectBoxFilled(field){
	if(field == "x"){
		return false;
	}
	return true;
}

function validateStringLength(field, lBoundLength, uBoundLength){
	if(field == ""){
		return false;
	}
	else if(field.length < lBoundLength){
		return false;
	}
	else if(field.length > uBoundLength){
		return false;
	}
	return true;
}

function validateStringMatch(field1, field2){
	if(field1 != field2){
		return false;
	}
	return true;
}

function validateInvalidPasswordChars(field){
	var regExpMatch = /[^0-9a-zA-Z]/;
	return !(regExpMatch.test(field));
}

function validateDigitsOnly(field) {
	for( var i = 0; i < field.length; i++ ) {		// make sure the number is all digits.. (by design)
		var c = field.charAt(i);
		
		if( c < '0' || c > '9' ) {
			return false;
		}
		return true;
	}
}

function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;
	
	for( i = 0; i < cardNumber.length; ++i ) {
		ar[i] = parseInt(cardNumber.charAt(i));
	}
	
	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
		ar[i] *= 2;							 							// every second digit starting with the right most (check digit)
		if( ar[i] > 9 ) ar[i]-=9;			 				// will be doubled, and summed with the skipped digits.
	}										 										// if the double digit is > 9, add those individual digits together 
		
	for( i = 0; i < ar.length; ++i ) {
		sum += ar[i];						 							// if the sum is divisible by 10 mod10 succeeds
	}
	
	return (((sum%10)==0)?true:false);	 	
}

//** form functions ********************************************************************************

//** Contact forms validation
function submitContactForm(){
	return validateContactForm();
}

function validateContactForm(){
	var f = document.fContact;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.name.value)){
		strErrorMsg += "---Name\n";
	}
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** address book functions
function setDefaultAddress(){
	var f = document.fAddressBook;
	f.action = "Account_Address_Default.asp";
	f.submit();
}

function deleteAddress(){
	var f = document.fAddressBook;
	f.action = "Account_Address_Delete.asp";
	if(confirm("Are you sure you wish to delete this address from your Address Book?")){
		f.submit();
	}
}

//** edit profile functions
function submitEditProfile(){
	return validateEditProfile();
}

function validateEditProfile(){
	var f = document.fEditProfile;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.firstName.value)){
		strErrorMsg += "---First name\n";
	}
	
	if(!validateIfBlankField(f.surname.value)){
		strErrorMsg += "---Surname\n";
	}
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(!validateIfBlankField(f.telephone.value)){
		strErrorMsg += "---Telephone number\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** register new functions
function submitRegisterNew(){
	return validateRegisterNew();
}

function validateRegisterNew(){
	var f = document.fRegisterNew;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.firstName.value)){
		strErrorMsg += "---First name\n";
	}
	
	if(!validateIfBlankField(f.surname.value)){
		strErrorMsg += "---Surname\n";
	}
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(!validateIfBlankField(f.telephone.value)){
		strErrorMsg += "---Telephone number\n";
	}
	
	if(!validateIfBlankField(f.address.value)){
		strErrorMsg += "---Address\n";
	}
	
	if(!validateIfBlankField(f.postcode.value)){
		strErrorMsg += "---Postcode\n";
	}
	
	if(!validateIfBlankField(f.username.value)){
		strErrorMsg += "---Username\n";
	}
	
	if(!validateIfBlankField(f.password.value)){
		strErrorMsg += "---Password\n";
	}
	
	if(!validateIfBlankField(f.passwordConfirm.value)){
		strErrorMsg += "---Confirm Password\n";
	}
	
	if(!validateStringLength(f.password.value, 5, 20) || !validateStringLength(f.passwordConfirm.value, 5, 20)){
		strErrorMsg += "---Your password must be between 5 and 20 characters long\n";
	}	
	
	if(!validateStringMatch(f.password.value, f.passwordConfirm.value)){
		strErrorMsg += "---Your password confirmation does not match your new password\n";
	}
	
	if(!validateInvalidPasswordChars(f.password.value)){
		strErrorMsg += "---Passwords can only contain the characters a-z or numbers 0-9\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** edit address functions
function submitEditAddress(){
	return validateEditAddress();
}

function validateEditAddress(){
	var f = document.fEditAddress;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.addressName.value)){
		strErrorMsg += "---Address name\n";
	}
	
	if(!validateIfBlankField(f.address.value)){
		strErrorMsg += "---Address\n";
	}
	
	if(!validateIfBlankField(f.postcode.value)){
		strErrorMsg += "---Postcode\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** can't find form functions
function submitCantFind(){
	return validateCantFind();
}

function validateCantFind(){
	var f = document.fCantFind;
	var strErrorMsg = "";
	
	f.mode.value = "sendEmail";
	
	if(!validateIfBlankField(f.name.value)){
		strErrorMsg += "---Name\n";
	}
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(!validateIfBlankField(f.cantFind.value)){
		strErrorMsg += "---Tell us what you are looking for\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** checkout1.asp functions
function submitCheckout1(){
	return validateCheckout1();
}

function validateCheckout1(){
	var f = document.fCheckout;
	var strErrorMsg = "";
		
	if(!validateIfBlankField(f.firstName.value)){
		strErrorMsg += "---Invoice First Name\n";
	}
	
	if(!validateIfBlankField(f.surname.value)){
		strErrorMsg += "---Invoice Surname\n";
	}
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(!validateIfBlankField(f.telephone.value)){
		strErrorMsg += "---Invoice Telephone\n";
	}
	
	if(!validateIfSelectBoxFilled(f.idxCustomerAddress.options[f.idxCustomerAddress.selectedIndex].value)){
		if(!validateIfBlankField(f.address.value) || !validateIfBlankField(f.postcode.value)){
			strErrorMsg += "---Invoice Address and Postcode\n";
		}		
	}
	
	if(validateIfSelectBoxFilled(f.idxCustomerAddress.options[f.idxCustomerAddress.selectedIndex].value)){
		if(validateIfBlankField(f.address.value) || validateIfBlankField(f.postcode.value)){
			strErrorMsg += "---Please choose an Invoice Address from your list\n    OR\n---Enter a new address and postcode in the boxes provided\n";
		}	
	}
	
	if(!validateIfSelectBoxFilled(f.Country.options[f.Country.selectedIndex].value)){
		strErrorMsg += "---Country\n";
	}
	
	if(!validateIfSelectBoxFilled(f.cbWhere.options[f.cbWhere.selectedIndex].value)){
		strErrorMsg += "---Where Found\n";
	}	
	
	if(!validateIfBlankField(f.firstNameDel.value)){
		strErrorMsg += "---Delivery First Name\n";
	}
	
	if(!validateIfBlankField(f.surnameDel.value)){
		strErrorMsg += "---Delivery Surname\n";
	}
	
	if(!validateIfBlankField(f.telephoneDel.value)){
		strErrorMsg += "---Delivery Telephone\n";
	}
	
	if(!validateIfSelectBoxFilled(f.idxCustomerAddressDel.options[f.idxCustomerAddressDel.selectedIndex].value)){
		if(!validateIfBlankField(f.addressDel.value) || !validateIfBlankField(f.postcodeDel.value)){
			strErrorMsg += "---Delivery Address and Postcode\n";
		}		
	}
	
	if(validateIfSelectBoxFilled(f.idxCustomerAddressDel.options[f.idxCustomerAddressDel.selectedIndex].value)){
		if(validateIfBlankField(f.addressDel.value) || validateIfBlankField(f.postcodeDel.value)){
			strErrorMsg += "---Please choose a Delivery Address from your list\n    OR\n---Enter a new address and postcode in the boxes provided\n";
		}	
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		if(confirm('If you are sure that your order details are correct, click the \"OK\" button to continue with your order.\nIf you wish to edit your order, click \"Cancel\".')) {
			f.submit();
		}
	}
}

//** checkout3.asp functions
function submitCheckout3(){
	return validateCheckout3();
}

function validateCheckout3(){
	var f = document.fCheckout;
	var strErrorMsg = "";
	f.mode.value = "checkPayment";
	
	if(!validateIfBlankField(f.CardHolder.value)){
		strErrorMsg += "---Card Holder's Name\n";
	}
	
	if(!validateIfSelectBoxFilled(f.CardType.options[f.CardType.selectedIndex].value)){
		strErrorMsg += "---Payment Method\n";
	}
	
	if(!validateIfBlankField(f.CardNumber.value)){
		strErrorMsg += "---Card Number\n";
	}
	else if(!validateDigitsOnly(f.CardNumber.value)) {	// check it is only digits
		strErrorMsg += "---Please ensure the card number is made up of digits only; no spaces or invalid characters\n";
	}
	else if(!mod10( f.CardNumber.value )) {	// LUHN algorithm check
		strErrorMsg += "---The card number does not appear to be valid\n";
	}
	else if(validateIfSelectBoxFilled(f.CardType.options[f.CardType.selectedIndex].value)) {	// length and validity checks against the type of card chosen
		var cardType = f.CardType.options[f.CardType.selectedIndex].value;
		var cardNumber = f.CardNumber.value;
		var length = cardNumber.length;
		
		switch( cardType ) {	
			case 'MC':
			
				if( length != 16 ) {
					strErrorMsg += "---Please enter a valid Mastercard number\n";
					//return;
				}
				
				var prefix = parseInt( cardNumber.substring(0,2));
				if( prefix < 51 || prefix > 55) {
					strErrorMsg += "---Please enter a valid Mastercard number\n";
					//return;
				}
				break;
				
			case 'Visa':
				
				if( length != 16 && length != 13 ) {
					strErrorMsg += "---Please enter a valid Visa number\n";
					//return;
				}
				
				var prefix = parseInt( cardNumber.substring(0,1));
				
				if( prefix != 4 ) {
					strErrorMsg += "---Please enter a valid Visa number\n";
					//return;
				}
				break;
			
			case 'Delta':
				
				if( length != 16 && length != 13 ) {
					strErrorMsg += "---Please enter a valid Visa Delta number\n";
					//return;
				}
				
				var prefix = parseInt( cardNumber.substring(0,1));
				
				if( prefix != 4 ) {
					strErrorMsg += "---Please enter a valid Visa Delta number\n";
					//return;
				}
				break;
			
			case 'Switch':
				
				if( length < 16 || length > 19 ) {
					strErrorMsg += "---Please enter a valid Switch number\n";
					//return;
				}
				
				break;
			
			case 'Solo':
				
				if( length < 16 || length > 19 ) {
					strErrorMsg += "---Please enter a valid Solo number\n";
					//return;
				}
				
				break;
		}
	}
	
	if(!validateIfBlankField(f.CV2.value)){
		strErrorMsg += "---CV2 Code\n";
	}
	else {
		if(!validateStringLength(f.CV2.value, 3, 4)){
			strErrorMsg += "---CV2 Code must be 3 or 4 characters long\n";
		}
	}
		
	/*
	if(f.CardType.options[f.CardType.selectedIndex].value == "Switch"){
		if(!validateIfSelectBoxFilled(f.StartMonth.options[f.StartMonth.selectedIndex].value) || !validateIfSelectBoxFilled(f.StartYear.options[f.StartYear.selectedIndex].value)){
			strErrorMsg += "---Start Date\n";
		}
	}

	*/
	if(!validateIfSelectBoxFilled(f.ExpiryMonth.options[f.ExpiryMonth.selectedIndex].value) || !validateIfSelectBoxFilled(f.ExpiryYear.options[f.ExpiryYear.selectedIndex].value)){
		strErrorMsg += "---Expiry Date\n";
	}

	/*
	if(!validateIfBlankField(f.IssueNumber.value)){
		if(f.CardType.options[f.CardType.selectedIndex].value == "Switch"){
			strErrorMsg += "---Issue Number\n";
		}
	}
	*/
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		if(confirm('Your order could take over 30 seconds to process.\nOnce you click \"OK\", your order will start processing.\nPlease do not make any other mouse clicks or refresh your page as this may corrupt your order.')) {
			f.submit();
		}
	}
}

//** contact us form functions
function submitContactUs(){
	return validateContactUs();
}

function validateContactUs(){
	var f = document.fContactUs;
	var strErrorMsg = "";
	
	f.mode.value = "sendEmail";
	
	if(!validateIfBlankField(f.name.value)){
		strErrorMsg += "---Name\n";
	}
	
	if(!validateIfBlankField(f.email.value) && !validateIfBlankField(f.telephone.value)){
		strErrorMsg += "---Please provide an email address or telephone number so we can contact you\n";
	}
	else{
		if(validateIfBlankField(f.email.value)){
			if(!validateEmail(f.email.value)){
				strErrorMsg += "---Email (must be a valid address)\n";
			}
		}
	}		
	
	if(!validateIfBlankField(f.queries.value)){
		strErrorMsg += "---Please enter any queries or comments you may have\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** contact us form functions
function submitForgottenPassword(){
	return validateForgottenPassword();
}

function validateForgottenPassword(){
	var f = document.fForgottenPassword;
	var strErrorMsg = "";
	
	f.mode.value = "checkPassword";
	
	if(!validateEmail(f.email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}
//-->
