var sendForm=false;

function validateFormStoreId(form)
{
	sendForm=true;
	if(form.slsid.value < 1)
	{
		alert("Please select a store from the list");
	    sendForm=false;
	}
	return sendForm;

}


function validateForm(form1) {
// This function checks that at least one of the postcode or town fields
// have been entered.

//use the validate flag to change a hidden field if more than one type of submit exists
var hasValidateFlag = $(form1).find("input.validateflag")[0];
if (hasValidateFlag) {
	//will halt validation procedure
	if ($(hasValidateFlag).val() == "false") {
		return;
	}
	//reset the validation flag
	$("form#ConfirmResItemsAndStoreDetails input.validateflag").attr("value","false");
}

if (!sendForm) validatePostcode(form1.slpc);
if (sendForm) validateTownName(form1.sltwn);
	//alert(sendForm + " " + form1.<%= HomebaseConstants.STORE_LOCATOR_FIELD_POST_CODE %>.value + " " + form1.<%= HomebaseConstants.STORE_LOCATOR_FIELD_TOWN %>.value);
if (sendForm && form1.slpc.value == "" && form1.sltwn.value == "") {
	alert("Please enter your postcode or town name");
	sendForm=false;	
	}
	
	
return sendForm;
}


function validateFormDirection(form1) {
// This function checks that at least one of the postcode or town fields
// have been entered.
if (!sendForm) validatePostcode(form1.slpc);
if (sendForm && form1.slpc.value == "") {
	alert("Please enter your postcode");
	sendForm=false;	
	}
return sendForm;
}

function isValidPartialPostcode(field) {
var outwardPass = false;
var postcodeStr = trimSpaces(""+field.value);
//Check for partial postcode 
//AN
if((postcodeStr.length == 2 || postcodeStr.length == 5) && (isAlpha(postcodeStr.charAt(0)) && isNumeric(postcodeStr.charAt(1)))) outwardPass=true;
//ANN & AAN & ANA
if((postcodeStr.length == 3 || postcodeStr.length == 6) && (isAlpha(postcodeStr.charAt(0)) && isNumeric(postcodeStr.charAt(1)) && isNumeric(postcodeStr.charAt(2)) || isAlpha(postcodeStr.charAt(0)) && isAlpha(postcodeStr.charAt(1)) && isNumeric(postcodeStr.charAt(2)) || isAlpha(postcodeStr.charAt(0)) && isNumeric(postcodeStr.charAt(1)) && isAlpha(postcodeStr.charAt(2)))) outwardPass=true;
//AANN & AANA
if((postcodeStr.length == 4 || postcodeStr.length == 7) && (isAlpha(postcodeStr.charAt(0)) && isAlpha(postcodeStr.charAt(1)) && isNumeric(postcodeStr.charAt(2)) && isNumeric(postcodeStr.charAt(3)) || isAlpha(postcodeStr.charAt(0)) && isAlpha(postcodeStr.charAt(1)) && isNumeric(postcodeStr.charAt(2)) && isAlpha(postcodeStr.charAt(3)))) outwardPass=true;
return outwardPass;
}

function hasCIKMOV(refStr) {
var seqError = false;
for (var i=0; i<refStr.length; i++) {
	if (arrayContainsElement(hasCIKMOV.arguments,hasCIKMOV.arguments[0].charAt(i))) seqError = true;
	}
return seqError;
}

function validatePostcode(field) {
// This function checks the postcode has been entered. It also checks the size
// and the format of the postcode.
sendForm=true;
var postcodeStr = trimSpaces(""+field.value);
field.value=postcodeStr;
	if(postcodeStr=="") {
	return;
	}
	else
 	if(!isAlphaAndNumeric(postcodeStr) && postcodeStr!="") {
 	alert("Please enter a valid postcode (letters and numbers only)");
 	highlightField(field);
 	sendForm=false;
	}
	else
	if(postcodeStr.length < 2 || postcodeStr.length > 7) {
	alert("Please enter a postcode length between 2 - 7 characters");
	highlightField(field);
	sendForm=false;
	}
	else
	if(hasCIKMOV(postcodeStr.substring(postcodeStr.length, postcodeStr.length-2).toLowerCase(),"c","i","k","m","o","v")) {
	alert("Please enter a valid postcode");
	highlightField(field);
	sendForm=false;
	return sendForm;
	}
	else
	if(!isValidPartialPostcode(field)) {
	alert("Please enter a valid postcode");
	highlightField(field);
	sendForm=false;
	}
	else
	if(postcodeStr.length > 4 && (!isAlpha(postcodeStr.charAt(0)) || 
!isAlpha(postcodeStr.charAt(postcodeStr.length-1)) || 
!isAlpha(postcodeStr.charAt(postcodeStr.length-2)) || 
!isNumeric(postcodeStr.charAt(postcodeStr.length-3)))) {
	alert("Please enter a valid postcode");
	highlightField(field);
	sendForm=false;
	}
}

function validateTownName(field) {
// This function checks the town field contains no numbers.
sendForm=true;
var townnameStr = trimOutsideSpaces(""+field.value);
if(townnameStr=="") return;
field.value=townnameStr;
	if(townnameStr.length < 3 ) {
	alert("The town name must contain a minimum of 3 characters");
	highlightField(field);
    sendForm=false;
	}
	else
	if(!isValidSequenceAndAlpha(townnameStr,"'"," ","-",",",".")) {
	alert("Please enter letters, which can include - or ' or , or . only");
	highlightField(field);
	sendForm=false;
	}
}

	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";		
			},
			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: "Opera"
				},
				{
					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"
				}
			]
			
		};
	
	BrowserDetect.init();

