var inputtitles;
var sitestatform;
// tabs
$(function(){
	$("#tabs").tabs({
		select: function(event, ui) {
			if(window.reorderjumplist) reorderjumplist();
			if(window.sitestaturlcounter && window.sitestat){	// sitestat-call
				sitestat(sitestaturlcounter+'.'+encodeURIComponent($(ui.tab).text()));
			}
		}
	});
	$(".sectionStart").each(function(){
		formTwist($(this).attr("id").substr(12));
	});
	var floatFixes = function(loadingComplete) {
		$('.floatRight, .floatLeft').each(function(i, e) {
			var imgs = $('img', e);
			var img = 0;
			for(i = 0; !img && i < imgs.length; ++i) {
				var tmp = imgs[i];
				if ($(tmp).parents('.imageSubline').length) { break;}
				img = tmp;
			}
			if (img && img.width) { $('.imageSubline', e).css('width', img.width);}
		});
		var fix1 = function(e, loadingComplete) {
			var floating = $(e);
			var complete = true;
			var w = 0;
			if (!loadingComplete) {
				$('img', e).each(function(i, img) {
					complete = complete && img.complete;
					if (img.complete === false) {
						$(img).bind("load", e, function(event) {
							fix1(event.data, true);
						});
					}
				});
			}
			if (complete) {
				w = floating.parent().innerWidth() - floating.outerWidth(true);
				if (w > 20) {
					floating.next('.imageSubline, .paraText').css('width', w).css('float', 'left').css('overflow', 'hidden').css('margin', '0');
				}
			}
		};
		$('.col0 .floatRight, .col1 .floatRight, .col0 .floatLeft, .col1 .floatLeft').each(function(i, e) {
			fix1(e, loadingComplete);
		});
		$('body:not(.edit) #main .paraFloat').each(function() {
			var floatdiv = $("div.floatLeft", this);
			var flOffset = floatdiv.offset();
			var flHeight = floatdiv.height();
			var flWidth = floatdiv.width();
			$('ul', this).each(function(){
				var that = $(this);
				var ulOffset = that.offset();
				if ($("li", this).filter(function(){
					return $(this).height() > parseInt($(this).css("line-height"), 10);
				}).length > 0 && ulOffset.top < flOffset.top + flHeight) {
					that.css({
						"padding-left":  flWidth + 32,
						"list-style-position":  "outside"
					});
				}
			});
		});
	};
	$(window).load(function() { floatFixes(true);});
	floatFixes();
	positionOversizedElements();
	check_valuetransferred();
	if(window.sitestatSitename){
		if(document.location.host.indexOf(".softwerk.") == -1){
			if($("form[name='_Response']").length){
				inputtitles=$("form[name='_Response'] input[title!=''], form[name='_Response'] textarea[title!=''], form[name='_Response'] select[title!='']");
				if(inputtitles && window.ns_API){
					sitestatform = new ns_API.form('_Response', true);
					var inputtitlevalues = $.map(inputtitles, function(e){return $(e).attr("title");});
					if(inputtitlevalues) inputtitlevalues = inputtitlevalues.join(";");
					sitestatform.addAbandonEvent(inputtitlevalues, "", "");
				}
			}
		}
	}
	if (location.search.indexOf("popup") != -1) {
		$("body").css("margin-left", "10px");
	}
});

var positionOversizedElements = function() {
    $('body:not(".edit") #main iframe, body:not(".edit") #main table').each(function(){
        if ($(this).css("position") != "absolute" && $(this).width() > 560) {
			if($(this).is(':visible')) {
				var width = $(this).width();
				$(this).css({"position": "absolute", "width": width});
				var div = $("<div></div>").css("height",$(this).height());
				$(this).after(div);
			}
        }
    });    
}
var twist = function(paraId, twisterId) {
	if ($("#"+paraId).css("display") == "none") {
		$("#"+twisterId).removeClass("displaySectionLabelClose");
		$("#"+twisterId).addClass("displaySectionLabelOpen");
		$("#"+paraId).slideDown("fast");
	} else {
		$("#"+twisterId).removeClass("displaySectionLabelOpen");
		$("#"+twisterId).addClass("displaySectionLabelClose");
		$("#"+paraId).slideUp("fast");
	}
	positionOversizedElements();
}
var formTwist = function(fieldId) {
	var table = $("#sectionStart"+fieldId).parents("table")[0];
	var tr = $("#sectionStart"+fieldId).parents("tr")[0];
	var found = false;
	var affected = $(tr).nextAll().filter(function(){
		if ($(this).find(".sectionEnd, .sectionStart").length > 0) {
			found = true;
		}
		return !found;
	});
	if ($("#twister"+fieldId).is(".formSectionLabelClose")) {
		$("#twister"+fieldId).removeClass("formSectionLabelClose");
		$("#twister"+fieldId).addClass("formSectionLabelOpen");
		affected.removeClass("formSectionHidden");
	} else {
		$("#twister"+fieldId).removeClass("formSectionLabelOpen");
		$("#twister"+fieldId).addClass("formSectionLabelClose");
		affected.addClass("formSectionHidden");
	}
};

// global Functions
var popup;
var no_valuetransferred;	// verhindert raufschreiben
if(location.href.toLowerCase().indexOf('wedgewood') != -1) titleStr = 'Wedgewood Analytical';
else titleStr = 'Endress+Hauser';
functionframe=false;
try{ if(parent.functionframe) functionframe=parent.functionframe;} catch(e){}
// else functionframe=self;
if(self.docUNID && parent && parent.check_activestatus){
	// parent.check_activestatus(self.docUNID);
	if(!window.prohibitNavigation) { parent.check_activestatus(self.docUNID); }
}
else{
	if(self.docPath){
		if(self.docPath.indexOf("dach") != -1){
			if(document.referrer){
				if(document.referrer.toLowerCase().indexOf("google")>0 || document.referrer.toLowerCase().indexOf("yahoo")>0 || document.referrer.toLowerCase().indexOf("msn")>0 || document.referrer.toLowerCase().indexOf(".aol.")>0){
						  parent.parent.location.href=docPath+"systemcontentview/X_selectcountry.html?Open"+"&DirectURL="+docUNID;
				}
			}
		}
	}	
}
function get_sitestaturl(sitecountry, prename, products) { // en, bookmark. , products
// sitecountry not used, now it is in the sitestatSitename
	var sitestaturl='';
	if(!window.sitestatSitename) return sitestaturl;
	if(document.location.host.indexOf(".softwerk.") == -1){
	/*	if (sitecountry){
			if(sitestatSitename.indexOf(sitecountry+".")!=-1){
				if(sitecountry.indexOf(prename)==-1) sitecountry=prename+sitecountry;
				sitestaturl="http://de.sitestat.com/endress/"+sitestatSitename+sitestatpath;
				sitecountry=sitecountry.split(prename).join('');
			}
			else sitestaturl="http://de.sitestat.com/endress/"+sitestatSitename+prename+sitestatpath;
		}
		else sitestaturl="http://de.sitestat.com/endress/"+sitestatSitename+prename+sitestatpath; */
		sitestaturl="http://de.sitestat.com/endress/"+sitestatSitename+prename+sitestatpath; 
		if(products){	// change to dbType == '2'
			if(top.sitestatwebsitename && sitestatWebSite){
				if(top.sitestatwebsitename && (top.sitestatwebsitename!= sitestatWebSite)){
					sitestaturl= "http://de.sitestat.com/endress/"+top.sitestatwebsitename+"/s?"+(top.sitecountry?top.sitecountry+'.' :'') +prename+sitestatpath;
				}
			}
			else {	sitestaturl=''; }
		}
		if(sitestaturl.indexOf('/s?.') != -1) { 	// corporate Website, AU
			if(sitestatSitename!='/s?.') sitestaturl= sitestaturl.split('/s?.').join('/s?');
		}
	}
	return sitestaturl;
}
function get_country(){
	var host = window.location.host;
	try {
		host = top.location.host;
	} catch (e) { /* ignore errors */}
	location_country=host.split("endress.")[0];
	if(location_country.indexOf(".")>0)  location_country=location_country.split(".")[1];
	if(location_country.indexOf("www")!=-1)  location_country=location_country.split("www").join("");
	if(!location_country) location_country="de";
	if(location_country.indexOf('/>')!= -1) location_country='';
	return location_country;	
}
function setSitecountry() {		// needed for get_sitestaturl
	var land='';
	if(parent.parent.location_country) land=parent.parent.location_country;
	else{ if(window.get_country) land=get_country();}
	if(land.length==2){	
		if(self.docPath.indexOf("dach")!=-1 || (self.docPath.indexOf("de")!=-1 && self.docPath.indexOf("productDBs")!=-1)) sitecountry=land;
	}
	if(parent){
		if(parent.parent.sitecountry!= null) sitecountry=parent.parent.sitecountry;
	}
}
function setEHTitle(){	
	if(functionframe && titletxt && ! document.layers) functionframe.document.title=titleStr+" - " + titletxt.replace(/~~~/ig,"'");
}
function setTheme(themetext){
	if(themetext) { themeheadline=themetext; }//<$homenavcountry/>'
}
function check_frameset(){
	try{ if( top.rBP) return;} catch(e){}
	if(self.location.href.toLowerCase().indexOf( "popup" ) < 0  && self.location.href.toLowerCase().indexOf( "&ponr" )<0 && self.location.href.toLowerCase().indexOf( "&sonr" )<0 && self.location.href.toLowerCase().indexOf( "&rfnr" )<0) if(self==top) {
		top.location.href=docPath+'?Open'+'&DirectURL='+docUNID;
	}
}
function check_valuetransferred(){
	try{ if( top.rBP) return;} catch(e){}
	if(window.valuetransferred) {
		if(window.valuetransferred && !window.no_valuetransferred) parent.valuetransferred_index=valuetransferred; 
	}
	else {	// valuetransferred=''
		if(!window.no_valuetransferred) parent.valuetransferred_index='';
	}
}
function printWin(){
	var num="";
	var url=self.location.href;
	if(url.indexOf("&SONR=")>0) num=url.substring(url.indexOf("&SONR="),url.indexOf("~~"))+"~~";		
	if(url.indexOf("&PONR=")>0) num=url.substring(url.indexOf("&PONR="),url.indexOf("~~"))+"~~";
	if(url.indexOf("&RFNR=")>0) num=url.substring(url.indexOf("&RFNR="),url.indexOf("~~"))+"~~";	
	popup=window.open(docPath+"printview/"+docUNID+"?Open"+num+ "&anticache=" + String(Math.random()).split('.')[1],"popup");
	if(popup) popup.focus();
	if(sitestatSitename){		// global Parameter
		var sitestaturl=get_sitestaturl(sitecountry,'print.');
		if(sitestaturl) sitestat(sitestaturl);
	}
}
function recommendWin(){
	popup=window.open(docPath+"systemcontentview/x_recommend.html?Open" +"&"+docUNID,"popup","width=410,height=440,resizable=no");
	if(popup) popup.focus();
	if(sitestatSitename){		// global Parameter
		var sitestaturl=get_sitestaturl(sitecountry,'recommend.');
		if(sitestaturl) sitestat(sitestaturl);
	}
}
function bookmarkWin(bookmarkcomment){
	var bookURL= location.href.split('home.nsf')[0] + 'home.nsf/?Open&DirectURL='+docUNID;
	if(window.SChomepath){		// products
		var schome='/eh/home.nsf';
		if(self==top){
			if(SChomepath.indexOf('home.nsf')!=-1) schome=SChomepath;
		}
		else{ 
			if(parent.parent.doc_path) schome= parent.parent.doc_path;
		}
		bookURL= location.protocol + '//' + location.host+schome.split('home.nsf')[0] + 'home.nsf/?Open&DirectProductURL='+docUNID;
	}
	if(bookURL.indexOf('.softwerk.') !=-1) bookURL=bookURL.split('endress.softwerk.de').join('www.endress.com');
	if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Win') != -1) {
		window.external.AddFavorite(bookURL,titleStr+' - '+titletxt);
	}
	else {
		alert(bookmarkcomment);	//'<$bookmark comment/>'
		EHbookmarkOwn = window.open(bookURL,"EHbookmarkOwn","width=800,height=500,resizable=yes,menubar=yes,scrollbars=yes");
		if(EHbookmarkOwn) EHbookmarkOwn.focus();
	}
	if(sitestatSitename){		// global Parameter
		var sitestaturl=get_sitestaturl(sitecountry,'bookmark.');
		if(sitestaturl) sitestat(sitestaturl);
	}
}
function selectBookmark(){
	var framedocPath=docPath;
	if(self==top){
		if(window.SChomepath) if(SChomepath.indexOf('home.nsf')!=-1) framedocPath=SChomepath;
	}
	else{
		if(parent.parent.doc_path) framedocPath=parent.parent.doc_path;
	}
	var EHbookmarkSelect=window.open(framedocPath+'systemcontentview/x_bookmark.html?Open&anticache=' + String(Math.random()).split('.')[1], 'EHbookmarkSelect','width=570,height=300,left=250,top=160,resizable=yes,menubar=no,scrollbars=yes');
	if(EHbookmarkSelect) EHbookmarkSelect.focus();
	if(sitestatSitename){		// global Parameter
		var sitestaturl=get_sitestaturl(sitecountry,'bookmarkselect.');
		if(sitestaturl) sitestat(sitestaturl);
	}
}
function replaceInternalLinks() {
	allLinks = document.getElementsByTagName('a');
	for(var i=0; i<allLinks.length; i++) {
		if(allLinks[i].href.indexOf('internalhost')!=-1 || allLinks[i].href.indexOf('internalcountry')!=-1){
			var link = allLinks[i].href.split('internalhost').join(self.location.host).split('internalcountry').join(sitecountry);
			if (allLinks[i].innerHTML) {
				var tmp = allLinks[i].innerHTML;
				allLinks[i].href = link;
				allLinks[i].innerHTML = tmp;
			} 
			else allLinks[i].href = link;
		}
    }
}
function do_applicatorlink(workingArea, measureTask){
		var sitelang='en';
		if(window.sitelanguage) sitelang=window.sitelanguage;
		var land='';
		if(sitecountry) land=sitecountry;
		var applicatorparas="";
		if(workingArea || measureTask) applicatorparas="workingArea="+workingArea+"&measureTask="+measureTask+"&";
		theUrl="https://wapps.endress.com/applicator9x/callapplicator.do?"+applicatorparas+"country="+land+"&language="+sitelang;
		popup = window.open(theUrl,"popup");
}
function do_download(prodArea,prodID,bundleID,conttype,type) {
		var sitelang='en';
		if(window.sitelanguage) sitelang=window.sitelanguage;
		theUrl='https://wa001.endress.com/sap/bc/bsp/euh/dla/extern/call.htm?language='+sitelang+'&cont_type=&type=';
		if(prodID){
			if(type) theUrl=theUrl+type;
			theUrl=theUrl+"&root_id="+prodID;
			document.location.href=theUrl;
		}
		else if (bundleID){
			if(type) theUrl=theUrl+type;
			theUrl=theUrl+"&bundle_id="+bundleID;
			document.location.href=theUrl;
		}	
}
function do_openfeed(feedname) {
	feeds=window.open(docPath+"contentview/"+feedname+"?Open&popup","feeds","width=840, scrollbars=yes, resizable=yes").focus();
}
/*   -- form functions -- */
function do_check(formular) {
	var errorMsg = formular.elements['errorMsg'].value;
	if (!errorMsg) errorMsg='Please insert a value for:';
	var error_feld='';
	if(document.getElementById('OrderNrDisp') && document.getElementsByName('OrderNrSelector') && window.orderNumber) {
			if(document.getElementById('OrderNrDisp').innerHTML.indexOf('ONnotSet')!=-1) error_feld+= '- '+orderNumber;
	}
	if(error_feld!='') {				
		alert(errorMsg+'\n'+error_feld);
		return false;			
	}				
	else{  
		if(document.getElementById('OrderNrDisp') && document.getElementsByName('OrderNrSelector') && window.orderNumber) {
			OrderSelectors = document.getElementsByName('OrderNrSelector');
			for(var i=0; i<OrderSelectors.length; i++) {
				OrderSelectors[i].disabled = true;
			}
			for(var i=0; i<formular.elements.length; i++) {
				if(formular.elements[i].value == document.getElementById('OrderNrPrefix').innerHTML) formular.elements[i].value = orderNumber;
			}
		}
		if(window.sitestatSitename && window.sitestatform){
			var inputtitlevalues = $.map(inputtitles, function(e){return $(e).attr("title");});
			if(inputtitlevalues){
				inputtitlevalues = inputtitlevalues.join(";");
				sitestatform.addSubmitEvent(inputtitlevalues, "", "");
			}
		}
		return true; 
	}
}
function display(id) {				// section
	thisEl = document.getElementById(id);
	thisTwist = document.getElementById('twist'+id);
	if(thisEl.style.display=='none') {
		thisEl.style.display='block';
		thisTwist.src=docPath+'imgref/N_twist_open_2n.gif/$FILE/twist_open_2n.gif';
	}
	else {
		thisEl.style.display='none';
		thisTwist.src=docPath+'imgref/N_twist_close_2n.gif/$FILE/twist_close_2n.gif';
	}
	self.focus();
}
function check_form(){
	for (var z=0;z<document.forms[0].elements.length;z++) {
		if (document.forms[0].elements[z].value=='valuetransferred' ) {
			if(parent.valuetransferred_index && no_valuetransferred) {
			document.forms[0].elements[z].value=parent.valuetransferred_index;
			}
			else document.forms[0].elements[z].value='';
		}
	}	
}
function setFormOrderNr() {		// orderselector
	if(!document.getElementById('OrderNrDisp') || !document.getElementsByName('OrderNrSelector')) return;
	error = false;
	OrderNr = '';
	OrderNrDsp = document.getElementById('OrderNrDisp');
	OrderNrFld = document.getElementById('OrderNrField');
	OrderSelectors = document.getElementsByName('OrderNrSelector');
	for(var i=0; i<OrderSelectors.length; i++) {
		opt = OrderSelectors[i].options[OrderSelectors[i].selectedIndex].text;
		if(opt.indexOf(' ')==-1 || OrderSelectors[i].selectedIndex==0) {
			error = true;
			OrderNr+='<span class="ONnotSet">X</span>';
		}
		else OrderNr+=opt.split(' ')[0];
	}
	OrderNrDsp.innerHTML = OrderNr;
	//OrderNrFld.value = OrderNr;
	OrderNrFld.value = document.getElementById('OrderNrPrefix').innerHTML + OrderNr;
}
function openPopupLink(url, params) {
	params = params.toLowerCase();
	if(params.indexOf('height=,') > -1 || params.indexOf('height=0,') > -1 || params.indexOf('width=,') > -1 || params.indexOf('width=0,') > -1) {
		params = params.split('height=,').join('').split('height=0,').join('').split('width=,').join('').split('width=0,').join('');
		params = params.split('scrollbars=no').join('scrollbars=yes');
	}
	var popup = window.open(url, '', params);
	if (popup && popup.focus) { popup.focus();}
}
function openImageLink(url, params) {
	var popup = window.open(url, '', params);
	if (popup && popup.focus) { popup.focus();}
}
