// This is the default text for the ZIP Code field
var zipCodeText = "Enter ZIP Code";
// First segment of the search by ZIP Code URL
var zipCodeSearchURL01 = "http://pg.links.channelintelligence.com/oemsites/19483251/localbundle.asp?cii_nClientId=1&cii_sProductfamilyid=CASC&cii_sZip=";
// Second segment of the search by ZIP Code URL
var zipCodeSearchURL02 = "&cii_sProducttype=upc&cii_nProductid=";
// Third segment of the search by ZIP Code URL
var zipCodeSearchURL03 = "&cii_nRadius=25&nUI=0";
/** When the DOM is ready, assign events */
$(document).ready(function(){
	$("#zipCode").focus(zipCodeFocus);
	$("#zipCode").blur(zipCodeBlur);
	$("#goBtn").click(handleZipCodeField);
});
/** This function validate the provided value and build the final URL */
function handleZipCodeField (e){
	// Regulas Expression for clean the product list string
	var re = new RegExp("\\|[0-9]", "g");
	// This is the cleaned list of products
	var list = upc_list.replace(re, "");
	// The ZIP Code provided by the user
	var zipCode = $.trim($("#zipCode").val());
	if(isNaN(zipCode)){
		$(this).attr("href", "#");
		$("#zipCode").val(zipCodeText).css({"background-color": "#ff8080", "color": "white"});
		// Stops the propagation of the link
		e.preventDefault();
	} else {
		// This is the final URL used in the search by ZIP Code
		var finalURL = zipCodeSearchURL01 + zipCode + zipCodeSearchURL02 + list + zipCodeSearchURL03;
		$(this).attr("href", finalURL);
	}
}
/** OnFocus event of the ZIP Code field */
function zipCodeFocus(){
	var zipCode = $.trim($("#zipCode").val());
	if(zipCode == zipCodeText){ $("#zipCode").val(""); }
	$(this).css({"background-color": "#FFFFFF", "color": "#888888", "border-color": "red"});
}
/** OnBlur event of the ZIP Code field */
function zipCodeBlur(){
	var zipCode = $.trim($("#zipCode").val());
	if(zipCode == ""){ $("#zipCode").val(zipCodeText); }
	$(this).css({"background-color": "#FFFFFF", "color": "#888888", "border-color": "#585858"});
}