﻿// JScript File

preloadNavImages();

function preloadNavImages()
{
    var oImgList = new Array("navbar-global.gif","navbar-top.gif");
    var oImages = new Array();
    var iCnt = 0;
    
    for (iCnt = 0; iCnt < oImgList.length; iCnt++)
    {
        oImages[iCnt] = getNavImage(oImgList[iCnt]);
    }
}

function getNavImage(sFileName)
{
    var oImg = new Image();
    oImg.src = "/images/nav" + sFileName;
    return oImg;
}

function updateLocationLists(sVal,sCityVal,sCountyVal)
{
    var oRefCntr = document.getElementById("cntrFindLocationsAtms");
    var aSelects = oRefCntr.getElementsByTagName("select");
    var aSource = null;
    var aCitySource = null;
    var aCountySource = null;
    var iCnt;
    var bSelected = false;
    var iSelectedIndex = 0;
            
    InitLocationsSelects(aSelects[0],aSelects[1]);
    
    var responseSuccess = function(oResp)
    {
        try
        {
            aSource = SplitSource(oResp.responseText, "~");
            aCitySource = SplitSource(aSource[0], "|");
            aCountySource = SplitSource(aSource[1], "|");
            
            if (typeof(aCitySource) != "string")
            {
                aSelects[0].disabled = false;
                for (iCnt = 0; iCnt < aCitySource.length; iCnt++)
                {
                    bSelected = (sCityVal == aCitySource[iCnt]) ? true : false;
                    if (bSelected)
                        iSelectedIndex = iCnt;
                    aSelects[0].options[iCnt] = new Option(aCitySource[iCnt], aCitySource[iCnt], bSelected);
                }
                if (iSelectedIndex > 0)
                {
                    aSelects[0].options[iSelectedIndex].selected = true;
                    iSelectedIndex = 0;
                }
            }
            
            if (typeof(aCountySource) != "string")
            {
                aSelects[1].disabled = false;
                for (iCnt = 0; iCnt < aCountySource.length; iCnt++)
                {
                    bSelected = (sCountyVal == aCountySource[iCnt]) ? true : false;
                    if (bSelected)
                        iSelectedIndex = iCnt;
                    aSelects[1].options[iCnt] = new Option(aCountySource[iCnt], aCountySource[iCnt], bSelected);
                }
                if (iSelectedIndex > 0)
                {
                    aSelects[1].options[iSelectedIndex].selected = true;
                    iSelectedIndex = 0;
                }
            }
        }
        catch (e)
        {
            alert(e);
        }
    };

    var responseFailure = function(oResp)
    {
        alert(oResp.statusText);
    }

    var callback =
    {
      success:responseSuccess,
      failure:responseFailure
    };
    
    if (aSelects.length == 2)
    {
        aSelects[0].disabled = true;
        aSelects[1].disabled = true;
        var transactionCities = YAHOO.util.Connect.asyncRequest('GET', '/utilities/getlocationslists.aspx?state=' + sVal, callback, null);
    }
}

function validateLocationForm(sBtnName)
{
    var oStateRef = document.getElementsByName("rbState");
    var oStateErrRef = document.getElementById("errSelectState");
    var oCityCountyErrRef = document.getElementById("errCityCountySelect");
    var oCityref = document.getElementById("CitySelect");
    var oCountyref = document.getElementById("CountySelect");
    
    oStateErrRef.style.display = "none";
    oCityCountyErrRef.style.display = "none";
    
    if (!oStateRef[0].checked && !oStateRef[1].checked)
    {
        oStateErrRef.style.display = "block";
        return false;
    }
    
    if (oCityref.selectedIndex == 0 && oCountyref.selectedIndex == 0)
    {
        oCityCountyErrRef.style.display = "block";
        return false;
    }
    
    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(sBtnName, "", false, "", "/locations/results.aspx", false, false));
    return true;
}

function SplitSource(sVal, sDelimiter)
{
    var sErrMsg = "Error extracting values: " + sVal;
    
    if (sVal.indexOf(sDelimiter) != -1)
    {
        var aRetVal = sVal.split(sDelimiter);
        
        if (aRetVal.length > 0)
            return aRetVal;
        else
            throw sErrMsg;
    }
    else
    {
        if (sVal.indexOf("Data error--") == 0)
            throw sErrMsg;
        else
            return sVal;
    }
}

function InitLocationsSelects()
{
    var iCnt;
    var iOptionCnt;
    
    try
    {
        for (iCnt = 0; iCnt < arguments.length; iCnt++)
        {
            for (iOptionCnt = arguments[iCnt].options.length; iOptionCnt >= 0; iOptionCnt--)
            {
                arguments[iCnt].options[iOptionCnt] = null;
            }
            arguments[iCnt].options[0] = new Option("Loading...");
        }
    }
    catch (e) { throw "Error clearing Select boxes"; }
}

function deselectOtherList(oRef)
{
    var oRefList;
    
    switch (oRef.name)
    {
        case "CitySelect":
            oRefList = document.getElementById("CountySelect");
            oRefList.options[0].selected = true;
            break;
        case "CountySelect":
            oRefList = document.getElementById("CitySelect");
            oRefList.options[0].selected = true;
            break;
        default:
            oRefList = document.getElementById("CountySelect");
            oRefList.options[0].selected = true;
            oRefList = document.getElementById("CitySelect");
            oRefList.options[0].selected = true;
            break;
    }
}