// Version: "4.0.0"
// Copyright (c) Kore Technologies LLC .  All rights reserved.
var NOTEBUTTON = 101;
var ATTACHBUTTON = 102;

function makeNamedWindow(url, name, h, w, menu)
{
   
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var dims = "height=" + h + ",width=" + w+ ",top=" +wint+ ",left=" +winl;;
	var options = dims + ",scrollbars=yes,resizable=yes,location=no";
	if (menu)
	  options += ",toolbar=yes,status=yes,menubar=yes";
	else
	  options += ",toolbar=no,status=no,menubar=no";
	var popUrl = new String(url);
	// Add the popup qstring reference
	if (popUrl.indexOf("?")>=0)
		popUrl += "&";
	else
		popUrl += "?";
			
	popUrl += "ksHdrNone=Y";
		
	Win = open(popUrl, name, options, true);
	if (Win!=null) {
		Win.focus();
	}	
	return Win;
}

function makeSingleWindow(hPop, url, name, w, h, menu)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var dims = "height=" + h + ",width=" +w+ ",top=" +wint+ ",left=" +winl;
	var options = dims + ",scrollbars=yes,resizable=yes,location=no";
	if (menu)
	  options += ",toolbar=yes,status=yes,menubar=yes";
	else
	  options += ",toolbar=no,status=no,menubar=no";

var popUrl = new String(url);

	// Add the popup qstring reference
	qSep = popUrl.indexOf("?");
	if (qSep != popUrl.length-1) {
		if (qSep>=0) {
			popUrl += "&";
		} else {
			popUrl += "?";
		}
	}
			
	popUrl += "ksHdrNone=Y";
		
	if (!isOpen(hPop)) {
		try {
			hPop = open(popUrl, "", options, true);
		} catch (e) {
			throw 'Cannot execute browsers open function, this may be due to a popup blocker.';
		}
	}

	if (hPop!=null) {
	    // the following line sets the focus to the open window, i.e. brings it to the front
		hPop.focus();
	}

	return hPop;
}

function isOpen(hPop) {
	try {
		if (hPop!=null) { 
			if (!(hPop.closed || hPop.focus()))  {
				//alert("popup is open");
				return true;
			} else {
				throw "popup was open, but it has been closed";
			}
		} else {
			throw "not open";
		}
	} catch (e) {
		//alert("popup is not open, " + (e instanceof Error?e.description:e));
		hPop = null;
	} 	
	return false;
}


function refreshParent() {
	try {
		window.opener.formAction("REFRESH!"); // don't call form reload if we can help it
	} catch(ex) {
		try {	
			window.opener.location.reload(false);
		} catch(e) {
			;
		}
	}
}

function refreshFrame() {
	try {
		window.opener.top.location.reload(false);
	} catch(e) {
		;
	}
}

function refreshButtonBar(button, count) {
	if (window.opener) {
		window.opener.top.refreshButtonBar(button, count);
	}
}

function openPopUp(url, title, w, h, menu) {
	openPopUpEx(url, title, w, h, menu);
}

function openPopUpEx(url, title, w, h, menu) {
    var popup = null;
    url = url.replace(/#/g, '%23');
    try {
		if (title==null) title = "Popup";
		if (menu==null) menu = false;
		if (title.indexOf(" ")>0) throw "The popup title cannot contain spaces.";
		var evalPop = 'top.'+title+'=makeSingleWindow(top.'+title+',"'+url+'","'+title+'",'+w+','+h+','+menu+');'
		popup = eval(evalPop);
	} catch (e) {
		var errText = "";
		if (typeof e != "object") 
			errText = e 
		else 
			errText = e.description;
		alert("Cannot open popup due to syntax error, "+ errText +"\n" + evalPop);
	}
	return popup;
}



function openAttachment(url)
{
	var popUrl = new String(url);
	popUrl = popUrl.replace("#", "%23");
	if (popUrl.indexOf("ksHdrNone")<0) {
		if (popUrl.indexOf("?")<0) {
			popUrl += "?ksHdrNone=Y";
		} else {
			popUrl += "&ksHdrNone=Y";
		}
	}
	open(popUrl,"","toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=750,height=450");
}


function ksViewReturn(targetVw)
{
	try {
		// Page call back to get control
		var ctrlWindow = window.opener;
		if (ctrlWindow!=null) {
			var dataForm = ctrlWindow.ksGetDataForm();
			if (dataForm!=null) {
				var htmlCtl = dataForm.kspgrestore;
				var tbCtl = dataForm.toolbar_action;
		
				if (htmlCtl==null) {  // callback should have registered
					alert("Form postback cannot find the registered column on the parent window.");
				} else {
					// stuff search value into control
					htmlCtl.value = "Y";
				}
				
				if (tbCtl==null) {  // callback should have registered
					alert("Form postback cannot find the toolbar registered on the parent window.");
				} else {
					// stuff search value into control
					tbCtl.value = "ksfrmrefresh";
				}
				
				if (targetVw!=null) {
					htmlCtl = dataForm.kspgvwreturn;
		
					if (htmlCtl!=null) {  // callback should have registered
						htmlCtl.value=targetVw;
					}
				}				
				dataForm.submit();
			} else {
				alert("No data form found on parent window.");
			}
		} else {
			alert("Parent window not found.");
		}
	} catch(e) {
		alert(e.message);
	}
}

function closeAndNotify()
{
	refreshParent();
	close();
}


