﻿
// check if the number expression is a valid decimal expression
function checkNumericExpression(strNumericExp, checkInt) {
    if (!strNumericExp)
        return false;
    if (typeof (strNumericExp) != "string")
        return false;
    if (strNumericExp.match((checkInt) ? /^(\-|)\d+$/g : /^(\-|)\d+(\.\d+|)$/g))
        return true;
    return false;
}

function checkRequiredField(inputControl, labelControl) {
    if (inputControl && labelControl) {
        if (inputControl.value == "") {
            labelControl.innerHTML = "* Field Required *";
            return false;
        }
        else {
            labelControl.innerHTML = "";
            return true;
        }
    }
    else
        return false;
}

function checkInputIntControl(inputControlId, labelControlId) {
    var input = document.getElementById(inputControlId);
    var span = document.getElementById(labelControlId);
    if (input && span) {
        if (input.value == "")
            span.innerHTML = "";
        else {
            if (!checkNumericExpression(input.value, true)) {
                span.innerHTML = 'Input Error.';
            }
            else {
                span.innerHTML = '';
            }
        }
    }
}
function checkInputDoubleControl(inputControlId, labelControlId) {
    var input = document.getElementById(inputControlId);
    var span = document.getElementById(labelControlId);
    if (input && span) {
        if (input.value == "")
            span.innerHTML = "";
        else {
            if (!checkNumericExpression(input.value, false)) {
                span.innerHTML = 'Input Error.';
            }
            else {
                span.innerHTML = '';
            }
        }
    }
}

// get the decimal truncated of specified digits count
function formatFloat(number, fractionDigits) {
    if (typeof (number) != "number")
        number = 0;
    return number.toFixed(fractionDigits);
}

// encode the non-digitletter charactors in the url.
function encodeUrl(keyword) {
    if (!keyword)
        return null;
    var strTemp = "";
    for (var i = 0; i < keyword.length; i++) {
        var code = keyword.charCodeAt(i);
        if ((code >= 97 && code <= 122) || (code >= 65 && code <= 90) || (code >= 48 && code <= 57))
            strTemp += keyword.charAt(i);
        else if (code < 256)
            strTemp += "%" + ((code < 16) ? "0" : "") + code.toString(16).toUpperCase();
        else
            strTemp += "%u" + ((code < 4096) ? "0" : "") + code.toString(16).toUpperCase();
    }
    return strTemp;
}

// judge if the element is in other one.
function containsElement(elmParent, elm) {
    for (; elm; elm = elm.parentNode)
        if (elm == elmParent) return true;
    return false;
}

function setCookie(name, value) {
    var Days = 1;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}

function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}

function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
}

