﻿/// <reference path="jquery-1.3.2.js" />
function CreateButtons(text) {
    /// <summary>Look for all inputs with a button class and replace them with HTML buttons, making sure to keep the attributes and making them work for postbacks.</summary>
    var isIE6 = ($.browser.msie && parseInt(jQuery.browser.version) == 6);
    // button: Red with arrow.
    // button2: Red without arrow.
    // button3: Blue without arrow
    // button4: Small blue with small arrow.
    $("input.button,input.button2,input.button3,input.button4").each(function(i) {
        var input = $(this);

        //var button = $("<button type=\"submit\" value=\"" + input.attr("value") + "\">" + input.attr("value") + "</button>");
        var button = $("<button type=\"submit\" value=\"" + input.attr("value") + "\"><span>" + input.attr("value") + "</span></button>");
        button.attr("class", input.attr("class"));
        button.attr("id", input.attr("id"));
        button.attr("name", input.attr("name"));
        button.attr("style", input.attr("style"));
        button.onmouseover = input.attr("onmouseover");
        button.onmouseout = input.attr("onmouseout");

        if (isIE6) {
            button.bind("click", function() {
                DisableButtons(this);
            });
        }

        // As long as a button doesn't have a class of "noPleaseWait", then add a spinner and 'please wait' message to all red buttons with arrows (which aren't disabled), which are shown when clicked to proceed (but only when the page is valid).
        var spinner = null;
        var message = null;
        if (button.hasClass("button2") && !button.hasClass("noPleaseWait")) {
            spinner = $("<img src=\"/images.net/v2/global/icons/smallCircleWaitTransp.gif\" alt=\"*\" class=\"buttonSpinner\" />");
            message = $("<div class=\"buttonPleaseWait\">" + text.pleasewait + "</div>");
            button.bind("click", function() {
                if (!$(this).hasClass("disabled"))// && Page_ClientValidate()) 
                {
                    $(this).addClass("disabledAlt");
                    spinner.show();
                    spinner.css("display", "inline");
                    spinner.css("visibility", "visible");
                    message.css("visibility", "visible");
                    // Disable any other next step buttons if necessary.
                    if ($(this).hasClass("nextstep")) {
                        $(".nextstep").addClass("disabledAlt");
                    }
                }
            });

            // Preload the disabled state images.
            var preloadedImage1 = new Image(); preloadedImage1.src = "/images.net/v2/global/buttons/v7/buttondisabledright.gif";
            var preloadedImage2 = new Image(); preloadedImage2.src = "/images.net/v2/global/buttons/v7/buttondisabledleft.gif";
        }

        if (typeof (input.attr("onclick")) == "function") {
            eval("button.bind('click', " + input.attr("onclick") + ")");
        } else {
            eval("button.bind('click', function() { __doPostBack('" + input.attr("name") + "', '') });");
        }

        input.replaceWith(button);
        if (spinner != null) { spinner.insertBefore(button); }
        if (message != null) { message.insertAfter(button); }
    });


    
}

function DisableButtons(clickedButton) {
    // Disabled all buttons except the one that's been clicked. This is to fix a bug in IE6 where all button values are sent to the server, not just the one that's been clicked.
    var buttons = $("button");
    buttons.attr("disabled", "disabled");
    if (clickedButton != null) {
        $(clickedButton).removeAttr("disabled");
    }
    setTimeout("EnableButtons();", 2000);
}

function EnableButtons() {
    // Enable all buttons on the page.
    var buttons = $("button");
    buttons.removeAttr("disabled");
}

$(document).ready(function() {
    $("a.button").parent("div.button, div.frm_btn").each(function(i) {
        var outerDiv = $(this);
        outerDiv.removeClass("button");
        outerDiv.addClass("linkbutton");
    });
});


$(document).ready(function() {
    var isIE6 = ($.browser.msie && parseInt(jQuery.browser.version) == 6);
    //if IE 6 disable buttons
    if (isIE6) {
        $("a").each(function(i) {
            //if this is a link button add a click event to disable buttons and allow this event to work
            var link = $(this);
            if (link.attr("href")) {
            	if (link.attr("href").indexOf("javascript:__") == 0 || link.attr("href").indexOf("javascript:WebForm") == 0) {
                    link.click(function() { DisableButtons(null); });
                }
            }
        });
    }
});