function setupSearchDataAndSubmit() {
	resetFormFields();

	if(validateAndSetupNAData()) {
		copyCheckboxValues("feature", "features");

		document.getElementById("storeSearchForm").submit();
		return true;
	}

	return false;
}

function setupSortDataAndSubmit() {
	copyTextValue("searchSortBy", "featureUniqueID");
	document.getElementById("sortByCategoryForm").submit();			
}

function validateAndSetupNAData() {
	var postalCode = getTextValue("postalCode_NA");
	
    if (postalCode.length > 0) {
		if(!setupUSPostalCodeSearch(postalCode) && !setupCAPostalCodeSearch(postalCode) ) {
			document.getElementById("SLPostalCodeErr").style.display="block";
			document.getElementById("SLPostalCode").className = "alertBorder";
			/*alert('Please enter a valid postal code to continue.');*/
			return false;            
		}
	} else {		
		var city = getTextValue("city_NA");				
		var state = getTextValue("state_NA");			

		if(city.length == 0) {
			document.getElementById("SLPostalCodeErr").style.display="block";
			document.getElementById("SLPostalCode").className = "alertBorder";
			document.getElementById("SLCityStateErr").style.display="block";
			document.getElementById("SLCityName").className = "alertBorder";
			/*alert('Please enter a valid postal code or city name to continue.');*/
			return false;
		}

		if(state.length == 0) {
			alert('Please select a state or province to continue.');
			return false;
		}

        setupCityStateSearch();
	}











	return true;
}

function setupUSPostalCodeSearch(postalCode) {
    var USPostalCodeRegExp  =  /^\d{5}(-\d{4})?$/; 

    if(USPostalCodeRegExp.test(postalCode)){
        if (postalCode.length > 5) {
            postalCode = postalCode.substr(0, 5);
        }
        setupPostalCodeSearch(postalCode);
        return true;
    }

    return false;
}

function setupCAPostalCodeSearch(postalCode) {
    var CAPostalCodeRegExp  = /^[A-CEGHJ-NPR-TVXY]\d[A-Z](\s|-)?\d[A-Z]\d$/i;

    if(CAPostalCodeRegExp.test(postalCode)) {
        postalCode = postalCode.toUpperCase().replace(/-/, " ");
        if(postalCode.indexOf(" ") == -1) {
            postalCode = postalCode.substr(0, 3).concat(" ", postalCode.substr(3, 3));
        }
        setupPostalCodeSearch(postalCode);  
        return true;
    }
    return false;
}

function setupPostalCodeSearch(postalCode) {    
    setTextValue("searchType", "POSTAL_CODE");
    setTextValue("postalCode", postalCode);
    copyDropDownValue("radius_NA", "radius");    
}

function setupCityStateSearch() {
    copyTextValue("city_NA", "city");
    copyDropDownValue("state_NA", "state");
    setTextValue("searchType", "CITY_STATE");





}

function resetFormFields() {
	document.getElementById("features").value = "";
	document.getElementById("searchType").value = "";
}

function copyTextValue(srcId, targetId) {
    document.getElementById(targetId).value = document.getElementById(srcId).value; 
}

function setTextValue(targetId, value) {
    document.getElementById(targetId).value = value; 
}

function getTextValue(srcId) {
    return trim(document.getElementById(srcId).value);
}

function copyCheckboxValues(srcId, targetId) {
    var checkboxes = document.getElementsByName(srcId);
    var checked =  new Array();
    var target = document.getElementById(targetId);
    
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            checked.push(checkboxes[i]);
        }
    }

	for (var i = 0; i < checked.length; i++) {
        target.value += checked[i].value;
        if (i < checked.length - 1) {
            target.value += ",";
        }
    }
}

function copyDropDownValue(srcId, targetId) {
    var dropDown = document.getElementById(srcId);
    document.getElementById(targetId).value = dropDown.options[dropDown.selectedIndex].value;
}

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; } 
   return temp;
}


