<!--
//** generic functions *****************************************************************************

function toggleMainImage(imgSrc){
	if(document.all || document.layers){
		if (imgSrc != ""){
			eval("document.mainImage.src=\"" + imgSrc + "\"");
		}
	}
}

function anesiProductAlert(){
	alert("Anesi products are only available to registered Anesi Trade Customers. If you do not have an Anesi Trade Account with Smart Buy, please enquire via our 'Contact Us' Page.");
}

function addToCart(idxProduct, qty){
	var f = document.fProductListing;
	//submit the form
	f.mode.value = "add";
	f.idxProduct.value = idxProduct;
	f.qty.value = qty;
	f.action = "Cart.asp";
	f.submit();
}

function updateProductDetails(theForm, mode){
	var f = eval("document." + theForm);
	f.mode.value = mode;
	//submit the form
	if(mode == "add"){
		f.action = "Cart.asp";
	}
	else {
		f.action = "ProductDetail.asp?idxProduct=" + f.idxProduct.value;
	}
	f.submit();
}

function removeCartItem(mode, idxProduct, productName){
	if(productName == "") {
		productName = "this product";
	}
	if(confirm("Are you sure you wish to remove " + productName + " from your shopping bag?")){
		var f = document.fCart;
		f.mode.value = mode;
		f.idxProduct.value = idxProduct;
		f.action = "Cart.asp";
		f.submit();
	}
}

function updateCart(mode){
	
	var f = document.fCart;
	f.mode.value = mode;
	
	var i = 0;
	
	var iFormLength = f.length;				//form length
	var element;							//represents current form element
	
	var arrQty = new Array();				//array
	var arrIdxProduct = new Array();		//array
	var strQty = "";						//string variable for quantity
	var strIdxProduct = "";					//string variable for idxProduct
	
	//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
				if(isNaN(element.value)) {											//stop people entering erroneus quantities
					element.value = 1;
				}
				strQty = strQty + element.value + ",";
			}
		}
		if(element.type == "hidden"){
			if(element.name.indexOf("idxProduct-") == 0){				//we have found idxProduct field
				strIdxProduct = strIdxProduct + element.value + ",";
			}
		}
	}
	
	f.arrQty.value = strQty;
	f.arrIdxProduct.value = strIdxProduct;
	f.action = "Cart.asp";
	f.submit();
}

//** minimum Order Check
function minOrderCheck(orderValue) {
  var minOrderValue = 20;
  
  if(orderValue < minOrderValue) {
    alert("Minimum order value is £" + minOrderValue + ".\n\nPlease top-up your cart.");
    return false;
  }
  else{
    return true;
  }
}

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 update(textbox){
	textbox.value = parseInt(textbox.value);
	if(textbox.value == 'NaN')
		textbox.value = 1;
	if (textbox.value < 1)
		textbox.value = 1;
}

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;	
}

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.
  if(cardNumber == "4111111111111111"){
    return true;
  }
	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 ********************************************************************************

//**Search form validation
function submitSearch(){
	return validateSearch();
}

function validateSearch(){
	var f = document.fSearch;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.searchString.value)){
		strErrorMsg += "---Product Name\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following field in order to search:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//**Trade login form validation
function submitTradeLogin(){
	return validateTradeLogin();
}

//**Trade login form validation
function submitRetrieveLogin(){
	return validateRetrieveLogin();
}

function validateRetrieveLogin(){
	var f = document.fRetrieve;
	var strErrorMsg = "";
		
	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{
	  f.mode.value = "sendEmail";
		return true;
	}
}

function validateTradeLogin(){
	var f = document.fTradeLogin;
	var strErrorMsg = "";
	
	if(!validateIfBlankField(f.username.value)){
		strErrorMsg += "---Username\n";
	}
	
	if(!validateIfBlankField(f.password.value)){
		strErrorMsg += "---Password\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following field in order to login:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** checkout1.asp functions
function submitCheckout1(){
	if (validateCheckout1()==true){
		//seek user confirmation
		return confirm("Are you sure?");
	}
	else{
		return false;
	}
}

function validateCheckout1(){
	var f = document.fCheckout1;
	var strErrorMsg = "";
	
	//invoice and delivery details
	if(!validateIfBlankField(f.InvoiceName.value)){
		strErrorMsg += "---Invoice Name\n";
	}

	if(!validateIfBlankField(f.InvoiceTelephone.value)){
		strErrorMsg += "---Invoice telephone\n";
	}

	
	if(!validateEmail(f.InvoiceEmail.value)){
		strErrorMsg += "---Invoice Email (must be a valid address)\n";
	}
	
	if(!validateIfBlankField(f.InvoiceAddress.value)){
		strErrorMsg += "---Invoice Address\n";
	}
	
	if(!validateIfBlankField(f.InvoicePostcode.value)){
		strErrorMsg += "---Invoice Postcode\n";
	}
	
	if(f.TradeUserName.value == "") {
		//we have a retail customer, so we need to validate the delivery details against the checkbox
		if(!(f.SameAsInvoiceDetails.checked == true)) {
			if(!validateIfBlankField(f.DeliveryName.value)){
				strErrorMsg += "---Delivery Name\n";
			}
			
			if(!validateIfBlankField(f.DeliveryAddress.value)){
				strErrorMsg += "---Delivery Address\n";
			}
			
			if(!validateIfBlankField(f.DeliveryPostcode.value)){
				strErrorMsg += "---Delivery Postcode\n";
			}
		}
	}
	
	if(f.Terms.value == "False"){
	  //payment details
	  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(!(f.chkTC.checked)){
		  strErrorMsg += "---Terms & Conditions\n";
	  }

	if(strErrorMsg != ""){
		strErrorMsg = "Please complete/amend the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}

//** 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.Comments.value) || (f.Comments.value == "Enter your enquiry/comments here...")){
		strErrorMsg += "---Please enter your enquiry/comments\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;
	}
}

//** trade login register form functions
function submitTradeLoginRegister(){
	return validateTradeLoginRegister();
}

function validateTradeLoginRegister(){
	var f = document.fTradeLoginRegister;
	var strErrorMsg = "";
	
	f.mode.value = "sendEmail";
	
	if(!validateIfBlankField(f.Name.value)){
		strErrorMsg += "---Company Name\n";
	}
	
	if(!validateIfBlankField(f.ContactName.value)){
		strErrorMsg += "---Contact Name\n";
	}
	
	if(!validateEmail(f.Email.value)){
		strErrorMsg += "---Email (must be a valid address)\n";
	}	
	
	if(!validateIfBlankField(f.Username.value)){
		strErrorMsg += "---Username\n";
	}
	
	if(!validateIfBlankField(f.Password.value)){
		strErrorMsg += "---Password\n";
	}
	
	if(strErrorMsg != ""){
		strErrorMsg = "Please complete the following fields:\n" + strErrorMsg;
		alert(strErrorMsg);
		return false;
	}
	else{
		return true;
	}
}
//-->
