﻿function onError(result) {
    _updating = false;
    if (_progressLayerId) {
        var progressLayer = $get(_progressLayerId);
        if (progressLayer)
            progressLayer.style.display = 'none';
    }
//    var stackTrace = result.get_stackTrace();
//    var message = result.get_message();
//    var statusCode = result.get_statusCode();
//    var exceptionType = result.get_exceptionType();
//    var timedout = result.get_timedOut();

    // Display the error.    
//    var RsltElem = 'Stack Trace: ' + stackTrace + '\n' +
//        'Service Error: ' + message + '\n' +
//        'Status Code: ' + statusCode + '\n' +
//        'Exception Type: ' + exceptionType + '\n' +
//        'Timedout: ' + timedout;
    
    //Set General Error
    RsltElem = 'Error connecting to server.';
    if(_updating)
        alert(RsltElem);
}
function onErrorEmptyOutput(result) {
}
function elementHasEvent(elemId, evt) {
    var elem = document.getElementById(elemId);

    if (elem != null) {
        if (eval('elem.' + evt))
            return true;
    }

    return false;
}
function getText(element) {
    if (element) {
        if (typeof element.innerText != 'undefined') {
            return element.innerText;
        }
        else if (typeof element.textContent != 'undefined') {
            return element.textContent;
        }
    }
    else {
        return '';
    }
}
function getHtml(element) {
    if (element) {
        if (typeof element.innerHTML != 'undefined') {
            return element.innerHTML;
        }
    }
    else {
        return '';
    }
}
function setText(element, text) {
    if (element) {
        if (element.type == 'text') {
            element.value = text;
        }
        else if (typeof element.innerText != 'undefined') {
            element.innerText = text;
        }
        else if (typeof element.textContent != 'undefined') {
            element.textContent = text;
        }
    }
}
function setHtml(element, html) {
    if (element) {
        if (typeof element.innerHTML != 'undefined') {
            element.innerHTML = html;
        }
    }
}
function encodeMessage(message) {
    return message.split("'").join("\'");
}
function isArrayValid(element) {
    if ((element != null) && (typeof element != 'undefined') && (element.length > 0))
        return true;
    else
        return false;
}
function elementExists(element) {
    if ((element != null) && (typeof element != 'undefined'))
        return true;
    else
        return false;
}
function isIE() {
    if (document.all && !window.opera && document.createElement) {
        /*
        MSIE 6 for windows does not support DOM-insertion of radio buttons.
        The following code works around that lack of support.
        */
        return true;
    }
    return false
}
function clearListBox(listBox) {
    for (var i = listBox.childNodes.length - 1; i > -1; i--) {
        listBox.removeChild(listBox.childNodes[i]);
    }
}
function addListBoxItem(listBox, text, value) {
    var opt = document.createElement('option');

    listBox.appendChild(opt);

    setText(opt, text);

    if (value != null) {
        opt.value = value;
    }
}
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}
function trimChars(str, chars)    
{   
    return ltrim(rtrim(str, chars), chars);   
}
function lTrimChars(str, chars)    
{   
    chars = chars || "\\s";   
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");   
}
function rTrimChars(str, chars)    
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");   
}
function formatNumber(intNumber) {
    var result = intNumber / 100;
    result = result.toFixed(2);
    if (_decimalSeparator != '.')
        result = result.replace('.', _decimalSeparator);
    return result;
}
function formatFloat(floatNumber) {
    var stringNumber = floatNumber.toString();
    if (_decimalSeparator != '.')
        stringNumber = stringNumber.replace('.', _decimalSeparator);
    return stringNumber;
}
function parseNumber(stringNumber) {
    var resultNumber;
    if(_decimalSeparator.indexOf(',') > -1)
        stringNumber = stringNumber.replace(_decimalSeparator, '.');
    resultNumber = parseFloat(stringNumber);
    if (resultNumber == 'NaN')
        resultNumber = 0;
    return resultNumber.toFixed(2);
}
function getUrlParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
function divide(numerator, denominator) {
    // In JavaScript, dividing integer values yields a floating point result (unlike in Java, C++, C)
    // To find the integer quotient, reduce the numerator by the remainder first, then divide.
    var remainder = numerator % denominator;
    var quotient = (numerator - remainder) / denominator;
    return quotient;
}
function getCookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    //debugger;
    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
function setCookie(name, value, expires, expireType, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());
    /*if the expires variable is set, make the correct
    expires time */
    if (expires) {
        if (expireType) {
            switch (expireType) {
                case 'M':
                    expires = expires * 1000 * 60; // for minutes delete * 60 * 24
                    break;
                case 'H':
                    expires = expires * 1000 * 60 * 60; // to make it for hours delete * 24
                case 'D':
                default:
                    expires = expires * 1000 * 60 * 60 * 24; //for x number of days
                    break;
            }
        }
        //Default To Days
        //expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    var cookieText = name + '=' + escape(value) +
                    ((expires) ? ';expires=' + expires_date.toGMTString() : '') +
                    ((path) ? ';path=' + path : '') +
                    ((domain) ? ';domain=' + domain : '') +
                    ((secure) ? ';secure' : '');
    document.cookie = cookieText;    
}
// this deletes the cookie when called
function deleteCookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

String.prototype.startsWith = function(str)
{ return (this.match("^" + str) == str) }

String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }