function setupSearchDataAndSubmit() {
	resetFormFields();

	if(validateAndSetupNAData()) {
		copyDropDownValue("feature", "features");
		document.getElementById("storeSearchForm").submit();
		return true;
	}

	return false;
}
function setupSearchDataAndSubmitCanada() {
	resetFormFields();

	if(validateAndSetupNADataCanada()) {
		copyDropDownValue("feature_NA_CA", "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");
	var sltType = getTextValue("typeId");
	
    if (postalCode.length > 0) {
		if(!setupUSPostalCodeSearch(postalCode)) {
			document.getElementById("SLUPostalCodeErr").style.display="block";
			document.getElementById("SLUPostalCode").className += " alertBorder";
			return false;            
		}
		
			copyDropDownValue("typeId", "reqTypeId");
		
	} else {		
		var city = getTextValue("city_NA");				
		var state = getTextValue("state_NA");			

		if(city.length == 0) {
			document.getElementById("SLUPostalCodeErr").style.display="block";
			document.getElementById("SLUPostalCode").className += " alertBorder";
			document.getElementById("SLUCityStateErr").style.display="block";
			document.getElementById("SLUCity").className += " alertBorder";
			return false;
		}

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

        setupCityStateSearch();
	}
	
	return true;
}
function validateAndSetupNADataCanada() {
	var postalCode = getTextValue("postalCode_NA_CA");
	var sltType = getTextValue("typeId_CA");

    if (postalCode.length > 0) {
		if(!setupCAPostalCodeSearch(postalCode) ) {
			document.getElementById("SLCPostalCodeErr").style.display="block";
			document.getElementById("SLCPostalCode").className += " alertBorder";

			return false;            
		}

		
			copyDropDownValue("typeId_CA", "reqTypeId");
		

	}else {		
		var city = getTextValue("city_NA_CA");				
		var state = getTextValue("state_NA_CA");			

		if(city.length == 0) {
			document.getElementById("SLCPostalCodeErr").style.display="block";
			document.getElementById("SLCPostalCode").className += " alertBorder";
			document.getElementById("SLCCityStateErr").style.display="block";
			document.getElementById("SLCCity").className += " alertBorder";

			return false;
		}

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

        setupCityStateSearchCanada();
	}

	return true;
}

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

    if(USPostalCodeRegExp.test(postalCode)){
        if (postalCode.length > 5) {
            postalCode = postalCode.substr(0, 5);
        }
        setupPostalCodeSearchForUS(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));
        }
        setupPostalCodeSearchForCA(postalCode);  
        return true;
    }
    return false;
}

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

function setupPostalCodeSearchForCA(postalCode) {    
    setTextValue("searchType", "POSTAL_CODE");
    setTextValue("postalCode", postalCode);
    copyDropDownValue("radius_NA_CA", "radius");    
}

function setupCityStateSearch() {
    copyTextValue("city_NA", "city");
    copyDropDownValue("state_NA", "state");
    setTextValue("searchType", "CITY_STATE");
}
function setupCityStateSearchCanada() {
    copyTextValue("city_NA_CA", "city");
    copyDropDownValue("state_NA_CA", "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;
}

function verifyCountry()
{
   	var country = document.countryForm.country.selectedIndex;
    if (country == 0)
    {
        alert('Please select a country to continue.');
    }
    else
    {
		copyDropDownValue("country", "countryName");
		var dropDown = document.getElementById("country");
	    document.getElementById("countryFullName").value = dropDown.options[dropDown.selectedIndex].text;
		document.countryForm.submit();
    }
}
