var ns = document.layers ? true : false;
var ie = document.all && !window.opera ? true : false;;    // IEs
var dom = document.getElementById && !ie ? true : false;  // DOM-Browser

var gbloading = "<img src='images/loading.gif' hspace='20' vspace='20' /><br />";

function swapNavi(id,dir) {
	if(dir == "over") {
		if($('navi_'+id+'_normal')) $('navi_'+id+'_normal').hide();
		if($('navi_'+id+'_highlight')) $('navi_'+id+'_highlight').show();
	} else {		
		if($('navi_'+id+'_highlight')) $('navi_'+id+'_highlight').hide();
		if($('navi_'+id+'_normal')) $('navi_'+id+'_normal').show();
	}
}

function swapProduktButton(id,dir) {
	if(dir == "over") {
		if($('produktbutton_'+id+'_normal')) $('produktbutton_'+id+'_normal').hide();
		if($('produktbutton_'+id+'_highlight')) $('produktbutton_'+id+'_highlight').show();
	} else {		
		if($('produktbutton_'+id+'_highlight')) $('produktbutton_'+id+'_highlight').hide();
		if($('produktbutton_'+id+'_normal')) $('produktbutton_'+id+'_normal').show();
	}
}


function sizeToSession(breite,hoehe) {
	new Ajax.Request('ajax/sizeToSession.php?breite='+breite+'&hoehe='+hoehe, {
		method: 'get',	
		onSuccess: function(t) {
			//$('modulecontent').update(t.responseText);
		},
		onFailure: function(t) {
			//$('modulecontent').update('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
}

function loading(){
	if($('scroll_content') && $('loading_div')){
	Element.clonePosition($('loading_div'),$('scroll_content'));
	$('loading_div').show();
	//alert($('loading_div').getHeight());
	}
}
function unloading(){
	if($('loading_div')) $('loading_div').hide();	
}

function escapeTxt(os){
var ns='';
var t;
var chr='';
var cc='';
var tn='';
for(i=0;i<256;i++){
tn=i.toString(16);
if(tn.length<2)tn="0"+tn;
cc+=tn;
chr+=unescape('%'+tn);
}
cc=cc.toUpperCase();
os.replace(String.fromCharCode(13)+'',"%13");
for(q=0;q<os.length;q++){
t=os.substr(q,1);
for(i=0;i<chr.length;i++){
if(t==chr.substr(i,1)){
t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
i=chr.length;
}}
ns+=t;
}
return ns;
}

function request(method,url,ziel,postform,loadingani,newserialize){
	if($(loadingani)) $(loadingani).show();
	else loading(); //$(ziel).update("<img src='images/loading_sitemap.gif' />");
	//alert(loadingani);
	if(method=='post' || method=='POST'){
		//alert(postform);
		if(newserialize) var pbody=$(postform).serialize(true);
		var pbody=$(postform).serialize();
	}else var pbody="";
	new Ajax.Request(url, {
		method: method,
		postBody: pbody,
		onSuccess: function(t) {
			$(ziel).update(t.responseText);
			if($(loadingani)) $(loadingani).hide();
			else unloading();
		},
		onFailure: function(t) {
			$(ziel).update('Error ' + t.status + ' -- ' + t.statusText);
			if($(loadingani)) $(loadingani).hide();
			else unloading();
		},
		evalScript: true,
		asynchronous: true
		}
	);
}

function doAjax(method,url,ziel,postform,loadingani,newserialize){
	
	request(method,url,ziel,postform,loadingani,newserialize);

//alert("request('"+method+"','"+url+"','"+ziel+"','"+postform+"',"+newserialize+")");
	/*
	var ajaxString=escapeTxt("request('"+method+"','"+url+"','"+ziel+"','"+postform+"','"+loadingani+"',"+newserialize+")");
	if($('hiddenFrame')){
		$('hiddenFrame').src='history.php?request='+ajaxString;
		//alert("OK");
	}
	*/
/*
	if(method=='post' || method=='POST'){
		if(newserialize) var pbody=postform.serialize(true);
		var pbody=postform.serialize();
	}else var pbody="";
	new Ajax.Request(url, {
		method: method,
		postBody: pbody,
		onSuccess: function(t) {
			$(ziel).update(t.responseText);
		},
		onFailure: function(t) {
			$(ziel).update('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
	*/
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight =  yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function openLanguageLayer() {
	showLanguage();
}

var languageAktiv = false;
var languageTimeout;

function showLanguage() {	
	if(languageAktiv == false) {
		window.clearTimeout(languageTimeout);
		$('language_div').show();
		Effect.toggle('language_inner','slide', {duration: 0.5});
		languageTimeout=window.setTimeout("language_inner = true;",500);
	} else {
		window.clearTimeout(languageTimeout);
		Effect.toggle('language_inner','slide', {duration: 0.5, afterFinish: language_inner});
		languageTimeout=window.setTimeout("language_inner = false;",500);
	}
}

function hideLanguage() {
	$('language_div').hide();
}

function openSucheLayer() {
	showSuche();
}

function searchSite(offset) {
	new Ajax.Request('ajax/searchToSession.php?search='+Base64.encode($('search').value),
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   //loadModule(10, '&offset='+offset);
			if(offset == -1) {
				Effect.toggle('suche_inner','slide', {duration: 0.5, afterFinish: hideSuche});
				sucheAktiv = false;
			}
			location.href = "?kat=15&offset="+offset;
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);	
	
}

var sucheAktiv = false;
var sucheTimeout;

function showSuche() {	
	if(sucheAktiv == false) {
		window.clearTimeout(sucheTimeout);
		$('suche_layer').show();
		Effect.toggle('suche_inner','slide', {duration: 0.5});
		sucheTimeout=window.setTimeout("sucheAktiv = true;",500);
	} else {
		window.clearTimeout(sucheTimeout);
		Effect.toggle('suche_inner','slide', {duration: 0.5, afterFinish: hideSuche});
		sucheTimeout=window.setTimeout("sucheAktiv = false;",500);
	}
}

function hideSuche() {
	$('suche_layer').hide();
}


function konfiguratorGetContent(step,val) {
	//h = $('step_content_'+step).getHeight();
	$('step_content_'+step).innerHTML = "<table border='0' cellspacing='0' cellpadding='0' width='100%'><tr><td align='center' valign='middle'><img src='/images/loading_konfigurator.gif' /></td></tr></table>";
	new Ajax.Request('/ajax/konfigurator.php?step='+step+'&val='+val, {
		method: 'get',
		asynchronous: true,
		onComplete: function(t) {	
		   $('step_content_'+step).update(t.responseText);
		   $('step_header_'+step).className = 'konfigurator';
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);	
}

function displayImg(img,title) {
	gbid = showGrayBox();
	
	new Ajax.Request('/ajax/displayImage.php?img='+img,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   	$('gb_title_'+gbid).innerHTML = title;
			$('gb_text_'+gbid).update(t.responseText);
		   	center('gb_'+gbid);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function rmMM(val) {
	val = val.value.replace(" mm", "");
	val = val.replace("mm", "");
	return val;
}

function rmMM2(val) {
	val = val.replace(" mm", "");
	val = val.replace("mm", "");
	return val;
}

function addMM(val) {
	val = rmMM(val);
	return val+" mm";
}

function checkDurchmesserLaenge(val) {
	vali = rmMM(val);
	vali = Math.abs(vali);	
	if(vali > 40) {
		vali = 40;
		$('error_bubble_msg').innerHTML = "Der Durchmesser darf<br>maximal 40 mm betragen.";
		pos = $(val).positionedOffset();
		$('error_bubble').setStyle({left: pos[0]+'px', top: (pos[1]+20)+'px'});
		$('error_bubble').show();
		hideSelect();
	}
	
	if(vali < 10) {
		vali = 10;
		$('error_bubble_msg').innerHTML = "Der Durchmesser muss<br>mindestens 10 mm betragen.";
		pos = $(val).positionedOffset();
		$('error_bubble').setStyle({left: pos[0]+'px', top: (pos[1]+20)+'px'});
		$('error_bubble').show();
		hideSelect();
	}
	return vali;
}

function checkLaenge(current,min,max) {
	val = rmMM(current);
	
	error = false;
	if(val < min) {
		val = min;
		error = true;
	}
	if(val > max) {
		val = max;
		error = true;
	}
	
	if(error == true) {
		$('error_bubble_msg').innerHTML = "Der Wert darf nur zwischen<br>"+min+" mm und "+max+" mm sein.";
		pos = $(current).positionedOffset();
		$('error_bubble').setStyle({left: pos[0]+'px', top: (pos[1]+20)+'px'});
		$('error_bubble').show();
		hideSelect();
	}
	
	return val;
}

function konfiguratorSaveVal(typ,val) {
	new Ajax.Request('/ajax/konfiguratorSaveVal.php?typ='+typ+'&val='+val,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
			$('konfigurator_response').update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function updateKonfiguratorPreis() {
	laenge = rmMM2($('laengen').value)/1000;
	menge = $('menge').value;
	
	$('konfigurator_preis').innerHTML = number_format(laenge*menge*30,2,',','\.')+" &euro;";
}

function roundIt(val,prec) {
	base = 10;
	for(i = 0; i <= prec;i++) base*=10;	
	val = Math.round(val*base);
	
	val = (val/base);
	return val;
}

function number_format (number, decimals, dec_point, thousands_sep) {
	  var exponent = "";
	  var numberstr = number.toString ();
	  var eindex = numberstr.indexOf ("e");
	  if (eindex > -1)
	  {
		exponent = numberstr.substring (eindex);
		number = parseFloat (numberstr.substring (0, eindex));
	  }
	  
	  if (decimals != null)
	  {
		var temp = Math.pow (10, decimals);
		number = Math.round (number * temp) / temp;
	  }
	  var sign = number < 0 ? "-" : "";
	  var integer = (number > 0 ? 
		  Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	  
	  var fractional = number.toString ().substring (integer.length + sign.length);
	  dec_point = dec_point != null ? dec_point : ".";
	  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
				   (dec_point + fractional.substring (1)) : "";
	  if (decimals != null && decimals > 0)
	  {
		for (i = fractional.length - 1, z = decimals; i < z; ++i)
		  fractional += "0";
	  }
	  
	  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
					  thousands_sep : null;
	  if (thousands_sep != null && thousands_sep != "")
	  {
		for (i = integer.length - 3; i > 0; i -= 3)
		  integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	  }
	  
	  return sign + integer + fractional + exponent;
}

  ////////////////////////////////////////////////////////////////
 // POPUP FUNKTION											   //
////////////////////////////////////////////////////////////////

Xoffset=10;   // modify these values to ...
Yoffset=10;    // change the popup position.
var x = 0;
var y = 0;
document.onmousemove=get_mouse;
	
function popup(text){
	if(text != '') {
		var content= "<table border='0' cellspacing='0' cellpadding='2'><tr><td>"+text+"</td></tr></table>";
		$('hoverdiv').innerHTML=content;
		$('hoverdiv').style.left=(x+Xoffset)+"px";
		$('hoverdiv').style.top=(y+Yoffset)+"px";
		$('hoverdiv').show();
	}
}

function sitemapPopup(url){
	$('hoverdiv').show();
	$('hoverdiv').innerHTML="<table border='0' cellspacing='0' cellpadding='2'><tr><td><img src='images/loading_small2.gif' /></td></tr></table>";
	
	$('hoverdiv').style.left=(x+Xoffset)+"px";
	$('hoverdiv').style.top=(y+Yoffset)+"px";
	
	new Ajax.Request('ajax/sitemap.php?url='+url,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('ausgabe').update("<table border='0' cellspacing='0' cellpadding='2' width='300'><tr><td style='width: 300px;font-size: 11px;'>"+t.responseText+"</td></tr></table>");
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}
	
function get_mouse(e){
	if(!e) e = window.event;
	x = e.clientX+document.documentElement.scrollLeft;
	//	if (document.documentElement.scrollTop!=0) alert(document.documentElement.scrollTop );
    y = e.clientY+document.documentElement.scrollTop ;
}
function kill(){
	$('hoverdiv').hide();	
}

/*
  ////////////////////////////////////////////////////////////////
 // Login													   //
////////////////////////////////////////////////////////////////

function doLogin(e){
	$("login_button").value = "Verarbeite...";
	
	new Ajax.Request('ajax/checkLogin.php', {
		method: 'post',
		postBody: $("login_form").serialize(),
		onSuccess: function(t) {
			$("login_button").value = t.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
}
*/