﻿///////////////////////////////////////////////////
//    Javascript for P-Challenge-Search.html     //
///////////////////////////////////////////////////

var xmlHttp;

var form = document.frmSearch;
var fldState, fldCity, fldClub, fldTrainer, fldAgeRange, fldGender;
var Ustate, Ucity, Uclub, Utrainer, Uagerange, Ugender;
var ajaxFld = 0;

// -------------------------------------------------------------------------------
function Search_onClick()
{
    fldState = document.getElementById("selState");
    fldCity = document.getElementById("selCity");
    fldClub = document.getElementById("selClub");
    fldTrainer = document.getElementById("selTrainer");
    fldAgeRange = document.getElementById("selAgeRange");
    fldGender = document.getElementById("selGender");
    Ustate = fldState[fldState.selectedIndex].value;
    Ucity = fldCity[fldCity.selectedIndex].value;
    Uclub = fldClub[fldClub.selectedIndex].value;
    Utrainer = fldTrainer[fldTrainer.selectedIndex].value;
    Uagerange = fldAgeRange[fldAgeRange.selectedIndex].value;
    Ugender = fldGender[fldGender.selectedIndex].value;

    var strURL = "./P-Challenge-Results.asp?s=" + Ustate + "&c=" + Ucity;
    strURL = strURL + "&cl=" + Uclub + "&t=" + Utrainer + "&a=" + Uagerange + "&g=" + Ugender + "&sf=0";
  
    window.location = strURL;
}


// -------------------------------------------------------------------------------
function ajaxFunction(fld)
{
    ajaxFld = fld;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert("Your browser does not support AJAX!");
       return;
    } 
    
    switch (fld)
    {
        case 1 :
            fldState = document.getElementById("selState");
            Ustate = fldState[fldState.selectedIndex].value;
            var url = "P-Challenge-Search.asp?fld=1&value=" + Ustate;
            break;
        case 2 :
            fldState = document.getElementById("selState");
            Ustate = fldState[fldState.selectedIndex].value;
            fldCity = document.getElementById("selCity");
            Ucity = fldCity[fldCity.selectedIndex].value;
            var url = "P-Challenge-Search.asp?fld=2&value=" + Ustate + "&value2=" + Ucity;
            break;
        case 3 :
            fldClub = document.getElementById("selClub");
            Uclub = fldClub[fldClub.selectedIndex].value;
            var url = "P-Challenge-Search.asp?fld=3&value=" + Uclub;
            break;
    }

    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


// -------------------------------------------------------------------------------
function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
        var returnMsg = xmlHttp.responseText;
        switch (ajaxFld)
        {
            case 1 :
                    document.getElementById("selCity").outerHTML = String(returnMsg);
                    document.getElementById("selClub").outerHTML = "<select name='selClub' id='selClub' onchange='fillTrainer()'><option value='0' selected>Club</option></select>";
                    document.getElementById("selTrainer").outerHTML = "<select name='selTrainer' id='selTrainer'><option value='0' selected>Trainer</option></select>"
                    break;
            case 2 :
                    document.getElementById("selClub").outerHTML = String(returnMsg);
                    document.getElementById("selTrainer").outerHTML = "<select name='selTrainer' id='selTrainer'><option value='0' selected>Trainer</option></select>"
                    break;
            case 3 :
                    document.getElementById("selTrainer").outerHTML = String(returnMsg);
                    break;
        }
    }
}

// -------------------------------------------------------------------------------
function GetXmlHttpObject()
{
    var xmlHttp = null;
    try
    {    
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest(); 
    }
    catch (e)
    {    
        // Internet Explorer 
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        }
    }
    return xmlHttp;
}


// -------------------------------------------------------------------------------
function fillCity()
{
    fldState = document.getElementById("selState");
    if (fldState.selectedIndex != 0)
        ajaxFunction(1);
}

// -------------------------------------------------------------------------------
function fillClub()
{
    fldState = document.getElementById("selState");
    fldCity = document.getElementById("selCity");
    if ((fldState.selectedIndex != 0) && (fldCity.selectedIndex != 0))
        ajaxFunction(2);
}

// -------------------------------------------------------------------------------
function fillTrainer()
{
    fldClub = document.getElementById("selClub");
    if (fldClub.selectedIndex != 0)
        ajaxFunction(3);
}



