// openwnd.js $Revision: 1.26 $ $Date: 2006/04/24 08:55:45 $ $Author: gulpi $

var browserName = navigator.userAgent.toUpperCase();
var isICABIdx = browserName.indexOf("ICAB");
var	isICAB = (isICABIdx > 0);
var	isIE = (browserName.indexOf("MSIE") >= 0);
var	isNS = (browserName.indexOf("MOZILLA") >= 0);
var	isMac = (browserName.indexOf("MAC") >= 0);
var	isWIN = (browserName.indexOf("WIN") >= 0);
var	isSAF = (browserName.indexOf("SAFARI") >= 0);

var workaround_ie_mac_local = false;
var pvt_islocalversion = false;
var openwnd_debug = false;

function openwnd_debug_alert(alert_str) {
	if (openwnd_debug == true) {
		alert(alert_str);
	}
}

function openwnd_init(in_islocalversion) {
	pvt_islocalversion = in_islocalversion;
	workaround_ie_mac_local = pvt_islocalversion && isIE && isMac;

}

function openwnd_getSreenAvailWidth() { return  screen.availWidth-50; }
function openwnd_getSreenAvailHeight() { return  screen.availHeight-50; }

function openwnd_checkWidth(myLeft,myWidth) {
	if (myWidth && myWidth != -1) {
		if ((myLeft + myWidth) > openwnd_getSreenAvailWidth()) {
			myWidth = openwnd_getSreenAvailWidth()-myLeft;
		}
		
		return myWidth;
	}	
}

function openwnd_checkHeight(myTop,myHeight) {
	if (myHeight && myHeight != -1) {
		if ((myTop + myHeight) > openwnd_getSreenAvailHeight()) {
			myHeight = openwnd_getSreenAvailHeight()-myTop;
		}
		
		return myHeight;
	}	
}

function openwnd_makeOpenWindowString(myLeft,myTop,myWidth,myHeight) {
	var s = "";
	s += "screenX=" + myLeft + ",screenY=" + myTop + ",left=" + myLeft + ",top=" + myTop + ",";
	if (myWidth && myWidth != -1) {
		s += "width=" + myWidth + ",";
	}
	
	if (myHeight && myHeight != -1) {
		s += "height=" + myHeight + ",";
	}
	
	s += "scrollbars=yes,resizable=yes,directories=no,hotkeys=no,location=no,status=no,toolbar=no";
	return s;
}

function openwnd_obj (in_left, in_top , in_width, in_height )  {
	this.left = in_left;
	this.top = in_top;
	this.width = in_width;
	this.height = in_height;

	this.width = openwnd_checkWidth(this.left, this.width);
	this.height = openwnd_checkHeight(this.top, this.height);
	this.wnd_open_paramstr = openwnd_makeOpenWindowString(this.left, this.top, this.width, this.height);
	this.wnd_open_srcwnd = window;
	this.tmppath = "files/html/dummy.html";
	this.do_focus = "true";
	this.do_add_wh2query = true;
	this.do_delay = 1000;
	this.newwnd = null;
	this.do_not_close = false;
	this.do_resize = true;
	this.do_add_wh2wnd = false;
	this.do_replace_parent_when_ready = false;
	this.do_replace_parent_origin = false;
	this.do_use_dummy = true;
	this.do_simple = false;
	this.do_external = false;
	if (isSAF == true) this.do_use_dummy = false;
	
	if (isIE && isMac && pvt_islocalversion) {
		this.do_simple = false;
		this.do_use_dummy = true;
	}
	else {
		this.do_simple = false;
 		this.do_use_dummy = false;
 	}
 	
 	if (isIE && isWIN) {
		this.width+=12;
		this.height+=58; //35
		window.status="width=" + this.width + ";height=" + this.height;
 	}
 	else if (isNS && isWIN) {
 		this.width+=8;
  		this.height+=33;
 		window.status="width=" + this.width + ";height=" + this.height;
 	}
 	 
}

var do_with_openwnd_obj = null;
var doTimeout = false;
var doTimeoutCount = 0;

function openwnd_getDoTimeout() {
	return doTimeout;
}

function openwnd_doOpenWindow_Obj_Delayed() {
	var doIt = true;
	var ow_obj = do_with_openwnd_obj;
	if (doTimeout == true && ow_obj && doTimeoutCount>=0) {
	/*	openwnd_debug_alert("check1+" + ow_obj.newwnd);
		openwnd_debug_alert("check2+" + ow_obj.newwnd + ow_obj.newwnd.focus); */
		/*if (ow_obj.newwnd &&
				ow_obj.newwnd.focus  && 
				ow_obj.newwnd.resizeTo &&
				ow_obj.newwnd.document.location) {*/
		if (!ow_obj.newwnd.closed && ow_obj.newwnd &&
				ow_obj.newwnd.focus  && 
				ow_obj.newwnd.document &&
				ow_obj.newwnd.document.location) {
			if (doIt == true) {
				if (ow_obj.do_focus && ow_obj.do_focus=="true") {
					openwnd_debug_alert("focus");				
					ow_obj.newwnd.focus();
				}
					
				if (ow_obj.do_resize && !ow_obj.newwnd.closed ) {
					openwnd_debug_alert("resizeTo");
					ow_obj.newwnd.resizeTo(ow_obj.width,ow_obj.height);
				}
				
				openwnd_debug_alert("do_add_wh2wnd");
				if (ow_obj.do_add_wh2wnd && !ow_obj.newwnd.closed ) {
					ow_obj.newwnd.ow_obj_width = ow_obj.width;
					ow_obj.newwnd.ow_obj_height  = ow_obj.height;
				}
				
				openwnd_debug_alert("replace:" + ow_obj.dstpath);
				if (ow_obj.do_use_dummy == true && !ow_obj.newwnd.closed ) {
					ow_obj.newwnd.document.location.replace(ow_obj.dstpath);
				}
				
				//alert(ow_obj.do_replace_parent_when_ready);
				if (ow_obj.do_replace_parent_when_ready) {
					if (ow_obj.do_replace_parent_when_ready.indexOf("javascript:") != -1) {
						var tmpReplace = ow_obj.do_replace_parent_when_ready.substring(11);
						window.status="drwr: " + tmpReplace;
						eval(tmpReplace);
					}
					else {
						window.status="drwr: " + ow_obj.do_replace_parent_when_ready;
						ow_obj.do_replace_parent_origin.replace(ow_obj.do_replace_parent_when_ready);
					}
				}
			}	
		}
		else {
			if (!ow_obj.newwnd.closed) {
				openwnd_debug_alert("window.setTimeout(\"openwnd_doOpenWindow_Obj_Delayed()\"," + ow_obj.do_delay + ");");
				doIt = false;
				doTimeoutCount--;
				window.setTimeout("openwnd_doOpenWindow_Obj_Delayed()",ow_obj.do_delay);
			}
		}
	}
	else {
		if (!ow_obj.newwnd.closed) {
			//alert("UNEXPECTED ERROR: doTimeout= " + doTimeout + ";do_with_openwnd_obj= " +  do_with_openwnd_obj);
			alert("To use CASUS, you have to enable Pop Ups in your browser settings!!!!");
		}
	}
	
	if (doIt == true) {
		do_with_openwnd_obj = null;
		doTimeout = false;
	}
}

function openwnd_doOpenWindow_Obj(in_openwnd_obj) {	
	if (doTimeout == true) {
		openwnd_debug_alert("doTimeout= " + doTimeout + ";do_with_openwnd_obj= " +  do_with_openwnd_obj + ";doTimeoutCount=" + doTimeoutCount);
		return;
	}
	
	
	doTimeout = true;
	var ow_obj = in_openwnd_obj;
	if (ow_obj.dstbase && ow_obj.dstpath.indexOf("http://") != 0 && pvt_islocalversion==false) {
			delimeter = "/";
			dstbase_len = ow_obj.dstbase.length;
			lastSlash = ow_obj.dstbase.lastIndexOf("/");
			if (lastSlash == (dstbase_len - 1)) delimeter = "";
			ow_obj.dstpath = ow_obj.dstbase + delimeter + ow_obj.dstpath;
	}
	
	if (ow_obj.do_use_dummy == true) {
		//alert("ow_obj.wnd_open_srcwnd");
		ow_obj.newwnd = ow_obj.wnd_open_srcwnd.open(ow_obj.tmpbase + ow_obj.tmppath, ow_obj.wnd_open_target, ow_obj.wnd_open_paramstr);
	}
	else {
		//alert("ow_obj.wnd_open_srcwnd");
		//alert("ow_obj.wnd_open_srcwnd.open(" + ow_obj.dstpath + ", " + ow_obj.wnd_open_target + ", " + ow_obj.wnd_open_paramstr + ")");
		ow_obj.newwnd = ow_obj.wnd_open_srcwnd.open(ow_obj.dstpath, ow_obj.wnd_open_target, ow_obj.wnd_open_paramstr);
	}
	

	if (!ow_obj.do_add_wh2query || ow_obj.do_add_wh2query == true) {
		var paramaddchar = "&";
		if (ow_obj.dstpath.indexOf("?") == -1) paramaddchar = "?";
		ow_obj.dstpath = ow_obj.dstpath + paramaddchar + "width=" + ow_obj.width + "&height=" + ow_obj.height;
	}
	
	do_with_openwnd_obj = ow_obj;
	if (do_with_openwnd_obj.do_external == false) {
		if (do_with_openwnd_obj.do_simple == false) {
			//openwnd_debug_alert("window.setTimeout(\"openwnd_doOpenWindow_Obj()\"," + do_delay + ");");
			doTimeout = true;
			doTimeoutCount = 10;
			window.setTimeout("openwnd_doOpenWindow_Obj_Delayed()",ow_obj.do_delay);
		}
		else {
			doTimeoutCount = 10;
			doTimeout = true;
			if (ow_obj.do_replace_parent_when_ready) {
				if (ow_obj.do_replace_parent_when_ready.indexOf("javascript:") != -1) {
					var tmpReplace = ow_obj.do_replace_parent_when_ready.substring(11);
					window.status="drwr: " + tmpReplace;
					eval(tmpReplace);
				}
				else {
					window.status="drwr: " + ow_obj.do_replace_parent_when_ready;
					ow_obj.do_replace_parent_origin.replace(ow_obj.do_replace_parent_when_ready);
				}
			}
		}

		var topParentWindow = getTopParentWindow();
		if (!topParentWindow.all_open_windows) {
			topParentWindow.all_open_windows = new Array();
			topParentWindow.all_open_windows[0] = in_openwnd_obj;
		}
		else {
			topParentWindow.all_open_windows[topParentWindow.all_open_windows.length] = in_openwnd_obj;
		}
	}
	else {
		doTimeoutCount = 0;
		doTimeout = false;
	}
	//openwnd_displayAllWindows();
	return ow_obj.newwnd;

}

function getTopParentWindow() {
	return top;
}

function getOpenerTopParentWindow() {
	return opener.top;
}

function openwnd_doCloseWindow(wnd) {
	if (wnd && wnd.closed == false) {
		wnd.close();
	}
}


function openwnd_doCloseAllWindows() {
//alert("openwnd_doCloseAllWindows");
	var topParentWindow = getTopParentWindow();
	if (topParentWindow && topParentWindow.all_open_windows) {
		if (topParentWindow.all_open_windows.length > 0) {
			var i;
			for (i=0; i < topParentWindow.all_open_windows.length;i++) {
				var tmp_openwnd_obj = topParentWindow.all_open_windows[i];
				
				if (tmp_openwnd_obj && tmp_openwnd_obj.newwnd && !tmp_openwnd_obj.do_not_close) {
					//openwnd_debug_alert(tmp_openwnd_obj.dstpath);
					openwnd_doCloseWindow (tmp_openwnd_obj.newwnd);
					topParentWindow.all_open_windows[i] = null;
				}
				//if(tmp_openwnd_obj)
					//alert("tmp_openwnd_obj.newwnd.name= " + tmp_openwnd_obj.newwnd.name);

			}
		}
		
		topParentWindow.all_open_windows = null;
	}
}

function openwnd_doCloseNamedWindows(name) {
	//alert("openwnd_doCloseNamedWindows");
	var topParentWindow = getTopParentWindow();
	if (topParentWindow && topParentWindow.all_open_windows) {
		if (topParentWindow.all_open_windows.length > 0) {
			var i;
			for (i=0; i < topParentWindow.all_open_windows.length;i++) {
				var tmp_openwnd_obj = topParentWindow.all_open_windows[i];
				
				if (tmp_openwnd_obj && tmp_openwnd_obj.newwnd && tmp_openwnd_obj.newwnd.name==name) {
					openwnd_doCloseWindow (tmp_openwnd_obj.newwnd);
					topParentWindow.all_open_windows[i] = null;
				}
			}
		}
	}
}


function openwnd_Sample() {
	var my_openwnd_obj = new openwnd_obj (0, 0 , 975, 720 );
	 
	my_openwnd_obj.wnd_open_srcwnd = window;
	my_openwnd_obj.wnd_open_target = "Test";
	my_openwnd_obj.tmpbase = "";
	my_openwnd_obj.tmppath = "dummy.html";
	my_openwnd_obj.dstbase = "";
	my_openwnd_obj.dstpath = "dummy.html";
	my_openwnd_obj.do_focus = "true";
	my_openwnd_obj.do_add_wh2query = true;
	my_openwnd_obj.do_delay = 1000;
	
	msgWindow = openwnd_doOpenWindow_Obj(my_openwnd_obj);
}

