// ------------------------------------------------
// This file includes general Javascript utilities for use 
// in Cogent web applications
// Bill Tamashunas
// ------------------------------------------------


// ------------------------------------------------
// Change the browser's cursor to an hourglass.
// NOTE: The most common desire is to have this occur when a 
// page post's back.  To do so, call this function in the body tag for the 
// following events: onbeforeunload (IE), onunload (non-IE browsers).
// Eg: <body onbeforeunload="DoHourglass();" onunload="DoHourglass();">
function DoHourglass() {
	// Comment out the line below to disable the use of the hourglass cursor by the web application:
	document.body.style.cursor = 'wait'
}

function DoCursorDefault() {
	document.body.style.cursor = 'default'
}

function ClickButton(e, buttonid) {
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      
      if (bt) { 
          if (evt.keyCode == 13) { 
               bt.click(); 
               return false; 
          } 
     } 
}



function skm_LockScreen(str) {
    scroll(0, 0);
    var back = document.getElementById('skm_LockBackground');
    var pane = document.getElementById('skm_LockPane');
    var text = document.getElementById('skm_LockPaneText');

    if (back)
        back.className = 'LockBackground';
    if (pane)
        pane.className = 'LockPane';
    if (text)
        text.innerHTML = str;
}




// ------------------------------------------------
function TestAlert(yourMessage) {
	alert(yourMessage)
}

// ------------------------------------------------
function ClearContents(sourceElemName, targetElemName) {
	var sourceElem = document.getElementById(sourceElemName);
	var targetElem = document.getElementById(targetElemName);
	if ((targetElem != null) && (sourceElem != null)) {
		if (sourceElem.value != "") {
			targetElem.value = "";
		}
	}
}


// ------------------------------------------------
function ConfirmBox(hiddenControlName, confirmMessage) {
	var agree = confirm(confirmMessage);
	var confirmElem = document.getElementById(hiddenControlName);
	if (agree) {
		if (confirmElem.value != "") {
			confirmElem.value = "TRUE";
		}
		return true;
	}
	else {
		if (confirmElem.value != "") {
			confirmElem.value = "FALSE";
		}
		return false;
	}
}

// ------------------------------------------------
// Variant of ConfirmBox.  No elem lookup checking.
// ------------------------------------------------
function GetConfirm(controlName, confirmMessage) {
	try {
		var ans = window.confirm(confirmMessage);
		var confirmControl = document.getElementById(controlName);

		if (ans==true) {
			confirmControl.value = "TRUE";
			return true;
		}
		else {
			confirmControl.value = "FALSE";
			return false;
		}
	}
	catch(e) {
	}
}

// ------------------------------------------------
function PopUndecoratedWindow(winName, URL, widthValue, heightValue) {
	/*
	newwindow = window.open(URL, winName, 'width='+widthValue
		+',height='+heightValue
		+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes,left=50,top=10,screenX=100,screenY=100');

	if (window.focus) {
		newwindow.focus()
		}

	return false;
	*/

	child = window.open(URL, winName, 'width='+widthValue
		+',height='+heightValue
		+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes,left=50,top=10,screenX=100,screenY=100');

}


// ------------------------------------------------
function GetFrameDocName(frameName, destControlName) {
	var targetElem = document.getElementById(destControlName);
	if (targetElem != null) {
		targetElem.value = top.main.location.href;
	}
}

// ------------------------------------------------
// Display a modal dialog (to this window) and get return values from dialog when closed
// Based on article: http://www.c-sharpcorner.com/Code/2003/Sept/ModalPopupInASP.NET.asp
// This function can be called for a button's onclick event in an ASP.NET code-behind via Attributes.Add (see below)
// Bill T    (see also ChildInit and ChildDone functions below)
// ------------------------------------------------
function ShowModalChild(childURL, width, height, control1Name, control2Name, control3Name, control4Name) {
	var parm1 = ""
	var parm2 = ""
	var parm3 = ""
	var parm4 = ""

	var control1 = document.getElementById(control1Name);
	if (control1 != null) parm1 = control1.value
	var control2 = document.getElementById(control2Name);
	if (control2 != null) parm2 = control2.value
	var control3 = document.getElementById(control3Name);
	if (control3 != null) parm3 = control3.value
	var control4 = document.getElementById(control4Name);
	if (control4 != null) parm4 = control4.value

	var MyArgs = new Array(parm1, parm2, parm3, parm4);
	var WinSettings = "center:yes;resizable:no;unadorned:yes;scroll:no;dialogHeight:"
					+ height
					+ "px;dialogWidth:"
					+ width + "px"
	var MyArgs = window.showModalDialog(childURL, MyArgs, WinSettings);
	if (MyArgs == null)
	{
		// window.alert("Nothing returned from child.")	 // ### Diag message only
	}
	else
	{
		if (control1 != null) control1.value = MyArgs[0].toString();
		if (control2 != null) control2.value = MyArgs[1].toString();
		if (control3 != null) control3.value = MyArgs[2].toString();
		if (control4 != null) control4.value = MyArgs[3].toString();
	}
}

// ------------------------------------------------
// Init the window (assumed to have been called as a child of another window) 
// and store the arguments passed in from the parent in the named controls.
// This currently is 'hardcoded' to work with up to 4 args/controls.
//
// This function would typically be called in the body onblur event of the child window:
// <BODY onload="ChildInit('txtControl1', 'txtControl2', ...)">
// ------------------------------------------------
function ChildInit(control1Name, control2Name, control3Name, control4Name) {
	var parm1 = ""
	var parm2 = ""
	var parm3 = ""
	var parm4 = ""
	var MyArgs = new Array(parm1, parm2, parm3, parm4);
	MyArgs =  window.dialogArguments;

	var control1 = document.getElementById(control1Name);
	if (control1 != null) control1.value = MyArgs[0].toString();

	var control2 = document.getElementById(control2Name);
	if (control2 != null) control2.value = MyArgs[1].toString();

	var control3 = document.getElementById(control3Name);
	if (control3 != null) control3.value = MyArgs[2].toString();

	var control4 = document.getElementById(control4Name);
	if (control4 != null) control4.value = MyArgs[3].toString();
}


// ------------------------------------------------
// This function is expected to be called when a button click
// is triggering a child window to close.  Here we will retrieve
// the values from named controls and return them to the parent window.
//
// This function would typically be called in the onclick event of a button:
// <BUTTON onclick="Done()" type="button">OK</BUTTON>
// or in the ASP.NET code behind as an Attribute of a [server control] command button:
// m_cmdShowChooser.Attributes.Add("onclick", "javascript:ChildDone('txtControl1', ...)")
// ------------------------------------------------
function ChildDone(control1Name, control2Name, control3Name, control4Name) {
	var parm1 = ""
	var parm2 = ""
	var parm3 = ""
	var parm4 = ""

	var control1 = document.getElementById(control1Name);
	if (control1 != null) parm1 = control1.value
	var control2 = document.getElementById(control2Name);
	if (control2 != null) parm2 = control2.value
	var control3 = document.getElementById(control3Name);
	if (control3 != null) parm3 = control3.value
	var control4 = document.getElementById(control4Name);
	if (control4 != null) parm4 = control4.value

	var MyArgs = new Array(parm1, parm2, parm3, parm4);
	window.returnValue = MyArgs;
	window.close();
}







