﻿function IsEmptyLbl(obj, msg, lbl)
{
    var str = Trim(obj.value);
    if (str == "") {
        obj.focus();
        lbl.innerHTML = msg;
        return false;
    }
    return true;
}

function Dropdown(obj, msg, lbl)
{
    if(obj.selectedIndex == 0){
        lbl.innerHTML = msg;
        obj.focus();
        return false;
    }
    return true;
}

//Trim functions
function Trim(TRIM_VALUE) {
//    if (TRIM_VALUE.length < 1) {
//        return "";
//    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if (TRIM_VALUE == "") {
        return "";
    }
    else {
        return TRIM_VALUE;
    }
}

//Trim functions
function RTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if (v_length < 0) {
        return "";
    }
    var iTemp = v_length - 1;
    while (iTemp > -1) {
        if (VALUE.charAt(iTemp) == w_space) {
        }
        else {
            strTemp = VALUE.substring(0, iTemp + 1);
            break;
        }
        iTemp = iTemp - 1;
    }
    return strTemp;
}

//Trim functions
function LTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    if (v_length < 1) {
        return "";
    }
    var v_length = VALUE.length;
    var strTemp = "";
    var iTemp = 0;
    while (iTemp < v_length) {
        if (VALUE.charAt(iTemp) == w_space) {
        }
        else {
            strTemp = VALUE.substring(iTemp, v_length);
            break;
        }
        iTemp = iTemp + 1;
    }
    return strTemp;
}

function chkEMail(obj, msg, lbl) {

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (obj.value != "") {
        if (filter.test(obj.value)) {
            return true;
        }
    }
    if (obj.value == "") {
        return true;
    }
    obj.focus();
    lbl.innerHTML = msg;
    return false;
}
function SetDivPosition(div, options) {
    var options = options || {};
    var height = options.height || 250;
    var width = options.width || 450;
    var top = options.top || 200;
    var left = options.left || 300;

    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        var pageWidth = document.body.scrollWidth + 'px';
        var pageHeight = document.body.scrollHeight + 'px';
        if (left != 0) {
            left = (document.body.scrollWidth - width) / 2;
        }
    }
    else if (document.body.offsetWidth) {
        var pageWidth = document.body.offsetWidth + 'px';
        var pageHeight = document.body.offsetHeight + 'px';
        if (left != 0) {
            left = (document.body.offsetWidth - width) / 2;
        }
    }
    else {
        var pageWidth = '100%';
        var pageHeight = '100%';
    }

    if (height == 0 && width == 0) {
        height = pageHeight;
        width = pageWidth;
        div.style.width = width;
        div.style.height = height;
    }
    else {
        height = height + 'px';
        width = width + 'px';
        div.style.width = width;
    }
    //document.body.scrollTop
    div.style.top = top + 'px';
    div.style.left = left + 'px';
}

var divTop;
function ShowHideItemDiv(IsShow, divId) {
    if (IsShow) {
        document.getElementById("darkenScreenObject").style.display = 'block';
        document.getElementById(divId).style.display = 'block';
        divTop = parseInt(document.getElementById(divId).style.top.replace("px", ""));
        document.getElementById(divId).style.top = (divTop + document.body.scrollTop) + 'px';
    }
    else {
        document.getElementById("darkenScreenObject").style.display = 'none';
        document.getElementById(divId).style.display = 'none';
        if (typeof (divTop) != "undefined")
            document.getElementById(divId).style.top = divTop + 'px';
    }
}

function isInteger(obj, msg, lbl) {
    var s = Trim(obj.value);
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
        {
            lbl.innerHTML = msg;
            obj.focus();
            return false;
        }
    }
    // All characters are numbers.
    return true;
}

function IsZero(obj, msg, lbl)
{
    var s = Trim(obj.value);
    if(s <= 0 && s!='')
    {
        lbl.innerHTML = msg;
            obj.focus();
            return false;
    }
    return true;
}

function ValidateProductDiscount(obj, msg, lbl) {
    var val = obj.value;
    if ((val > 100))
    {
        lbl.innerHTML = "Product Discount should be between 0 to 100.";
        obj.focus();
        return false;
    }
    else
    {
        lbl.innerHTML = "";
        return true;
    }

}  

function validateDate(string, msg, lbl)

{

var str = Trim(string.value);

var m = /^(\d\d)-(\w{3})-(\d{4})$/.exec(str);

if(m == null)

{

    lbl.innerHTML = msg;

    string.focus();

    return false;

}

if (!m)

{

    lbl.innerHTML = msg;

    string.focus();

    return false;

}

var month = validateDate.months[m[2].toLowerCase()];

if(month == null)

{

    lbl.innerHTML = msg;

    string.focus();

    return false;

}

if (typeof month != "number")

{

    lbl.innerHTML = msg;

    string.focus();

    return false;

}

var date = +m[1];

var year = +m[3];

var d = new Date(year, month, date);

if (d.getDate() != date || d.getMonth() != month)

{

    lbl.innerHTML = msg;

    string.focus();

    return false;

}

return true;

}

 

validateDate.months = {

jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5,

jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11

};

