var faxPopupPath = "/pokis/registrierung_bestaetigung.shtml?printfax=true";

var pokis = {
	/* page related functions */
	page: {
		/* Inhalt verlagert in text.js */
		/* add function OBEJCT or list of function OBJECTS which is/are called on document ready */
		ready: function(callback) {
			if (typeof callback != 'function' && callback.length) {
				for (var i=0; i<callback.length; i++) {
					if ($.browser.msie) pokis.page.load(callback[i]);
					else $(document).ready(callback[i]);
				}
			}
			else {
				if ($.browser.msie) pokis.page.load(callback);
				else $(document).ready(callback);	
			}
		},
		_loadCalls: [],
		/* add function OBJECT or STRING which is called on document load */
		load: function(callback) {
			pokis.page._loadCalls.push(callback);
		},
		_unloadCalls: [],
		/* add function OBJECT or STRING which is called on document unload */
		unload: function(callString) {
			pokis.page._unloadCalls.push(callString);
		},
		_execLoadCalls: function() {
			for(var i=0; i<pokis.page._loadCalls.length; i++) {
				try { 
					var callback = pokis.page._loadCalls[i];
					if (typeof callback == 'string') eval(callback);					
					else callback();
				}
				catch (e) {} 
			}
		},
		_execUnloadCalls: function() {
			for(var i=0; i<pokis.page._unloadCalls.length; i++) { 
				try { 
					var callback = pokis.page._unloadCalls[i];
					if (typeof callback == 'string') eval(callback);					
					else callback();
				}
				catch (e) {} 
			}
		},
		/* adds print link to .print-area .print if not printpreview */
		addPrintlink: function(url, width) {
			if (location.search.indexOf('print=true') > -1 || location.hash.indexOf('print=true') > -1) return;
			if ($('div.print-area p.print').size()) {
				var printlink = '<a href="#" class="printSymbol" onclick="return pokis.page.printView(null, 735);" alt="'+ msgs.messages.PRINT_TEXT +'" title="'+ msgs.messages.PRINT_TEXT +'">'+ msgs.messages.PRINT_TEXT +'</a>';
				$('div.print-area p.print').prepend(printlink);
			}
			if ($('div.print-area p.printartikel').size()) {
				var printlink = '<a href="#" class="printSymbol" onclick="return pokis.page.printView(null, 735);" alt="'+ msgs.messages.PRINTARTIKEL_TEXT +'" title="'+ msgs.messages.PRINTARTIKEL_TEXT +'">'+ msgs.messages.PRINTARTIKEL_TEXT +'</a>';
				$('div.print-area p.printartikel').prepend(printlink);
			}
		},
		/* opens popup with print view */
		printView: function(url, width) {
			url = (url || location.href).replace(location.hash, '');
			width = width || 588;
			printparam = (url.indexOf('?') > -1) ? '&amp;print=true' : '?print=true'
			var url = url.replace(/#/g, "");
			return this.popup(url + printparam, width, 500, -1, -1, false, 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1', 'printpopup');
		},
		/* disables links, buttons, forms and inserts print head for DPAG */
		initPrintView: function() {
			if (location.search.indexOf('print=true') == -1 && location.hash.indexOf('print=true') == -1) return;
			
			$("a").each(function() {
				if($(this).attr("id") != 'printfooter' && $(this).attr("id") != 'printheaderbutton') {
					$(this).removeAttr("href");
					$(this).removeAttr("onclick");
					$(this).removeAttr("onmouseover");
					$(this).removeAttr("onmouseout");
				}
			});
			$("input[@type='submit']").each(function() {
				this.onclick = function(){return false;}
			});
			$("input[@type='file']").each(function() {
				this.setAttribute("disabled", "true");
			});
			$("form").each(function() {
				this.onsubmit = function(){return false;}
				this.removeAttribute("action", "false");
			});

			//if (location.href.indexOf('/dpag') > -1) {
				var url = location.href.replace('&lang=', '&amp;lang=').replace('&print=true', '').replace('?print=true&', '?');;
				var printhead = '<div class="printbar">'
				var printfoot = printhead; 
				printhead += ' <button onclick="print();">'+ msgs.messages.PRINT_NOW_TEXT +'</button>';
				var printfoot = printhead; 
				printfoot += '</div>';
			//	printhead += ' <h2>URL: '+ url +'</h2>';
				printhead += '</div>';
				$('div.head-area').prepend(printhead);
				if ($('p.copyright').size()) $('p.copyright').append(printfoot); // page
				else $('div.print-area').append(printfoot); // popup
				
				var url = url.replace(/\?print=true/g, "");
				var printlinkshow = 'URL: '+ url +' ';
				$('div.printlinkshow').prepend(printlinkshow);
			//}
		},
		/* adds close buttton to .head-area if not printpreview */
		addPopupbar: function(url, width) {
			if (location.search.indexOf('print=true') > -1 || location.hash.indexOf('print=true') > -1) return;
			if ($('div.type-popup div.head-area').size() || $('div.type-flash div.head-area').size()) {
				var closebar = '<a href="#" onclick="window.close()">'+ pokis.page.messages.CLOSE_TEXT +'</a>';
				$('div.type-popup div.head-area').prepend(closebar);
				$('div.type-flash div.head-area').prepend(closebar);
			}
		},
		popupwindow: false,
		/* notused was linkfield */
		popup: function(url, width, height, left, top, NOTUSED, options, winname) {
			var screenW = screen.availWidth || 800;
			var screenH = screen.availHeight || 600;
			
			width = Math.min(width || (screenW - 100), screenW - 100);
			height = Math.min(height || (screenH - 100), screenH - 100);

			left = (left && left > 0) ? left : (screenW - width) / 2;  
			top = (top && top > 0) ? top : (screenH - height) / 2 - 20;  

			options = options || 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1';
			winname = winname || 'w';
			// check if popup already open
			if (self.pokis_IS_POPUP && winname == 'w') winname = 'w2';
			
			if(pokis.page.popupwindow && (pokis.page.popupwindow.closed != true)) pokis.page.popupwindow.close();
			
			pokis.page.popupwindow = window.open(pokis.util.escapeZoomURL(url), winname, options+',width='+width+',height='+height+',left='+left+',top='+top);
			try {
				pokis.page.popupwindow.pokis_IS_POPUP = 1;
				}
			catch (e) {} // error if not a pokis window, but not needed then
			pokis.page.popupwindow.focus();
			// close self if it was a popup
			if (self.pokis_IS_POPUP && winname == 'w2') {
				pokis.page.popupwindow.name = 'w';
				self.close();
			}
			
			return false;
		},
		/* do not close if already a popup */
		popupclose: function() {
			if(!self.pokis_IS_POPUP && pokis.page.popupwindow && (pokis.page.popupwindow.closed == false)) pokis.page.popupwindow.close();
		},
		/*
		 * Functions/Properties related to specific modules
		 */
		modules: {
			/*
			 * Video control functions used by videochoice template in common.xsl & pokis/video.xsl
			 */
			hideVideoLayer: function(id) {
				document.getElementById("videoChoice_"+id+"_Player").style.display = "none";
				document.getElementById("videoChoice_"+id+"_Player_Cont").innerHTML = "";
				document.getElementById("videoChoice_"+id+"_Select").style.display = "block";
			},
			showVideoV2: function(node,id) {
				var video 		= node.videoChoiceSelect[node.videoChoiceSelect.selectedIndex].value;
	
				if(video != "none" && video != "") {
					var ext_end 	= (video.indexOf("?") != -1) ? video.indexOf("?") : video.length;
					var file_ext 	= video.substring(video.lastIndexOf(".")+1,ext_end);
					var src 		= video.substring(0,video.indexOf("?"));
					var width 		= 320;
					var height 		= 240;
					
					if(src.indexOf("/") == 0) src = node.domain.value + src;
					
					//Get width and height values from string
					if(video.indexOf("?") != -1) {
						var temp = video.split("?");
						temp = temp[1].split("&");
						for(i=0;i<temp.length;i++) {
							var keyval = temp[i].split("=");
							if(keyval[0] != "" && keyval[1] != "") eval(keyval[0] + "=" + keyval[1]);
						}
					}
					
					var player_cont = '<div class="videoChoiceContPlayer" align="center">';
					
					if(file_ext == "wmv") {
						height = height + 46; //Add height for player controls
						player_cont += 	'<OBJECT type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" HEIGHT="'+height+'" WIDTH="'+width+'">'+
										'	<PARAM NAME="showControls" VALUE="true">'+
										'	<PARAM NAME="autostart" VALUE="true">'+
										'	<PARAM NAME="url" VALUE="'+src+'">'+
										'	<EMBED SRC="'+src+'" autoplay="true" controller="true" enablejavascript="true" HEIGHT="'+height+'" WIDTH="'+width+'" AUTOSTART="true"></embed>'+
										'</OBJECT>';
					} else if(file_ext == "rm") {
						player_cont += 	'<OBJECT ID="id" name="id" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" HEIGHT="'+height+'" WIDTH="'+width+'">'+
			 							'	<PARAM NAME="controls" VALUE="ImageWindow">'+
										'	<PARAM NAME="console" VALUE="video_'+id+'">'+
										'	<PARAM NAME="autostart" VALUE="false">'+
										'	<PARAM NAME="src" VALUE="'+src+'">'+
										'	<EMBED SRC="'+src+'" type="audio/x-pn-realaudio-plugin" CONSOLE="video_'+id+'" CONTROLS="ImageWindow" HEIGHT="'+height+'" WIDTH="'+width+'" AUTOSTART="false"></embed>'+
										'</OBJECT>'+
										'<OBJECT ID="" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" HEIGHT="30" width="'+width+'">'+
										'	<PARAM NAME="controls" VALUE="ControlPanel">'+
										'	<PARAM NAME="console" VALUE="video_'+id+'">'+
										'	<PARAM NAME="autostart" VALUE="true">'+
										'	<EMBED type="audio/x-pn-realaudio-plugin" CONSOLE="video_'+id+'" CONTROLS="ControlPanel" HEIGHT="30" width="'+width+'" AUTOSTART="true"></embed>'+
										'</OBJECT>';
					} else if(file_ext == "mov") {
						player_cont += 	'<OBJECT ID="x" name="x" CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" HEIGHT="'+height+'" WIDTH="'+width+'">'+
										'	<PARAM NAME="controller" VALUE="true">'+
										'	<param name="type" value="video/quicktime">'+
										'	<PARAM NAME="autostart" VALUE="true">'+
										'	<PARAM NAME="src" VALUE="'+src+'">'+
										'	<PARAM NAME="pluginspage" VALUE="http://www.apple.com/quicktime/download/">'+
										'	<EMBED SRC="'+src+'" type="video/quicktime" autoplay="false" controller="true" enablejavascript="true" HEIGHT="'+height+'" WIDTH="'+width+'" AUTOSTART="true" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.html"></embed>'+
										'</OBJECT>';
					} else {
						document.location = video;
						return false;
					}
					
					player_cont += 	'</div>';
									
					document.getElementById("videoChoice_"+id+"_Player_Cont").innerHTML = player_cont;
					document.getElementById("videoChoice_"+id+"_Select").style.display = "none";
					document.getElementById("videoChoice_"+id+"_Player").style.display = "block";
				}
				
				return false;
			},
			/*
			 * If anchorlink on a question in FAQ open this question
			 * used by all clients except DPAG
			 */
			toggleFaq: function(faqlink) {
				if(typeof faqlink == "string") faqlink = faqlink.replace(/\./g, "_");
				var dt = $(faqlink);
				dt.toggleClass('open');
				dt.next('dd').slideToggle();
				return false;
			},
			toggleHeaderInfo: function(id) {
				$('#'+id).toggle();
				return false;
			},
			select_popups: {},
			/* 
			 * ele may be a FORM element (DHL, pokis) or the SELECT element itself (DPAG)
			 */
			handleSelectPopups: function(ele) {
				if (ele.nodeName == 'FORM') {
					var name = $(ele).attr('name');
					var select = ele.elements['href'];
				}
				else { // SELECT
					var name = $(ele).parents('form').attr('name');
					var select = ele;								
				}
				
				var index = select.selectedIndex;
				var value = select[index].value;
				var servlet = $(select).parents('form').get(0).elements["url"].value;
				
				// no href or pokis quicklinks 1st item should do nothing!
				if (value == "#" || value == "" || (index == 0 && select.id == "pokis-quicklink-select")) {
					return false;	
				}
				
				if(value.indexOf("http") != 0 && value.indexOf("ftp:") != 0 && value.indexOf("mailto:") != 0 && value.indexOf("/") != 0) {
					value = servlet + value;
				}
				
				var item = this.select_popups[name][index];

				if (item && item.width) {
					return pokis.page.popup(item.href, item.width, item.height);
				}
				else if (item) {
					var x = window.open(value);
					if (x) return false;
					else return true;					
				}
				else {
					window.location.href = value;
					return false;
				}
			}
		},
		/*
		 * Misc. functions
		 */
		misc: {
			setFlashVar: function(flashid,variable,value) {
				if(value == "COOKIE.ZS_SESSIONID") {
					value = $.cookie("ZS_SESSIONID");
				}
				eval("window.document."+flashid+".SetVariable('"+variable+"', '"+value+"')");
			},
			submitFOCUS: function(item) {
				if (document.all && item.className) {
					var currentClass = item.className;
					if (currentClass.indexOf('SubmitFOCUS') == -1) {
						item.className = currentClass.replace('Submit', 'SubmitFOCUS');
					} else {
						item.className = currentClass.replace('SubmitFOCUS', 'Submit');
					}
				}
			}
		}
	},
	util: {
		/*
		 * Escapes alt and title parameter values or href value because xsl does not escape these.
		 */
		escapeZoomURL: function(url) {
			if (url.indexOf('title=') > -1 || url.indexOf('alt=') > -1) {
				var part = url.split('&');
				url = '';
				for (i=0; i < part.length; i++) {
					//first is href + first param!
					var param = part[i].split('=');
					url = url + param[0] + '=' + escape(param[1]);
					if (i < part.length) url += '&';
				}
			}
			if (url.indexOf('disclaimer.jsp') > -1) {
				var part = url.split('href=');
				url = part[0] + 'href=' + escape(part[1]);
			}
			return url;
		},
		getURLParam: function(url,param) {
			if(url.indexOf("?") > -1 ) {
	    		var url 	= url.substr(url.indexOf("?")+1);
				var keyvals	= url.split("&");
				
				for(var i=0;i<keyvals.length; i++) {
					if(keyvals[i].split("=")[0] == param) return keyvals[i].split("=")[1];
				}
	  		}
			return null;
		}
	}
};

$(window).load(pokis.page._execLoadCalls);
$(window).unload(pokis.page._execUnloadCalls);

/*
 * General JS extensions
 */
if(!Array().indexOf) {
	Array.prototype.indexOf = function(needle) {
		for(var i=0;i<this.length;i++) if(this[i] == needle) return i;
		return -1;
	}
}



/* Popup General */
function openPopup(page,breite,hoehe) {
	var positionX=((screen.availWidth / 2) - breite / 2);
	var positionY=((screen.availHeight / 2) - hoehe / 2);
	var id="general";
	eval("popuppage"+id+"=window.open('"+page+"','popuppage','toolbar=0,scrollbars=1,location=0,status=0,menubars=0,resizable=1,width="+breite+",height="+hoehe+",top="+positionY+",left="+positionX+"')")
}

/* Auto-Popup Fax */
function faxAutoPopup() 
{
	var breite = 720;
	var hoehe = 500;
	var positionX=((screen.availWidth / 2) - breite / 2);
	var positionY=((screen.availHeight / 2) - hoehe / 2);
	var id="general";
	if(popupisopen!=true) {
		eval("popuppage"+id+"=window.open('"+faxPopupPath+"','popuppage','toolbar=0,scrollbars=1,location=0,status=0,menubars=0,resizable=1,width="+breite+",height="+hoehe+",top="+positionY+",left="+positionX+"')")
	}
}

/*
 * Kept for backwards compatibility
 * @call may be a function string or function object
 */
function globalOnLoad() { /* backwards compat stub */ }
function addOnLoad(call) { pokis.page.load(call); }
function addOnUnLoad(call) { pokis.page.unload(call); }
function popup(url, winBreite, winHoehe, posLinks, posTop, NOTUSED, options, winname) { pokis.page.popup(url, winBreite, winHoehe, posLinks, posTop, NOTUSED, options, winname); }
function setFlashVar(flashid,variable,value) { pokis.page.misc.setFlashVar(flashid,variable,value); }