﻿function disableBtn(btnID, newText) {

    //initialize to avoid 'Page_IsValid is undefined' JavaScript error
    Page_IsValid = null;

    //check if the page request any validation
    // if yes, check if the page was valid
    if (typeof (Page_ClientValidate) == 'function') {
        Page_ClientValidate(); //you can pass in the validation group name also
    }

    //variables	
    var btn = document.getElementById(btnID);
    var isValidationOk = Page_IsValid;


    //if not IE then enable the button on unload before redirecting/ rendering
    if (navigator.appName !== 'Microsoft Internet Explorer') {
        EnableOnUnload(btnID, btn.value);
    }

    // isValidationOk is not null
    if (isValidationOk !== null) {
        //page was valid
        if (isValidationOk) {
            btn.disabled = true;
            btn.value = newText;
            btn.style.background = "url(ajax-loader.gif)";

        }
        else { //page was not valid
            btn.disabled = false;
        }
    }
    else { //the page don't have any validation request
        setTimeout("setImage('" + btnID + "')", 10);
        btn.disabled = true;
        btn.value = newText;

    }
}

//set the background image of the button
function setImage(btnID) {
    var btn = document.getElementById(btnID);
    btn.style.background = 'url(ajax-loader.gif)';
}

//enable the button and restore the original text value
function EnableOnUnload(btnID, btnText) {
    window.onunload = function() {
        var btn = document.getElementById(btnID);
        btn.disabled = false;
        btn.value = btnText;
    }
}

