// General VC Javascript
// 9/4/08 Added support for ButtonSproc and ButtonClass

function ConfirmAction(
		formName,
		buttonaction,
		buttonpreaction,
		buttonredirect,
		buttonsproc,
		buttonclass,
		messageAction,
		transientKey,
		thisScreenName,
		thisFormName,
		newWindow,
		buttonMsg) {  //if there is a value here, it will be displayed in popup window with Yes/Cancel button
		
	//transientKey is not used in this function but is required (for now)
	//because the code that generates the call includes the same paramenter set
	//regardless of the function name
	// 9/11/02 Added code to set the transient key 
	var msg; 
	var actioncmd1, actioncmd2, actioncmd3, actioncmd4, actioncmd5;  
	var tkcmd;
	var keepGoing;
	keepGoing=false;
	//alert("1xxx");
	//alert ("formName=" + formName + "  " + "Action=" + buttonaction);
	
	if (formName == "0") {
		//alert("formName=0");
		actioncmd1 = "document.forms[0]" + ".ButtonAction.value = '" + buttonaction + "';";
		actioncmd2 = "document.forms[0]" + ".ButtonPreAction.value = '" + buttonpreaction + "';";
		actioncmd3 = "document.forms[0]" + ".ButtonRedirect.value = '" + buttonredirect + "';";
		actioncmd4 = "document.forms[0]" + ".ButtonSproc.value = '" + buttonsproc + "';";
		actioncmd5 = "document.forms[0]" + ".ButtonClass.value = '" + buttonclass + "';";
	}
	else  {
		//alert("2 buttonaction=" + buttonaction);
		actioncmd1 = "document.forms[0]" + ".ButtonAction.value = '" + buttonaction + "';";
		actioncmd2 = "document.forms[0]" + ".ButtonPreAction.value = '" + buttonpreaction + "';";
		actioncmd3 = "document.forms[0]" + ".ButtonRedirect.value = '" + buttonredirect + "';";
		actioncmd4 = "document.forms[0]" + ".ButtonSproc.value = '" + buttonsproc + "';";
		actioncmd5 = "document.forms[0]" + ".ButtonClass.value = '" + buttonclass + "';";
		//alert("3" + actioncmd1);
		//alert(actioncmd2);
		//alert(actioncmd3);
	}
	tkcmd = "document.forms." + formName + ".ThisTransientKey.value = '" + transientKey + "';";
	try {
		eval(tkcmd);
	}
	catch(exception){}

	tkcmd = "document.forms." + formName + ".TransientKey.value = '" + transientKey + "';";
	try {
		eval(tkcmd);
	}
	catch(exception) {}

	tkcmd = "document.forms." + formName + ".ThisScreenName.value = '" + thisScreenName + "';";
	try {
		eval(tkcmd);
	}
	catch(exception) {}

	tkcmd = "document.forms." + formName + ".ThisFormName.value = '" + thisFormName + "';";
	try {
		eval(tkcmd);
	}
	catch(exception) {}
	/*
	if ((buttonpreaction == "delete") || (buttonaction.indexOf("delete") == 0) || (messageAction == "delete")) {
		msg="Really Delete?";
		if (confirm(msg)) {
			eval(actioncmd1);
			eval(actioncmd2);
			eval(actioncmd3);
			eval("document.forms." + formName + ".target='_self'");
			eval("document.forms." + formName + ".submit();");
		}
	}
	
	
	else { */ //any action except 'delete' will set the ActionField to the 
			// passed in 'action' value, then submit the form
		if (buttonMsg == "")  {
			if (messageAction != "") {
				buttonMsg = messageAction;
			}
			else {
				//the delete button will always have a confirmation message
				if ((buttonpreaction == "delete") || (buttonaction.indexOf("delete") == 0) || (messageAction == "delete")) {
					buttonMsg="Really Delete?";
				}	
			}
		}
		if (buttonMsg == "")  {
			keepGoing=true;
		}
		else {
			if (confirm(buttonMsg))
				keepGoing=true;
		}
		if (keepGoing) {
			eval(actioncmd1);
			//alert("5" + actioncmd1); 
			eval(actioncmd2);
			//alert("6" + actioncmd2); 
			eval(actioncmd3);
			eval(actioncmd4);
			eval(actioncmd5);
			//alert("7 actcmd 3 " + actioncmd3); 
			//having trouble here
			actioncmd1 = "document.forms." + formName + ".target='_self'";
			//alert(document.forms[0].name + "  " + "passed in formname:" + formName);
			//alert("self cmd=" + actioncmd1);
			if (newWindow == "1") {
				//opens a new window
				//alert ("submit to new window");
				eval("document.forms." + formName + ".target='_blank'");
			}
			else {
			//submits to same window
			//alert ("submit to same window");
				eval("document.forms." + formName + ".target='_self'");
			}
			//alert("before submit");
			eval("document.forms." + formName + ".submit();");
		}
	//}
	/*
	if (action == "update") {
			eval(actioncmd);
			eval("document.forms." + formName + ".submit();");
	}
	if (action == "cancel") {
			eval(actioncmd);
			eval("document.forms." + formName + ".submit();");
	}
	*/			
}
function ConfirmActionNewWindow(
		formName,
		buttonaction,
		buttonpreaction,
		buttonredirect,
		buttonsproc,
		buttonclass,
		messageAction,
		transientKey,
		thisScreenName,
		thisFormName,
		newWindow,
		buttonMsg) {

	ConfirmAction(
		formName,
		buttonaction,
		buttonpreaction,
		buttonredirect,
		buttonsproc,
		buttonclass,
		messageAction,
		transientKey,
		thisScreenName,
		thisFormName,
		newWindow,
		buttonMsg); 
}

function ElementExists (formName,elementName) {
	var rtn;
	var i,j;
	rtn=false;
	for(i=0;i<document.forms.length;i++) {
		if (document.forms[i].name == formName) {
			for (j=0;j<document.forms[i].length;j++) {
				if (document.forms[i].elements[j].name == elementName) {
					rtn = true;
				}
			}
		}
	}
	return rtn;
}

function DoNothing(x,x,x) {
	x=x;
}

function popupWin(SubID,fieldLabel,thisField,rows,cols,height,width,editMode) {
	//Create the new window on the fly.  
	var readOnly=" ";
	if (editMode == 0) {
		readOnly = " readonly ";
	}
	fieldValue = thisField.value;
	//SubID="8000";
	//altert("1");
	text =  "<html>\n";
	text += "<script language=javascript src='/app/vc.fw.js'></script>";
	text += "<head>\n<title>" + fieldLabel + "</title><link rel='stylesheet' href='SubCon/Sub" + SubID + "/style.css'></head>\n<body>\n";
	text += "\n";
	text += "<form name='PopUpEditWin'>";
	text += "<b>" + "<font class='listtitle'>" + fieldLabel + "</font></b>";
	text += "<textarea name='localTextArea' title=''  rows='" + rows + "' cols='" + cols + "' wrap='virtual'" + readOnly + ">";
	text += fieldValue;
	text += "</textarea>";
	text += "<table  width='100%'  border='0'  cellpadding='0'  cellspacing='1' ><tr align='center' class='listhead'><td>";
	if (editMode == 1) {
		text += "<input type='button' name='btnAction' title='Copy text back to originating form' value='Save' id='btnAction' onclick='FillParentField(" + "\"" + thisField.name + "\"" + ")'>&nbsp;";
	}
	text += "<input type='button' name='btnAction' title='Discard any changes and close the form' value='Cancel' id='btnAction' onclick='self.close();'>&nbsp;";
	//text += "<input type='button' name='btnAction' title='' value='Printable' id='btnAction' onclick='eval(\"popupWinForPrint(self.document.PopUpEditWin.localTextArea)\")'>";
	//text += "<input type='button' name='btnAction' title='' value='Printable' id='btnAction' onclick='popupWinForPrint(\"" + self.document.PopUpEditWin.localTextArea.value + \" + ")'>";	
	//text += "<input type='button' name='btnAction' title='' value='Print' id='btnAction' onclick='window.print()'>";
	text += "</td></tr></table>";
	text += "</form>";
	text += "\n</body>\n</html>\n";
	//alert("2");
	windowProp(thisField.name,text,height,width);
	//alert("3");
	
}
function FillParentField(thisField) {
	//transfer the updated value back to the parent screen
   var evalCmd;
   evalCmd = "window.opener.document.forms[0].elements." + thisField + ".value = document.PopUpEditWin.localTextArea.value;";
   eval(evalCmd);
   self.close();
	
}
function windowProp(windowName,text,height,width) {
	var winParms;
	//alert(windowName);
	winParms = "width=" + width + ",height=" + height + ",status=yes,resizable=yes,menubar=yes,toolbar=yes";
	newWindow = window.open('',windowName,winParms);
	newWindow.document.write(text);
	newWindow.document.forms.PopUpEditWin.localTextArea.focus();
	//newWindow.focus();
	//center thw window
	//	newWindow.moveTo((newWindow.screen.width-newWindow.document.body.clientWidth)/2,
	//	(newWindow.screen.height-newWindow.document.body.clientHeight)/2);

}

function CloseWindow(formName,action,transientKey){
	window.close()
}

// TODO: evaluate and redo these.  Examples from various internet sites.
function OpenInMinimumWindow(url, target, width, height, resizable)
{
	__OpenWindow(url, target,
		"height="+height +",width="+width +",scrollbars=no,status=no,toolbar=no,menubar=no,location=no,resizable="+resizable, true);
}

function OpenInMinimumScrollingWindow(url, target, width, height, resizable)
{
	__OpenWindow(url, target,
		"height="+height +",width="+width +",scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable="+resizable, true);
}

function OpenInNewWindow(url, target)
{
	OpenInNewSizedWindow(url, target, 620, 490, 100, 30);
}

function CenterSelf()
{
	window.moveTo((window.screen.width-window.document.body.clientWidth)/2,
		(window.screen.height-window.document.body.clientHeight)/2);
}

function GotFocus(e)
{
	if (e != g_focusElement) {
		e.select();
		g_focusElement = e;
	}

	CallBack(e.name, g_focusCallback);
}

function GotFocusNoSelect(e)
{
	if (e != g_focusElement) {
		g_focusElement = e;
	}

	CallBack(e.name, g_focusCallback);
}

function LostFocus(e)
{
	g_focusElement=null;

	CallBack(e.name, g_blurCallback);
}

function SelectChanged(e)
{
	CallBack(e.name, g_selectChangeCallback);
}

function PageLoaded()
{
	for (var i = 0; i < g_loadCallback.length; i++) {
		eval(g_loadCallback[i]);
	}
}

function __WindowOpened(w, url)
{
	var a = GetDocCookie(w.document, "UPMLSESSIONID");
	var c = GetCookie("UPMLSESSIONID");
	var c2 = GetCookie("hostname");
	if (a != c) {
		ClearDocCookie(w.document, "UPMLSESSIONID");
		SetDocCookie(w.document, "UPMLSESSIONID", c);
		SetDocCookie(w.document, "hostname", c2);
	}

	w.navigate(url);
}
function __OpenWindow(url, target, options, now)
{
		afterloadcalled = true;
		var w = window.open("/UpShot/General/blank.html", target, options);
		__WindowOpened(w,url);
}

function __AfterLoad(name)
{
	afterloadcalled = true;
	var w = eval("top.WINDOWFOR_" + name);
	var url = eval("top.URLFOR_" + name);
	__WindowOpened(w, url);
}
function OpenInNewFixedSizeWindow(url, target)
{
	OpenInNewSizedWindow(url, target, '620', '490', '100', '30');
}

function OpenInNewSizedWindow(url, target, width, height, left, top, tool, menu)
{
	if ( !target.length )
		target = "Window" + Math.round(Math.random() * 10000);

	__OpenWindow(url, target,
		"height="+height +",width="+width+",left="+left+",top="+top+",scrollbars=yes,status=yes,toolbar="+tool+",menubar="+menu+","+
		"location=no,resizable=yes", false);
}


/*
function onLoad()
{
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = 'SubCon/Subcon.js';
	headID.appendChild(newScript);
}
window.onload = onLoad;
*/

