﻿
//Used to display some activity when the request is being processed
function InitializeRequest(sender, args) {
    if (prm.get_isInAsyncPostBack()) {
        args.set_cancel(true);
    }    
    ShowProgressMessage();
}

//End of the request
function EndRequest(sender, args) {
    HideProgressMessage();
    HideErrorMessage();
    EvaluateEndRequestArgs(args, "ErrorMessage");
}
        

function HideProgressMessage() {
    $get("ProgressStatus").style.display = "none";
}

function ShowProgressMessage() {
    $get("ProgressStatus").style.display = "block";
    var size = GetWindowSize();
    $get("ProgressStatus").style.left = ((size[0] / 2) - 20) + "px";
    $get("ProgressStatus").style.top = ((size[1] / 2) - 20) + "px";
}

function EvaluateEndRequestArgs(args, labelName) 
{
    if (!args.get_errorHandled()) {
        if (args.get_error() != undefined && args.get_error().httpStatusCode == '500') {
            ShowErrorMessage("Error occurred - <br/>" + args.get_error().message);
            args.set_errorHandled(true);
        }
    }
}

function HideErrorMessage() {
    $get("ErrorMessage").style.display = "none";
}

function ShowErrorMessage(txt) {
    $get("ErrorMessage").style.display = "block";
    $get("ErrorMessage").innerHTML = txt;
}

function SetStatus(elemName, txt, isError) 
{
    if (isError) {
        $get(elemName).className = "error";
        if ($get(elemName).setAttribute)
            $get(elemName).setAttribute("class", "error");
    }
    else {
        $get(elemName).className = "success";
        if ($get(elemName).setAttribute)
            $get(elemName).setAttribute("class", "success");
    }
    $get(elemName).style.display = "block";
    $get(elemName).innerHTML = txt;
}

function GetWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}



// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
    if (string == null)
        return "";
    var newString = '';
    var substring = '';
    var beginningFound = false;

    // copy characters over to a new string
    // retain whitespace characters if they are between other characters
    for (var i = 0; i < string.length; i++) {

        // copy non-whitespace characters
        if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {

            // if the temporary string contains some whitespace characters, copy them first
            if (substring != '') {
                newString += substring;
                substring = '';
            }
            newString += string.charAt(i);
            if (beginningFound == false) beginningFound = true;
        }

        // hold whitespace characters in a temporary string if they follow a non-whitespace character
        else if (beginningFound == true) substring += string.charAt(i);
    }
    return newString;
}
