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;
	}
}
