function chkProductList(f) {
	if (isBlank(f.value)) {
		alert("You must enter a quantity");
		f.focus();
		return false;
	}

	if (!isNumber(f.value)) {
		alert("The quantity must be numeric");
		f.focus();
		return false;
	}

	if (f.value < 1) {
	    alert("You must enter a valid quantity.");
	    f.focus();
	    return false;
	}
}

function chkProductListA(txtQuantity, pnlWait, pnlButtons) {
	if (isBlank(txtQuantity.value)) {
		alert("You must enter a quantity");
		txtQuantity.focus();
		return false;
	}

	if (!isNumber(txtQuantity.value)) {
		alert("The quantity must be numeric");
		txtQuantity.focus();
		return false;
	}

	if (txtQuantity.value < 1) {
	    alert("You must enter a valid quantity.");
	  	txtQuantity.focus();
	  	return false;
	}
	pnlWait.style.display="block";
	pnlButtons.style.display="none";
	//cmdAddToBasket.value = "Please wait..";
	//cmdAddToBasket.disabled = true;
}

function chkTastingList(f)
 {
	if (isBlank(f.value)) {
		alert("You must enter a quantity");
		f.focus();
		return false;
	}

	if (!isNumber(f.value)) {
		alert("The quantity must be numeric");
		f.focus();
		return false;
	}

	if (f.value < 1) {
	    alert("You must enter a valid quantity.");
	  	f.focus();
	  	return false;
	}
	
	if (f.value > 12)
	{
	    alert("You may only request a maximum of 12 tickets.");
	  	f.focus();
	  	return false;
	}
	 
}

function validQty(f,number) {	
	for (var x = 0;x < number; x++){
	    var v = eval("");
		v = q[x].value;
		if (!isValidQuantity(v)) {
		    alert("Please enter a valid quantity!");
			q[x].focus();
			return false;
			}
	}
}

function isValidQuantity(quantity){
    if(isNaN(quantity.replace(/ /g,"")) || quantity=='' || quantity=='0' || quantity< 0){
        return false;
    } else {
        return true;
    }
}

function isBlank(v) {
	var tmpStr;
	tmpStr = v.replace(/\s/g,"");
	if (tmpStr.length>0)
		return false;
	return true;
}

function isNumber(v) { 
	if (isNaN(v.replace(/ /g,"")))
		return false;
    return true;
}

function isEmail(v) {
    /* This should be replaced with a standard email RegEx JRN 29/07/2011*/
	var at=v.indexOf('@');
	var dot=v.lastIndexOf('.');
	var end=v.length;
	var subone=v.substring(at+1,dot);
	var domainbeforedot=subone.length;
	var subtwo=v.substring(dot+1,end);
	var domainafterdot=subtwo.length;
   	if (v.indexOf ('@',0) == -1 || v.indexOf ('.',0) == -1  || domainbeforedot < 1 || domainafterdot < 2 ) {
      		return false;
	}
   	else { 
   		return true; 
   	}
}

function isPostcode(v) {
 	var size = v.length;
  	//Strip leading spaces
 	while (v.slice(0,1) == " ") {
 		v = v.substr(1,size-1);
 		size = v.length;
 	}
  	//Strip trailing spaces
	while(v.slice(size-1,size)== " ") {
		v = v.substr(0,size-1);
		size = v.length;
	}	
 	if (size < 6 || size > 8){ 
 		//Code length rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(0)))){ 
 		//leftmost character must be alpha character rule
   		return false;
  	}
 	if (isNaN(v.charAt(size-3))){ 
 		//first character of inward code must be numeric rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-2)))){ 
 		//second character of inward code must be alpha rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-1)))){ 
 		//third character of inward code must be alpha rule
   		return false;
  	}
 	var count1 = v.indexOf(" ");
 	var count2 = v.lastIndexOf(" ");
 	if (count1 != count2){
 		// only one space rule
  		return false;
  	}
	return true;
}

function chkFieldDefaultText(v, defaultValue, action) {
	if (action == "blur") {
		if (isBlank(v.value)) {
		    v.value = defaultValue;
		}
	}
	if (action == "click") {
		if (v.value == defaultValue) {
		    v.value = "";
		}
	}
}

function validateProductCode(sender, args){
    var code = document.getElementById(sender.controltovalidate).value;
    if (code == 'product code' || code == '')
    {
        args.IsValid = false;
        return;
    }
    else{
        args.IsValid = true;
        return;
    }
}
    
function validateQuantity(sender, args){
    var qty = document.getElementById(sender.controltovalidate).value;
    if (qty > 0 || qty < 1000) {
        args.IsValid = true;
        return;
    } else {
        args.IsValid = false;
        return;
    }
}

function handleEnter1 (field, event){
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13)
		return false;
	return true;
}      

function handleEnter2 (btnId, event) {
	if(event.which || event.keyCode)
	{
	    if ((event.which == 13) || (event.keyCode == 13)) 
	        {
	            document.getElementById(btnId).click();
	            return false;
		}
	}
    return true;
  
}

function handleEnter3 (btnId, event) {
	if(event.which || event.keyCode)
	{
	    if ((event.which == 13) || (event.keyCode == 13)) 
	        {
	            //document.getElementById(btnId).click();
	            return false;
	        }
    } 
    return true;
}
/* handleEnter4 removed as only used by old QuickOrder */

function popUp(strUrl,strType,strHeight,strWidth) {
    var strOptions="";
    if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
    if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth+",menubar=0,resizable=0,scrollbars=no";
    if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
    window.open(strUrl, 'newWin', strOptions);
}
    
function ismaxlength(obj,orig,mlength){
    if (obj.value == orig){
        if ((obj.value.length > orig.length - 1) && (orig.length > mlength -1)){
            obj.value = obj.value.substring(0, orig.length - 2);
        }
    }else{
        if (obj.value.length > mlength -1)
        {
            obj.value = obj.value.substring(0, mlength - 2);
        }
    }
}

function getObject(objectId) {
    // checkW3C DOM, then MSIE 4, then NN 4.
    if (document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId).value;
    } else if (document.all && document.all(objectId)) {
        return document.all(objectId).value;
    } else if (document.layers && document.layers[objectId]) { 
        return document.layers[objectId];
    } else {
	    return false;
   }
}

function RemoveZero(value) {
    if (value == '01'){return 1;}
    if (value == '02'){return 2;}
    if (value == '03'){return 3;}
    if (value == '04'){return 4;}
    if (value == '05'){return 5;}
    if (value == '06'){return 6;}
    if (value == '07'){return 7;}
    if (value == '08'){return 8;}
    if (value == '09'){return 9;}else {return value;}
}

function bookmark(description,url)
{
    var netscape = "Netscape and Firefox User's hit CTRL+D to add a bookmark to this site.";
    if (navigator.appName=='Microsoft Internet Explorer'){
        window.external.AddFavorite(url, description);
    } else if (navigator.appName=='Netscape'){
        alert(netscape);
    } else { 
        alert("Sorry! Your browser doesn't support this function.");
    }
}
