// Module for javascript functionality shared amongst several static 
// theoLOG online pages.
// (All of the below to be found in JS.pm for dynamic pages!)
// 
// List of javascript functions:
//   fPreventTop()
//   fStatus()
//   fGoToURL(list_value, seprtr)
//   fGetDocName(aURL)
//   fGetDocExt(aURL)
//   fGetNavInfo()
//   fHiLite(onoff, obj)

//////////////////////////////////////////////////////////////////////
function fPreventTop() {
//////////////////////////////////////////////////////////////////////
	if (self == top)
		top.location.href = 'index.htm';
	return null;
}

//////////////////////////////////////////////////////////////////////
function fStatus(text) {
//////////////////////////////////////////////////////////////////////
	if (!text) text = '';
	self.status = text;
	return true;
}

////////////////////////////////////////////////////////////////////////////////////
// This function loads a URL from a select list.
////////////////////////////////////////////////////////////////////////////////////
function fGoToURL(list_value, seprtr) {
	if (list_value.length > 0 && seprtr.length > 0) {
		var pos_seprtr = list_value.indexOf(seprtr);	// position of separator
		if (pos_seprtr <= 0) { return null; }		// get name of target frame and document
		var target = list_value.substring(pos_seprtr + seprtr.length);
		var doc    = list_value.substring(0, pos_seprtr);
		var cgiFolder = 'cgi-bin/';

		var must_eval = (target.indexOf('javascript:') != -1 );
		if (must_eval) {
			eval(doc);
		} else {
			var extPerl = 'pl';
			var extCgi  = 'cgi';
			var extTrgt  = fGetDocExt(doc);
			var pathPrfx = '';

			if ( doc.indexOf("http://") > 0 || doc.indexOf(cgiFolder) > 0 || doc.indexOf("mailto:") > 0 ) {
				// most likely an 'external' document -or- path sufficient
			} else {
				// most likely an 'internal' document -- need to supply relative path?
				if ( document.URL.indexOf(cgiFolder) > 0 ) {
					if ( extTrgt != extPerl && extTrgt != extCgi) pathPrfx = '../';
				} else {
					if ( extTrgt == extPerl || extTrgt == extCgi) pathPrfx = cgiFolder;
				}
			}

			var trgtIsSrc = ((doc.indexOf( fGetDocName(document.URL) ) > 0 || document.URL.indexOf( doc ) > 0) && doc.indexOf("getProducts") < 0);	// reload current page?
			if ( trgtIsSrc && !confirm("Möchten Sie die aktuelle Seite erneut laden?") ) { return null; }

			if (target) {
				if (target == "_self") {
					if (trgtIsSrc) {
						self.location.replace(pathPrfx + doc);
					} else {
						self.location = pathPrfx + doc;
					}
					return null;
				} else {
					var subframed = (self != top && 
						 (top.location.href.indexOf('/gerdspann')    >= 0 ||
						  top.location.href.indexOf('/buecherspann') >= 0) );	// wrapper framesets

					// loop through frames
					for (var i = 0; i < top.frames.length; i++) {
						if (top.frames[i].name == target) { // target frame found
							if (trgtIsSrc) {
								top.frames[i].location.replace(pathPrfx + doc);
							} else {
								top.frames[i].location = pathPrfx + doc;
							}
							return null;
						} else {	// sub-framed ?
							if (subframed && top.frames[i].name == 'url_wrapper') {
								for (var j = 0; j < top.frames[i].frames.length; j++) { // find sub-frame
									if (top.frames[i].frames[j].name == target) { // target frame found
										if (trgtIsSrc) {
											top.frames[i].frames[j].location.replace(pathPrfx + doc);
										} else {
											top.frames[i].frames[j].location = pathPrfx + doc;
										}
										return null;
									}
								}
							}
						}
					}
				}
			}

			top.location = pathPrfx + doc; // no target frame given or target frame not found
		}
	}
	return null;
}

////////////////////////////////////////////////////////////////////////////////////
// This function retrieves the document name from a given URL.
////////////////////////////////////////////////////////////////////////////////////
function fGetDocName(aURL) {
	var str_URL = new String(aURL);
	return str_URL.substring(str_URL.lastIndexOf('/') + 1);
}

////////////////////////////////////////////////////////////////////////////////////
// This function retrieves the document extension from a given URL.
////////////////////////////////////////////////////////////////////////////////////
function fGetDocExt(aURL) {
	var str_URL = new String(aURL.substring(aURL.lastIndexOf('.') + 1));
	if (str_URL.indexOf('?') > 0) {	// querystring appended
		str_URL = (str_URL.substring(0, str_URL.indexOf('?')));
	}
	return str_URL;
}

////////////////////////////////////////////////////////////////////////////////////
// This function retrieves all available browser and os information.
////////////////////////////////////////////////////////////////////////////////////
function fGetNavInfo() {
		// var vCodeName = navigator.appCodeName;
		// var vuAgent   = navigator.userAgent;
	var appname  = 'appname:'  + navigator.appName;		// browser name
	var version  = navigator.appVersion;			// browser version
		// Netscape: <version> (<Operating_System>; I)
		// -> i.e. : 3.0 (Win95; I)
		// IE      : <compatible_Netscape_version> (compatible; <version>; <Operating_System>)
		// -> i.e. : 2.0 (compatible; MSIE 3.02; Windows 95)
	var platform = 'platform:' + navigator.platform;	// machine type
	var java = 'java:0';
	if (navigator.javaEnabled()) { java = 'java:1'; }	// java enabled
	var cookie   = 'cookie:';
	var speed    = 'speed:';
	if ( (appname == 'Microsoft Internet Explorer') && (version.substring(0, version.IndexOf(' ')) >= 4) ) {
		cookie = 'cookie:' + navigator.cookieEnabled;	// cookies enabled
		speed  = 'speed:' + navigator.connectionSpeed;	// connection speed
	}
	var version  = 'version:'  + navigator.appVersion;

	var nav_info = appname + '|' + version + '|' + platform + '|' + java + '|' + cookie + '|' + speed;
	var nav_info_out = "";

	for (var index=0; index < nav_info.length; index++) {	// remove semicolons
		if (nav_info.charAt(index) == ":" || nav_info.charAt(index) == ";") {
			if (nav_info.charAt(index) == ":") {
				nav_info_out = nav_info_out + "=";
			} else {
				nav_info_out = nav_info_out + ",";
			}
		} else {
			nav_info_out = nav_info_out + nav_info.charAt(index);
		}
	}

	document.form.X1_NAV_INFO.value = nav_info_out;
	return null;
}

//////////////////////////////////////////////////////////////////////
function fGetCookie(cookie_name) {
//////////////////////////////////////////////////////////////////////
	var str = new String(cookie_name);
	cookie_name = str.toUpperCase() + "=";
	var cookie_val = document.cookie; 
	var cook_len = cookie_val.length;
	var cook_bgn = 0;
	while (cook_bgn < cook_len) {
		var pos_from = cook_bgn + cookie_name.length;
		if (cookie_val.substring(cook_bgn, pos_from) == cookie_name) { 
			var pos_to = cookie_val.indexOf (";", pos_from);
			if (pos_to == -1) pos_to = cook_len;
			return unescape(cookie_val.substring(pos_from, pos_to));
		}
		cook_bgn = cookie_val.indexOf(" ", cook_bgn) + 1;
		if (cook_bgn == 0) break;
	}
	return '';
}

//////////////////////////////////////////////////////////////////////
function fSetCookie(cookie_name, cookie_val, expires) {
//////////////////////////////////////////////////////////////////////
	var str = new String(cookie_name);
	cookie_name = str.toUpperCase();
	if (!expires) { 
		expires = new Date();
		expires.setTime (expires.getTime() + (1000 * 60 * 60 * 24 * 10));		// cookie expires after 10 days
	}
	document.cookie = cookie_name + "=" + escape(cookie_val) + "; PATH=/; EXPIRES=" + expires.toGMTString();
	return null; // for downward compatibility
}

//////////////////////////////////////////////////////////////////////
function fDelCookie(cookie_name) {
//////////////////////////////////////////////////////////////////////
	document.cookie = cookie_name + "=; PATH=/" + "; EXPIRES=Thu, 01-Jan-80 00:00:00 GMT";
	return null; // for downward compatibility
}

//////////////////////////////////////////////////////////////////////
function fGetItem(str, item_to_get) {
//////////////////////////////////////////////////////////////////////
	var get_bgn = str.indexOf(item_to_get + ":");
	if (get_bgn < 0) {
		return '';	// item not found
	} else {
		var get_end = str.indexOf(";", get_bgn) + 1;
		return str.substring(get_bgn + (item_to_get + ":").length, get_end-1);
	}
	return null;
}

////////////////////////////////////////////////////////////////////////////////////
function fHiLite(onoff, obj) {
////////////////////////////////////////////////////////////////////////////////////
	var ns = (document.layers) ? true : false;
	var ie = (document.all) ? true : false;	
	// Netscape fix works only if called for forms
	var layerRef = (ns) ? 'document.form.' : 'document.all.';
	var styleRef = (ns) ? ''          : '.style';
	eval(layerRef + obj + styleRef + ".backgroundColor = '" + 
		(onoff == true ? "#FFFFFF" : "#EEEEEE") + 
	     "';");
}

//////////////////////////////////////////////////////////////////////
function fOpenWin(win_href,win_name,w,h,features,leftX,leftY) { // open a dependent window
//////////////////////////////////////////////////////////////////////
	var is_extern = win_href.indexOf('http://') >= 0;	// an external site
	if (leftX == null || !leftX || isNaN(leftX)) {
		leftX = (screen.availWidth - w) / 2;
		if (leftX < 0 ) leftX = 0;
		leftX = Math.round(leftX);
	}
	if (leftY == null || !leftY || isNaN(leftY)) {
		var leftY = (screen.availHeight - h) / 2;
		if (leftY < 0 ) leftY = 0;
		leftY = Math.round(leftY);
	}

	if (features != null && features && features.length > 1) {
		if (features.charAt(0) != ',') features = ',' + features;
	}
	var win_ref;
	if (navigator.userAgent.indexOf('MSIE') < 0) {	// NS
		win_ref = window.open(win_href, win_name, "width="+ w +",height="+ h +",screenX="+ leftX +",screenY="+ leftY + features);
	} else {						// IE
		win_ref = window.open(win_href, win_name, "width="+ w +",height="+ h + features);
		if (!is_extern) win_ref.moveTo(leftX ,leftY);
	}
	if (win_ref != null) {
		win_ref.focus();
	}

	eval("ref" + win_name + " = win_ref;");	// store a reference to the new window
	// eval("top.ref" + win_name + " = win_ref;");	// store a reference to the new window
	if (!is_extern) {
		if (win_ref != null && win_ref.opener == null) 
			win_ref.opener = self;	// set opener
		win_ref.theolog = top;		// store a top reference in the new window
	}
	return true;
}

//////////////////////////////////////////////////////////////////////
function fCloseWin(win_name) { // close a single or all open dependent windows
//////////////////////////////////////////////////////////////////////
	var closeall = (win_name == 'all');
	if (win_name == 'WinSys' || closeall) {
		if (self.refWinSys && !self.refWinSys.closed)
			self.refWinSys.close();
	}
	if (win_name == 'WinHlp' || closeall) {
		if (self.refWinHlp && !self.refWinHlp.closed)
			self.refWinHlp.close();
	}
	if (win_name == 'WinCalc' || closeall) {
		if (self.refWinCalc && !self.refWinCalc.closed)
			self.refWinCalc.close();
	}
	if (win_name == 'WinTour' || closeall) {
		if (self.refWinTour && !self.refWinTour.closed)
			self.refWinTour.close();
	}
	if (win_name == 'WinPic' || closeall) {
		if (self.refWinPic && !self.refWinPic.closed)
			self.refWinPic.close();
	}
	return true;
}

