﻿// JScript File

function SetPage() {
	if (document.forms[0].selBillIndex.selectedIndex != 0 && document.forms[0].txtLineItem.value.length > 0) {
            alert ('Please select a Bill Index or Line Item not both.');
            document.forms[0].selBillIndex.focus();
            return false;
    }
    else if (document.forms[0].txtLineItem.value == "*****") {
        alert ('***** is not allowed in the Line Item search.');
        document.forms[0].txtLineItem.focus();
        return false;
    }
    else {
        document.forms[0].hidAction.value = "S";    
        frmMain.submit(); 
    }    
}

function btnFirst_Click() {
    if (eval("document.forms[0].hidJS") !=  undefined) {
        document.forms[0].hidJS.value = "T";
        if (document.forms[0].hidOption.value == "S") {
            if (document.forms[0].hidSearchText.value == document.forms[0].txtSearch.value) {
                document.forms[0].hidAction.value = "B";
                document.forms[0].hidDirection.value = "F";
            }
            else {
                document.forms[0].hidDirection.value = "";
            }
            ValidateSearch()
        }
        else {    
           if (document.forms[0].hidPage.value == "0001") {
                return false;
           }
           else {
                document.forms[0].hidAction.value = "F"
           }
        }
    }
    else {
        return false;
    }
}

function btnPrev_Click() {
    if (eval("document.forms[0].hidJS") !=  undefined) {
        document.forms[0].hidJS.value = "T";
        if (document.forms[0].hidOption.value == "S") {
            if (document.forms[0].hidSearchText.value == document.forms[0].txtSearch.value) {
                document.forms[0].hidAction.value = "B";
                document.forms[0].hidDirection.value = "P";
            }
            else {
                document.forms[0].hidDirection.value = "";
            }
            ValidateSearch()
        }
        else {   
           if (document.forms[0].hidPage.value == "0001") {
                return false;
           }
           else {
                document.forms[0].hidAction.value = "P"
           }
        }
    }
    else {
        return false;
    }
}

function btnNext_Click() {
    if (eval("document.forms[0].hidJS") !=  undefined) {
        document.forms[0].hidJS.value = "T";
        if (document.forms[0].hidOption.value == "S") {
            if (document.forms[0].hidSearchText.value == document.forms[0].txtSearch.value) {
                document.forms[0].hidAction.value = "B";
                document.forms[0].hidDirection.value = "N";
            }
            else {
                document.forms[0].hidDirection.value = "";
            }
            ValidateSearch()
        }
        else {   
           if (document.forms[0].hidPage.value == document.forms[0].hidLastPage.value) {
                return false;
           }
           else {
                document.forms[0].hidAction.value = "N"
           }
        }
    }
    else {
        return false;
    }
}

function btnLast_Click() {
    if (eval("document.forms[0].hidJS") !=  undefined) {
        document.forms[0].hidJS.value = "T";
        if (document.forms[0].hidOption.value == "S") {
            if (document.forms[0].hidSearchText.value == document.forms[0].txtSearch.value) {
                document.forms[0].hidAction.value = "B";
                document.forms[0].hidDirection.value = "L";
            }
            else {
                document.forms[0].hidDirection.value = "";
            }
            ValidateSearch()
        }
        else {   
           if (document.forms[0].hidPage.value == document.forms[0].hidLastPage.value) {
                return false;
           }
           else {
                document.forms[0].hidAction.value = "L"
           }
        }
    }
    else {
        return false;
    }
}

function GoToSalaryRate(pstrPage, pstrPageNo, pstrBE) {
	location.href = pstrPage + "?FY=" + document.forms[0].hidFY.value + "&PG=" + pstrPageNo + "&BE=" + pstrBE;

}

function ValidateSearch() {
    if (eval("document.frmMain.btnSearch") !=  undefined) {
        if (document.forms[0].hidAction.value != "Y" && document.forms[0].hidAction.value != "R") {
            var pstrSearchText;
            pstrSearchText = document.forms[0].txtSearch.value;
            pstrSearchText = pstrSearchText.replace(/^\s+/, '');
	        for (var i = pstrSearchText.length - 1; i >= 0; i--) {
		        if (/\S/.test(pstrSearchText.charAt(i))) {
			        pstrSearchText = pstrSearchText.substring(0, i + 1);
			        break;
		        }
	        }
            if (pstrSearchText.length < 3) {
                document.forms[0].hidSearchText.value = "";
                alert ("Search text must be at least 3 characters");
                document.forms[0].txtSearch.focus();
                document.all.btnFirst.disabled = true;
                document.all.btnPrevious.disabled = true;
                document.all.btnNext.disabled = true;
                document.all.btnLast.disabled = true;
                document.all.btnFirstB.disabled = true;
                document.all.btnPreviousB.disabled = true;
                document.all.btnNextB.disabled = true;
                document.all.btnLastB.disabled = true;
                document.all.btnFirst.src = "Images/FirstG.gif";
                document.all.btnPrevious.src = "Images/PreviousG.gif";
                document.all.btnNext.src = "Images/NextG.gif";
                document.all.btnLast.src = "Images/LastG.gif";
                document.all.btnFirstB.src = "Images/FirstG.gif";
                document.all.btnPreviousB.src = "Images/PreviousG.gif";
                document.all.btnNextB.src = "Images/NextG.gif";
                document.all.btnLastB.src = "Images/LastG.gif";
                return false;
            } 
            else {
            	//Valid length of search  
				//Check for  Special Characters
				var result2 = ValidateSearchText();
				if (result2) {
					//clear hidden value
					document.forms[0].hidSearchText.value = "";
					document.forms[0].txtSearch.focus();
					alert("Invalid character ~ % & * ( ) - = { } | [ ] < > , ? _ % or ; entered in search.  Please remove the character and try again.");
					document.all.btnFirst.disabled = true;
                    document.all.btnPrevious.disabled = true;
                    document.all.btnNext.disabled = true;
                    document.all.btnLast.disabled = true;
                    document.all.btnFirstB.disabled = true;
                    document.all.btnPreviousB.disabled = true;
                    document.all.btnNextB.disabled = true;
                    document.all.btnLastB.disabled = true;
                    document.all.btnFirst.src = "Images/FirstG.gif";
                    document.all.btnPrevious.src = "Images/PreviousG.gif";
                    document.all.btnNext.src = "Images/NextG.gif";
                    document.all.btnLast.src = "Images/LastG.gif";
                    document.all.btnFirstB.src = "Images/FirstG.gif";
                    document.all.btnPreviousB.src = "Images/PreviousG.gif";
                    document.all.btnNextB.src = "Images/NextG.gif";
                    document.all.btnLastB.src = "Images/LastG.gif";
					return false;
				}
				else {
					//Valid search, submit request
					document.forms[0].hidSearchText.value = pstrSearchText;
					document.forms[0].txtSearch.value = pstrSearchText;
                    if (document.forms[0].hidAction.value != "B") {
                        document.forms[0].hidDirection.value = "";
                    }
                    document.forms[0].hidAction.value = "B"
					return true;
				}
			}
        }
    }
}

function Reset() {
    document.forms[0].hidJS.value = "T";
    document.forms[0].txtSearch.value = " ";
    document.forms[0].hidPage.value = "0001";
}

//    function window.onbeforeprint() { >> only works for IE
	function window_onbeforeprint() {
		var hidValue;
		hidValue = document.getElementById("hidBrowser");
		if (hidValue.value.substr(0,1) == "I") {
			//Make sure the browser is IE
			//Before printing document, hide Menu and other objects
			divScrollable.className = "scrollareaPrint";
			//divLogo.style.display = "none";
			divFixedMenu.style.display = "none";
			document.forms[0].btnPrint.style.display = "none";
			tblOptions.style.display = "none";
			trPaging.style.display = "none";
			tblBC.style.display = "none";
			tblBottomPaging.style.display = "none";
		}
    }

//    function window.onafterprint() { >> only works for IE
    function window_onafterprint() {
		var hidValue;
		hidValue = document.getElementById("hidBrowser");
		if (hidValue.value.substr(0,1) == "I") {
			//Make sure the browser is IE
			//After printing document, redisplay Menu and other objects
			divScrollable.className = "scrollarea";			
			//divLogo.style.display = "";
			divFixedMenu.style.display = "";
			document.forms[0].btnPrint.style.display = "";
			tblOptions.style.display = "";
			trPaging.style.display = "";
			tblBC.style.display = "";
			tblBottomPaging.style.display = "";
		}   
    }
    
   
   function ValidateSearchText() {
        var strSearch = document.forms[0].txtSearch.value;
        var strChar;
        strSearch = strSearch.toString();
        for (var i = 0; i < strSearch.length; i++) {
            strChar = strSearch.charAt(i).charCodeAt(0);
            if (strChar == 37 || strChar == 38 || strChar == 91 || strChar == 93 || strChar == 95 || (strChar > 58 && strChar < 64) || (strChar > 39 && strChar < 46) || (strChar > 122 && strChar < 127))  {
                return true;
            }
        }
   }
