var NS = document.layers;

var illegalChars= /[\<\>\\{\}\'\\\"\[\]]/
var strIllChars = "< > { } [ ] ' \\\ \"\ " 
var alteredQuantity = false;
var thsForm;
function checkout(){
	thsForm = document.frmlist;
	if (alteredQuantity){
	 fail("Please update the basket before continuing to checkout.","");
	}else if (check_options()){
	 if (checkQuantity("Please update the basket before continuing to checkout.")){
	   thsForm.action = "checkout.php";
	   thsForm.submit();
	}
	}
}
function emailorder(){
	thsForm = document.frmlist;
	if (alteredQuantity){
	 fail("Please update the basket before continuing to email your order.","");
	}else if (check_options()){
	 if (checkQuantity("Please update the basket before continuing to email your order.")){
	   thsForm.action = "email_order.php";
	   thsForm.submit();
	}
	}
}
function updateBasket(){
 thsForm = document.frmlist;
	if (checkQuantity("The quantity must be a number and larger than 0.")){
		  thsForm.action = "basket.php?update=1";
		  thsForm.submit();
	}
}
function check_options(){
	
	valFail = true;
	var valFail = false;
	var storeIndex;
	thsForm = document.frmlist;
	for (i=0; i < thsForm.elements.length; i++){
	   if (thsForm.elements[i].name.indexOf("options") != -1){
	   	  if (thsForm.elements[i].selectedIndex == 0){
		  	 valFail = true;
			 storeIndex = thsForm.elements[i];
			 i = thsForm.elements.length;
		  }
	   }	
	}
	
	if (valFail){
	   fail("Please select one of the options.",storeIndex);
	   return false;
	}else return true;	 	 
}

function checkQuantity(alertMessage){

thsForm = document.frmlist;
var valFail = false;
var storeIndex;

	if (alteredQuantity){
	   for (i=0; i < thsForm.elements.length; i++){
	 
		if (checkNumerical(thsForm.elements[i].name) && thsForm.elements[i].name > ""){
		   thsForm.elements[i].value = trimSpaces(thsForm.elements[i].value);
		 
	  
		   if (!checkNumerical(thsForm.elements[i].value) || thsForm.elements[i].value == 0  || thsForm.elements[i].value == ""){
		   	  valFail = true;
			  storeIndex = thsForm.elements[i];
			  i = thsForm.elements.length;
		   }
		
		}
	   
	   }
	}else return true;
	if (valFail){
	   fail(alertMessage,storeIndex);
	   return false;
	}else return true;	   	 
	
}
function checkNumerical(thsValue){
	var nums="1234567890";
	for(var i=0;i < thsValue.length;i++){
		if(nums.indexOf((thsValue.charAt(i)))==-1) return false;
		}
	return true;
}

function searchProducts(form){
		form.searchword.value = trimSpaces(form.searchword.value);
		if (!chkCharacters(form.searchword.value)){
		   alert ("Sorry you can not use these characters in the Search Box.\r\n\r\n"+strIllChars);
		}else form.submit(); 
}

function searchStores(form){
		form.catsearchword.value = trimSpaces(form.catsearchword.value);
		if (!chkCharacters(form.catsearchword.value)){
		   alert ("Sorry you can not use these characters in the Search Box.\r\n\r\n"+strIllChars);
		}else form.submit(); 
}
function chkCharacters(thsValue){

if (thsValue.match(illegalChars))return false;
return true; 
}

function trimInputValues(frmObjt){
	for (i = 0;i<frmObjt.elements.length;i++ ){
		frmObjt.elements[i].value = trimSpaces(frmObjt.elements[i].value)
	} 

}
function trimSpaces(strInput) { 
    
    while (strInput.substring(0,1) == ' ') 
        strInput = strInput.substring(1, strInput.length);

    while (strInput.substring(strInput.length-1,strInput.length) == ' ')
        strInput = strInput.substring(0, strInput.length-1);

   return strInput;
}

if (navigator.appVersion.lastIndexOf('Win') != -1) {
      dropline = "\r\n"  } else { dropline = "\n" }

function fail(msg,fld){
	alert("Error:"+ dropline + dropline + msg);
	if (fld > "") fld.focus();
	return false
}


