//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

function valEmailFormAdmin(theForm)
{
	if(theForm.emailFrom.value=="")
	{
		theForm.emailFrom.focus();
		alert('Please enter from email.');
		return false;
	}
	if (emailvalidation(theForm.emailFrom,"Invalid from email address")==false) {theForm.emailFrom.focus(); return false;};
	
	if(theForm.emailName.value=="")
	{
		theForm.emailName.focus();
		alert('Please enter name.');
		return false;
	}
	if(theForm.emailTo.value=="")
	{
		theForm.emailTo.focus();
		alert('Please enter email to.');
		return false;
	}
	if (emailvalidation(theForm.emailTo,"Invalid to email address")==false) {theForm.emailTo.focus(); return false;};
	
	if(theForm.emailHeader.value=="")
	{
		theForm.emailHeader.focus();
		alert('Please enter header.');
		return false;
	}
	if(theForm.emailMessage.value=="")
	{
		theForm.emailMessage.focus();
		alert('Please enter message.');
		return false;
	}

	
	 
}

function updateCart()
{
	//document.estimate.action = "default.asp?p=Bespoke&transfer=yes";
	document.frmCart.submit();
}
function checkoutCart()
{
	if (document.frmCart.delivery.value=="0")
	{
		alert('Please select a delivery location.');
		//return false;
	}
	else
	{
		document.frmCart.action = "checkout.asp";
		document.frmCart.submit();
	}
}
function checkStock()
{
	var qty = parseInt(document.frmCart.cartquantity.value)
	var stock = parseInt(document.frmCart.cartstock.value)
	if (qty>stock)
	{
		document.frmCart.cartquantity.value = stock;
		alert('Only ' + stock + ' left in stock');
		
		return false;
	}
}
function isNumeric(elem)
{
	var checkOK = "0123456789";
	
	
	if(elem.type == "text"){

	  var checkStr = elem.value;

		 if(checkStr==""){
			allValid = false;
			alert("Please enter a number (With no spaces).");
		    elem.focus();
		    return (false);
		  }
	  	if(checkStr!=""){

		  var allValid = true;

		  for (k = 0;  k < checkStr.length;  k++)
		  {
		    ch = checkStr.charAt(k);
		    for (j = 0;  j < checkOK.length;  j++)
		      if (ch == checkOK.charAt(j))
		        break;
		    if (j == checkOK.length)
		    {
		      allValid = false;
		      break;
		    }
		  }


		
		  if (!allValid)
		  {
		    alert("Please enter a number (With no spaces).");
		    elem.focus();
		    return (false);
		  }
		}
	}
}
function maskKeyPress(objEvent, elem)
{
	  var iKeyCode;  	
	  if (isIE) {
        iKeyCode = objEvent.keyCode;
      } 
      else {
        iKeyCode = objEvent.which; 
      }		
	  if((iKeyCode>=48 && iKeyCode<=57)||(iKeyCode==46))
	  {
		if(iKeyCode==46)
		{
			//decimal point count
			var checkStr = elem.value;
			var dec = 0;
			for (k = 0;  k < checkStr.length;  k++)
			{
				ch = checkStr.charAt(k);
				if(ch=="."){dec++;}
			}
			if(dec>0){return false;}
		}
		return true;
	  }
	  return false;
}


function maskKeyPressMinus (objEvent, elem)
{
	  var iKeyCode;  	
	  if (isIE) {
        iKeyCode = objEvent.keyCode;
      } 
      else {
        iKeyCode = objEvent.which; 
      }		
	  if((iKeyCode>=48 && iKeyCode<=57)||(iKeyCode==45)||(iKeyCode==46))
	  {
		if(iKeyCode==46)
		{
			//decimal point count
			var checkStr = elem.value;
			var dec = 0;
			for (k = 0;  k < checkStr.length;  k++)
			{
				ch = checkStr.charAt(k);
				if(ch=="."){dec++;}
			}
			if(dec>0){return false;}
		}
		return true;
	  }
	  return false;
}

function valCartForm(theForm)
{
	if(theForm.delivery.value=="0")
	{
		theForm.delivery.focus();
		alert('Please select a delivery location.');
		return false;
	}
}

function valSearch(theForm)
{
	if(theForm.search.value=="")
	{
		theForm.search.focus();
		alert('Please enter a search phrase.');
		return false;
	}
}

function valLoginForm(theForm)
{
	
	if(theForm.lemail.value=="")
	{
		theForm.lemail.focus();
		alert('Please enter your email address.');
		return false;
	}

	if (emailvalidation(theForm.lemail,"Invalid email address")==false) {theForm.lemail.focus(); return false;};
	if(theForm.lpassword.value=="")
	{
		theForm.lpassword.focus();
		alert('Please enter your password.');
		return false;
	}
}
function valEmailForm(theForm)
{
	
	if(theForm.emailList.value=="")
	{
		theForm.emailList.focus();
		alert('Please enter your email address.');
		return false;
	}

	if (emailvalidation(theForm.emailList,"Invalid email address")==false) {theForm.emailList.focus(); return false;};

}
function valContactForm(theForm)
{
	if(theForm.name.value=="")
	{
	theForm.name.focus();
	alert('Please enter your name.');
	return false;
	}
	if(theForm.email.value=="")
	{
		theForm.email.focus();
		alert('Please enter your email address.');
		return false;
	}

	if (emailvalidation(theForm.email,"Invalid email address")==false) {theForm.email.focus(); return false;};
	
	if(theForm.feedback.value=="")
	{
		theForm.feedback.focus();
		alert('Please enter your comments.');
		return false;
	}
}
function savePassword()
{
	if (document.frmCheckout.saveDetails.checked)
	{
		document.frmCheckout.password.disabled= false;
	}else{
		document.frmCheckout.password.disabled= true;
	}

}
function copyAddressOver()
{
	 
	if (document.frmYourDetails.copyAddress.checked)
	{
		 
		 document.frmYourDetails.del_address1.value = document.frmYourDetails.address1.value;
		 document.frmYourDetails.del_address2.value = document.frmYourDetails.address2.value;
		 document.frmYourDetails.del_town.value = document.frmYourDetails.town.value;
		 document.frmYourDetails.del_county.value = document.frmYourDetails.county.value;
		 document.frmYourDetails.del_country.value = document.frmYourDetails.country.value;
		 document.frmYourDetails.del_postcode.value = document.frmYourDetails.postcode.value;
	}

}

function valDeliveryForm(theForm)
{
	if(theForm.del_address1.value=="")
	{
		theForm.del_address1.focus();
		alert('Please enter your delivery address.');
		return false;
	}
	
	if(theForm.del_town.value=="")
	{
		theForm.del_town.focus();
		alert('Please enter your delivery town.');
		return false;
	}
	
	if(theForm.del_postcode.value=="")
	{
		theForm.del_postcode.focus();
		alert('Please enter your delivery postcode.');
		return false;
	}
	if(theForm.del_postcode.length<6)
	{
		theForm.del_postcode.focus();
		alert('Please enter 5 characters or more for your delivery postcode.');
		return false;
	}
}
function valCheckOutForm(theForm)
{
	if(theForm.title.value=="")
	{
		//theForm.title.focus();
		//alert('Please select your title.');
		//return false;
	}
	if(theForm.first_name.value=="")
	{
		theForm.first_name.focus();
		alert('Please enter your first name.');
		return false;
	}
	if(theForm.name.value=="")
	{
		theForm.name.focus();
		alert('Please enter your surname');
		return false;
	}
	if(theForm.address1.value=="")
	{
		theForm.address1.focus();
		alert('Please enter your address.');
		return false;
	}
	
	if(theForm.town.value=="")
	{
		theForm.town.focus();
		alert('Please enter your town.');
		return false;
	}
	
	if(theForm.postcode.value=="")
	{
		theForm.postcode.focus();
		alert('Please enter your postcode.');
		return false;
	}
	if(theForm.postcode.value.length<6)
	{
		theForm.postcode.focus();
		alert('Please enter 5 characters or more for your postcode.');
		return false;
	}
	
	if(theForm.email.value=="")
	{
		theForm.email.focus();
		alert('Please enter your email address.');
		return false;
	}
	if (theForm.member.value=="0")
	{
		if(theForm.saveDetails.checked && theForm.password.value=="")
		{
			theForm.password.focus();
			alert('Please enter a password.');
			return false;
		}
	}
	if (emailvalidation(theForm.email,"Invalid email address")==false) 
	{
		theForm.email.focus(); 
		return false;
	}

	
	if(theForm.wherehear.value =="")
	{
		//alert('Please enter where you heard of us.');
		//return false;
	}
	if(!theForm.terms.checked)
	{
		alert('You must tick to accept the Terms & Conditions.');
		return false;
	}
	return true;
}
function valMembersForm(theForm)
{
	if(theForm.title.value=="")
	{
		//theForm.title.focus();
		//alert('Please select your title.');
		//return false;
	}
	if(theForm.first_name.value=="")
	{
		theForm.first_name.focus();
		alert('Please enter your first name.');
		return false;
	}
	if(theForm.name.value=="")
	{
		theForm.name.focus();
		alert('Please enter your surname');
		return false;
	}
	if(theForm.address1.value=="")
	{
		theForm.address1.focus();
		alert('Please enter your address.');
		return false;
	}
	
	if(theForm.town.value=="")
	{
		theForm.town.focus();
		alert('Please enter your town.');
		return false;
	}
	
	if(theForm.postcode.value=="")
	{
		theForm.postcode.focus();
		alert('Please enter your postcode.');
		return false;
	}
	if(theForm.postcode.length<6)
	{
		theForm.postcode.focus();
		alert('Please enter 5 characters or more for your postcode.');
		return false;
	}
	if(theForm.del_address1.value=="")
	{
		theForm.del_address1.focus();
		alert('Please enter your delivery address.');
		return false;
	}
	
	if(theForm.del_town.value=="")
	{
		theForm.del_town.focus();
		alert('Please enter your delivery town.');
		return false;
	}
	
	if(theForm.del_postcode.value=="")
	{
		theForm.del_postcode.focus();
		alert('Please enter your delivery postcode.');
		return false;
	}
	if(theForm.del_postcode.length<6)
	{
		theForm.del_postcode.focus();
		alert('Please enter 5 characters or more for your delivery postcode.');
		return false;
	}
	if(theForm.email.value=="")
	{
		theForm.email.focus();
		alert('Please enter your email address.');
		return false;
	}
	
	if (emailvalidation(theForm.email,"Invalid email address")==false) 
	{
		theForm.email.focus(); 
		return false;
	}

	
	if(theForm.password.value =="")
	{
		theForm.password.focus();
		alert('Please enter your password.');
		return false;
	}
	
	return true;
}
function emailvalidation(entered, alertbox)
{
// E-mail Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
apos=value.indexOf("@"); 
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
} 

function areYouSure()
{

	if (confirm('Are you sure?')==true)
	{
		return true;
	}else
	{
		return false;
	}
}
function autoFillEmail(h, m) {

	document.formEmail.emailHeader.value = h;
	document.formEmail.emailMessage.value = m;
	
}