function onEnter( evt, field,seshid,fieldid2) 
{
	var keyCode = null;
	if( evt.which ) 
	{
		keyCode = evt.which;
	} 
	else if( evt.keyCode ) 
	{
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode ) 
	{
		if (field.value)
		{
			if (field.value.length > 5)
			{
				ajax_validate(field.value, field.id,seshid,fieldid2);
				Set_Value(fieldid2 + '_Save','');
				//Focus(fieldid2);
				return false;
			}
		}
	}
	return true;
}


function disableEnterKey()
{
//alert("keycode " + window.event.keyCode + "'");
if (window.event.keyCode == 13) window.event.keyCode = 0;
}
function entsub(event,ourform) {
  	if (event && event.which == 13)
	{
//	 	alert("key code '" + event.which + "'");
		return false;
	}
}

function handleClick(num)
{
  var elevalue;
  var Element;
  var elename;
  var badfields;
  var delivfound = "N";
  var postcodefound = "N";
  var propertyfound = "N";
  var delivery_postcodefound = "N";
  var delivery_propertyfound = "N";
  var outMessage;
  var Form = document.myForm;
  var BadFields = "" ;

  if ( num == 0 )
  {
	document.myForm.ACTION.value = "recalc";
	for ( i = 0; i < Form.length; i++ )
	{
		Element = Form.elements[i];
		elename= Element.name
		elevalue= Element.value
//		alert(elename + " " + elevalue);
		if (elename == "Non_UK" && elevalue > 0 && Element.checked)
		{
			postcodefound = "Y";
			propertyfound = "Y";
		}
		if (elename == "logged_in" && elevalue  == "TRUE")
		{
			postcodefound = "Y";
			propertyfound = "Y";
		}
		if (elename == "Billing_Postcode" && elevalue.length >= 4 & elevalue.substring(0,5) != "Error")
		{
			postcodefound = "Y";
		}
		if (elename == "Billing_PostKey" && selectval(Element) != "")
		{
			//alert(elename + ":" + selectval(Element))
			propertyfound = "Y";
		}
		if (elename == "Billing_PostKey_Save" && elevalue != "")
		{
			propertyfound = "Y";
		}
		if (elename == "Delivery_Postcode" && elevalue.length >= 4  & elevalue.substring(0,5) != "Error")
		{
			delivery_postcodefound = "Y";
		}
		if (elename == "Delivery_PostKey" && selectval(Element) != "")
		{
			delivery_propertyfound = "Y";
		}
		if (elename == "Delivery_PostKey_Save" && elevalue != "")
		{
			delivery_propertyfound = "Y";
		}
	}
			
	if (postcodefound == "Y" && propertyfound == "N")
//	if (postcodefound == "N")
	{
		BadFields += " - Billing address 1\n";
	}
	if (delivery_postcodefound == "Y" && delivery_propertyfound == "N")
//	if (delivery_postcodefound == "Y" )
	{
		BadFields += " - Delivery address\n";
	}
	if ( BadFields )
	{
		outMessage = 'We are unable to proceed as the following\n';
		outMessage += "required fields have not been completed:\n\n";
		outMessage += BadFields;
	
		alert(outMessage);
		return false;
	}

  }
  else
  {
	if ( num == 1 )
	{

		for ( i = 0; i < Form.length; i++ )
		{
			Element = Form.elements[i];
			elename= Element.name
			elevalue= Element.value
			//alert(elename)
			//alert (elename.indexOf('-del=1'))
			if (elename == "Non_UK" && elevalue > 0 && Element.checked)
			{
				postcodefound = "Y";
				propertyfound = "Y";
			}
			if (elename == "logged_in" && elevalue  == "TRUE")
			{
				postcodefound = "Y";
				propertyfound = "Y";
			}
			if (elename == "Billing_Postcode" && elevalue.length >= 4 & elevalue.substring(0,5) != "Error")
			{
				postcodefound = "Y";
			}
			if (elename == "Billing_PostKey" && selectval(Element) != "")
			{
				propertyfound = "Y";
			}
			if (elename == "Billing_PostKey_Save" && elevalue != "")
			{
				propertyfound = "Y";
			}
			if (elename == "Delivery_Postcode" && elevalue.length >= 4  & elevalue.substring(0,5) != "Error")
			{
				delivery_postcodefound = "Y";
			}
			if (elename == "Delivery_PostKey" && selectval(Element) != "")
			{
				delivery_propertyfound = "Y";
			}
			if (elename == "Delivery_PostKey_Save" && elevalue != "")
			{
				delivery_propertyfound = "Y";
			}
			
		}
		if (postcodefound == "Y" && propertyfound == "N")
//		if (postcodefound == "Y" )
		{
			BadFields += " - Billing address 2\n";
		}
		if (postcodefound == "N")
		{
			BadFields += " - UK Postcode and address or address outside the UK\n";
		}
		if (delivery_postcodefound == "Y" && delivery_propertyfound == "N")
		{
			BadFields += " - Delivery address\n";
		}
		if ( BadFields )
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "required fields have not been completed:\n\n";
			outMessage += BadFields;
	
			alert(outMessage);
			return false;
		}


		document.myForm.target = "_top";
		document.myForm.action= document.myForm.securebasket.value
		document.myForm.ACTION.value = "mailorder";
		
	}
	else
	{
		if ( num == 10 )
		{

			for ( i = 0; i < Form.length; i++ )
			{
				Element = Form.elements[i];
				elename= Element.name
				elevalue= Element.value
				//alert(elename)
				//alert (elename.indexOf('-del=1'))
				if (elename.indexOf('-del') > -1 && 
					((elevalue > 0 && Element.checked) ||
					(elevalue == "free")))
				{
					//alert(elename)
					//alert (elevalue)
					delivfound = "Y";
				}
				if (elename == "Non_UK" && elevalue > 0 && Element.checked)
				{
					postcodefound = "Y";
					propertyfound = "Y";
				}
				if (elename == "logged_in" && elevalue  == "TRUE")
				{
					postcodefound = "Y";
					propertyfound = "Y";
				}
				if (elename == "Billing_Postcode" && elevalue.length >= 4 & elevalue.substring(0,5) != "Error")
				{
					postcodefound = "Y";
				}
				if (elename == "Billing_PostKey" && selectval(Element) != "")
				{
					propertyfound = "Y";
				}
				if (elename == "Billing_PostKey_Save" && elevalue != "")
				{
					propertyfound = "Y";
				}
				if (elename == "Delivery_Postcode" && elevalue.length >= 4 & elevalue.substring(0,5) != "Error")
				{
					delivery_postcodefound = "Y";
				}
				if (elename == "Delivery_PostKey" && selectval(Element) != "")
				{
					delivery_propertyfound = "Y";
				}
				if (elename == "Delivery_PostKey_Save" && elevalue != "")
				{
					delivery_propertyfound = "Y";
				}
				
			}
			if (postcodefound == "Y" && propertyfound == "N")
//			if (postcodefound == "Y" )
			{
				BadFields += " - Billing address 3\n";
			}
			if (postcodefound == "N")
			{
				BadFields += " - UK Postcode and address, or address outside the UK\n";
			}
			if (delivery_postcodefound == "Y" && delivery_propertyfound == "N")
			{
				BadFields += " - Delivery address\n";
			}
			if (delivfound == "N")
			{
				BadFields += " - Delivery Option\n";
			}
			if ( BadFields )
			{
				outMessage = 'We are unable to proceed as the following\n';
				outMessage += "required fields have not been completed:\n\n";
				outMessage += BadFields;
		
				alert(outMessage);
				return false;
			}
		
	
			//document.myForm.target = "_top";
			//document.myForm.action= document.myForm.securebasket.value
			document.myForm.ACTION.value = "mailorder";
			
		}
		else
		{
			document.myForm.ACTION.value = "clear";
		}
		}
  }
  document.myForm.submit();

}


function validate_basket(Form)
{
	Form.Submit.disabled = true;
	var Element;
	var elename;
	var i;
	var BadFields = "";
	var outMessage = "";
	var ccnumber = "";
	var cctype = "";
	var ccsecno = "";
	var ccissueno = "";
	var email = "";
	var email2 = "";
	var hire = "";
	var trade = "";
	var startmonth;
	var startyear;
	var password1 = "";
	var password2 = "";
	var how_heard_select =  selectval(document.form1.How_heard_Select);
	var how_heard_other = how_heard_select.indexOf('please specify'); 
	var how_heard = "";


	for ( i = 0; i < Form.length; i++ )
	{
		Element = Form.elements[i];

		if (Element.name == 'Hire')
		{
			hire = Element.value;
		}
		if (Element.name == 'Trade')
		{
			trade = Element.value;
		}
		if (Element.name == 'Card_Type')
		{
			cctype = selectval(Element)
			//alert(cctype);
		}
		if (Element.name == 'Card_Number')
		{
			ccnumber = Element.value;
		}
		if (Element.name == 'Issue_Number')
		{
			ccissueno = Element.value;
		}
		if (Element.name == 'Security_Code')
		{
			ccsecno = Element.value;
		}
		if (Element.name == 'Card_Startyear'  )
		{
			startyear = Element.value;
		}	
		if (Element.name == 'Card_Startmonth'  )
		{
			startmonth = Element.value;
		}	
		if (Element.name == 'Email')
		{
			email = Element.value;
		}
		if (Element.name == 'Email2')
		{
			email2 = Element.value;
		}
		if (Element.name == 'password1')
		{
			password1 = Element.value;
		}
		if (Element.name == 'password2')
		{
			password2 = Element.value;
		}
		if (Element.name == 'How_heard')
		{
			how_heard = Element.value;
		}

	}
	if (how_heard == "" && how_heard_other > 0)
	{
		BadFields += "- " + "How did you hear about us?"	 + "\n";
	}
	if (ccnumber != '' && cctype == "Switch" && (startyear == "" && startmonth == "" && ccissueno == ""))
	{
		BadFields += "- " + "Card Start Date or Issue number"	 + "\n";
	}

	if (ccnumber == '' && hire == '' && trade == '' && password1 == '')
	{
		for ( i = 0; i < Form.length; i++ )
	  	{
			Element = Form.elements[i];
		
			if ( 
				 Element.name == 'Forename'    ||
				 Element.name == 'Surname' ||
				 Element.name == 'Contact' ||
				 Element.name == 'Email'  ||
				 Element.name == 'Email2'  )
			{
				if ( Element.value == ""  ||
			 		Element.value == " "  )
					{
					if ( Element.name == 'Contact' )
					{
							BadFields += "- " + Element.name + " Phone No.\n";
					}
					else
					{
							BadFields += "- " + Element.name + "\n";
					}
			 	}
			}
		}
	}
	else
	{
		for ( i = 0; i < Form.length; i++ )
		{
			Element = Form.elements[i];
		
			if ( 
				 Element.name == 'Forename'     ||
				 Element.name == 'Surname'     ||
				 Element.name == 'Contact'    ||
				 Element.name == 'Address1'   ||
				 Element.name == 'Town'		||
				 Element.name == 'PostalCode' ||
				 Element.name == 'Country'    ||
				 Element.name == 'Card_Type'    ||
				 Element.name == 'Card_Name'    ||
				 Element.name == 'Card_Expiremonth'    ||
				 Element.name == 'Card_Expireyear'    ||
				 Element.name == 'Security_Code'    ||
				 Element.name == 'Delivery_Instructions'    ||
				 Element.name == 'Delivery_Select'    ||
				 Element.name == 'Car'    ||
				 Element.name == 'Tyres'    ||
				 Element.name == 'Email'  ||
				 Element.name == 'Email2'  )
			{
				if ( Element.value == ""  ||
			 		Element.value == " "  )
					{
					elename = Element.name;
					if ( elename == 'Address1' )
					{
						elename = "Address"
					}
					if ( elename == 'Contact' )
					{
						elename = "Contact Phone No."
					}
					if ( elename == 'Card_Startmonth' )
					{
						elename = "Valid from month"
					}
					if ( elename == 'Card_Startyear' )
					{
						elename = "Valid from year"
					}
					if ( elename == 'Card_Expiremonth' )
					{
						elename = "Valid to month"
					}
					if ( elename == 'Card_Expireyear' )
					{
						elename = "Valid to year"
					}
					if ( elename == 'Delivery_Select' )
					{
						elename = "Standard Delivery Instructions"
					}
					if ( elename == 'How_heard' )
					{
						elename = "How did you hear about us"
					}

					BadFields += "- " + elename + "\n";
				}
			}
		}
	}
	if ( BadFields )
	{
		outMessage = 'We are unable to proceed as the following\n';
		outMessage += "required fields have not been completed:\n";
		outMessage += BadFields;

		alert(outMessage);
		Form.Submit.disabled = false;
		return false;
	}
	if (ccnumber != '')
	{
		//reg = new RegExp(/ /g);
		//ccnumber = ccnumber.replace(reg,"");
		if (ccnumber.length > 19 || !LuhnCheck(ccnumber))
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "fields have not been completed correctly:\n";
			outMessage += "Credit/ debit card number (must be up to 19 digits with no spaces)";
		
			alert(outMessage);
			Form.Submit.disabled = false;
			return false;

		}
	}
	if (ccsecno != '')
	{
		if (ccsecno.length != 3 && ccsecno.length != 4) 
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "fields have not been completed correctly:\n";
			outMessage += "Security Code (must be 3 or 4 digits)";
		
			alert(outMessage);
			Form.Submit.disabled = false;
			return false;

		}
	}
	
	email = email.toLowerCase();
	if (!isEmailAddressValid(email))
	{
		outMessage = 'We are unable to proceed as your email address:\n';
		outMessage += email + '\n';
		outMessage += "appears to be invalid.\n";

		alert(outMessage);
		Form.Submit.disabled = false;
		return false;
	}
	
	email2 = email2.toLowerCase();
	if(email != email2)
	{
		outMessage = 'We are unable to proceed as your email addresses\n';
		outMessage += "appear to mismatch.\n";
		
		alert(outMessage);
		Form.Submit.disabled = false;
		return false;
	}
	
	if (password1 != '' || password2 != '')
	{
		if (password1.length < 6 || password2.length < 6)
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "fields have not been completed correctly:\n";
			outMessage += "New password (must be 6 or more characters)";
		
			alert(outMessage);
			Form.Submit.disabled = false;
			return false;

		}
		if (password1 != password2)
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "fields have not been completed correctly:\n";
			outMessage += "New password (must be the same as confirmation password)";
		
			alert(outMessage);
			Form.Submit.disabled = false;
			return false;

		}
	}

	//alert ("ok");
	Form.Submit.disabled = true;
	Form.Back.disabled = true;
	Form.Reset.disabled = true;
	Form.submit();
	alert("What happens next?\nYou'll see a confirmation page including your online order number.  We'll also send you an e-mail confirmation, although some customers will find that their spam filters block these e-mails. But if you don't receive the immediate confirmation page then we haven't received your order, so you'll need to order again.  Don't worry - we won't charge your card before we're ready to send the goods.");
	return true;
}




// Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by
// John Viega and Matt Messier (O'Reilly 2003)

var rfc822_specials = "()<>@,;L\\\"[]";

function isEmailAddressValid(str)
{
	
	var c, count, domain;
	var quot = '"';
	var backslash = "\\";

	for (c = 0;  c < str.length;  c++)
	{
		if (str.charAt(c) == ' ') continue;

		if (str.charAt(c) == quot &&
			(!c || str.charAt(c - 1) == "." || str.charCharAt(c - 1) == quot))
		{
			while (++c < str.length)
			{
				if (str.charAt(c) == quot) break;
				if (str.charAt(c) == backslash && (str.charAt(++c) == ' ')) continue;
				if (str.charCodeAt(c) <= 32 || str.charCodeAt(c) >= 127)
					return false;
			}
			if (c++ >= str.length) return false;
			if (str.charAt(c) == "@") break;
			if (str.charAt(c) != ".") return false;
			continue;
		}
		if (str.charAt(c) == "@") break;
		if (str.charCodeAt(c) <= 32 || str.charCodeAt(c) >= 127) return false;
		if (rfc822_specials.indexOf(str.charAt(c)) != -1) return false;
	}
	if (!c || str.charAt(c - 1) == ".") return false;

	if ((domain = ++c) >= str.length) return false;
	count = 0;
	do
	{
		if (str.charAt(c) == ' ') continue;

		if (str.charAt(c) == ".")
		{
			if (c == domain || str.charAt(c - 1) == ".") return false;
			count++;
		}
		if (str.charCodeAt(c) <= 32 || str.charCodeAt(c) >= 127) return false;
		if (rfc822_specials.indexOf(str.charAt(c)) != -1) return false;
	} while (++c < str.length);

	return (count >= 1 ? true : false);
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}

function selectval(Sel)
{
return Sel.options[Sel.selectedIndex].value;
}
function DoLogin(Form)
{
	if (Form.loginemail.value == "" ||
		Form.password.value == "")
	{
		outMessage = 'We are unable to proceed as the following\n';
		outMessage += "fields have not been completed correctly:\n";
		outMessage += "Email address and password (must be entered)";
	
		alert(outMessage);
		return false;
	}

		Form.ACTION.value = "login";
		//Form.action.value = "login";
	Form.submit();
	return true;

}
function Set_Existing_Customer (Sel)
{
var opt = Sel.options[Sel.selectedIndex].value;
if (opt.indexOf('existing customer') > -1)
{
	document.form1.Ordered_before.value = "Yes";
}
}

function deletefrombasket(prodref)
{
//var Form = document.myForm;
document.getElementById(prodref).value=0;
handleClick(0);
}

function Focus(id)
{
document.getElementById(id).focus();
}