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 = "" ;
	var i; 

	if ( num == 0 )
	{
		Form.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;
		}
		
		Form.target = "_top";
		Form.action = Form.securebasket.value
		Form.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_option') > -1 && 
				((elevalue > 0 && Element.checked) ||
				(elevalue == "free" || elevalue == "-1")))
			{
				//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;
		}
		
		//Form.target = "_top";
		//Form.action= Form.securebasket.value
		Form.ACTION.value = "mailorder";
		
	}
	else
	{
		Form.ACTION.value = "clear";
	}
	Form.submit();
}



function validate_basket(Form)
{
	//Form.securebasket.value = "ajax_rbadminorder.php";
	
	var valid;
	//re-jigged old validate_basket
	valid = validate_basket_init(Form);
	
	if(valid)
	{
		//alert('valid');
		
		var myform = top.document.form1;
		
		myform.Submit.disabled = true;
		//myform.Submit.value = 'Please Wait...';
		$('submit_button').removeClass('complete_order');
		$('submit_button').addClass('please_wait');
		//myform.Back.disabled = true;
		//myform.Reset.disabled = true;
		
		$('form1').set('action', 'rbbasket_ajax.php');
		
		//Empty the res and show the spinning indicator.
		//var res = $('basket').empty().addClass('ajax-loading');
		var res = $('basket_form_div').addClass('ajax-loading');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('basket_form') element).
		var browser = BrowserDetect.browser ;
		var	browserversion = BrowserDetect.version
		
		
		
		
		
		
		
		
		var url = myform.getProperty('action') + "?" + myform.toQueryString();
		new Request({
		   url: url,
		   method: 'post',
		   onFailure: function(instance){
				// ...
				alert('Sorry the request failed. Please wait and try again. If problems persist, please contact us for help.');
		   },
		   onSuccess: function(response){
				// ...
				var myres = response.substring(0, 2);
				//alert(myres);
				
				if(myres == 'OK') { 
					//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.");
					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.");
					//remove OK
					
					//display thank you page
					top.location.href='/scripts/thank-you.php';
					
					
					// response = response.substring(2);
					// //$('form1').set('action', '".$roofshop."');
					// $('form1').empty();
					// $('form1').set('html', response); 
				}
				else {
					alert("Sorry, there was a problem with your order. Please see issues above in order to proceed.");
					//remove ERROR
					response = response.substring(5);
					$('ajax-message').removeClass('hidden');
					$('ajax-message').addClass('error');
					$('ajax-message').set('html', response); 
					myform.Submit.disabled = false;
					//myform.Submit.value = 'Complete Order';
					$('submit_button').removeClass('please_wait');
					$('submit_button').addClass('complete_order');
					//myform.Back.disabled = false;
					//myform.Reset.disabled = false;
					//$('form1').set('action', '".$roofshop."');
				}
				
				res.removeClass('ajax-loading');
		   }
		}).send(); 
		
		
		
		
		
		
		
		
		
		
		
		
		//if ( browser == "Explorer")
		/*
		if (browserversion <= 7 && browser == "Explorer")
		{
			$('form1').set('action', 'rbbasket.php');
			myform.submit();
		}
		else
		{
		*/
			/*
			$('form1').set('send', {
				onComplete: function(response) {
					var myres = response.substring(0, 2);
					//alert(myres);
					
					if(myres == 'OK') { 
						//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.");
						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.");
						//remove OK
						response = response.substring(2);
						//$('form1').set('action', '".$roofshop."');
						$('form1').empty();
						$('form1').set('html', response); 
					}
					else {
						alert("Sorry, there was a problem with your order. Please see issues above in order to proceed.");
						//remove ERROR
						response = response.substring(5);
						$('ajax-message').removeClass('hidden');
						$('ajax-message').addClass('error');
						$('ajax-message').set('html', response); 
						myform.Submit.disabled = false;
						//myform.Submit.value = 'Complete Order';
						$('submit_button').removeClass('please_wait');
						$('submit_button').addClass('complete_order');
						myform.Back.disabled = false;
						//myform.Reset.disabled = false;
						//$('form1').set('action', '".$roofshop."');
					}
					
					res.removeClass('ajax-loading');
					
				},
				 onFailure: function() {
					alert('Sorry the request failed. Please wait and try again. If problems persist, please contact us for help.');
					//res.set('html', 'AJAX error - The request failed.');
				 }
			 }).send();
			 */
		//}
		
	}
	else
	{
		return false;
	}
	return false;
}


//re-jig of old validate_basket for AJAX version
function validate_basket_init(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 Contact = "";
	var Contact2 = "";
	var hire = "";
	var trade = "";
	var startmonth;
	var startyear;
	var expiremonth;
	var expireyear;
	var password1 = "";
	var password2 = "";
	var how_heard_select =  selectval(Form.How_heard_Select);
	var how_heard_other = how_heard_select.indexOf('please specify'); 
	var how_heard = "";
	var del_inst = "";


	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 == 'Card_Expireyear'  )
		{
			expireyear = Element.value;
		}	
		if (Element.name == 'Card_Expiremonth'  )
		{
			expiremonth = Element.value;
		}	
		if (Element.name == 'Email')
		{
			email = Element.value;
		}
		if (Element.name == 'Contact')
		{
			Contact = Element.value;
		}
		if (Element.name == 'Contact2')
		{
			Contact2 = 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_Other')
		{
			how_heard = Element.value;
		}
		if (Element.name == 'Delivery_Instructions')
		{
			del_inst = 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";
	}
	var today = new Date();
	var thismonth = today.getMonth() + 1;
	//var thisyear = today.getYear();
	var thisyear = today.getFullYear();
	
	thismonth = parseInt(thismonth);
	thisyear = parseInt(thisyear);
	
	/*
	if (thismonth < 10)
	{
		thismonth = "0" + thismonth.toString();
	}
	var thisyearmonth = thisyear.toString() + thismonth.toString();
	var startyearmonth = startyear.toString() + startmonth.toString();
	var expireyearmonth = expireyear.toString() + expiremonth.toString();
	*/

	//if (startyearmonth != "" && startyearmonth > thisyearmonth)
	if ((startyear != "" && startmonth != "") && (parseInt(startyear) > thisyear || (parseInt(startyear) == thisyear && parseInt(startmonth) > thismonth)))
	{
		//BadFields += "- " + "Card Start Date is not in the past"	 + "\n";
	}
	//if (expireyearmonth != "" && expireyearmonth < thisyearmonth)
	if ((expireyear != "" && expiremonth != "") && (parseInt(expireyear) < thisyear || (parseInt(expireyear) == thisyear && parseInt(expiremonth) < thismonth)))
	{
		//BadFields += "- " + "Card Expiry Date is not in the future"	 + "\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 == 'Contact2'    ||
				 Element.name == 'Address1'   ||
				 Element.name == 'Town'		||
				 Element.name == 'PostalCode' ||
				 Element.name == 'Country'    ||
				 Element.name == 'Card_Number'    ||
				 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 == 'Contact2' )
					{
						elename = "Retype Contact Phone No."
					}
					if ( elename == 'Card_Startmonth' )
					{
						elename = "Valid from month"
					}
					if ( elename == 'Card_Number' )
					{
						elename = "Card number"
					}
					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) || ccnumber.length < 13)
		{
			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 between 13 and 19 digits inclusive 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;

		}
	}
	if (del_inst != '')
	{
		if (del_inst.length > 180) 
		{
			outMessage = 'We are unable to proceed as the following\n';
			outMessage += "fields have not been completed correctly:\n";
			outMessage += "Detailed delivery instructions are longer than 180 characters";
		
			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(Contact != Contact2)
	{
		outMessage = 'We are unable to proceed as your contact phone numbers\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;
  var i;
  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();
}

function Non_UK_Click()
{
var cb = document.getElementById("Non_UK").checked;
if (cb == true)
{
	alert("checked");
	Clear_Postcodes();

}

}


function Postcode_Missing_Click()
{
var cb = document.getElementById("Postcode_Missing").checked;
if (cb == true)
{
	alert("checked");
	Clear_Postcodes();


}
}
function Clear_Postcodes()
{
var bp = document.getElementById("Billing_Postcode");
bp.value = "";
var dp = document.getElementById("Delivery_Postcode");
dp.value = "";
select_ID = "Billing_PostKey";
Clear_Property_Selector(true,false);
select_ID = "Delivery_PostKey";
Clear_Property_Selector(true,false);
Set_Value("Billing_PostKey_Save","");
Set_Value("Delivery_PostKey_Save","");

var message = document.getElementById("Billing_PostcodeResponse");
message.innerHTML = ""
var message = document.getElementById("Delivery_PostcodeResponse");
message.innerHTML = ""

ajax_validate("", "Billing_Postcode","","")
ajax_validate("", "Delivery_Postcode","","")

}

/*
function TBC_Delivery_Click(del)
{
	var cb = del.checked;
	if (cb == true)
	{
		del.checked = confirm("! You've selected a delivery option with a TBC cost. This is intended for deliveries outside the GB mainland. We'll have to contact you to confirm the delivery charge.\nPlease confirm that delivery is to an address outside the GB mainland.");
		non_uk = document.getElementById("Non_UK");
		non_uk.checked = del.checked;
	}
}
*/

function TBC_Delivery_Click(del)
{
	if (del.className == "TBC_del")
	{
		var res = confirm("! You've selected a delivery option with a TBC cost. This is intended for deliveries outside the GB mainland. We'll have to contact you to confirm the delivery charge.\nPlease confirm that delivery is to an address outside the GB mainland.");
		non_uk = document.getElementById("Non_UK");
		non_uk.checked = res;
		
		if(res == false)
		{
			//uncheck radiobutton
			setCheckedValue(del,'');
		}
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function TBC_del()
{
	var aryClassElements = getElementsByClassName('TBC_del', document.body);
	var tbc = aryClassElements[0];
	
	var non_uk = document.getElementById("Non_UK");
	if(tbc)
	{
		tbc.checked = non_uk.checked;
	}
		//alert('checked');
		
		//if(confirm("! As your address as not listed above we've selected a delivery option with a TBC cost. This is intended for deliveries outside the GB mainland. We'll have to contact you to confirm the delivery charge."))
		//{
		//}
}

function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    
    return ar;
}


function deletebillingaddress()
{
var bp = document.getElementById("Billing_Postcode");
bp.value = "";
select_ID = "Billing_PostKey";
Clear_Property_Selector(true,true);
Set_Value("Billing_PostKey_Save","");

ajax_validate("", "Billing_Postcode","","")
ajax_validate("", "Billing_PostKey","","")
var message = document.getElementById("Billing_PostcodeResponse");
message.innerHTML = "&nbsp;"


}

function deletedeliveryaddress()
{
var dp = document.getElementById("Delivery_Postcode");
dp.value = "";
select_ID = "Delivery_PostKey";
Clear_Property_Selector(true,true);
Set_Value("Delivery_PostKey_Save","");

ajax_validate("", "Delivery_Postcode","","")
ajax_validate("", "Delivery_PostKey","","")
var message = document.getElementById("Delivery_PostcodeResponse");
message.innerHTML = "&nbsp;"


}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();




function Update_Order_Total(price, ttl)
{
	var ele;
	var new_ttl;
	
	ele = document.getElementById('ttl_inc_del');
	
	if(ele)
	{
		if(price == "TBC")
		{
			new_ttl = ttl;
		}
		else
		{
			new_ttl = parseFloat(ttl) + parseFloat(price);
			new_ttl = new_ttl.toFixed(2);
		}
		
		ele.innerHTML = "&pound;"+new_ttl; 
	}
}


