function validateMakeZipForm(formName, makeSelectName, zipName) {
        //alert("validateMakeZipForm called...");
        var makeSelectObject = eval("document." + formName + "." + makeSelectName);
        if(makeSelectObject.selectedIndex==0) {
        alert("Please select a Make");
        return false;
    }

    var zip = eval("document." + formName + "." + zipName);
    var alertMsg = "Please enter valid city and state or ZIP code, \ne.g. santa monica, ca or 90404";
    if(isNaN(zip.value)) {
        alert("Please enter the city and state separated by a comma, \ne.g. santa monica, ca");
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }
    if(zip.value.length !=5) {
        alert(alertMsg);
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }
    if(zip.value == "") {
        alert(alertMsg);
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }
    //if you reached here, then check if the zip is actually a valid one.
        if (checkZipValidity(zip.value)) {
                return true;
        }
        else {
            return false;
        }
}

function clearZipField(formName, zipName) {
    var zip = eval("document." + formName + "." + zipName); 
    var zipVal = zip.value;   
    if(zipVal.indexOf(" or ") > -1) {
        zip.value = '';
    }
}

function validateMakeModelZipForm(formName, makeSelectName, modelSelectName, zipName) {
    //alert("validateMakeZipForm called...");
    var makeSelectObject = eval("document." + formName + "." + makeSelectName);
    var modelSelectObject = eval("document." + formName + "." + modelSelectName);
    var alertMsg = "Please enter a valid 5-digit ZIP code, \ne.g. 90404";
    var zip = eval("document." + formName + "." + zipName);
    zip.value = zip.value.replace(/^\s+|\s+$/g, '');
    if(isNaN(zip.value)) {
        alert(alertMsg);
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }
    if(zip.value.length !=5) {
        alert(alertMsg);
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }
    if(zip.value === "") {
        alert(alertMsg);
        eval("document." + formName + "." + zipName + ".focus()");
        return false;
    }


    if(makeSelectObject.selectedIndex === 0 || modelSelectObject.selectedIndex === 0) {
        alert("You need to select a make and a model");
        return false;
    }

    return true;
}

function validateMakeGeoForm(formName, makeSelectName, geoName, showMake) {
    //alert("validateMakeZipForm called...");
    if ('true' == showMake) {
	    var makeSelectObject = eval("document." + formName + "." + makeSelectName);
        if(makeSelectObject.selectedIndex==0) {
            alert("Please select a Make");
            return false;
        }
    }
    
    var geo = eval("document." + formName + "." + geoName);    
    //copy paste from localservice.js
    var geoValue = geo.value;
    var searchArray = geoValue.split(",", 2);
    var alertMsg = "Please enter valid city and state or ZIP code, \ne.g. santa monica, ca or 90404";
    if (searchArray.length == 1) {   //then it is a zipcode
        geoValue = geoValue.replace(/^\s+|\s+$/g, '');        
	    if(isNaN(geoValue)) {
	        alert("Please enter the city and state separated by a comma, \ne.g. santa monica, ca");
	        eval("document." + formName + "." + geoName + ".focus()");
	        return false;
	    }
	    if(geoValue.length !=5) {
	        alert(alertMsg);
	        eval("document." + formName + "." + geoName + ".focus()");
	        return false;
	    }
	    if(geoValue == "") {
	        alert(alertMsg);
	        eval("document." + formName + "." + geoName + ".focus()");
	        return false;
	    }
	    //if you reached here, then check if the zip is actually a valid one.
	    if (checkZipValidity(geoValue)) {
	        geo.value = geoValue.replace(/\s+/g, "");
	        return true;
	    }
	    else {
	        alert(alertMsg);
	        return false;
	    }
    } else if (searchArray.length == 2) {
        var city = searchArray[0];
        var state = searchArray[1];
        //if you reached here, then check if the city and state are valid.
        //First check if any numeric pattern in city or state
        var pattern = /\d+/g;
        if (pattern.test(city) || pattern.test(state)) {
            alert(alertMsg);
            return false;
        }
        if (checkCityValidity(city, state)) {
            return true;
        } else {
            alert(alertMsg);
            return false;
        }
    }
    return false;
}

//AJAX call for zip code validity
function getXmlHttpObject() {
        var http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    return http_request;
}

function checkZipValidity(zip) {
        var xmlHttp = getXmlHttpObject();
        xmlHttp.open('GET', '/apps/localsearch/ValidateZip.do?zip='+zip, false);
        xmlHttp.send(null);

        if (xmlHttp.status == 200) {
            //alert(xmlHttp.responseText);
            return checkZip(xmlHttp.responseXML);
        }
        else {
            //something wrong in request/response
            alert("Error " + xmlHttp.status + ": " + xmlHttp.statusText);
        }

}

// validating city
function checkCityValidity(city, state) {
    var cityWithoutSpaces = city.replace(/\s+/g, "");
    var stateWithoutSpaces = state.replace(/\s+/g, "");
    //alert("checkCityValidity -- called:" + cityWithoutSpaces + ":" + stateWithoutSpaces);
    var xmlHttp = getXmlHttpObject();
    xmlHttp.open("GET", "http://www.edmunds.com/apps/localsearch/ValidateCity.do?city=" + cityWithoutSpaces + "&state=" + stateWithoutSpaces, false);
    xmlHttp.send(null);
    if (xmlHttp.status == 200) {
        return checkCity(xmlHttp.responseXML);
    } else {
        alert("Error " + xmlHttp.status + ": " + xmlHttp.statusText);
    }
}

function checkZip(xmlDoc) {
    var zip = xmlDoc.getElementsByTagName("zip");
    if (zip.length == 0) {
        return false;
    }
    return true;
}

function checkCity(xmlDoc) {
    var city = xmlDoc.getElementsByTagName("city");
    if (city.length == 0) {        
        return false;
    }
    return true;
}
