﻿/***********************************************
*   BIKE SEARCH                                *
***********************************************/

function SelectKids() {
    kids = document.getElementById("ddlKids").value;
    ddlGender = document.getElementById("ddlGender");

    document.getElementById("ddlGender").selectedIndex = 0;
    document.getElementById("ddlAge").selectedIndex = 0;
    document.getElementById("ddlRiding").selectedIndex = 0;
    document.getElementById("ddlLocation").selectedIndex = 0;
    document.getElementById("ddlBikeType").selectedIndex = 0;

    if (kids == "-1") {
        document.getElementById("trGender").style.display = "none";
        document.getElementById("trAge").style.display = "none";
        document.getElementById("trRiding").style.display = "none";
        document.getElementById("trLocation").style.display = "none";
        document.getElementById("trType").style.display = "none";

    } // end if
    else {
        document.getElementById("trGender").style.display = "";
        document.getElementById("trAge").style.display = "none";
        document.getElementById("trRiding").style.display = "none";
        document.getElementById("trLocation").style.display = "none";
        document.getElementById("trType").style.display = "none";

        if (kids == "0") {
            removeAllOptions(ddlGender);
            addOption(ddlGender, "Please Select", "-1");
            addOption(ddlGender, "Male", "0");
            addOption(ddlGender, "Female", "1");

        } // end if
        if (kids == "1") {
            removeAllOptions(ddlGender);
            addOption(ddlGender, "Please Select", "-1");
            addOption(ddlGender, "Boy", "0");
            addOption(ddlGender, "Girl", "1");

        } // end if

    } // end else if

    SearchBikes();

} // end function SelectKids

function SelectGender() {
    
    gender = document.getElementById("ddlGender").value;
    kids = document.getElementById("ddlKids").value;

    document.getElementById("ddlAge").selectedIndex = 0;
    document.getElementById("ddlRiding").selectedIndex = 0;
    document.getElementById("ddlLocation").selectedIndex = 0;
    document.getElementById("ddlBikeType").selectedIndex = 0;

    document.getElementById("trType").style.display = "none";
    
    if (gender == "-1") {
        document.getElementById("trAge").style.display = "none";
        document.getElementById("trRiding").style.display = "none";
        document.getElementById("trLocation").style.display = "none";

    } // end if
    else {
        if (kids == "0") {
            document.getElementById("trAge").style.display = "none";
            document.getElementById("trType").style.display = "none";
            document.getElementById("trLocation").style.display = "none";
            document.getElementById("trRiding").style.display = "";
            LoadRiding();

        } // end if
        else if (kids == "1") {
            document.getElementById("trAge").style.display = "";
            document.getElementById("trRiding").style.display = "none";

        } // end else if

        document.getElementById("trLocation").style.display = "none";

    } // end if    

    SearchBikes();

} // end function SelectGender

function LoadRiding() {
    kids = document.getElementById("ddlKids").value;
    age = document.getElementById("ddlAge").value;
    gender = document.getElementById("ddlGender").value;
    ddlRiding = document.getElementById("ddlRiding");
    removeAllOptions(ddlRiding);

    addOption(ddlRiding, "Please Select", "-1");

    if (kids == "1") {

        // Kids
        
        if (age == "3" && gender == "0") {
            document.getElementById("trRiding").style.display = "";
            addOption(ddlRiding, "Freestyle", 2);
            addOption(ddlRiding, "Social / Leisure", 4);

        } // end if
        else
            document.getElementById("trRiding").style.display = "none";

    } // end if
    else if (kids == "0") {

    // Adult 
        
        if (gender == "0") 
        {
            addOption(ddlRiding, "Adventure / Off Road", 0);
            addOption(ddlRiding, "Commuting", 1);
            addOption(ddlRiding, "Freestyle", 2);
            addOption(ddlRiding, "Racing", 3);
            addOption(ddlRiding, "Social / Leisure", 4);

//            addOption(ddlRiding, "Commuting", "1");
//            addOption(ddlRiding, "Competition - BMX", "2");
//            addOption(ddlRiding, "Competition - Road", "3");
//            addOption(ddlRiding, "Cross Country", "4");
//            addOption(ddlRiding, "Fitness - Road", "5");
//            addOption(ddlRiding, "Freestyle - BMX", "6");
//            addOption(ddlRiding, "Freestyle - Street", "7");
//            addOption(ddlRiding, "Recreational", "0");

        } // end if
        else {

            addOption(ddlRiding, "Adventure / Off Road", 0);
            addOption(ddlRiding, "Commuting", 1);
            //addOption(ddlRiding, "Racing", 3);
            addOption(ddlRiding, "Social / Leisure", 4);
//            
//            addOption(ddlRiding, "Commuting", "1");
//            addOption(ddlRiding, "Cross Country", "4");
//            addOption(ddlRiding, "Fitness - Road", "5");
//            addOption(ddlRiding, "Recreational", "0");

        } // end else

    } // end if

} // end function LoadRiding

function SelectAge() {
    LoadRiding();
    
    SearchBikes();

} // end function SelectAge

function SelectType() {

    SearchBikes();

} // end function SelectAge

function SelectRiding() {
    riding = document.getElementById("ddlRiding").value;
    ddlLocation = document.getElementById("ddlLocation");
    ddlGender = document.getElementById("ddlGender");
    ddlBikeType = document.getElementById("ddlBikeType");

    document.getElementById("trLocation").style.display = "none";
    document.getElementById("trType").style.display = "none";

    removeAllOptions(ddlLocation);
    addOption(ddlLocation, "Please Select", "-1");

    removeAllOptions(ddlBikeType);
    addOption(ddlBikeType, "Please Select", "-1");

    if (kids == "0") {
        if (riding == "4" && gender == "1") {
            document.getElementById("trLocation").style.display = "";
            addOption(ddlLocation, "Beach", "7");
            addOption(ddlLocation, "Road", "9");
            addOption(ddlLocation, "Park Trail", "8");
            addOption(ddlLocation, "Park Trail and Road", "10");

        } // end if

        else if (riding == "0" && gender == "0") {
            document.getElementById("trType").style.display = "";
            addOption(ddlBikeType, "Hardtrail", "0");
            addOption(ddlBikeType, "Full Suspension", "1");

        } // end if
        else if (riding == "1" && gender == "0") {
            document.getElementById("trType").style.display = "";
            addOption(ddlBikeType, "Flat Bar Road", "2");
            addOption(ddlBikeType, "Single Speed / Fixie", "3");

        } // end if
        else if (riding == "2" && gender == "0") {
            document.getElementById("trLocation").style.display = "";
            addOption(ddlLocation, "Skate Park", "0");
            addOption(ddlLocation, "Street", "1");

        } // end if
        else if (riding == "3" && gender == "0") {
            document.getElementById("trLocation").style.display = "";
            addOption(ddlLocation, "BMX Race Track", "4");
            addOption(ddlLocation, "Road", "9");
            addOption(ddlLocation, "Track", "6");

        } // end if
        else if (riding == "4" && gender == "0") {
            document.getElementById("trLocation").style.display = "";
            addOption(ddlLocation, "Beach", "7");
            addOption(ddlLocation, "Park Trail", "8");
            addOption(ddlLocation, "Road", "9");
            addOption(ddlLocation, "Park Trail and Road", "10");

        } // end if
    
    } // end if

    SearchBikes();

} // end function SelectRiding

function SelectLocation() {

    removeAllOptions(ddlBikeType);
    addOption(ddlBikeType, "Please Select", "-1");
    
    SearchBikes();

} // end function SelectLocation

function SearchBikes() {
    kids = document.getElementById("ddlKids").value;
    gender = document.getElementById("ddlGender").value;
    age = document.getElementById("ddlAge").value;
    riding = document.getElementById("ddlRiding").value;
    loc = document.getElementById("ddlLocation").value;
    type = document.getElementById("ddlBikeType").value;

//    document.getElementById("_debug").innerHTML = "<p>";

//    document.getElementById("_debug").innerHTML += "kids: " + kids + "<br />";
//    document.getElementById("_debug").innerHTML += "gender: " + gender + "<br />";
//    document.getElementById("_debug").innerHTML += "age: " + age + "<br />";
//    document.getElementById("_debug").innerHTML += "riding: " + riding + "<br />";
//    document.getElementById("_debug").innerHTML += "loc: " + loc + "<br />";
//    document.getElementById("_debug").innerHTML += "type: " + type + "<br />";
//    
//    document.getElementById("_debug").innerHTML += "</p>";

    catid = 0;
    subcatid = 0;
    catid2 = 0;
    subcatid2 = 0;

    if (kids == "0") {

        if (gender == "0") {

            if (riding == "0") {

                if (type == "0") {
                    catid = 1;
                    subcatid = 2;

                } // end if
                else if (type == "1") {

                    catid = 1;
                    subcatid = 3;

                } // end else if

            } // end if
            else if (riding == "1") {

                if (type == "2") {
                    catid = 5;
                    subcatid = 15;

                } // end if
                else if (type == "3") {

                    catid = 12;
                    subcatid = 11;

                } // end else if

            } // end if
            else if (riding == "2") {

                if (loc == "1") {
                    catid = 1;
                    subcatid = 4;

                } // end if
                else if (loc == "0") {

                    catid = 3;
                    subcatid = 12;
                    catid2 = 3;
                    subcatid2 = 13;

                } // end else if

            } // end if
            else if (riding == "3") {

                if (loc == "9") {

                    catid = 5;
                    subcatid = 17;

                } // end if
                else if (loc == "4") {

                    catid = 3;
                    subcatid = 18;

                } // end else if
                else if (loc == "6") {

                    catid = 12;
                    subcatid = 18;

                } // end else if

            } // end if
            else if (riding == "4") {

                if (loc == "8") {

                    catid = 1;
                    subcatid = 1;

                } // end if
                else if (loc == "9") {

                    catid = 5;
                    subcatid = 16;

                } // end else if
                else if (loc == "10") {

                    catid = 4;
                    subcatid = 14;

                } // end else if
                else if (loc == "7") {

                    catid = 4;
                    subcatid = 21;

                } // end else if

            } // end if

        } // end if
        else if (gender == "1") {
            if (riding == "0") {

                catid = 6;
                subcatid = 2;

            } // end if
            else if (riding == "1") {

                catid = 6;
                subcatid = 15;

            } // end if
            else if (riding == "4") {
                if (loc == "7") {
                    catid = 6;
                    subcatid = 21;

                } // end if
                else if (loc == "8") {
                    catid = 6;
                    subcatid = 1;

                } // end if
                else if (loc == "9") {
                    catid = 6;
                    subcatid = 16;

                } // end if
                else if (loc == "10") {
                    catid = 6;
                    subcatid = 14;

                } // end if

            } // end if
        }
    
    } // end if
    else if (kids == "1") {

        if (gender == "0") {

            if (age == "0") {

                catid = 2;
                subcatid = 5;

            } // end if
            else if (age == "1") {

                catid = 2;
                subcatid = 6;

            } // end if
            else if (age == "2") {

                catid = 2;
                subcatid = 7;

            } // end if
            else if (age == "3") {

                if (riding == "2") {

                    catid = 3;
                    subcatid = 60;

                } // end if
                else if (riding == "4") {

                catid = 2;
                subcatid = 19;
                
                } // end if

            } // end if    

        } // end if
        else if (gender == "1") {

            if (age == "0") {

                catid = 2;
                subcatid = 8;

            } // end if
            else if (age == "1") {

                catid = 2;
                subcatid = 9;

            } // end if
            else if (age == "2") {

                catid = 2;
                subcatid = 10;

            } // end if
            else if (age == "3") {

                catid = 2;
                subcatid = 20;

            } // end if    
        
        } // end if 
    
    } // end else if

    SaveState();

    if (catid > 0 && subcatid > 0) {
        xmlHttp = GetXmlHttpObject();

        if (xmlHttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }

        var url = "search_results.ashx?g=" + gender + "&catid=" + catid + "&subcatid=" + subcatid + "&catid2=" + catid2 + "&subcatid2="+subcatid2;
        url += "&dt=" + Date();
        xmlHttp.onreadystatechange = searchResults;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

    } // end if
    else {
        //alert("here");
        document.getElementById("pnlProducts").innerHTML = "<div style='height: 300px'></div>";

    } // end else

} // end function SearchBikes

function searchResults() {
    
    if (xmlHttp.readyState == 1)
        document.getElementById("pnlProducts").innerHTML = "<div style=\"text-align: center; margin-top: 20px\"><img src=\"/images/loading.gif\" alt=\"Loading\" /></div>";
        
    if (xmlHttp.readyState == 4)
        document.getElementById("pnlProducts").innerHTML = xmlHttp.responseText;

} // end function

function SaveState() {
    selectedKidsIndex = document.getElementById("ddlKids").selectedIndex;
    selectedGenderIndex = document.getElementById("ddlGender").selectedIndex;
    selectedAgeIndex = document.getElementById("ddlAge").selectedIndex;
    selectedRidingIndex = document.getElementById("ddlRiding").selectedIndex;
    selectedLocIndex = document.getElementById("ddlLocation").selectedIndex;
    selectedTypeIndex = document.getElementById("ddlBikeType").selectedIndex;

    ddlKids = document.getElementById("ddlKids");
    ddlGender = document.getElementById("ddlGender");
    ddlAge = document.getElementById("ddlAge");
    ddlRiding = document.getElementById("ddlRiding");
    ddlLocation = document.getElementById("ddlLocation");
    ddlBikeType = document.getElementById("ddlBikeType");

    kidsVisible = "";

    if (document.getElementById("trKids").style.display == "none")
        kidsVisible = "none";

    genderVisible = "";

    if (document.getElementById("trGender").style.display == "none")
        genderVisible = "none";

    ageVisible = "";

    if (document.getElementById("trAge").style.display == "none")
        ageVisible = "none";

    ridingVisible = "";
    if (document.getElementById("trRiding").style.display == "none")
        ridingVisible = "none";

    locVisible = "";
    if (document.getElementById("trLocation").style.display == "none")
        locVisible = "none";

    typeVisible = "";
    if (document.getElementById("trType").style.display == "none")
        typeVisible = "none";

    ridingText = "";
    ridingValue = "";

    typeText = "";
    typeValue = "";

    locText = "";
    locValue = "";

    for (i = 0; i < ddlRiding.options.length; i++) {
        ridingText += ddlRiding.options[i].text + ",";
        ridingValue += ddlRiding.options[i].value + ",";

    } // end for

    for (i = 0; i < ddlBikeType.options.length; i++) {
        typeText += ddlBikeType.options[i].text + ",";
        typeValue += ddlBikeType.options[i].value + ",";

    } // end for

    for (i = 0; i < ddlLocation.options.length; i++) {
        locText += ddlLocation.options[i].text + ",";
        locValue += ddlLocation.options[i].value + ",";

    } // end for

    xmlHttp2 = GetXmlHttpObject();

    if (xmlHttp2 == null) {
        alert("Your browser does not support AJAX!");
        return;

    } // end if

    var url = "search_state.aspx?";
    url += "ki=" + selectedKidsIndex;
    url += "&gi=" + selectedGenderIndex;
    url += "&ai=" + selectedAgeIndex;
    url += "&ri=" + selectedRidingIndex;
    url += "&li=" + selectedLocIndex;
    url += "&ti=" + selectedTypeIndex;

    url += "&gv=" + genderVisible;
    url += "&av=" + ageVisible;
    url += "&rv=" + ridingVisible;
    url += "&lv=" + locVisible;
    url += "&tv=" + typeVisible;

    url += "&rt=" + ridingText;
    url += "&rvl=" + ridingValue;
    url += "&lt=" + locText;
    url += "&lvl=" + locValue;
    url += "&tt=" + typeText;
    url += "&tvl=" + typeValue;

    // Add Date for caching purposes
    url += "&dt=" + Date();

    //alert(url);

    xmlHttp2.onreadystatechange = saveStateChanged;
    xmlHttp2.open("GET", url, true);
    xmlHttp2.send(null);

} // end function SaveState

function saveStateChanged() {
    if (xmlHttp2.readyState == 4) {
        //alert(xmlHttp2.responseText);

    } // end if

} // end function

/***********************************************
*   END OF BIKE SEARCH                         *
***********************************************/