﻿function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

//forceValidatorIsValid(true);
//ValidatorUpdateIsValid()

function customValidatorEnable2(obj, enable) {
    obj.enableClientScript = enable;
    ValidatorUpdateDisplay(obj);
}

function customValidatorEnable(obj, enable) {
    obj.enabled = enable;
    ValidatorUpdateDisplay(obj);
}

function isPageValid() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        if (!Page_Validators[i].isvalid) {
            return false;
        }
    }
    return true;
}

function forceValidatorIsValid(blnTrue) {
    Page_IsValid = blnTrue;
    Page_ValidationActive = !blnTrue;
//    var i;
//    for (i = 0; i < Page_Validators.length; i++) {
//        Page_Validators[i].IsValid = blnTrue;
//        
////        if (i < 10)
////            alert(Page_Validators[i].id + ': ' + Page_Validators[i].IsValid)
//    }
}

function showProgressUpdatePanel() {
    showObject(GlobalUpdateProgressPanelControlId);
    //showObject(document.getElementById('ctl00_PleaseWaitModal_upPleaseWait'));
}

function hideProgressUpdatePanel() {
    hideObject(GlobalUpdateProgressPanelControlId);
    //hideObject(document.getElementById('ctl00_PleaseWaitModal_upPleaseWait'));
}

function onPrintableReportButtonClick(page) {
    if (document.forms[0] != null) {
        var oldAction = document.forms[0].action;
        var oldMethod = document.forms[0].method;
        var oldTarget = document.forms[0].target;
    
        document.forms[0].action = page;
        document.forms[0].method = 'post';
        document.forms[0].target = '_blank';
        document.forms[0].submit();
        document.forms[0].action = oldAction;
        document.forms[0].method = oldMethod;
        document.forms[0].target = oldTarget;
    }
}

function changeFormTarget() {
    var oldTarget = document.forms[0].target;
    document.forms[0].target = '_blank';
    document.forms[0].submit();
    document.forms[0].target = oldTarget;
}

function doButtonClick(buttonName, e) {
    if (!e) var e = window.event;

    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;

    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13) {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) { //If we find the button click it
            btn.click();
            if (window.event)
                e.keyCode = 0;
        }
    }
}

// Removes leading whitespaces
function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value) {
    return LTrim(RTrim(value));
}

function RemoveSpaces(control) {
    var string = control.value;
    var tstring = "";
    string = '' + string;
    splitstring = string.split(" ");
    for (i = 0; i < splitstring.length; i++)
        tstring += splitstring[i];
    control.value = tstring;
}

function PreventSubmit(control) {
    if (!control) var e = window.event;
    if (event.keyCode == 13) {
        event.returnValue = false;

        return false;
    }
    return true;
}


// remove any whitespace from the email such that the compare validator will work properly
function SuppressRightClickPaste(control) {
    var responseString = 'Cannot paste into Confirm Email entry field.';

    if (navigator.appName == 'Netscape' &&
        (e.which == 3 || e.which == 2)) {
        alert(responseString);
        return false;
    }

    else if (navigator.appName == 'Microsoft Internet Explorer' &&
        (event.button == 2 || event.button == 3)) {
        alert(responseString);
        return false;
    }

    return true;
}

// disable past function via javascript
// need to disable paste in an input control so that the user cannot copy/paste their email address
function SuppressPaste(control) {
    // Check if the control key is pressed.
    // If the Netscape way won't work (event.modifiers is undefined),
    // try the IE way (event.ctrlKey)
    var ctrl = typeof event.modifiers == 'undefined' ?
    event.ctrlKey : event.modifiers & Event.CONTROL_MASK;

    // Check if the 'V' key is pressed.
    // If the Netscape way won't work (event.which is undefined),
    // try the IE way (event.keyCode)
    var v = typeof event.which == 'undefined' ?
    event.keyCode == 86 : event.which == 86;

    // If the control and 'V' keys are pressed at the same time
    if (ctrl && v) {
        //discard the keystroke and clear the text box
        //control.value = '';
        return false;
    }

    // now prevent Shift-insert
    var shift = typeof event.modifiers == 'undefined' ?
        event.shiftKey : event.modifiers & Event.SHIFT_MASK;

    var insert = typeof event.which == 'undefined' ?
    event.keyCode == 45 : event.which == 45;


    // If the control and 'V' keys are pressed at the same time
    if (insert && shift) {
        //discard the keystroke and clear the text box
        //control.value = '';
        return false;
    }

    return true;
}

function StopEvent(pE) {
    if (!pE)
        if (window.event)
        pE = window.event;
    else
        return;
    if (pE.cancelBubble != null)
        pE.cancelBubble = true;
    if (pE.stopPropagation)
        pE.stopPropagation();
    if (pE.preventDefault)
        pE.preventDefault();
    if (window.event)
        pE.returnValue = false;
    if (pE.cancel != null)
        pE.cancel = true;
}

function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp('(^|\\\\s)' + searchClass + '(\\\\s|$)');
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}

function getRadioSelectedValue(objRadioList) {
    var options = objRadioList;
    for (i = 0; i < options.length; i++) {
        var opt = options[i];
        if (opt.checked) {
            return opt.value;
        }
    }
}

//Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/
//theElements is an array of objects designated as a comma separated list of their IDs
//If an element in theElements is not a checkbox, then it is assumed
//that the function is recursive for that object and will check/uncheck
//all checkboxes contained in that element
function checkUncheckSome(controller, theElements) {
    var formElements = theElements.split(',');
    var theController = document.getElementById(controller);

    for (var z = 0; z < formElements.length; z++) {
        theItem = document.getElementById(formElements[z]);
        if (theItem.type) {
            if (theItem.type == 'checkbox') {
                theItem.checked = theController.checked;
            }
        } else {
            theInputs = theItem.getElementsByTagName('input');
            for (var y = 0; y < theInputs.length; y++) {
                if (theInputs[y].type == 'checkbox' && theInputs[y].id != theController.id) {
                    theInputs[y].checked = theController.checked;
                }
            }
        }
    }
}

function CheckCheckAll(makeChecked) {
    var fmobj = document.forms[0];
    for (var i = 0; i < fmobj.elements.length; i++) {
        var e = fmobj.elements[i];
        if (e.type == 'checkbox') {
            e.checked = makeChecked;
        }
    }
}


function cancelEvent(e) {
    if (!e)
        var e = window.event;

    e.cancelBubble = true;
    if (e.stopPropagation)
        e.stopPropagation();
}

function changeBackgroundColor(obj, color) {
    obj.bgColor = color;
}

function InitializeRequest(sender, args) {
    //alert('This call is async: ' + prm.get_isInAsyncPostBack());
    if (prm.get_isInAsyncPostBack())
        args.set_cancel(true);

    postBackElement = args.get_postBackElement();

    //alert($get('GlobalUpdateProgressPanelControlId').style.display);
    //if (postBackElement.id == 'Button1')
    var modalMessage = $get('GlobalUpdateProgressPanelControlId');

    if (modalMessage != null)
        modalMessage.style.display = 'block';
}

function EndRequest(sender, args) {
    //if (postBackElement.id == 'Button1')
    //alert($get('GlobalUpdateProgressPanelControlId').style.display);
    var modalMessage = $get('GlobalUpdateProgressPanelControlId');

    if (modalMessage != null)
        modalMessage.style.display = 'none';
}
