var areaKeys = new Array("","adv","ecm","crm","ers","eri","web","eco","bis","pra","dia","eva","dsa","con","mda","isp","oms","chr","pma");
var dirtyFlag = false; // sets true when user changes form input values in any way
var isIE = navigator.appName=='Microsoft Internet Explorer';

function browserCheck() {
	if (isIE && (navigator.appVersion.indexOf('MSIE 5.0')>=0 || navigator.appVersion.indexOf('MSIE 4')>=0 
		|| navigator.appVersion.indexOf('MSIE 3')>=0)) {
		document.location.href='/browser.jsp';
	}
}

/*
 * init url for ajax requests depending on which system we're on:
 * local, dev, live
 */
var serverUrl = null;	 
function initServerUrl() {
	if (serverUrl==null) {
		
		if (document.location.href.indexOf('localhost')>=0) {
			serverUrl = 'http://localhost:8080/com.benchpark.www/';
		} else if (document.location.href.indexOf('macmini')>=0) {
			serverUrl = 'http://macmini:8080/com.benchpark.www/';			
		} else if (document.location.href.indexOf('bpkdev.com')>=0) {
			serverUrl = 'http://www.bpkdev.com:8080/';			
		} else if (document.location.href.indexOf('www.benchpark.de')>=0) {
			serverUrl = 'http://www.benchpark.de/';	
		} else if (document.location.href.indexOf('www.benchpark.at')>=0) {
			serverUrl = 'http://www.benchpark.at/';								
		} else if (document.location.href.indexOf('www.benchpark.ch')>=0) {
			serverUrl = 'http://www.benchpark.ch/';								
		} else {		
			serverUrl = 'http://www.benchpark.com/';		
		}
	}
}

var MAX_MESSAGE_LENGTH = 2000;
if (navigator.appName=='Microsoft Internet Explorer') MAX_MESSAGE_LENGTH = 1500;
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function today() {
  var mydate= new Date()
  var theyear=mydate.getFullYear();
  var themonth=mydate.getMonth()+1;
  if (themonth<10) themonth = '0'+themonth;
  var thetoday=mydate.getDate();
  if (thetoday<10) thetoday = '0'+thetoday;
  return (thetoday + "." + themonth + "."+ theyear );

}

function setDirty() {
	dirtyFlag = true;
}
function checkDirty() {
	if (dirtyFlag) {
		//showConfirmation("Möchten Sie diese Seite wirklich verlassen, ohne Ihre Angaben auf dieser Seite zu übernehmen?");
		return confirm("Möchten Sie diese Seite wirklich verlassen, ohne Ihre Angaben auf dieser Seite zu übernehmen?");
	}
}

function hide(obj) {  
	if (obj!=null) {
	   var c = obj.className;
	   c = replaceAll(c,'showstyle','');
	   c = replaceAll(c,'hidestyle','');
	   obj.className = c + " hidestyle";	   
	}
}
function show(obj) {    
	if (obj!=null) {        
	   var c = obj.className;
	   c = replaceAll (c,'hidestyle','');
	   c = replaceAll (c,'showstyle','');
  	   obj.className = c + " showstyle";
	}
}

function showNew(obj) {  
  if (obj!=null) {        
	   var c = obj.className;
	   c = replaceAll (c,'hidestyle','');
	   c = replaceAll (c,'showstyle','');
  	   obj.className = c + " showstyle";
	}
}

function showHide(obj) {
	if (obj.className.indexOf('hidestyle')>=0) {
		show(obj);
	} else {
		hide(obj);
	}
}

function checkLoginLayer(tab, tab1) {
	if (tab == "login"){
		document.getElementById("loginitem").className = "itemActive";
		document.getElementById("hinweisitem").className = "item";
	}else{
		document.getElementById("loginitem").className = "item";
		document.getElementById("hinweisitem").className = "itemActive";
	}
    show(document.getElementById(tab));
    hide (document.getElementById(tab1));
}

function enc(text) {  
  var s = '';for (i=0;i<=text.length-1;i++) {s = s + String.fromCharCode(text.charCodeAt(i)+10);}; return s;
}

function dec(text) {
  var s = '';for (i=0;i<=text.length-1;i++) {s = s + String.fromCharCode(text.charCodeAt(i)-10);};return s;
}

function maxlength(s,max) {
  if (s.length>max) {
  	s = s.substring(0,max) + '..';
  }
  return s;
}

// Favorite object definition
function Favorite() {
}

function replaceAll(s, regExp, replacement) {
	while (s.indexOf(regExp)>=0) s = s.replace(regExp,replacement);
	return s;
}

// Trim (cut off trailing spaces left and right)
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function checkCellBackground(cellId, bgColor) {
	if (document.getElementById('tab' + cellId).className.indexOf('hidestyle')>=0) {		
		document.getElementById('cell' + cellId).style.background = "";
	} else {
		document.getElementById('cell' + cellId).style.background = bgColor;
	}
}

function switchVisibility(tabId) {
	if (document.getElementById('tab' + tabId).className.indexOf('hidestyle')>=0) {
		show(document.getElementById('tab' + tabId));
		if (document.getElementById('switchTab' + tabId))document.getElementById('switchTab' + tabId).src = "../../../img/schliessen.gif";
	} else {
		hide(document.getElementById('tab' + tabId));
		if (document.getElementById('switchTab' + tabId))document.getElementById('switchTab' + tabId).src = "../../../img/oeffnen.gif";
	}
}

function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
    }

function setCheckedValue(radioObj, newValue) {
		if(!radioObj)
			return;
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++) {
			radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
				radioObj[i].checked = true;
			}
		}
}

function googleUmlautsHandling(q) {
	while (q.indexOf("ä")>=0) q = q.replace("ä","ae"); 
	while (q.indexOf("ö")>=0) q = q.replace("ö","oe"); 
	while (q.indexOf("ü")>=0) q = q.replace("ü","ue"); 
	while (q.indexOf("ß")>=0) q = q.replace("ß","ss"); 
	while (q.indexOf("Ä")>=0) q = q.replace("Ä","Ae"); 
	while (q.indexOf("Ö")>=0) q = q.replace("Ö","Oe"); 
	while (q.indexOf("Ü")>=0) q = q.replace("Ü","Ue");
	return q;
}

function encodeUmlauts(s) {
	while (s.indexOf(" ")>=0) {s = s.replace(" ","%20");}
	while (s.indexOf("ä")>=0) {s = s.replace("ä","%E4");}
	while (s.indexOf("ö")>=0) {s = s.replace("ö","%F6");}
	while (s.indexOf("ü")>=0) {s = s.replace("ü","%FC");}
	while (s.indexOf("Ü")>=0) {s = s.replace("Ü","%DC");}
	while (s.indexOf("Ö")>=0) {s = s.replace("Ö","%D6");}
	while (s.indexOf("Ä")>=0) {s = s.replace("Ä","%C4");}	
	while (s.indexOf("ß")>=0) {s = s.replace("ß","%DF");}
	return s;
}

function addToFavorites(){    
    var windowLocation = window.location.toString();
	if (windowLocation.indexOf("jsession")>=0) {
  	  var windowLocationPart1 = windowLocation.substring(0,windowLocation.indexOf("jsession")-1);
      var windowLocationPart2 = windowLocation.indexOf("?")>=0?windowLocation.substring(windowLocation.indexOf("?"),windowLocation.length):"";
	  windowLocation = windowLocationPart1 + windowLocationPart2;
 	  //while (windowLocation.indexOf("&")>=0) {windowLocation = windowLocation.replace("&","%26");};
    }
	
	if (document.all) {
		window.external.AddFavorite(windowLocation, document.title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(document.title, windowLocation, "");
	} else {
		alert("Sorry! Ihr Browser unterst"+unescape('%D5')+"tzt diese Funktion nicht."); 
	}
}

function getCurrentWindowUrl() {
  var url = window.location.toString();
  if (url.indexOf("jsession")>=0) {
  	var windowLocationPart1 = url.substring(0,url.indexOf("jsession")-1);
  	var windowLocationPart2 = url.indexOf("?")>=0?url.substring(url.indexOf("?"),url.length):"";
	url = windowLocationPart1 + windowLocationPart2;
	while (url.indexOf("&")>=0) {url = url.replace("&","%26");};
  }  
  //url = url.replace("e","a")
  return url;
}
function getCurrentWindowTitle() {
  var documentTitle = document.title;
  return documentTitle;
}
function getCurrentWindowTitleEncoded() {
  var documentTitle = encodeUmlauts(document.title);  
  return documentTitle;
}

function recommend(documentTitle) {
  //var documentTitle = encodeUmlauts(document.title);  
  var windowLocation = getCurrentWindowUrl();  
  document.location.href = "mailto:?subject=Empfehlung%20einer%20interessanten%20Website: " + documentTitle + "&body=Folgende%20Seite%20kann%20ich%20empfehlen: \n" + windowLocation;
}



//Custom js confirm box
var newMask;
var newDiv;

function showConfirmation(msg){
 newMask = document.createElement("maskdiv");
 newMask.style.position = "absolute";
 newMask.style.zIndex = "0";
 newMask.style.width = document.body.scrollWidth + "px";
 newMask.style.height = document.body.scrollHeight + "px";
 newMask.style.top = "0px";
 newMask.style.left = "0px";
 newMask.style.background = "#000000"; 
 setOpacity(newMask, 50);
 newMask.margin = "center";
 document.body.appendChild(newMask);

 newDiv = document.createElement("messagediv")
 newDiv.style.position = "absolute";
 newDiv.style.zIndex = "9999";
 newDiv.style.width = "300px"; 
 newDiv.style.top = "40%";
 newDiv.style.left = "40%"
 newDiv.style.background = "#ffffff";
 newDiv.style.border = "2px solid #336633";
 newDiv.style.padding = "1px";
 newDiv.margin = "center"; 
 newDiv.innerHTML = "<table cellspacing='2' cellpadding='1' width='100%' border='0'><tbody><tr><td width='20'></td><td><font size='2'>" + msg + "<br/></font></td></tr><tr><td colspan='2'><a href='javascript:hideConfirmation();'>cancel</a></td></tr></tbody></table>";
 document.body.appendChild(newDiv);
}

function hideConfirmation(){    
    if(newMask) document.body.removeChild(newMask);
    if(newDiv) document.body.removeChild(newDiv);
}

function setOpacity(obj, opacity) {           
	  opacity = (opacity == 100)?99.999:opacity;
	  
	  // IE/Win           
	  obj.style.filter="alpha(opacity:"+opacity+")";
	  
	  // Safari<1.2, Konqueror
	  obj.style.KHTMLOpacity = opacity/100;
	  
	  // Older Mozilla and Firefox
	  obj.style.MozOpacity = opacity/100;
	  
	  // Safari 1.2, newer Firefox and Mozilla, CSS3
	  obj.style.opacity = opacity/100;
}

var currIndex=100000001;
var currObj=null;	
function changeZIndex(obj){
	if (navigator.appName=='Microsoft Internet Explorer') {
		if (currObj && currObj!=null){
			currObj.style.setAttribute("z-index", 0, false);
			currObj.style.setAttribute("zIndex", 0, false);
	   }
	   currIndex++;
	   obj.style.setAttribute("z-index", currIndex, false);
	   obj.style.setAttribute("zIndex", currIndex, false);
	   currObj = obj;
    } else {
    	if (currObj && currObj!=null){
			currObj.style.zIndex=0;
    	}
    	currIndex++;
    	obj.style.zIndex=currIndex;
    	currObj = obj;
    }
	   
}
