// Key Code Constants
var vbKeyInsert = 45;
var vbKey_HO = 79;
var vbKey_LO = 111;
var vbKeyF3 = 114;
var vbKeyF11 = 122;
var vbKeyF12 = 123;
var vbKeyB = 66;
var vbKeyE = 69;
var vbKeyF = 70;
var vbKeyH = 72;
var vbKeyI = 73;
var vbKeyL = 76;
var vbKeyN = 78;
var vbKeyO = 79;
var vbKeyP = 80;
var vbKeyR = 82;
var vbKeyV = 86;
var vbKeyW = 87;
// Message Box Button Constants
var jsOKOnly = 0				//Display OK button only. 
var jsOKCancel = 1				//Display OK and Cancel buttons. 
var jsAbortRetryIgnore = 2		//Display Abort, Retry, and Ignore buttons. 
var jsYesNoCancel = 3			//Display Yes, No, and Cancel buttons. 
var jsYesNo = 4					//Display Yes and No buttons. 
var jsRetryCancel = 5			//Display Retry and Cancel buttons. 
// Message Box Icon Constants	
var jsCritical = 16				//Display Critical Message icon. 
var jsQuestion = 32				//Display Warning Query icon. 
var jsExclamation = 48			//Display Warning Message icon. 
var jsInformation = 64			//Display Information Message icon. 
// UserAction Constants
var jsOK = 1					//OK button was clicked. 
var jsCancel = 2				//Cancel button was clicked. 
var jsAbort = 3					//Abort button was clicked. 
var jsRetry = 4					//Retry button was clicked. 
var jsIgnore = 5				//Ignore button was clicked. 
var jsYes = 6					//Yes button was clicked. 
var jsNo = 7					//No button was clicked. 

function openform(formname) {
    var WinSettings = "center:yes;resizable:no;dialogHeight:300px";
    // ALTER BELOW LINE - supply correct URL for Child Form
    var MyArgs = window.showModalDialog(formname, MyArgs, WinSettings);
}

function showprint() {
    window.print();
}

function maskNumeric(keyCode)
//--------------------------------------------------------------------------------
// Purpose
//		Check the key code of the key that was pressed and determine if it is a
//		valid numeric key for the current field fields.
//
// INPUT PARAMS:
//		None
//
// RETURNS:
//		None
//--------------------------------------------------------------------------------
{
    if (!((keyCode >= 48) && (keyCode <= 57))) {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}


//--------------------------------------------------------------------------------
// KeyHandler
//
// Purpose:
//		
//
// Inputs:
//		None
//
// Outputs:
//		None
//
// Returns:
//		None
//--------------------------------------------------------------------------------
function KeyHandler(blnCancel) {

    var vbKeyBack = 8;
    var vbF5 = 116;
    var vbKeyRButton = 2;
    var osrcelement = event.srcElement;
    var strSrcElement = osrcelement.name;
    var intSrcElementLen = 0;
    var blnSrcElementisDefined = true;
    var blnBKSpaceOK = false;
    var blnCtlKey = false;



    // Check to see if the object is valid
    if (strSrcElement != null) {
        intSrcElementLen = strSrcElement.length;
    }
    else {
        //return;
    }

    // Evaluate source element name length
    if (intSrcElementLen == 0) {
        blnSrcElementisDefined = false
    }
    // If we have a valid source element and it has a name
    // determine if it is a text object so we can allow the backspace
    if (blnSrcElementisDefined == true) {
        strSrcElement = strSrcElement.substr(0, 3)
        if ((strSrcElement.toLowerCase() == 'txt' || strSrcElement.toLowerCase() == 'mem') && event.keyCode == vbKeyBack) {
            blnBKSpaceOK = true;
        }
    }

    if (event.ctrlKey == true && (event.keyCode == vbKey_HO || event.keyCode == vbKey_LO || event.keyCode == vbKeyN || event.keyCode == vbKeyB || event.keyCode == vbKeyE || event.keyCode == vbKeyF || event.keyCode == vbKeyH || event.keyCode == vbKeyI || event.keyCode == vbKeyL || event.keyCode == vbKeyO || event.keyCode == vbKeyR || event.keyCode == vbKeyW)) {
        blnCtlKey = true;
        blnCancel = true;
        event.keyCode = 0;
        event.returnValue = false;
    }


    //Is it the Sshow session Var Combination Ctrl+Shift+F12	
    if (event.ctrlKey == true && event.shiftKey && event.keyCode == vbKeyF12) {
        blnCtlKey = true;
        blnCancel = true;
        event.keyCode = 0;
        event.returnValue = false;
        window.open("../admin/frmAdminedit.aspx")
    }

    //is it the F11 key (Full Screen or Search)
//    if (event.keyCode == vbKeyF11 || event.keyCode == vbKeyF3) {
//        event.keyCode = 0;
//        event.returnValue = false;
//    }
    //  if the F5 key was pressed then bypass it not allowing the user
    // to refresh the page
//    if ((blnCancel == true) && (event.keyCode == vbF5)) {
//        event.keyCode = 0;
//        event.returnValue = false;
//    }
//    else
//    // Well it was not the F5 key so if it was the Backspace key
//    // pressed and we are not on a text field disable it
//        if (event.keyCode == vbKeyBack && blnBKSpaceOK != true) {
//        event.keyCode = 0;
//        event.returnValue = false;
//    }

    // if the right mouse button was pressed then cancel it
    // since we will use dynamic menus
    if (event.keyCode == 0 && event.button == vbKeyRButton) {
        if (blnCancel == true) {
            alert("This function has been disabled in this application.\n If you wish to print press the Ctl+P key combination")
            event.button = 0;
            event.returnValue = false;
        }
    }

    return
    //Call the Malicious Script Handler here
    blnElementOkForCheck = false;
    if ((strSrcElement.toLowerCase() == 'txt' || strSrcElement.toLowerCase() == 'mem')) {
        blnElementOkForCheck = true;
        if (blnBKSpaceOK != true) {
            if (cmnJS_IsTextMalicious("") == true) {
                //Call alert function here
                cmnJS_AlertMalicious(false, true);
                //	retuen to caller
                event.keyCode = 0;
                event.returnValue = false;
            }
        }
    }

}

function cmnJS_AlertMalicious(blnPaste, blnShowAlert) {
    var alertMsg = "";
    //Create message based on type of text box
    if (blnPaste == true) {
        alertMsg = "Warning !\n The text you are about to paste contains potentionally harmful scripts. \n Therefore this operation will not be allowed.";
    }
    else {
        alertMsg = "Warning !\n The text you are typing in contains potentionally harmful scripts. \n Therefore this operation will not be allowed.";
    }

    // Show Alert if caller insists
    if (blnShowAlert == true) {
        alert(alertMsg);
    }

}


function cmnJS_IsTextMalicious(strString) {
    // Malicious declares
    var kStrScript = "<SCRIPT"
    var kStrScriptPre = "<"
    var kStrLangJava = "JAVASCRIPT"
    var kStrLangVB = "VBSCRIPT"
    var kStrRunAtSvr = "RUNAT=SERVER"
    var kStrLang = "LANGUAGE="
    var kStrScriptEnd = "</SCRIPT>"
    var kstrMaliciousString = "[[XXXXXX]]"
    var blnElementOkForCheck = false

    if (blnElementOkForCheck != true) {
        return false;
    }
    // ** Declares and default values **
    var strSource = ""
    //Get the text to validate
    if (strString == "") {
        strSource = event.srcElement.value;
    }
    else {
        strSource = strString.valueOf();
    }


    //Set the default return value
    var blnIsMalicious = true;
    var strTmp = "";
    var strTmp1 = "";


    //Translate the source text for validation
    strTmp1 = strSource.toUpperCase();
    strTmp1.replace(" ", "");
    strTmp1.replace('"', "''");

    //Search string for start 0r stop script symbols
    if ((strTmp1.search(kStrScript.valueOf()) >= 0) || (strTmp1.search(kStrScriptEnd.valueOf()) >= 0)) {
        strTmp = kstrMaliciousString.valueOf();
    }

    // Search string for language declarations
    if (((strTmp1.search(kStrLang.valueOf()) >= 0) || (strTmp1.search(kStrLangJava.valueOf()) >= 0 || strTmp1.search(kStrLangVB.valueOf()) >= 0)) && (strTmp != kstrMaliciousString.valueOf())) {
        strTmp = kstrMaliciousString.valueOf();
    }

    //Search string for Server side Declare
    if (strTmp1.search(kStrRunAtSvr.valueOf()) >= 0) {
        strTmp = kstrMaliciousString.valueOf();
    }

    if (strTmp == kstrMaliciousString.valueOf()) {
        blnIsMalicious = true;
    }
    else {
        blnIsMalicious = false;
    }

    return blnIsMalicious;
}

function GoBack() {
    window.history.back();
}

function CloseOpener() {
    var oParent = window.opener;
    oParent.close();
    //window.parent.close();
}




//----------------------------------------------------------------------
// cmnJS_LimitMemoKeyPress
//
// Purpose:
//		This function limits the number of characters that can be keyed
//		into a TextArea HTML element.
//
// Inputs:
//		lngLimit: Number of characters to limit the text area to
//
// Outputs:
//		None
//
// Returns:
//		None
//
// Developer Note:
//		This routine assumes that the element associated with the event
//		object is a TextArea HTML element.
//
//		This routine is intended to be used with the onkeypress event
//		because the onkeypress event only fires for the alpha-numeric
//		and symbol character keys. This allows the navigation and other
//		keys like Delete and Backspace to be processed.
//----------------------------------------------------------------------
function cmnJS_LimitMemoKeyPress(lngLimit) {
    // Add 1 for the character that is being typed
    event.returnValue = ((event.srcElement.value.length + 1) <= lngLimit);
}

//----------------------------------------------------------------------
// cmnJS_LimitMemoPaste
//
// Purpose:
//		This function limits the number of characters that can be pasted
//		into a TextArea HTML element.
//
// Inputs:
//		lngLimit: Number of characters to limit the text area to
//
// Outputs:
//		None
//
// Returns:
//		None
//
// Developer Note:
//		This routine assumes that the element associated with the event
//		object is a TextArea HTML element.
//
//		This routine is intended to be used with the onkeydown event
//		and will trap only paste operations (ctrl-V or Shift-Insert). 
//----------------------------------------------------------------------
function cmnJS_LimitMemoPaste(lngLimit) {
    var strMemoData;
    var lngMemoLength;
    var strPasteData;
    var lngPasteLength;
    var blnAllowEvent = true;

    if ((event.ctrlKey && event.keyCode == vbKeyV) || (event.shiftKey && event.keyCode == vbKeyInsert)) {
        // Determine the length of the memo data and the clipboard data
        strMemoData = event.srcElement.value
        lngMemoLength = event.srcElement.value.length;
        strPasteData = clipboardData.getData("Text");
        lngPasteLength = strPasteData.length

        //Call the Malicious Script Handler here
        blnElementOkForCheck = true;
        if (cmnJS_IsTextMalicious(strPasteData) == true) {
            //Call alert function here
            cmnJS_AlertMalicious(true, true);
            //	retuen to caller
            event.keyCode = 0;
            event.returnValue = false;
            blnAllowEvent = false;
            event.returnValue = blnAllowEvent;
            return
        }

        // If all the data in the clipboard will not fit in the text area
        // paste as much as possible.
        if ((lngMemoLength + lngPasteLength) > lngLimit) {
            // Paste some of the text from the clipboard
            event.srcElement.value = strMemoData + strPasteData.slice(0, (lngLimit - lngMemoLength));

            // Since we have done the paste here throw the event away
            blnAllowEvent = false;
        }
    }

    event.returnValue = blnAllowEvent;
}