﻿// This page contains all the functions of the pages "LRUtil.js" and "LRUtilV2.js"
// This is the combined page of "LRUtil.js" and "LRUtilV2.js"

var STVUtil =
{
    blnCheckAllClicked: true,

    isDropDownSelected: function(drpObj) {
        var retAns = false;

        if (drpObj != null) {
            if (drpObj.options.length > 1) {
                if (drpObj.selectedIndex > 0) {
                    retAns = true;
                }
                else {
                    retAns = false;
                }
            }
            else {
                retAns = true;
            }
        }
        else {
            retAns = true;
        }

        return retAns;
    },

    dropDownSelectedItem: function(drpObj) {
        if (drpObj != null) {
            item.selectedIndex = drpObj.selectedIndex;
            item.value = drpObj.options[drpObj.selectedIndex].value;
            item.text = drpObj.options[drpObj.selectedIndex].text;
        }
        else {
            item.selectedIndex = -1;
            item.value = "";
            item.text = "";
        }

        return item;
    },

    isRadioChecked: function(rdoObj) {
        var retAns = false;

        if (rdoObj != null) {
            if (rdoObj.checked) {
                retAns = true;
            }
            else {
                retAns = false;
            }
        }

        return retAns;
    },

    isRepeaterRadioChecked: function(rptrID) {
        var retAns = false;
        var elms = document.getElementsByTagName("input");

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].name.toLowerCase().indexOf(rptrID) != -1) {
                if (elms[i].checked == true) {
                    retAns = true;
                    break;
                }
            }
        }

        return retAns;
    },

    radioCheckedItem: function(mode, r) {
        var item = new Object;

        item.index = -1;
        item.value = "";
        if (mode == "name") {
            var elms = document.getElementsByName(r);
            for (var i = 0; i < elms.length; i++) {
                if (elms[i].checked == true) {
                    item.index = i;
                    item.value = elms[i].value;
                    break;
                }
            }
        }

        return item;
    },

    isCheckboxSelected: function(chkName) {
        var retAns = false;
        var elms = document.getElementsByName(chkName);

        for (var i = 0; i < elms.length; i++) {
            if (elms[i].checked == true) {
                retAns = true;
                break;
            }
        }

        return retAns;
    },


    //LRUTIL.JS

    //    isChecked: function()
    //    {
    //	    var frm = document.forms[0];
    //	    var intLen = frm.chkCS.length;
    //	    var blnCheck = false;
    //	    if (typeof(intLen) == "undefined")
    //	    {
    //		    if (frm.chkCS.checked)
    //		    {
    //			    blnCheck = true;
    //		    }
    //	    }
    //	    else
    //	    {
    //		    for(i=0; i<intLen; i++)
    //		    {
    //			    if (frm.chkCS[i].checked)
    //			    {
    //				    blnCheck = true;
    //				    break;
    //			    }
    //		    }
    //	    }
    //	    return blnCheck;
    //    },

    //    /*
    //    ********************************************
    //     Purpose :	To check/Uncheck all Check Boxes
    //    ********************************************
    //    */ 

    ChkAll: function() {
        var frm = document.forms[0];
        var blnCheck;
        if (!this.blnCheckAllClicked) {
            blnCheck = true;
        }
        else {
            blnCheck = false;
        }
        var intLen = frm.chkCS.length;
        if (typeof (intLen) == "undefined") {
            frm.chkCS.checked = blnCheck;
        }
        else {
            for (i = 0; i < intLen; i++) {
                frm.chkCS[i].checked = blnCheck;
            }
        }
        if (typeof (frm.chkGrp) != "undefined") {
            intLen = frm.chkGrp.length;
            if (typeof (intLen) == "undefined") {
                frm.chkGrp.checked = blnCheck;
            }
            else {
                for (i = 0; i < intLen; i++) {
                    frm.chkGrp[i].checked = blnCheck;
                }
            }
        }

        this.blnCheckAllClicked = !this.blnCheckAllClicked;
    },

    //    /*
    //    ********************************************
    //     Purpose :	Validates the fields for number
    //    ********************************************
    //    */ 
    //    IsNumber: function(UserValue)
    //    {
    //    var lFlag  = true
    //    if (UserValue.indexOf(",", 0) >= 0 || UserValue.indexOf("-", 0) >= 0)
    //	    lFlag = false
    //    else
    //	    lFlag = true
    //    return 	lFlag
    //    },
    //    
    //    /*
    //    **********************************************************************************
    //     Purpose :	Validates the fields for sapces
    //			    If space is in between the string , returns true
    //			    else returns false
    //    ***********************************************************************************
    //    */
    checkBetweenSpace: function(strValue) {
        var lflag = false;
        for (i = 0; i < strValue.length; i++) {
            if (strValue.charAt(i) == " ") {
                lflag = true;
                break;
            }
        }
        return lflag
    },

    //    /*
    //    **********************************************************************************
    //     Purpose :	Validates the fields for sapces
    //			    If all characters are space , returns false
    //			    else returns true
    //    ***********************************************************************************
    //    */
    checkSpace: function(strValue) {
        var lflag = false;
        for (i = 0; i < strValue.length; i++) {
            if (strValue.charAt(i) != " " && strValue.charAt(i) != "\t") {
                lflag = true;
                break;
            }
        }
        return lflag
    },
    //    
    //    
    //    /*
    //    **********************************************************************************
    //     Purpose :	Removes all spaces in a String
    //    ***********************************************************************************
    //    */
    TrimSpace: function(strText) {
        var intLength = strText.length
        var intStartPos = 0
        var intEndPos = intLength
        var strOut = ""
        for (i = 0; i < intEndPos; i++) {
            if (strText.charAt(i) != " ") {
                strOut = strOut + strText.charAt(i)
            }
        }
        return strOut
    },


    validate_alphanumeric_data: function(urstr) {
        var i;
        var result;

        for (i = 0; i < urstr.length; i++) {
            if (
          ((urstr.charAt(i) >= "A") && (urstr.charAt(i) <= "Z")) ||
          ((urstr.charAt(i) >= "a") && (urstr.charAt(i) <= "z")) ||
          ((urstr.charAt(i) >= "0") && (urstr.charAt(i) <= "9")) ||
          ((urstr.charAt(i) == "-"))
          )
                result = true;
            else
                return false;
        }

        if (result == true)
            return true;
    },

    ReplaceText: function(pContent, pTag, pValue) {
        pContent = pContent.replace(pTag, pValue);
        if (pContent.indexOf(pTag) != -1) {
            pContent = this.ReplaceText(pContent, pTag, pValue);
        }
        return pContent;
    },

    LineBreak: function(pTxt, MaxChars) {
        MaxChars--;
        pTxt = "" + pTxt;
        var strOut = "";
        var chcount = 0;
        for (var i = 0; i < pTxt.length; i++) // each char
        {
            var ch = pTxt.substring(i, i + 1);

            if (ch == '\n') // if hard return
            {
                strOut += ch;
                chcount = 1;
            }
            else {
                //if (chcount == MaxChars) // max now
                if (chcount >= MaxChars) // max now
                {
                    if (ch == ' ') {
                        //strOut += '\n' + ch; // go to next line
                        strOut += '\n'; // go to next line and don't add the space
                        chcount = 1; // reset chcount
                    }
                    else //already hit max and found space.
                    {
                        strOut += ch;
                        chcount++; // so add 1 to chcount				
                    }
                }
                else  // Not a newline or max characters ...
                {
                    strOut += ch;
                    chcount++; // so add 1 to chcount
                }
            }
        }
        return (strOut);
    },

    Trim: function(s) {
        // spaces and carriage returns at beginnig
        while ((s.substring(0, 1) == ' ') || (s.substring(0, 1) == '\n') || (s.substring(0, 1) == '\r')) {
            s = s.substring(1, s.length);
        }

        // spaces and carriage returns at end

        while ((s.substring(s.length - 1, s.length) == ' ') || (s.substring(s.length - 1, s.length) == '\n') || (s.substring(s.length - 1, s.length) == '\r')) {
            s = s.substring(0, s.length - 1);
        }
        return s;
    },


    QSValue: function(pQS, pID) {
        var intPos = pQS.indexOf('?');
        if (intPos >= 0) {
            pQS = pQS.substr(intPos + 1, pQS.length);
        }

        var rootArr = pQS.split("&");
        var strOut = "";
        for (i = 0; i < rootArr.length; i++) {
            var tempArr = rootArr[i].split("=");
            if (tempArr.length == 2) {
                if (tempArr[0] == pID) {
                    strOut = tempArr[1];
                    break;
                }
            }
        }
        return strOut;
    },

    UParam: function(pT) {
        var intIndex = pT.indexOf('?');
        if (intIndex != -1) {
            pT = pT.slice(intIndex + 1);
        }
        //pT = escape(pT);
        return pT;
    },

    //    Pop_Win: function(pURL, pName, pW, pH, pSCR)
    //    {
    //	    var PWin = window.open(pURL,pName,'left=0,top=0,width=' + pW + ',height=' + pH + ',scrollbars=' + pSCR);
    //	    PWin.focus();
    //    },

    DatePick: function(pObj, pRestrict) {
        //var strObj = "frmMain." + pObj;
        var strObj = pObj;
        if (pRestrict == null) { pRestrict = "N"; }
        var objWin = window.open("/calendar.aspx?R=" + pRestrict + "&fill=" + strObj, 'caledar', 'width=220,height=160,left=0,top=0,scrollbars=0,resizable=0,toolbar=0,status=0');
        objWin.focus();
    },

    //    isInteger: function(s)
    //    {
    //	    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"))) return false;
    //        }
    //        // All characters are numbers.
    //        return true;
    //    },

    stripCharsInBag: function(s, bag) {
        var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++) {
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    },

    daysInFebruary: function(year) {
        // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        var strYear = new String(year);
        switch (strYear.length) {
            case 1:
                strYear = "200" + strYear;
                break;
            case 2:
                strYear = "20" + strYear;
                break;
        }
        year = parseInt(strYear);
        return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
    },

    DaysArray: function(n) {
        for (var i = 1; i <= n; i++) {
            this[i] = 31
            if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
            if (i == 2) { this[i] = 29 }
        }
        return this
    },

    isDate: function(dtStr, pMsg, pYRLength) {

        dtStr = this.Trim(dtStr);
        if (pYRLength == null) {
            pYRLength = 2;

        }
        var dtCh = "/";
        var dtCh1 = "-";
        var minYear = 1900;
        var maxYear = 2100;

        var daysInMonth = this.DaysArray(12)
        var pos1 = dtStr.indexOf(dtCh)
        if (pos1 == -1) {
            pos1 = dtStr.indexOf(dtCh1)
            dtCh = dtCh1;
        }
        var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
        var strMonth = dtStr.substring(0, pos1)
        var strDay = dtStr.substring(pos1 + 1, pos2)
        var strYear = dtStr.substring(pos2 + 1)
        strYr = strYear

        if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
        if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)

        strMonth = this.Trim(strMonth);
        strDay = this.Trim(strDay);
        strYr = this.Trim(strYr);

        month = parseInt(strMonth)
        day = parseInt(strDay)
        year = parseInt(strYr)
        if (pMsg != "")
            pMsg = pMsg + "\n\n";
        else
            pMsg = "";

        if (!this.checkSpace(dtStr)) {
            alert(pMsg + "Should not be empty");
            return false;
        }
        if (pos1 == -1 || pos2 == -1) {
            alert(pMsg + "The date format should be : mm-dd-yy");
            return false;
        }
        if (strMonth.length < 1 || month < 1 || month > 12) {
            alert(pMsg + "Please enter a valid month");
            return false;
        }
        if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > this.daysInFebruary(year)) || day > daysInMonth[month]) {
            alert(pMsg + "Please enter a valid day");
            return false;
        }
        if (strYear.length != 2 && strYear.length != 4) {
            alert("Please enter a valid 2 or 4 digit year")
            return false;
        }

        if (pYRLength == 4 && strYear.length != 4) {
            alert("Please enter a 4 digit year");
            return false;
        }

        if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || this.chkNumVal(this.stripCharsInBag(dtStr, dtCh)) == false) {
            alert(pMsg + "Please enter a valid date")
            return false;
        }
        return true;
    },
    
    chkNumVal: function(val)       // Value as parameter
    {
        var validChars = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
        var i = 0;
        var j = 0;
        var retAns = true;

        if (val.length > 0) {
            for (i = 0; i < val.length; i++) {
                retAns = false;
                for (j = 0; j < validChars.length; j++) {
                    if (val.substr(i, 1) == " ") {
                        break;
                    }
                    else {
                        if (val.substr(i, 1) == validChars[j]) {
                            retAns = true;
                            break;
                        }
                    }
                }
                if (!(retAns)) {
                    break;
                }
            }
        }
        if (retAns) {
            return true;
        }
        else {
            return false;
        }
    },
    CDateLR: function(pDate) {
        if (this.checkSpace(pDate)) {
            var reg = /-/g;
            pDate = pDate.replace(reg, "/");
            var dtCh = "/";
            var pos1 = pDate.indexOf(dtCh)
            var pos2 = pDate.indexOf(dtCh, pos1 + 1)
            var strMonth = pDate.substring(0, pos1)
            var strDay = pDate.substring(pos1 + 1, pos2)
            var strYear = pDate.substring(pos2 + 1)
            if (strYear.length == 2)
                strYear = "20" + strYear;

            pDate = strMonth + "/" + strDay + "/" + strYear;
        }
        else
            pDate = ""

        return pDate;
    },

    Go: function(pPage, pFrame) {
        if (pFrame == "WF")
            window.location.href = pPage;
        else
            window.parent.location.href = pPage;
    },

    //    GoBlank: function(pPage)
    //    {
    //	    window.location.href = "/blank.htm";
    //    },

    showPopUp: function(pgWithPath, pgQryStr, popNm, pTop, pLeft, pWidth, pHeight, pScrollBars, pResizable, pMenuBar, pToolbar, pStatus, pLocation, pDirectories) {

        //        Notes:
        //        All the parameters are optional except pgWithPath
        //        top=pixels        Specifies the Y coordinate of the top left corner of the new window. (Not supported in version 3 browsers.)
        //        left=pixels       Specifies the X coordinate of the top left corner of the new window. (Not supported in version 3 browsers.)
        //        width=pixels     Specifies the width of the new window.
        //        height=pixels       Specifies the height of the new window.
        //        scrollbars=0|1      Specifies whether the new window should have scrollbars.
        //        resizable=0|1       Specifies whether the new window is resizable.
        //        menubar=0|1       Specifies whether to display the browser menu bar.
        //        toolbar=0|1       Specifies whether to display the toolbar in the new window.
        //        status=0|1        Specifies whether to display the browser status bar.
        //        location=0|1     Specifies whether to display the address line in the new window.
        //        directories=0|1     Specifies whether to display the Netscape directory buttons.

        var url, features, myPopUp;

        if ((!pgQryStr) || (pgQryStr.length == 0)) {
            url = pgWithPath;
        }
        else {
            url = pgWithPath + "?" + pgQryStr
        }
        if ((!popNm) || (popNm.length == 0)) {
            popNm = "myPopup";
        }
        if ((!pTop) || (pTop.length == 0)) {
            pTop = 0;
        }
        if ((!pLeft) || (pLeft.length == 0)) {
            pLeft = 0;
        }
        if ((!pWidth) || (pWidth.length == 0)) {
            pWidth = 500;
        }
        if ((!pHeight) || (pHeight.length == 0)) {
            pHeight = 500;
        }
        if ((!pScrollBars) || (pScrollBars.length == 0)) {
            pScrollBars = 0;
        }
        if ((!pResizable) || (pResizable.length == 0)) {
            pResizable = 0;
        }
        if ((!pMenuBar) || (pMenuBar.length == 0)) {
            pMenuBar = 0;
        }
        if ((!pToolbar) || (pToolbar.length == 0)) {
            pToolbar = 0;
        }
        if ((!pStatus) || (pStatus.length == 0)) {
            pStatus = 0;
        }
        if ((!pLocation) || (pLocation.length == 0)) {
            pLocation = 0;
        }
        if ((!pDirectories) || (pDirectories.length == 0)) {
            pDirectories = 0;
        }
        features = "'";
        features += "top=" + pTop + ",";
        features += "left=" + pLeft + ",";
        features += "width=" + pWidth + ",";
        features += "height=" + pHeight + ",";
        features += "scrollbars=" + pScrollBars + ",";
        features += "resizable=" + pResizable + ",";
        features += "menubar=" + pMenuBar + ",";
        features += "toolbar=" + pToolbar + ",";
        features += "status=" + pStatus + ",";
        features += "location=" + pLocation + ",";
        features += "directories=" + pDirectories;
        features += "'";
        myPopUp = window.open(url, popNm, features);
        myPopUp.focus();
    },

    midNameYesNo: function(midTxtId, midDivId, optYesId) {
        var midTxt = document.getElementById(midTxtId);
        var midDiv = document.getElementById(midDivId);
        var optYes = document.getElementById(optYesId);
        if (optYes.checked) {
            midDiv.style.display = "inline";
        }
        else {
            midDiv.style.display = "none";
            midTxt.value = "";
        }
    },
    
    AddDropDownListItem: function(text, value, cbo) 
    {
        cbo.options[cbo.options.length] = new Option(text, value, false, false);
    },

    RemoveAllDropDownListItem: function(selectbox) 
    {
        var count;
        for (count = selectbox.options.length-1; count>=0; count--) 
        {
            selectbox.remove(count);
        }
    }
    
};


