var hiddenMagicName = "PJMR";
var hiddenActionName = "ACTION_REQUESTED";
var hiddenActionCategoryName = "ACTION_CATEGORY";
var hiddenClassName = "DXGenClassName";
var hiddenRequestIDName = "PJ_REQ_ID";
var realSubmit;
var magicTargetName = "PJMRRC";
var magicParamName = "PJMRP1";
var hiddenOpHttpId = "opid";
var jsPageLock = false;

function isPageLocked() {
  return jsPageLock;
}
function unLockPage() {
  jsPageLock = false;
}
function lockPage() {
  jsPageLock = true;
}

function loadPage(uri) {
 location = uri;
}

function displayError(message) {
	if (message != null && message.length > 0) {
		alert(message);
	}
}

function performMagic(magicId) {
	return performMagic(magicId, null);
}

function performMagic(magicId, targetWindow, properties) {
	if (isPageLocked()) {
		return false;
	}
	
    getElement(hiddenMagicName).value=magicId;
    if (targetWindow != null) {

	if (properties) {
		// let's ensure the target window is open & has the properties applied
		openWindow ('about:blank', targetWindow, properties);
	}

	  originaltarget = getForm().target;
			getForm().target = targetWindow;
			getForm().submit();
			getForm().target = originaltarget;
	}
	else {
	  getForm().submit();
	}
	getElement(hiddenMagicName).value="";
	return false;
}

function performWindowMagic(sMagicKey, sTarget, sProperties) {
	if (isPageLocked()) {
		return false;
	}
	opened = openWindow('about:blank', sTarget, sProperties);
	return performMagic (sMagicKey, sTarget);
}

function performActionCategory(actionCategoryId, actionId) {
	return performActionCategory(actionCategoryId, actionId, null);
}

function performActionCategory(actionCategoryId, actionId, targetWindow) {
	if (isPageLocked()) {
		return false;
	}
	getElement(hiddenActionCategoryName).value = actionCategoryId;
	return performAction(actionId, targetWindow);
}

function performAction(actionId) {
	return performAction(actionId, null);
}

function performAction(actionId, targetWindow, properties) {

	if (isPageLocked()) {
		return false;
	}
	
	getElement(hiddenMagicName).value="";
	getElement(hiddenActionName).value=actionId;
	
	if (targetWindow != null) {
	
	if (properties) {
		// let's ensure the target window is open & has the properties applied
		openWindow ('about:blank', targetWindow, properties);
	}
	originaltarget = getForm().target;
		getForm().target = targetWindow;
		getForm().submit();
		getForm().target = originaltarget;
	}
	else {
	  getForm().submit();
	}
	getElement(hiddenActionName).value="";
	return false;
}

function performNewWindowAction(actionId, targetWindow, windowWidth, windowHeight) {
	if (isPageLocked()) {
		return false;
	}

	wHandle = window.open ('about:blank', targetWindow, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+ windowWidth + ',height=' + windowHeight);
	if (!wHandle.opener)
    		wHandle.opener = self;
	return performAction (actionId, targetWindow);
}

function performNewWindowMagic(magicId, targetWindow, width, height) {
	if (isPageLocked()) {
		return false;
	}

	openWindowUsingProperties("", targetWindow, width, height, false, false, false, false, false, false, false, false);

	return performMagic (magicId, targetWindow);
}

function doMagic(magicId, targetObjectKey, magicParams) { 
	if (isPageLocked()) {
		return false;
	}

     getElement(magicTargetName).value=targetObjectKey; 
     getElement(magicParamName).value=magicParams; 
     return performMagic(magicId, null);
}

function doMagic(magicId, targetObjectKey, magicParams, targetWindow) { 
	if (isPageLocked()) {
		return false;
	}

     getElement(magicTargetName).value=targetObjectKey; 
     getElement(magicParamName).value=magicParams; 
     return performMagic(magicId, targetWindow); 
}

function getElementById(elementid) {
	return document.getElementById(elementid);
}

function getElement(elementName) {
	return getForm().elements[elementName];
}

function getForm() {
	return document.forms["theForm"];
}

function setRequestID(reqID) {
	getElement(hiddenRequestIDName).value = reqID;
}
	



// code to disable the return key for the submit buttons
function keyPress (e) {
// method altered on 12-5-2003 by JE to allow the return button to kick off searches
// when a parameter input field has focus, and to support mozilla
        var keycode;
        var source;
        // ie
        if (window.event) {
                keycode = window.event.keyCode;
        }
        // netscape
        else if (e) {
                keycode = e.which;
        }
        else {
                return true;
        }


        // keycode for the enter key is 13
        if (keycode == 13) {
                if (window.event || e) {
                        var source;


                        if (window.event) {
                                // ie
                                source = window.event.srcElement;
                        }


                        if (e) {
                                // mozilla
                                source = e.target;
                        }


                        if (source.type && source.type == 'textarea') {
                                // allow enter on text area
                                return true;
                        }
                        // if the source is a text field then find the button corresponding to that
                        // text field and click it (12/05/2003 JE)
                        if (source.name) {
                                underscore = source.name.indexOf ('_');
                                // when the textfield name is something like ?1234567_0 the corresponding button
                                // name is ?1234567
                                if (underscore > 0) {
                                        targetButton = getElement (source.name.substring (0, underscore));


                                        if (targetButton && targetButton.type && targetButton.type == 'button') {
                                                targetButton.click();
                                        }
                                }
                        }
                }
                return false;
        }


        return true ;
}


document.onkeypress = keyPress;


/**
 * Open a new window with the given url, target, and properties.  Below is a list
 * of properties a window may have.  Width and height are ints, and the rest are
 * either 'yes' or 'no'.
 * 
 *  width ------- window width
 *  height        window height
 *  toolbar ----- show toolbar
 *  location      show location bar (address bar)
 *  directories - show directors (bookmarks / links)
 *  status        show status bar
 *  menubar ----- show menu bar
 *  scrollbars    show scroll bars
 *  resizable --- allow resizing
 *  copyhistory   copy history (back & forward - previsouly visited pages
 * 
 * Properties must be in the format <property name>=<yes | no> with a comma in
 * between each property.  (e.g. "width=300,height=300,toolbar=yes,scrollbars=no")
 *
 * @param sUrl         location of the new window
 * @param sTarget      name of the new window the url should load in
 * @param sProperties  properties of the new window (see other
 *                     openWindow method for different properties)
 */
function openWindow(sUrl, sTarget, sProperties) {
  newWin = window.open(sUrl, sTarget, sProperties);
	if (!newWin.opener)
		newWin.opener = self;

	newWin.focus();
	return false;
}

 
/**
 * Gets 'yes' or 'no' depending upon the boolean passed in.
 * 
 * return yes or no
 */
function getYesOrNo(bValue) {
  if (bValue) {
    return "yes";
  }
  else {
    return "no";
  }
}


/**
 * Open a new window with the given url, target, and properties.
 *
 * @param sUrl          location of the new window
 * @param sTarget       name of the new window the url should load in
 * @param iWidth        window width
 * @param iHeight       window height
 * @param bToolbar      show toolbar
 * @param bLocation     show location bar (address bar)
 * @param bDirectories  show directors (bookmarks / links)
 * @param bStatus       show status bar
 * @param bMenubar      show menu bar
 * @param bScrollbars   show scroll bars
 * @param bResizable    allow resizing
 * @param bCopyHistory  copy history (back & forward - previsouly visited pages
 */
function openWindowUsingProperties(sUrl, sTarget, iWidth, iHeight, bToolbar, bLocation, bDirectories, bStatus, bMenubar, bScrollbars, bResizable, bCopyHistory) {

  var sProperties = "";

  // ********** width **********
  if (iWidth != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("width=" + iWidth);
  }

  // ********** height **********
  if (iHeight != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("height=" + iHeight);
  }

  // ********** toolbar **********
  if (bToolbar != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("toolbar=" + getYesOrNo(bToolbar));
  }

  // ********** location **********
  if (bLocation != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("locationr=" + getYesOrNo(bLocation));
  }

  // ********** directories **********
  if (bDirectories != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("directories=" + getYesOrNo(bDirectories));
  }

  // ********** status **********
  if (bStatus != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("status=" + getYesOrNo(bStatus));
  }

  // ********** menubar **********
  if (bMenubar != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("menubar=" + getYesOrNo(bMenubar));
  }

  // ********** scrollbars **********
  if (bScrollbars != null) {
    if (sProperties != "") {
      sProperties = sProperties + ",";
    }

    sProperties += ("scrollbars=" + getYesOrNo(bScrollbars));
  }

  // ********** resizable **********
  if (bResizable != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("resizable=" + getYesOrNo(bResizable));
  }

  // ********** copyhistory **********
  if (bCopyHistory != null) {
    if (sProperties != "") {
      sProperties += ",";
    }

    sProperties += ("copyhistory=" + getYesOrNo(bCopyHistory));
  }

  openWindow(sUrl, sTarget, sProperties);
}

/**
 * Gets users confirmation before carrying out a
 * delete action.
 */
function confirmDelete() {
	return confirmDelete(null, false);
}
function confirmDelete(sMessage) {
	return confirmDelete(sMessage, false);
}
/**
 * Gets users confirmation before carrying out a
 * delete action.  If a message is provided, then that
 * message is used when prompting the user.
 *
 * @param sMessage  message to use when prompting the usser.
 */
function confirmDelete(sMessage, bIsJoin) {
	if (sMessage == null || sMessage == "") {
		if (bIsJoin) {
			sMessage = "This will effect multiple database files.  Are you sure you wish to delete this?";
		}
		else {
			sMessage = "Are you sure you wish to delete this?";
		}
	}
	if (confirm(sMessage)) {
		return true;
	}

	return false;
}

/**
 * Shows the specified exception data in a new window
 */
function showException (data) {
	ewin = window.open ('about:blank', 'exceptionData', 'menubar=no,status=no,width=800,height=800,resizable=yes,scrollbars=yes,menubar=no');
	ewin.document.write('<html><head><title>Exception Data</title></head><body><h3>Exception Stacktrace:</h3><br/>');
	ewin.document.write(data);
	ewin.document.write('</body></html>');
	ewin.document.close();
}

function popUpImage(sImg, iWidth, iHeight) {
	var x,y;
	x=(screen.width - iWidth) / 2;
	y=(screen.height - iHeight) / 2;

	var winProps = "scrollbars=0,toolbar=0,status=0,menubar=0,resizeable=0,location=0,directories=0";
	winProps += ",width=" + iWidth + ",height=" + iHeight;
	
	if (navigator.appName.indexOf("Microsoft")>=0) {
		winProps += "";
	}
	else {
		winProps += ",screenx=" + x + ",screeny=" + y;
	}

	var popUp = window.open(sImg, "Image", winProps);
	popUp.focus();
	
	return false;
}
/**
 * Used to get the next or previous row collection of possible values for a field.
 */
function changePossibleValues(sValue, sNextMagicKey, sPrevMagicKey) {
  if (sValue == '*PJ_NEXT') {
    return performMagic(sNextMagicKey);
  }
  else if (sValue == '*PJ_PREV') {
    return performMagic(sPrevMagicKey);
  }
  return true;
}
function doAction(magicId, magicParams, targetWindow) { 
	if (isPageLocked()) {
		return false;
	}

     getElement(magicParamName).value=magicParams; 
     return performMagic(magicId, targetWindow); 
}
function doAction(magicId, magicParams, targetWindow, windowProperties) { 
	if (isPageLocked()) {
		return false;
	}

	 getElement(magicParamName).value=magicParams; 
     return performMagic(magicId, targetWindow, windowProperties); 
}
function insertTab(event,obj) {
	var tabKeyCode = 9;
	if (event.which) // mozilla
		var keycode = event.which;
	else // ie
		var keycode = event.keyCode;
	if (keycode == tabKeyCode) {
		if (event.type == "keydown") {
			if (obj.setSelectionRange) {
				// mozilla
				var s = obj.selectionStart;
				var e = obj.selectionEnd;
				obj.value = obj.value.substring(0, s) + 
					"\t" + obj.value.substr(e);
				obj.setSelectionRange(s + 1, s + 1);
				obj.focus();
			} else if (obj.createTextRange) {
				// ie
				document.selection.createRange().text="\t"
				obj.onblur = function() { this.focus(); this.onblur = null; };
			} else {
				// unsupported browsers
			}
		}
		if (event.returnValue) // ie ?
			event.returnValue = false;
		if (event.preventDefault) // dom
			event.preventDefault();
		return false; // should work in all browsers
	}
	return true;
}

function checkAll(sName) {
	var fields = document.getElementsByName(sName);
	
	if (fields && fields.length > 0) {
		// loop through elements and check
		for (var i = 0; i < fields.length; i++) {
			fields[i].checked = true;
		}					 
	}
}
					 
function uncheckAll(sName) {
	var fields = document.getElementsByName(sName);
	
	if (fields && fields.length > 0) {
		// loop through elements and check
		for (var i = 0; i < fields.length; i++) {
			fields[i].checked = false;
		}					 
	}
}
function runOp(iOpId) {
	var hiddenE = getElement(hiddenOpHttpId);
	if (hiddenE) {
		hiddenE.value = iOpId;
		getForm().submit();
	}
	return false;
}

function createCookie(sName, sValue, iDays) {
	var expires = "";
	if (iDays) {
		var date = new Date();
		// days * hours * minutes * seconds * milliseconds
		date.setTime(date.getTime()+(iDays*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	
	document.cookie = sName + "=" + sValue + expires + "; path=/";
}
function readCookie(sName) {
	var start = document.cookie.indexOf(sName + "=");
	if (start != -1) {
		start = start + sName.length+1;
		
		var end = document.cookie.indexOf(";", start);
		if (end == -1) {
			end = document.cookie.length;
		}
		
		return unescape(document.cookie.substring(start, end));
    }
    
	return null;
}

function eraseCookie(sName) {
	createCookie(sName, "", -1);
}

function cookiesAllowed() {
	var name = "CookiesAllowedTest";
	
	// first create a cookie
	createCookie(name, "Are cookies allowed?");
	
	// check if cookie exists
	if (readCookie(name)) {
		// clear cookie after test finishes
		eraseCookie(name);
		return true;
	}
	
	return false;
}