/*
    /js/search_form.js
    Brad Hanebury
    anti_dentite@hotmail.com
    December, 2007

    This file is to be used in conjuction with vehicle search pages
    It was written with the file forms/search_form.php in mind
    But it will also work with other files so long as the proper
    form elements have values and the form iteself is called usedcarsearch
*/

var currentCat = "<?=$_GET['category']?>";
var currentMake = "<?=$_GET['make']?>";
var currentModel = "<?=$_GET['model']?>";
var currentSortby = "<?=$_GET['sortby']?>";
var currentSortorder = "<?=$_GET['sortorder']?>";

function toggleSearchForm() {
    form = document.getElementById("searchform");
    button = document.getElementById("searchFormButton");

    if (form) {

        if (form.style.display == "none") {
            form.style.display = "inline";
            button.value = "Hide Search Form";
        }
        else {
            form.style.display = "none";
            button.value = "New Search";
        }
    }
}

function resetSearchFormPage() {
    document.usedcarsearch.currentpage.value = 0;
    document.usedcarsearch.startitem.value = 0;
    document.usedcarsearch.submit();
}

function getVehicleMakes(category) {

    var elem = document.usedcarsearch.make; //the select form element
    var url = "ajax/getVehicleMakes.php?category=" + category;
    loadXMLDoc (url, "", "GET");
    var result = ajaxResponseText; //result of ajax call

    if (!result) {
        return false;
    }

    //clear current options for make field
    //clearSelectOptions (elem);
    elem.length = 0;

    //clear the list of models
    document.usedcarsearch.model.length = null;
    document.usedcarsearch.model.options[0] = new Option("---Select Model---","");


    //format of string is makename=id&makename=id&...&makename=id
    var makes = result.split("&");

    //iterate through array and get make name and id
    for (var i=0; i<makes.length; i++) {
        thisMake = makes[i].split("=");
        vMake = thisMake[0];
        makeID = thisMake[1];
        elem.options[i] = new Option(vMake, makeID);
    }
}


function getVehicleModels(makeID) {

    var elem = document.usedcarsearch.model;
    var url = "ajax/getVehicleModels.php?makeID=" + makeID;
    loadXMLDoc (url, "", "GET");
    var result = ajaxResponseText;

    if (!result) {
        return false;
    }

    //clear current options for make field
    //clearSelectOptions (elem);
    elem.length = 0;

    //format of string is makename=id&makename=id&...&makename=id
    var models = result.split("&");

    //iterate through array and get make name and id
    for (var i=0; models.length; i++) {

        if (!models[i]) {
            break;
        }

        thisModel = models[i].split("=");
        vModel = thisModel[0];
        modelID = thisModel[1];
        elem.options[i] = new Option(vModel, modelID);


    }


}

//CODE BELOW HERE IS NO LONGER USED

function refineSearch()
{
    var sortby      = "";
    var sortorder   = "";
    var category    = "";
    var region      = "";
    var make        = "";
    var model       = "";
    var yearlow     = "";
    var yearhigh    = "";
    var pricelow    = "";
    var keywords    = "";
    var startitem   = "";
    var currentpage = "";
    var trans       = "";

    	if (document.usedcarsearch.sortby)
            sortby = document.usedcarsearch.sortby.value;

		if (document.usedcarsearch.sortorder)
            sortorder = document.usedcarsearch.sortorder.value;

    	if (document.usedcarsearch.category)
            category = document.usedcarsearch.category.value;

    	if (document.usedcarsearch.region)
            region = document.usedcarsearch.region.value;

    	if (document.usedcarsearch.make)
            make = document.usedcarsearch.make.value;

        if (document.usedcarsearch.model)
            model = document.usedcarsearch.model.value;

        if (document.usedcarsearch.yearlow)
            yearlow = document.usedcarsearch.yearlow.value;

        if (document.usedcarsearch.yearhigh)
            yearhigh = document.usedcarsearch.yearhigh.value;

        if (document.usedcarsearch.pricelow)
            pricelow = document.usedcarsearch.pricelow.value;

        if (document.usedcarsearch.pricehigh)
            pricehigh = document.usedcarsearch.pricehigh.value;

        if (document.usedcarsearch.keywords)
            keywords = document.usedcarsearch.keywords.value;

        if (document.usedcarsearch.startitem)
            startitem = document.usedcarsearch.startitem.value;

        if (document.usedcarsearch.currentpage)
            currentpage = document.usedcarsearch.currentpage.value;

        if (document.usedcarsearch.trans)
            trans = document.usedcarsearch.trans.value;

        if (make != currentMake || model != currentModel || category != currentCat ||
            sortorder != currentSortorder || sortby != currentSortby) {
            currentpage = 0;
            startitem = 0;
        }

		strpage =  "searchlist.phtml?category=" + category
                       + "&sortby=" + sortby
    				   + "&sortorder=" + sortorder
                       + "&region=" + region
                       + "&make=" + make
                       + "&model=" + model
                       + "&yearlow=" + yearlow
                       + "&yearhigh=" + yearhigh
                       + "&pricelow=" + pricelow
                       + "&pricehigh=" + pricehigh
                       + "&keywords=" + keywords
                       + "&startitem=" + startitem
                       + "&currentpage=" + currentpage
                       + "&trans=" + trans;


        window.location = strpage;
}


function refineSearch_2()
{
    var sortby      = "";
    var sortorder   = "";
    var category    = "";
    var region      = "";
    var make        = "";
    var model       = "";
    var yearlow     = "";
    var yearhigh    = "";
    var pricelow    = "";
    var keywords    = "";
    var startitem   = "";
    var currentpage = "";
    var trans       = "";

    	if (document.usedcarsearch_2.sortby)
            sortby = document.usedcarsearch_2.sortby.value;

		if (document.usedcarsearch_2.sortorder)
            sortorder = document.usedcarsearch_2.sortorder.value;

    	if (document.usedcarsearch_2.category)
            category = document.usedcarsearch_2.category.value;

    	if (document.usedcarsearch_2.region)
            region = document.usedcarsearch_2.region.value;

    	if (document.usedcarsearch_2.make)
            make = document.usedcarsearch_2.make.value;

        if (document.usedcarsearch_2.model)
            model = document.usedcarsearch_2.model.value;

        if (document.usedcarsearch_2.yearlow)
            yearlow = document.usedcarsearch_2.yearlow.value;

        if (document.usedcarsearch_2.yearhigh)
            yearhigh = document.usedcarsearch_2.yearhigh.value;

        if (document.usedcarsearch_2.pricelow)
            pricelow = document.usedcarsearch_2.pricelow.value;

        if (document.usedcarsearch_2.pricehigh)
            pricehigh = document.usedcarsearch_2.pricehigh.value;

        if (document.usedcarsearch_2.keywords)
            keywords = document.usedcarsearch_2.keywords.value;

        if (document.usedcarsearch_2.startitem)
            startitem = document.usedcarsearch_2.startitem.value;

        if (document.usedcarsearch_2.currentpage)
            currentpage = document.usedcarsearch_2.currentpage.value;

        if (document.usedcarsearch_2.trans)
            trans = document.usedcarsearch_2.trans.value;

        if (make != currentMake || model != currentModel || category != currentCat ||
            sortorder != currentSortorder || sortby != currentSortby) {
            currentpage = 0;
            startitem = 0;
        }


		strpage =  "searchlist.phtml?category=" + category
                       + "&sortby=" + sortby
    				   + "&sortorder=" + sortorder
                       + "&region=" + region
                       + "&make=" + make
                       + "&model=" + model
                       + "&yearlow=" + yearlow
                       + "&yearhigh=" + yearhigh
                       + "&pricelow=" + pricelow
                       + "&pricehigh=" + pricehigh
                       + "&keywords=" + keywords
                       + "&startitem=" + startitem
                       + "&currentpage=" + currentpage
                       + "&trans=" + trans;


        window.location = strpage;
}
