/*****************************************************************************
 * Javascript code to talk with the remote API via proxied Ajax
 *
 * Version History
 * ---------------
 *  2006-07-27  jdp created
 *
 *****************************************************************************/

var oAjax;
var sThisUri = "/js/makeManu.js.php";
var sRoot = "";
var oWaitingImg = null;

/**
 * Gets a list of models given a manufacturer.
 *
 * @param   sUri    the URI of the remote API to query
 * @param   oSelect the SELECT element that triggered the query (from which to
 *                  get the value)
 * @param   sDId    the ID of the SELECT element to display results in
 */
function getModelsByManuf(sUri, oSelect, sDId)
{
    // 1. get values
    var sSelectedValue = oSelect.options[oSelect.selectedIndex].value;
    var oDestSelect = document.getElementById(sDId);
    if (oDestSelect == null)
    {
        alert("Element ID:"+sDId+" not found");
        return true;
    }

    // 1a. check
    if (sSelectedValue == "0")
    {
        oDestSelect.innerHTML = "";
        oDestSelect.disabled = true;
        endisLabel(oDestSelect, false);
        return true;
    }

    // 2. construct URI
    sUri += "?ManCode="+sSelectedValue;

    // 3. get our Ajax object
    oAjax = getAjaxObject();
    if (oAjax == null)
    {
        alert("Ajax object not found");
        return true;
    }

    // 4. add our image element...
    if (oWaitingImg == null)
    {
        oWaitingImg = document.createElement("img");
        oWaitingImg.src = sRoot+"/img/experian/loading.gif";
        oWaitingImg.style.display = "none";
        var oTmp = oSelect.nextSibling;

        oSelect.parentNode.insertBefore(oWaitingImg, oTmp);
    }
    oWaitingImg.style.display = "inline";

    // 5. send the request
    oAjax.onreadystatechange = function() { _getModelsByManuf(oDestSelect); }
    oAjax.open("GET", sThisUri+"?ru="+escape(sUri), true);
    oAjax.send(null);

    return true;
}

/**
 * Callback function from JS
 *
 * @param   oDestSelect the display object
 */
function _getModelsByManuf (oDestSelect)
{
    // 1. get rid of useless triggers
    if (oAjax.readyState != 4)
        return true;

    // 2. let's go through here. We're using the XML object because IE seems to
    //    have /really/ serious issues with innerHTML...
    for (i = 0; i < oDestSelect.options.length; i++)
    {
        oDestSelect.options[i] = null;
    }
    var oXml = oAjax.responseXML;
    var aoOptions = oXml.getElementsByTagName("option");
    for (i = 0; i < aoOptions.length; i++)
    {
        oDestSelect.options[i] = new Option(aoOptions[i].firstChild.nodeValue,
                                            aoOptions[i].getAttribute("value"));
    }
    oDestSelect.disabled = false;

    // 2a. er, do stuff to the container as well
    endisLabel(oDestSelect, true);

    // 3. and get rid of the image
    oWaitingImg.style.display = "none";

    return true;
}

/**
 * Enables and disables a label. In a seperate method because the HTML is awful
 *
 * @param   oNode   the select node
 * @param   bShow   true to enable the node
 */
function endisLabel(oNode, bShow)
{
    var oDiv = oNode.parentNode.previousSibling;

    while (oDiv.nodeType != 1)
    {
        oDiv = oDiv.previousSibling;
    }

    // remove
    var sClass = oDiv.className;
    sClass = sClass.replace(/\s+promptDisabled/i, "");

    // add if required
    if (!bShow)
        sClass += " promptDisabled";

    oDiv.className = sClass;

    return true;
}

/**
 * Gets an XMLHttpRequest-type object applicable for the browser.
 */
function getAjaxObject()
{
    var oAjax = null;
    try
    {
        oAjax = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            oAjax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e2)
        {
            try
            {
                oAjax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e3)
            {}
        }
    }

    return oAjax;
}
