﻿
function ShowSessionExpireMsg() {

    var strRedirectUrl = "window.parent.location.href='../default.aspx?logout=true'"

    var t;
    t = setTimeout(strRedirectUrl, 5000);


}

function Processing(strMsg) {
    if (strMsg.toString().length > 0) {
        if (document.getElementById('divLoading')) document.getElementById('divLoading').innerHTML = "<img src=\"images/loading.gif\" width=\"16\" height=\"16\" border=\"0\" />" + strMsg;
    }
    else {
        if (document.getElementById('divLoading')) document.getElementById('divLoading').innerHTML = "";
    }

}
function ProcessingLoader(divId, strMsg) {
    if (strMsg.toString().length > 0) {
        if (document.getElementById(divId)) document.getElementById(divId).innerHTML = "<img src=\"images/loading.gif\" width=\"16\" height=\"16\" border=\"0\" />" + strMsg;
    }
    else {
        if (document.getElementById(divId)) document.getElementById(divId).innerHTML = "";
    }

}
function ProcessingInnerLoader(divId, strMsg) {
    if (strMsg.toString().length > 0) {
        if (document.getElementById(divId)) document.getElementById(divId).innerHTML = "<img src=\"../images/loading.gif\" width=\"16\" height=\"16\" border=\"0\" />" + strMsg;
    }
    else {
        if (document.getElementById(divId)) document.getElementById(divId).innerHTML = "";
    }

}

function GetURLParamsValue(name) {
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var tmpURL = window.location.href;
    var results = regex.exec(tmpURL);
    if (results == null) return "";
    else return results[1];
}

function SetInnerText(obj, text) {

    var element = obj;


    if (element) {

        if (typeof element.textContent != 'undefined') {
            element.textContent = text;
        }

        else if (typeof element.innerText != 'undefined') {
            element.innerText = text;
        }

    }
}
function ValidatePhoneOld() {
    p = p1.value;
    p2 = p1.name;
    if (p.length == 3) {
        //d10=p.indexOf('(')
        pp = p;
        d4 = p.indexOf('(')
        d5 = p.indexOf(')')
        if (d4 == -1) {
            pp = "(" + pp;
        }
        if (d5 == -1) {
            pp = pp + ")";
        }
        //pp="("+pp+")";
        document.getElementById(p2).value = "";
        document.getElementById(p2).value = pp;
    }
    if (p.length > 3) {
        d1 = p.indexOf('(')
        d2 = p.indexOf(')')
        if (d2 == -1) {
            l30 = p.length;
            p30 = p.substring(0, 4);
            //alert(p30);
            p30 = p30 + ")"
            p31 = p.substring(4, l30);
            pp = p30 + p31;
            //alert(p31);
            document.getElementById(p2).value = "";
            document.getElementById(p2).value = pp;
        }
    }
    if (p.length > 5) {
        p11 = p.substring(d1 + 1, d2);
        if (p11.length > 3) {
            p12 = p11;
            l12 = p12.length;
            l15 = p.length
            //l12=l12-3
            p13 = p11.substring(0, 3);
            p14 = p11.substring(3, l12);
            p15 = p.substring(d2 + 1, l15);
            document.getElementById(p2).value = "";
            pp = "(" + p13 + ")" + p15;
            document.getElementById(p2).value = pp;

            //obj1.value="";
            //obj1.value=pp;
        }
        l16 = p.length;
        p16 = p.substring(d2 + 1, l16);
        l17 = p16.length;
        if (l17 > 3 && p16.indexOf('-') == -1) {
            p17 = p.substring(d2 + 1, d2 + 4);
            p18 = p.substring(d2 + 4, l16);
            p19 = p.substring(0, d2 + 1);
            //alert(p19);
            pp = p19 + p17 + "-" + p18;
            document.getElementById(p2).value = "";
            document.getElementById(p2).value = pp;

        }
    }
    //}
    setTimeout(ValidatePhoneOld, 100)
}

function FormateCellNumber(obj) {
    if (!FormatePhoneNumber(obj)) {
        ClearTopStatus();
        WarningBox('Invalid cell number');
        return false;
    }
    else
        return true;
}



function FormatePhoneNumber(obj) {
    if (document.getElementById(obj.id).value != "") {

        var iChars = "!`@#$%^&*+=[]\\\;,/{}|\:<>?'''";

        for (var i = 0; i < document.getElementById(obj.id).value.length; i++) {
            if (iChars.indexOf(document.getElementById(obj.id).value.charAt(i)) != -1) {

                document.getElementById(obj.id).value = "";
                document.getElementById(obj.id).focus();
                WarningBox('Invalid phone number');
                return false;
            }
        }
        if (document.getElementById(obj.id).value.length > 13) {

            document.getElementById(obj.id).value = "";
            document.getElementById(obj.id).focus();
            WarningBox('Invalid phone number');
            return false;
        }
        n = obj.name;
        p1 = obj;
        ValidatePhoneOld();


        if (document.getElementById(obj.id).value.length > 13) {
            WarningBox('Invalid phone number');
            document.getElementById(obj.id).value = "";
            document.getElementById(obj.id).focus();
            return false;
        }
        return true;
    }

}

//call function on onkeyup and onChange 
function ValidateTextAreaMaxLength(vObject, maxLength) {
    var vObj = document.getElementById(vObject);
    var iDifference = maxLength - vObj.value.length;
    if (iDifference < 0) {
        vObj.value = vObj.value.substring(0, maxLength);
    }
}


/****************New added Phone Formate Script Start Here***************************/

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object) {
    phonevalue1 = ParseChar(object.value, zChar);
}

function ParseForNumber2(object) {
    phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object, e) {
    if (e) {
        e = e
    } else {
        e = window.event
    }
    if (e.which) {
        var keycode = e.which
    } else {
        var keycode = e.keyCode
    }

    ParseForNumber1(object)

    if (keycode >= 48) {
        ValidatePhone(object)
    }
}

function backspacerDOWN(object, e) {
    if (e) {
        e = e
    } else {
        e = window.event
    }
    if (e.which) {
        var keycode = e.which
    } else {
        var keycode = e.keyCode
    }
    ParseForNumber2(object)
}

function GetCursorPosition() {

    var t1 = phonevalue1;
    var t2 = phonevalue2;
    var bool = false
    for (i = 0; i < t1.length; i++) {
        if (t1.substring(i, 1) != t2.substring(i, 1)) {
            if (!bool) {
                cursorposition = i
                window.status = cursorposition
                bool = true
            }
        }
    }
}

function ValidatePhone(object) {

    var p = phonevalue1

    p = p.replace(/[^\d]*/gi, "")

    if (p.length < 3) {
        object.value = p
    } else if (p.length == 3) {
        pp = p;
        d4 = p.indexOf('(')
        d5 = p.indexOf(')')
        if (d4 == -1) {
            pp = "(" + pp;
        }
        if (d5 == -1) {
            pp = pp + ")";
        }
        object.value = pp;
    } else if (p.length > 3 && p.length < 7) {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ") "

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        object.value = pp;

    } else if (p.length >= 7) {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ") "

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        l40 = pp.length;
        p40 = pp.substring(0, 9);
        p40 = p40 + "-"

        p41 = pp.substring(9, l40);
        ppp = p40 + p41;

        object.value = ppp.substring(0, maxphonelength);
    }

    GetCursorPosition()

    if (cursorposition >= 0) {
        if (cursorposition == 0) {
            cursorposition = 2
        } else if (cursorposition <= 2) {
            cursorposition = cursorposition + 1
        } else if (cursorposition <= 4) {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 5) {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 6) {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 7) {
            cursorposition = cursorposition + 4
        } else if (cursorposition == 8) {
            cursorposition = cursorposition + 4
            e1 = object.value.indexOf(')')
            e2 = object.value.indexOf('-')
            if (e1 > -1 && e2 > -1) {
                if (e2 - e1 == 4) {
                    cursorposition = cursorposition - 1
                }
            }
        } else if (cursorposition == 9) {
            cursorposition = cursorposition + 4
        } else if (cursorposition < 11) {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 11) {
            cursorposition = cursorposition + 1
        } else if (cursorposition == 12) {
            cursorposition = cursorposition + 1
        } else if (cursorposition >= 13) {
            cursorposition = cursorposition
        }

        var txtRange = object.createTextRange();
        txtRange.moveStart("character", cursorposition);
        txtRange.moveEnd("character", cursorposition - object.value.length);
        txtRange.select();

    }

}

function ParseChar(sStr, sChar) {

    if (sChar.length == null) {
        zChar = new Array(sChar);
    }
    else zChar = sChar;

    for (i = 0; i < zChar.length; i++) {
        sNewStr = "";

        var iStart = 0;
        var iEnd = sStr.indexOf(sChar[i]);

        while (iEnd != -1) {
            sNewStr += sStr.substring(iStart, iEnd);
            iStart = iEnd + 1;
            iEnd = sStr.indexOf(sChar[i], iStart);
        }
        sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

        sStr = sNewStr;
    }

    return sNewStr;
}
/****************New added Phone Formate Script End Here***************************/
/**Check Date Ranage***/

function CheckDateRange(strDateFromId, strDateToId) {

    var strDateFrom = document.getElementById(strDateFromId).value;
    var strDateTo = document.getElementById(strDateToId).value;

    var dateTimeFrom = new Date(strDateFrom);
    var dateTimeTo = new Date(strDateTo);

    if (dateTimeFrom > dateTimeTo) {

        document.getElementById(strDateFromId).focus();
        WarningBox("Invalid Date Range");
        return false;
    }
    return true;

}
function CompareWithPreviousDate(astrDateControlId) {
    var currentDate = new Date();
    var date = currentDate.getDate();
    var month = currentDate.getMonth();
    var yy = currentDate.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;
    var date1 = new Date(year, month, date);

    //new code
    if (astrDateControlId.length > 0) {
        for (var i = 0; i < astrDateControlId.length; i++) {
            if (trim(document.getElementById(astrDateControlId[i]).value) != "") {
                var Controldate = document.getElementById(astrDateControlId[i]).value;
                var arrDate = Controldate.split('/');
                Controldate = new Date();
                
                var Cdate = Controldate.getDate();
                var Cmonth = Controldate.getMonth();
                var Cyy = Controldate.getYear();
                var Cyear = (yy < 1000) ? yy + 1900 : yy;
                var date2 = new Date(Cyear, Cmonth, Cdate);

                Controldate.setFullYear(arrDate[2], arrDate[0] - 1, arrDate[1]);
                if (date2 < date1) {
                    WarningBox('Date cannot be less than current date!');
                    if (document.getElementById(astrDateControlId[i])) {
                        try { document.getElementById(astrDateControlId[i]).focus(); }
                        catch (e) { return false; }
                    }
                    return false;
                }

            }
        }
        return true;
    }
    //end new code
}


function CompareWithCurrentDate(astrDateControlId) {
    var currentDate = new Date();
    var date = currentDate.getDate();
    var month = currentDate.getMonth();
    var yy = currentDate.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;
    var date1 = new Date(year, month, date);
    
    if (astrDateControlId.length > 0) {
        for (var i = 0; i < astrDateControlId.length; i++) {
            if (trim(document.getElementById(astrDateControlId[i]).value) != "") {
                var Controldate = document.getElementById(astrDateControlId[i]).value;
                var arrDate = Controldate.split('/');
                Controldate = new Date();

                var Cdate = Controldate.getDate();
                var Cmonth = Controldate.getMonth();
                var Cyy = Controldate.getYear();
                var Cyear = (yy < 1000) ? yy + 1900 : yy;
                var date2 = new Date(Cyear, Cmonth, Cdate);
                
                Controldate.setFullYear(arrDate[2], arrDate[0] - 1, arrDate[1]);
                if (date2 > date1) {
                    WarningBox('Date cannot be greater than current date!');
                    if (document.getElementById(astrDateControlId[i])) {
                        try { document.getElementById(astrDateControlId[i]).focus(); }
                        catch (e) { return false; }
                    }
                    return false;
                }

            }
        }
        return true;
    }
}
/**Check Date Ranage end***/

function DivToOpen(divOpenId) {

    if (document.getElementById(divOpenId) != null) {
        document.getElementById(divOpenId).style.display = 'block';
    }
}
function DivToClose(divCloseId) {
    if (document.getElementById(divCloseId) != null) {
        document.getElementById(divCloseId).style.display = 'none';
    }
}
