//var MA=new Array();
//var MO=new Array();
//var arry;
// -------------------------------------------------------------------
// Creates an XMLHTTP object, and tells the object to execute a function called stateChanged when a change is triggered
// Opens the XMLHTTP object with the given url. 
//Sends an HTTP request to the server
// -------------------------------------------------------------------
function showMakeModel(vType, lstPrimary, lstSecondary, strVal, topText) {
    //alert('hi');
    var catid;
    var mkid;

    if (eval(vType + ".options.selectedIndex") != -1 && eval(vType + ".options[" + vType + ".selectedIndex].value") != '') {
        catid = eval(vType + ".options[" + vType + ".selectedIndex].value");
    }
    else {

        eval(lstPrimary + ".options.length = 0");
        eval(lstPrimary + ".options[0] = new Option('All','')");
        eval(lstSecondary + ".options.length = 0");
        eval(lstSecondary + ".options[0] = new Option('All','')");

        return false;
    }
    //alert('catid:' + catid);
    mkid = eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value");
    /*
    if(eval(lstPrimary+".options["+lstPrimary+".selectedIndex].value") == '' && catid !='')
    {
    mkid=eval(lstPrimary+".options["+lstPrimary+".selectedIndex].value");
    }
    else
    {
	
		eval(lstSecondary+".options.length = 0");
    eval(lstSecondary+".options[0] = new Option('Select a model','')");

		return false;
    }
    */

    //alert('makeid:' + mkid);

    xmlHttp = createXMLHttpRequest();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = "../Ajaxservice/makemodelarray.aspx"
    url = url + "?liCategoryID=" + catid
    url = url + "&liMakeID=" + mkid
    url = url + "&liType=" + strVal
    url = url + "&sid=" + Math.random()

    //alert(url);
    xmlHttp.onreadystatechange = function() { stateChanged(lstPrimary, lstSecondary, strVal, catid, topText); };
    /*
    function () {
    if (xmlHttp.readyState == 4) {
    alert("Got response.");
    }
    };
    */


    //stateChanged
    //xmlHttp.onreadystatechange=function() {
    //if (xmlHttp.readyState != 4)  { return; }
    //if (xmlHttp.status != 200)  { return; }
    //arry= xmlHttp.responseText;
    // }

    xmlHttp.open("GET", url, true)
    xmlHttp.send(null);
    //alert(arry);

    //changeOptions(lstPrimary,lstSecondary,strVal,arry ,'');
    //alert('changeOptions(\'' + lstPrimary + '\',\'' + lstSecondary + '\',\'' + strVal + '\',\'' +arry + '\',\'\')');
    //setTimeout('changeOptions(\'' + lstPrimary + '\',\'' + lstSecondary + '\',\'' + strVal + '\',\'' +arry + '\',\'\')',1000);
    //changeOptions(lstPrimary,lstSecondary,strVal ,arry ,catid,'');
    //stateChanged1(lstPrimary,lstSecondary,strVal);
    //var arraySA = new Array ([ "Austria" ],  [ "Belgium" ])
    /*
    for(i=0;i<1000;i++)
    {
    }
    */
}

// -------------------------------------------------------------------
// Creates an XMLHTTP object, and tells the object to execute a function called stateChanged when a change is triggered
// Opens the XMLHTTP object with the given url. 
//Sends an HTTP request to the server
// -------------------------------------------------------------------
function showSubcategory(lstPrimary, lstSecondary, topText, whichText, includeModel, addField) {
    
    var catid;

    if ((eval(lstPrimary + ".value") != "-1") && (eval(lstPrimary + ".value") != "")) {
        catid = eval(lstPrimary + ".value");
        
    }
    else if ((eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value") != '-1') && (eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value") != ''))  //(eval(lstPrimary+".options.selectedIndex.value")!="-1")
    {
        catid = eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value");
    }
    else {
        eval(lstSecondary + ".options.length = 0");
        eval(lstSecondary + ".options[0] = new Option('" + topText + "','-1')");
        if (addField != "") {
            eval(addField + ".options.length = 0");
            eval(addField + ".options[0] = new Option('" + topText + "','-1')");
        }

        return false;
    }

    xmlHttp = createXMLHttpRequest();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = "../Ajaxservice/makemodelarray.aspx"
    url = url + "?liCategoryID=" + catid
    if (whichText == 'make')
        url = url + "&liType=" + whichText
    if (includeModel != "")
        url = url + "&liincModel=" + includeModel
    url = url + "&sid=" + Math.random()
    //alert(url);

    xmlHttp.onreadystatechange = function() { stateChanged(lstPrimary, lstSecondary, whichText, catid, topText); };

    xmlHttp.open("GET", url, true)
    xmlHttp.send(null);

}

// -------------------------------------------------------------------
// Called from above functin after receiving the HTTP response
// -------------------------------------------------------------------
function stateChanged(lstPrimary, lstSecondary, strVal, catid, topText) {
    //alert('hi');
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        //alert("Got response.");

        //alert(xmlHttp.responseText);
        //arry=xmlHttp.responseText;
        //alert(lstPrimary);
        //document.getElementById("txtHint").innerText = "<script>" + xmlHttp.responseText + "</script>";
        changeOptions(lstPrimary, lstSecondary, strVal, xmlHttp.responseText, catid, '', topText);
    }

}
/*
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
arry=xmlHttp.responseText;

}

}
*/


// -------------------------------------------------------------------
// Returns xmlhttp object depending on the browser type
// Supports IE and  Firefox
// -------------------------------------------------------------------

function GetXmlHttpObject() {
    var objXMLHttp = null
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest()
    }
    else if (window.ActiveXObject) {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
}

function createXMLHttpRequest() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
    try { return new XMLHttpRequest(); } catch (e) { }
    alert("XMLHttpRequest not supported");
    return null;
}



// -------------------------------------------------------------------
// Called from stateChanged function after receiving the HTTP response
//This populates the make and model combos
// -------------------------------------------------------------------

function changeOptions(lstPrimary, lstSecondary, strVal, stroutputArray, vtid, intSelectedID, topText) {
    var strReturn = '';
    var strArray;
    if (strVal == 'make') {
        var MA = eval(stroutputArray);
        strArray = 'MA';
    }
    if (strVal == 'model') {
        var MO = eval(stroutputArray);
        strArray = 'MO';
    }

    //if(eval(lstPrimary+".options.selectedIndex")!=-1)
    //{
    var strKey1 = eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value");
    //}
    /* commented on 03/11/06 when integrating vt with make
    if(strVal=='make')  //strVal!='model'
    {
    eval(lstPrimary+".options.length = 0");
    //eval(lstPrimary+".options[0] = new Option('All','')");  
    eval(lstPrimary+".options[0] = new Option('"+topText+"','')");  //topText
    }
    */
    if (lstSecondary != '') {
        eval(lstSecondary + ".options.length = 0");
        //eval(lstSecondary+".options[0] = new Option('All','')");
        eval(lstSecondary + ".options[0] = new Option('" + topText + "','-1')");  //topText
    }
    if (vtid != '' || strKey1 == '' || strVal != 'model') {
        var arrLen = eval(strArray + ".length");

        var listLen = 1;
        for (var i = 0; i < arrLen; i++) {
            /* commented on 03/11/06 when integrating vt with make
            if(strVal=='make')
            eval(lstPrimary+".options[listLen] = new Option("+strArray+"[i][1], "+strArray+"[i][0])");
            else
			
				if(strVal=='model')
            */
            eval(lstSecondary + ".options[listLen] = new Option(" + strArray + "[i][1], " + strArray + "[i][0])");

            //if(intSelectedID!=''&& intSelectedID!='-1'&& intSelectedID!='?'&& eval(strArray+"[i][0]")==intSelectedID)
            //{strReturn=listLen}
            listLen = listLen + 1;
        }
    }
    return strReturn
}

// -------------------------------------------------------------------
// Called from above functin after receiving the HTTP response
// -------------------------------------------------------------------
function resetcombo(lstPrimary) {
    //alert(eval(lstPrimary+".options.selectedIndex"));
    //alert(eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value"));
    if (eval(lstPrimary + ".options.selectedIndex") != -1 && eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value") != '') {
        //alert(eval(lstPrimary+".options.selectedIndex"));
        //alert(eval(lstPrimary+".options[0].selected")); //options[0].selected=true
        eval(lstPrimary + ".options.selectedIndex = 0");

    }
}
function resetroadtestcombo(lstPrimary) {

    //alert(eval(lstPrimary+".options.selectedIndex"));
    //alert(document.forms.RoadTestSearch.BasicType.options[document.forms.RoadTestSearch.BasicType.selectedIndex].value);
    //alert(eval(document.forms.RoadTestSearch.BasicType + ".options[" + document.forms.RoadTestSearch.BasicType + ".selectedIndex].value"));
    //checking if there is any selected value for the basetype. If there is then resetting the value of make.This is to fix a bug when the browser bcak button is pressed from search results pages.
    if (eval(lstPrimary + ".options[" + lstPrimary + ".selectedIndex].value") != '') {
        //alert('hi');
        //eval(lstPrimary+".options.selectedIndex = 0");
        showSubcategory('document.forms.RoadTestSearch.BasicType', 'document.forms.RoadTestSearch.Manufacturer', 'All', 'make', '0', 'document.forms.RoadTestSearch.Model');
    }
}


// ----------------------------------------------------------------------
// Functions for new less cluttered ad search Form
// ----------------------------------------------------------------------
function getNewSubCategories(parentList, childList, price, liMakelist, liModellist) {
    
    
    if (parentList.selectedIndex != 0) {

        document.getElementById("showSubVehicle").style.display = "inline";
        document.getElementById("lblSubVehicle").innerHTML = "Type of " + parentList.options[parentList.selectedIndex].text + ":";

        xmlHttp = createXMLHttpRequest();
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request")
            return
        }

        var url = "../Ajaxservice/NewSubCategories.aspx"
        var catID = parentList.options[parentList.selectedIndex].value;
        url = url + "?type=getList&ID=" + catID
        url = url + "&sid=" + Math.random()

        xmlHttp.onreadystatechange = function() { CategoriesRetrieved(childList, catID, price); };

        xmlHttp.open("GET", url, true)
        xmlHttp.send(null);


    }
    else {
        document.getElementById("showSubVehicle").style.display = "none";
        document.getElementById("lblSubVehicle").innerHTML = "Sub Type:";

        childList.length = 0;

        var y = document.createElement('option');
        y.text = 'All';
        y.value = '-1';

        try {
            childList.add(y, null); // standards compliant
        }
        catch (ex) {
            childList.add(y); // IE only
        }

        document.getElementById("spnCount").innerHTML = "";

    }
}

function CategoriesRetrieved(list, catID, price) {
    if (xmlHttp.readyState == 4) {

        UpdateOptions(list, xmlHttp.responseText, catID, price);
    }

}

function UpdateOptions(list, response, catID, price) {
    list.length = 0;
    var arr = eval(response);

    var y;
    y = document.createElement('option');
    y.text = 'All';
    y.value = '-1';

    try {
        list.add(y, null); // standards compliant
    }
    catch (ex) {
        list.add(y); // IE only
    }

    for (var i = 0; i < arr.length; i++) {

        y = document.createElement('option');
        y.text = arr[i][1];
        y.value = arr[i][0];

        try {
            list.add(y, null); // standards compliant
        }
        catch (ex) {
            list.add(y); // IE only
        }

    }
    
    showSubcategory('document.forms.AdvertSearch.liVehicleTypeID', 'document.forms.AdvertSearch.liMakeID', 'All', 'make', '0', 'document.forms.AdvertSearch.liModelID');
    
}





function ShowMakeModelDiv() {

    var makeModediv = document.getElementById("showMakeModelDiv");
    var linkChoice = document.getElementById("linkIncludeMakeModel");
    var makeDDL = document.getElementById("liMakeID");
    var modelDDL = document.getElementById("liModelID");
    
    
    if (makeModediv.style.display == "inline") {
        makeModediv.style.display = "none";
        linkChoice.innerHTML = "Click to Include Manufacturer/Model in Search";
        makeDDL.selectedIndex = 0;
        modelDDL.selectedIndex = 0;
    }
    else {
        if (makeModediv.style.display == "none") {
            makeModediv.style.display = "inline";
            linkChoice.innerHTML = "Click to Remove Manufacturer/Model from Search";
        }
    }
}





