function ROC(e)
{
    e = getEventObject(e);
    var input = getEventSource(this, e);

    addClass(input.form, "Refreshing");
    setFormEnabled(input.form, false);
    AJAX_MakeRequest("http://www.johnsonscars.co.uk/js/refresh.php?" + encodeURIComponent(input.name) + "=" + encodeURIComponent(input.value) + "&Refresh" + encodeURIComponent(input.name), ROCCallBack);
    return true;
}

function ROCCallBack(responseXML)
{
    var input = findAndUpdateParameters(responseXML.documentElement);
    if (input)
    {
        removeClass(input.form, "Refreshing");
        setFormEnabled(input.form, true);
    }
}

function findAndUpdateParameters(domElement)
{
    var param = false;

    for (var index = 0; index < domElement.childNodes.length; index++)
    {
        var childNode = domElement.childNodes[index];
        if (childNode.nodeType == 1)
        {
            if (childNode.nodeName == "PARAMETER")
            {
                if (param)
                {
                    updateParameter(childNode);
                }
                else
                {
                    param = updateParameter(childNode);
                }
            }
            else
            {
                if (param)
                {
                    findAndUpdateParameters(childNode);
                }
                else
                {
                    param = findAndUpdateParameters(childNode);
                }
            }
        }
    }
    return param;
}

function updateParameter(paramElement)
{
    var paramName = paramElement.getAttribute("Name");
    var paramInput = document.getElementById(paramName);
    if (paramInput)
    {
        while (paramInput.childNodes.length > 0)
        {
            paramInput.removeChild(paramInput.childNodes[0]);
        }

        for (var index = 0; index < paramElement.childNodes.length; index++)
        {
            var childNode = paramElement.childNodes[index];
            if (childNode.nodeType == 1 && childNode.nodeName == "OPTION")
            {
                var newOptionElem = document.createElement("OPTION");
                newOptionElem.setAttribute("value", childNode.getAttribute("Value"));
                newOptionElem.appendChild(document.createTextNode(childNode.getAttribute("Text")));
                paramInput.appendChild(newOptionElem);
            }
        }
        return paramInput;
    }
    return false;
}

// Check that the browser has appropriate AJAX and DOM support
if (AJAX_IsSupported() &&
    encodeURIComponent &&
    document.getElementById &&
    document.getElementsByTagName &&
    document.createElement &&
    document.createTextNode &&
    document.documentElement &&
    document.documentElement.childNodes &&
    document.documentElement.getAttribute &&
    document.documentElement.setAttribute &&
    document.documentElement.removeChild &&
    document.documentElement.appendChild)
{
    var inputs = document.getElementsByTagName("INPUT");
    if (inputs)
    {
        for (var index = 0; index < inputs.length; index++)
        {
            var input = inputs[index];
            if (input && input.type && input.type.toLowerCase &&
                input.id && input.id.toLowerCase && input.id.substr && input.id.length > 3 &&
                input.type.toLowerCase() == "submit" &&
                input.id.substr(input.id.length - 3, 3).toLowerCase() == "roc")
            {
                var submitID = input.id;
                var selectID = submitID.substr(0, submitID.length - 3);

                var selectNode = document.getElementById(selectID);
                if (addEventListener(selectNode, "change", ROC))
                {
                    addClass(selectNode.form, "JSROC");
                }
            }
        }
    }
}

/**
 * Adds an "Print this page" link on the Vehicle Details and Special Offer Detail pages.
 */
function addPrintLink()
{
    var nav_list, list_item, print_link;

    // Check we've got everything we need
    if (!document.getElementById) return;
    if (!document.createElement) return;
    if (!document.appendChild) return;
    if (!document.createTextNode) return;
    if (!document.getElementById("nav_vehicle_results")) return;

    nav_list = document.getElementById("nav_vehicle_results");
    list_item = document.createElement("li");
    print_link = document.createElement("a");
    print_link.appendChild(document.createTextNode("Print this page"));

    // Quick and dirty is fine for this
    print_link.onclick = function()
    {
        window.print();
        return (false);
    }

    print_link.setAttribute("href", "#");       // So that the hand cursor is still shown
    print_link.setAttribute("title", "Print this page");

    list_item.appendChild(print_link)
    nav_list.appendChild(list_item);
}

addPrintLink();

/**
 * Pop-up view of the vehicle image
 */
function showPopUpImage(e)
{
    e = getEventObject(e);

    var linkElem = getEventSource(this, e);

    if (linkElem && linkElem.tagName)
    {
        while (linkElem.tagName.toLowerCase() != 'a' && linkElem && linkElem.parentNode)
        {
            linkElem = linkElem.parentNode;
        }

        if (linkElem)
        {
            var maxWidth = 1280;
            var maxHeight = 960;

            var minWidth = 640;
            var minHeight = 480;

            var imgWidth = (window.screen.availWidth - 200);
            var imgHeight = (window.screen.availHeight - 200);

            if (imgHeight > imgWidth * 3 / 4)
            {
                imgHeight = imgWidth * 3 / 4;
            }

            if (imgWidth > maxWidth)
            {
                imgWidth = maxWidth;
            }

            imgHeight = imgWidth * 3 / 4

            if (imgWidth < minWidth || imgHeight < minHeight)
            {
                imgWidth = minWidth;
                imgHeight = minHeight;
            }

            var width = imgWidth + 20;
            var height = imgHeight + 30;

            var left = (window.screen.availWidth - width) / 2;
            var top = (window.screen.availHeight - height) / 2;

            var win = window.open(linkElem.href+"x="+imgWidth+"&y="+imgHeight+"&fixedSize=true", 'VehicleImage', 'height='+height+',width='+width+',left='+left+',top='+top+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
            if (win)
            {
                cancelEvent(e);
                return false;
            }
        }
    }
    return true;
}

if (document.getElementById &&
    window.screen &&
    window.screen.availWidth &&
    window.screen.availHeight)
{
    var popUpLink = document.getElementById('VehicleDetailImgPopup');
    if (popUpLink)
    {
        addEventListener(popUpLink, "click", showPopUpImage);
    }
}

