function createXMLHttpRequest(frm) {
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest();
//	alert("FireFox, Opera, Safari");
        return xmlHttp;
    }catch (e){
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//		alert("Internet Explorer");
            return xmlHttp;
        }catch (e){
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttp;
            }catch (e){
                alert("Your Browser not Supporting Ajax");
                return false;
            }
        }
    } 
}

function getMultiple_Models_Search_Car(frm) {
    var make = document.getElementById("srch_make");

    if(make.value!=""){

        var url = "/ajaxmultiplecarmodel?make=";
        for(i=0; i<make.length; i++){
            if(make[i].selected == true){
                url +=make[i].value + "-";  
            }
        }

        var models = "<select size=\"6\" multiple=\"multiple\" name=\"srch_model\" id=\"srch_model\" style=\"width:200px; font-size:12px;\"><option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;Loading...</option></select>";
        document.getElementById("viewmodel").innerHTML = models;
        models = "";

        xmlHttp = createXMLHttpRequest();

        xmlHttp.onreadystatechange = function gotModels() {
            if(xmlHttp.readyState==4){
                if(xmlHttp.status==200){
                    var msg = xmlHttp.responseXML.getElementsByTagName("root")[0];
                    models = "<select size=\"6\" multiple=\"multiple\" name=\"srch_model\" id=\"srch_model\" style=\"width:200px; font-size:12px;\" onchange=\"checkchanges();\">";
                    for(i=0; i<(msg.childNodes.length/2); i++){
                        if(msg.getElementsByTagName("modelid")[i].childNodes[0].nodeValue=="Make"){
                            if(i!=0){
                                models = models + "</OPTGROUP>";
                            }
                            models = models + "<OPTGROUP LABEL=\""+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"\">";
                        }else {
                            models = models + "<option value=\""+msg.getElementsByTagName("modelid")[i].childNodes[0].nodeValue+"\">"+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"</option>";
                        }

                    }
                    models = models + "</OPTGROUP>";
                    models =models + "</select>";
                    document.getElementById("viewmodel").innerHTML = models;
                    getstate_city(frm);
                }
            }
        }
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
}

function getstate_city(frm){

    var zip = document.getElementById("txtzip").value;

    document.getElementById("selstate").value = "";
    document.getElementById("txtcity").value = "";

    var url = "/getstatecity?zip="+zip;

    if(zip.length==5){
        if(!isNaN(zip)){
            xmlHttp = createXMLHttpRequest();
            xmlHttp.onreadystatechange = function gotCategories() {
                if(xmlHttp.readyState==4){
                    if(xmlHttp.status==200){
                        var msg = xmlHttp.responseXML.getElementsByTagName("statecity")[0];
                        var state = msg.getElementsByTagName("state")[0].childNodes[0].nodeValue;
                        var city = msg.getElementsByTagName("city")[0].childNodes[0].nodeValue;
                        if(state!="No"){

                            document.getElementById("selstate").value = state;
                            document.getElementById("txtcity").value = city;

                            document.getElementById("zip_status").innerHTML="<font color=\"#FF9966\" size=\"2\"><b>"+city+"-"+state+"</b></font>";
                        }else {
                            document.getElementById("zip_status").innerHTML="<font color=\"red\" size=\"2\"><b>Enter Valid Zip Code<b></font>";

                        }
                    }
                }
            }
            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);

        }else {
            document.getElementById("zip_status").innerHTML="<font color=\"red\" size=\"2\"><b>Enter Valid Zip Code<b></font>";
        }
    }else {
        document.getElementById("zip_status").innerHTML="";
    }
}


function advancedsearchcar(){
    var make = document.getElementById("srch_make");
    var model = document.getElementById("srch_model");
    var fmyear = document.getElementById("fmyear");
    var toyear = document.getElementById("toyear");
    var fmprice = document.getElementById("fmprice");
    var toprice = document.getElementById("toprice");
    var state = document.getElementById("selstate");

    document.getElementById("make").style.color="#000000";
    document.getElementById("model").style.color="#000000";
    document.getElementById("year").style.color="#000000";
    document.getElementById("price").style.color="#000000";
    document.getElementById("zip").style.color="#000000";
    document.getElementById("err").innerHTML = "";

    iserror = false;
    if(make.value==""){
        document.getElementById("make").style.color="red";
        iserror = true;
    }
    if(model.value==""){
        document.getElementById("model").style.color="red";
        iserror = true;
    }
    if(parseInt(fmyear.value)>parseInt(toyear.value)){
        document.getElementById("year").style.color="red";
        iserror = true;
    }
    if(parseInt(fmprice.value)>parseInt(toprice.value)){
        document.getElementById("price").style.color="red";
        iserror = true;
    }
    if(state.value==""){
        document.getElementById("zip").style.color="red";
        iserror = true;
    }

    if(iserror){
        document.getElementById("err").innerHTML = "Labels Marked RED are Mandatory";
        document.getElementById("err").style.color="red";
        return false;				
    }


}

function checkchanges(){
    var model = document.getElementById("srch_model");
    for(i=0; i<model.length; i++){
        if(model[i].selected == true){
            if((model[i].value).substring(0,3)=="al_"){
                for(j=i; j<model.length; j++){
                    if((model[i].value).substring(3,6)==(model[j].value).substring(0,3)){
                        model[j].selected=false;
                    }
                }

            }
        }
    }
}


