// used variables
var winW = 800; // browser window width
var winH = 600; // browser window height


//function to open a pop up
//URL: is the relative or absolute http URL
//Name: is the name of the new window (pop up)
//xWidth: the width of the pop up (int > 0)
//xHeight: the height of the pop up (int > 0).
function openPopUp(URL,Name,xWidth,xHeight,xScrollBars){
	xLeft=(screen.availWidth-xWidth)/2;
	xTop=(screen.availHeight-xHeight)/2;
	xWin = window.open(URL,Name,'scrollbars='+xScrollBars+',width='+xWidth+',height='+xHeight+',left='+xLeft+',top='+xTop+',dependent=yes,resizable=yes');
	xWin.focus();
}


//function to open an external URL in a new window
//the dimentions of this window are the dimentions of the screen
function openWindow(URL){
	xWidth = screen.availWidth;
	xHeight = screen.availHeight;
	xWin = window.open(URL,'','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,dependent=yes,resizable=yes,width='+ xWidth +',height='+ xHeight +',top=0,left=0');
	xWin.focus();
}


//function to open an image popup
function showImage(holderURL, imageURL, wi, hi, marge, resize){
	posX = 30; //(screen.availWidth - wi)/2;
	posY = 30; //(screen.availHeight- hi)/2;
	imageURL = encodeURI(imageURL);
	imageWin = window.open(holderURL +'?showImage='+ imageURL +'&margin='+ marge +'&resize='+ resize, '', 'width='+ wi +',height='+ hi +',left='+ posX +',top='+ posY +',dependent=yes,resizable=no');
	imageWin.focus();
}


//function to swap page language without changing the url path
function swapLang(lang){
	var URL = document.URL;
	var host = window.location.host;
	var leftPart = URL.substring(0, URL.indexOf(host) + host.length);
	var rightPart = URL.substring(URL.indexOf(host) + host.length, URL.length);
	if (rightPart.charAt(0) == "/") rightPart = rightPart.substring(1, rightPart.length);
	var newURL = "";
	if (rightPart.indexOf("TerziusClient2") == 0){
	    leftPart += "/TerziusClient2";
	    rightPart = rightPart.substring(rightPart.indexOf("/") + 1);
	}
	if (rightPart.indexOf("/") == 3){
		var origLang = rightPart.substring(0, rightPart.indexOf("/"));
		newURL = leftPart + "/" + lang + rightPart.substring(rightPart.indexOf("/"), rightPart.length);
	}else{
		if (URL.indexOf("lang=") < 0){
			newURL = (URL.indexOf("?") < 0) ? URL + "?lang=" + lang : URL + "&lang=" + lang;
		}else{
			var langVar = "lang=";
			leftPart = URL.substring(0, URL.indexOf(langVar) + langVar.length);
			rightPart = URL.substring(URL.indexOf(langVar) + langVar.length, URL.length);
			if (rightPart.indexOf("&") > -1) rightPart = rightPart.substring(rightPart.indexOf("&"), rightPart.length);
			newURL = leftPart + lang + rightPart;
		}
	}
	document.location = newURL;
}

// search function, redirects to search page with encoding the search field with "encodeURIComponent()"
function doSearch(lang, searchStr){
  lang = lang.toLowerCase();
  if (searchStr.length == 0) return false;
  if (lang=="eng" && searchStr.toLowerCase()=="search...") return false;
  if (lang=="deu" && searchStr.toLowerCase()=="suchen...") return false;
  if (lang=="fra" && searchStr.toLowerCase()=="rechercher...") return false;
  //searchStr = Base64.encode(searchStr);
  while(searchStr.indexOf("+")>-1){
    searchStr = searchStr.replace("+", "@AND@");
  }
  searchStr = encodeURIComponent(searchStr);
  alert(searchStr);
  var url = "http";
  var domain = document.domain;
  if (domain.indexOf("ssldatas")>-1) url = url + "s";
  url = url + "://" + domain;
  if (domain=="localhost") url = url + "/TerziusClient2";
  url = url + "/" + lang + "/intern/search.html?search="+ searchStr;
  document.location = url;
  return false;
}


//function to open a route planer (maps.goole.de)
function routePlaner(fromStreet, fromZIP, fromCity, fromCountry, toStreet, toZIP, toCity, toCountry){
	planerURL = "http://maps.google.de/maps?f=d&hl=de&geocode=";
	planerURL += "&saddr=" + fromStreet + "+" + fromZIP + "+" + fromCity + "+" + fromCountry;
	planerURL += "&daddr=" + toStreet + "+" + toZIP + "+" + toCity + "+" + toCountry;
	planer = window.open(planerURL, 'routePlaner', screen.availWidth-60, screen.availHeight-60 ,'yes');
	planer.focus();
}


//Function to get a defined number of decimals behind the comma.
//First parameter: is type of double
//Second parameter: is an integer (number of decimals to show)
function formatNumber(expr, decplaces) {
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "," + str.substring(decpoint, str.length);
	//return str.substring(0,decpoint) + (decplaces) ? "." + str.substring(decpoint,str.length) : "";
}


//Function to set inner text of a label
/*
function setInnerText(txt, txtHolder){
	if (typeof txtHolder.textContent != "undefined"){
		txtHolder.textContent = txt;
	}
	else if (typeof txtHolder.innerText != "undefined"){
		txtHolder.innerText = txt;
	}
	else if (typeof txtHolder.removeChild != "undefined"){
		while (txtHolder.hasChildNodes()){
			txtHolder.removeChild(txtHolder.lastChild);
		}
		txtHolder.appendChild(document.createTextNode(txt));
	}
}
*/

function setInnerText(inStr, txtHolder){
	/* OLD
	if (typeof txtHolder.textContent != "undefined"){
		txtHolder.textContent = txt;
	}else if (typeof txtHolder.innerText != "undefined")
		txtHolder.innerText = txt;
	}else if (typeof txtHolder.removeChild != "undefined"){
		while (txtHolder.hasChildNodes()){ txtHolder.removeChild(txtHolder.lastChild)}
		txtHolder.appendChild(document.createTextNode(txt));
	}
	*/	
	// remove existing nodes in this text holder
	while (txtHolder.hasChildNodes()){
		txtHolder.removeChild(txtHolder.lastChild);
	}
	// split input of line breaks in it.
	var splitStr = inStr.split("<br>");
	for (var i=0; i<splitStr.length; i++){
		txtHolder.appendChild(document.createTextNode(splitStr[i]));
		if (i<splitStr.length-1) txtHolder.appendChild(document.createElement("BR"));
	}
}


//Function to check email format validity
function isValidEmail(eMail) {
	if (window.RegExp) {
		reg = /^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/;
		return reg.test(eMail)
	} else {
		if (eMail.length<7)						return false;
		if (eMail.indexOf("@")<1)				return false;
		if (eMail.indexOf(".")==0)				return false;
		if (eMail.indexOf(".")==eMail.length-1)	return false;
		if (eMail.indexOf("-")==0)				return false;
		if (eMail.indexOf("-")==eMail.length-1)	return false;
		return true
	}
	return false
}


//Function2 crypt and decrypt as antispam protection.
function CryptMail(s,shift) {	//
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift)); 
	}
	return r;
}
  
// JS function for uncrypting spam-protected emails:

function SendMailTo(s,shift)	{	//
	location.href=CryptMail(s, shift);
}


// functions to get object offsets according to browser window
function getOffsetLeft(obj){
	var OL = obj.offsetLeft + 0;
	while (obj.parentNode){
		obj = obj.parentNode;
		if (isNaN(obj.offsetLeft) == false) OL += obj.offsetLeft;
	}
	return OL;
}
function getOffsetTop(obj){
	var OL = obj.offsetTop + 0;
	while (obj.parentNode){
		obj = obj.parentNode;
		if (isNaN(obj.offsetTop) == false && obj.tagName == 'TD') {alert(obj.tagName +':'+ obj.offsetTop); OL += obj.offsetTop;}
	}
	return OL;
}


// get browser window size
function getBodySize(){
	if (navigator.appName=="Netscape") {
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}
}

// JS function to add an iFrame node to page
function appendiFrame(iFrameId, src, width, height, scrolling, posLeft, posTop, posRight, posBottom){
	if (typeof posLeft != "number") posLeft = mousePosX;
	if (typeof posTop != "number") posTop = mousePosY;
	existFrame = document.getElementById(iFrameId);
	if (existFrame!=null) existFrame.parentNode.removeChild(existFrame);
	var theFrame = document.createElement("iframe");
	theFrame.setAttribute("id", iFrameId);
	theFrame.setAttribute("src", src);
	theFrame.setAttribute("frameborder", "0");
	theFrame.setAttribute("border", "0");
	theFrame.setAttribute("marginwidth", "0");
	theFrame.setAttribute("scrolling", scrolling);
	theFrame.style.width = width + "px";
	theFrame.style.height = height + "px";
	theFrame.style.border = 0;
	theFrame.style.position = "absolute";
	(typeof posRight == "number") ? theFrame.style.right = posRight : theFrame.style.left = posLeft;
	(typeof posBottom == "number") ? theFrame.style.bottom = posBottom : theFrame.style.top = posTop;
    document.body.appendChild(theFrame);
}
function removeFrame(frameId){
	var theFrame = window.parent.document.getElementById(frameId);
	theFrame.parentNode.removeChild(theFrame);
}


var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	reloadOpener:null,
	close:function(){
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	    if (this.reloadOpener){
	        document.location.reload();
	    }
	},
	open:function(){
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div class=\"modal-window\""
		modal += " id=\"" + this.windowId + "\""
		modal += " style=\""
		modal +=    "width:" + this.width + "px;"
		modal +=    "height:" + this.height + "px;"
		modal +=    "margin-top:-" + (this.height / 2) + "px;"
		modal +=    "margin-left:-" + (this.width / 2) + "px;"
		modal += "\">";
		modal += this.content;
		modal += "</div>";

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		//$(".modal-overlay").click(function(){modalWindow.close();});
	}
};

var openModal = function(modalId, width, height, reloadOpener, source){
	modalWindow.windowId = modalId;
	modalWindow.width = width;
	modalWindow.height = height;
	modalWindow.reloadOpener = reloadOpener;
	modalWindow.content = "<iframe width='"+ width +"' height='"+ height +"' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'>&lt/iframe>";
	modalWindow.open();
}
