function showError(msg, obj) {
   killEvent();
   if (msg && msg!="") alert(msg);
	if (obj) obj.focus();
	return false;
}

function getElement(elemNameID) {
   if (!elemNameID) return "";
   if (document.all) 
      return document.all(elemNameID);
   else if (document.getElementById)
      return document.getElementById(elemNameID);
   else return null;
}

function doRequestDialog(url, winW, winH) {
//alert(url);
   if (winW==null || winW<=0) winW = 600;
   if (winH==null || winH<=0) winH = 400;
   if (window.showModalDialog) {
      var res = window.showModalDialog("/start?v="+escape(url),window,"status:0;help:0;resizable:yes;scroll:1;dialogWidth:"+winW+"px;dialogHeight:"+winH+"px");
      if (res==null) res = "";
      return res;
   } else if (window.openDialog) {
      alert("before");
      //return openDialog("/start?v="+escape(url),"","modal=1,width="+winW+",height="+winH);
      openDialog("/start?v="+escape(url),"modalDialog","modal=1,width=10,height=10");
      alert("after");
      return "";
   } else return "";
}

function doRequestData(url) {
	var iApplet = document.applets["appletServerRequest"];
   var res = iApplet ? iApplet.requestData(escape(url)) : "";
   if (res==null) res = "";
   return res;
}

function doStaticDialog(url, winW, winH) {
   if (winW==null || winW<=0) winW = 600;
   if (winH==null || winH<=0) winH = 400;
   if (window.showModalDialog) {
      return window.showModalDialog(escape(url),window,"status:0;help:0;resizable:0;scroll:0;dialogWidth:"+winW+"px;dialogHeight:"+winH+"px");
   } else return "";
}

/*
function doRequestNewWindow(url, winW, winH) {
   if (winW==null || winW<=0) winW = window.screen.availWidth*0.8;
   if (winH==null || winH<=0) winH = window.screen.availHeight*0.8;
	var winL = (window.screen.availWidth  - winW)/2;
	var winT = (window.screen.availHeight - winH)/2;
	var wNewWindow = window.open("/inc/blank.htm", "reqNew", "top="+winT+",left="+winL+",height="+winH+",width="+winW+",status=no,toolbar=no,menubar=no,location=no,titlebar=no,scroll=yes,resizable=yes");
	while (!wNewWindow.document.body);
   wNewWindow.document.write("<html><head><title>PersonalFax.net</title></head><frameset framespacing=0 border=0 rows='0,*' frameborder='0'><frame name='header' scrolling='no' noresize target='main' src='/inc/blank.htm'><frame name='main' src='"+url+"' scrolling='auto'></frameset></html>");
   wNewWindow.focus();
   return wNewWindow;
}
*/
function doRequestNewWindow(url, winW, winH) {
   if (winW==null || winW<=0) winW = window.screen.availWidth*0.8;
   if (winH==null || winH<=0) winH = window.screen.availHeight*0.8;
	var winL = (window.screen.availWidth  - winW)/2;
	var winT = (window.screen.availHeight - winH)/2;
	var wNewWindow = window.open(url, "reqNew", "top="+winT+",left="+winL+",height="+winH+",width="+winW+",status=no,toolbar=no,menubar=no,location=no,titlebar=no,scroll=yes,resizable=yes");
	//while (!wNewWindow.document.body);
   //wNewWindow.focus();
   return wNewWindow;
}

function doRequestPrompt(sToken, sCaption, sText, sDefaultValue, sDataType, sContentURL) {
   if (!sToken) sToken = "";
   if (!sCaption) sCaption = "";
   if (!sText) sText = "";
   if (!sDefaultValue) sDefaultValue = "";
   if (!sDataType) sDataType = "";
   return doRequestDialog("prompt,"+sToken+","+sCaption+","+sText+","+sDefaultValue+","+sDataType+","+sContentURL, 450, 150);
}
function doValidateWithServer(valType, valData) {
	return (doRequestData(valType+","+valData)=="");
}

var wWait;
function doOpenWait(bTimerEnabled) {
	var winWidth = 300, winHeight=150;
	var winLeft = (window.screen.availWidth  - winWidth)/2;
	var winTop  = (window.screen.availHeight - winHeight)/2;
	wWait = window.open("/inc/processWait.htm", null, "top="+winTop+",left="+winLeft+",height="+winHeight+",width="+winWidth+",status=no,toolbar=no,menubar=no,location=no,titlebar=no,dependent=yes");
	//while (!wWait.document.body); //This "freezes" fast computers!!!
   //wWait.timerEnabled = bTimerEnabled;
	return wWait;
}
function doCloseWait() {
   if (wWait!=null) {
      if (!wWait.closed) {
         wWait.close();
         if (wWait.closed) wWait=null;
      }
   }
}

function doFindIndexByText(objSelect, textToFind, bPartial) {
	if (objSelect==null) return -1;
   if (objSelect.type!="select-one") return -1;
	for (var i=0; i<objSelect.options.length; i++) {
      if (!bPartial) {
         if (objSelect.options[i].text.toUpperCase() == textToFind.toUpperCase()) return i;
      } else {
         if (objSelect.options[i].text.toUpperCase().indexOf(textToFind.toUpperCase())==0) return i;
      }
	}
	return -1;
}
function doSelectByText(objSelect, textToFind, bPartial) {
	if (objSelect==null) return false;
   if (objSelect.type!="select-one") return false;
   objSelect.selectedIndex = doFindIndexByText(objSelect, textToFind, bPartial);
   return (objSelect.selectedIndex != -1);
}
function doFindIndexByValue(objSelect, valueToFind) {
	if (objSelect==null) return -1;
   if (objSelect.type!="select-one") return -1;
	for (var i=0; i<objSelect.options.length; i++) {
		if (objSelect.options[i].value == valueToFind) return i;
	}
	return -1;
}
function doSelectByValue(objSelect, valueToFind) {
	if (objSelect==null) return false;
   if (objSelect.type!="select-one") return false;
   objSelect.selectedIndex = doFindIndexByValue(objSelect, valueToFind);
   return (objSelect.selectedIndex != -1);
}
function addOption(objSelect, itemText, itemValue, iPos) {
	if (objSelect==null) return;
   if (objSelect.type!="select-one") return;
   var oOption = objSelect.document.createElement("OPTION");
   objSelect.options.add(oOption, iPos);
   oOption.innerText = itemText;
   oOption.value = itemValue;
   return iPos ? Math.min(iPos, objSelect.options.length-1) : objSelect.options.length-1;
}

function killEvent() {
   var e = window.event;
   if (e) {
      e.returnValue  = false;
      e.cancelBubble = true;
   }
}

function doBlockRefresh() {
	var codeR  = "R".charCodeAt(0);
   var codeF5 = 116; //F5 - Refresh
   var codeBS = 8;   //Backspace
   //
   var e = window.event;
   if (e) {
		var key  = e.keyCode;
		var ctrl = e.ctrlKey;
      var obj  = e.srcElement;
		if ((key==codeR && ctrl) || key==codeF5 || (key==codeBS && (!obj || !obj.type))) {
			e.keyCode = 0;
         killEvent();
		}
	}
}
window.onkeydown   = doBlockRefresh;
document.onkeydown = doBlockRefresh;

function doBlockRightClick() {
   var e = window.event;
   if (e) {
      var btn = e.button;
      if (btn==2 || btn==3 || btn==6 || btn==7) killEvent();
	}
}
//window.onmousedown   = doBlockRightClick;
//document.onmousedown = doBlockRightClick;

function doBlockContextMenu() {
   killEvent();
}
//window.oncontextmenu   = doBlockContextMenu;
//document.oncontextmenu = doBlockContextMenu;

function loadApplet(applId, applClass) {
   if (document.applets[applId]!=null) return;
	var appl = document.createElement("APPLET");
	with (appl) {
		codeBase = "/applets";
		code = applClass;
		id = applId;
		with (style) {
			position = "absolute";
			width = 0;
			height = 0;
			top = 0;
			left = 0;
		}
	}
	document.appendChild(appl);
}

function doPreloadApplets() {
   if (!document) return setTimeout("doPreloadApplets()", 200);
   if (!document.body) return setTimeout("doPreloadApplets()", 200);
   loadApplet("appletPasswordEncrypt", "passwordEncrypt.class");
   loadApplet("appletServerRequest", "serverRequest.class");
}
//setTimeout("doPreloadApplets()", 200);

function encryptPassword(orgPassword) {
   var iApplet = document.applets["appletPasswordEncrypt"];
   var res = iApplet ? iApplet.encryptPassword(orgPassword) : "";
   if (res==null) res = "";
   return res;
}

function checkJava() {
	var bJavaEnabled = false;
	try {
		var iApplet1 = document.applets["appletServerRequest"];
		var iApplet2 = document.applets["appletPasswordEncrypt"];
		if (iApplet1 && iApplet2) {
			bJavaEnabled  = true;
			bJavaEnabled &= (iApplet1.echo("a")=="a");
			bJavaEnabled &= (iApplet2.echo("b")=="b");
		}
	} catch (e) {
		bJavaEnabled  = false;
	} finally {
	}
	if (!bJavaEnabled) {
		var msg = "The application requires Java Scripting and Java Applets to be enabled in your browser.\n"+
				  "If you do not have a Java Virtual Machine (VM) installed, please go to http://java.com and download and install it.\n"+
				  "Otherwise, please make sure it is enabled before you proceed with this application.\n\n"+
				  "Would you like to download and install the Java VM now?\n\n"+
				  "Click OK if you would like to go to download and install the Java VM now.\n"+
				  "Click Cancel to return back to the screen.";
		if (confirm(msg)) window.open("http://java.com", "java");
	}
}

function checkPopups() {
   var bPopupsEnabled = false;
	try {
      bPopupsEnabled = doOpenWait();
      doCloseWait();
	} catch (e) {
		bPopupsEnabled  = false;
	} finally {
	}
   if (!bPopupsEnabled) {
      var msg = "The application requires Pop-up windows for communication with the server.\n"+
                "Presently your web browser is configured to block these popup windows.\n\n"+
                "Please re-configure your browser to allow popup windows for this web site.\n"+
                "For help and additional information please refer to the documentation of your Popup blocking software.";
      alert(msg);
   }
}
