var NS = document.layers;

var illegalChars= /[\<\>\\{\}\'\\\"\[\]]/
var strIllChars = "< > { } [ ] ' \\\ \"\ " 
var alteredQuantity = false;
var thsForm;

function checkOrder(returnUrl){
	if (alteredQuantity){
	 fail("Please update the basket before continuing to checkout.","");
	}else{
	 self.window.location = returnUrl;
	}
}

function checkLogOut(ths_url){
	ask = confirm("Are you sure you want to Log Out before sending order?\r\nIf you do all your selected products will be removed.\r\n\r\nClick Cancel to abort this opperation");
	if (ask){
	 	self.window.location = ths_url;
	}
}

function emailorder(){
	thsForm = document.frmproducts;
	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 = "emailorder.php";
	   thsForm.submit();
	}
	}
}

function updateBasket(){
 thsForm = document.frmproducts;
	if (checkQuantity("The quantity must be a number and larger than zero")){
		  thsForm.update.value = 1;
		  thsForm.submit();
	}
}

function addProduct(thsQuantity,productId){
	if (thsQuantity.value == 0 || thsQuantity.value == ""){
	   fail("Please select a quantity greater than zero",thsQuantity);
    }else{
	document.frmproducts.selectedProduct.value = productId;
	document.frmproducts.selectedQuantity.value = thsQuantity.value;
	document.frmproducts.submit();
	}
}

function checkQuantity(alertMessage){
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 checkTypedQuantity(frmField){
trimSpaces(frmField.value);

if (!checkNumerical(frmField.value)){
   frmField.value = "";
   fail("Quantity must be a whole number",frmField);
}
}

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(); 
}


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;
}

