var popup;
function vordergrund(anzahl){
document.getElementById("abdunkeln").style.visibility="visible";
opacity(0.0,"up",anzahl);
}

function info(anzahl){

document.getElementById(anzahl).style.visibility="visible";
document.getElementById(anzahl).style.height="auto";
var bild="bild_"+anzahl;
if(document.getElementById(bild)){
var height_bild=document.getElementById(bild).height;
}
else height_bild=0;
var height_rest=document.getElementById(anzahl).offsetHeight-height_bild;
    if(typeof window.innerWidth == 'undefined') {
	var obj=getBody(window);
	var height=parseInt(obj.clientHeight); 
	while((height-height_rest-height_bild)<100){
		height_bild=height_bild-1;
		document.getElementById(bild).style.maxHeight=height_bild+"px";

	}
	document.getElementById(anzahl).style.top=Math.round((height-document.getElementById(anzahl).offsetHeight)/2)+"px";
}

else{
	while(window.innerHeight-height_rest-height_bild<100){
		height_bild=height_bild-1;
		document.getElementById(bild).style.maxHeight=height_bild+"px";
	}
	document.getElementById(anzahl).style.top=Math.round((window.innerHeight-document.getElementById(anzahl).offsetHeight)/2)+"px";
}
if(document.getElementById("masse_"+anzahl)){
	einblenden(anzahl,true);
}
}

function getBody(w)
{
    return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ? w.document.documentElement : w.document.body || null;
}  

function zuruck(){
	var i;
	clearTimeout(popup);
	
	for(i=0;i<document.getElementsByTagName('div').length;i++){
	if(document.getElementsByTagName('div')[i].className=="popup"){
		document.getElementsByTagName('div')[i].style.height="0px";
		document.getElementsByTagName('div')[i].style.visibility="hidden";
	}
	}
	opacity(0.7,"down");
}

function opacity(wert,richtung,anzahl){
	if(richtung=="down"){
		if(wert>0){
			var neu=Math.round((wert-0.1)*10)/10;
			document.getElementById("abdunkeln").style.opacity=neu;
			document.getElementById("abdunkeln").style.filter = "alpha(opacity:"+neu*100+")";
			document.getElementById("abdunkeln").style.KHTMLopacity = neu;
			popup=window.setTimeout("opacity("+neu+",'"+richtung+"','"+anzahl+"')",50);
		}
		else{
			document.getElementById("abdunkeln").style.visibility="hidden";
		}
	}
	else{
		if(wert<0.7){
			var neu=wert+0.1;
			document.getElementById("abdunkeln").style.opacity=neu;
			document.getElementById("abdunkeln").style.filter = "alpha(opacity:"+neu*100+")";
			document.getElementById("abdunkeln").style.KHTMLopacity = neu;
			popup=window.setTimeout("opacity("+neu+",'"+richtung+"','"+anzahl+"')",50);
		}
		else info(anzahl);
	}
}

function hide(){
if(document.getElementById("gebiet_select")){
document.getElementById("gebiet_select").length=0;
document.getElementById("gebiet_select").style.visibility="hidden";
document.getElementById("gebiet_select").style.width=0;

document.getElementById("gebiet_label").style.visibility="hidden";
document.getElementById("gebiet_label").text="";
}
}

function formular(id){
if(id=="haendler_form"){
document.getElementById("name").value="";;
document.getElementById("ort").value="";
document.getElementById("plz").value="";
}
document.getElementById(id).submit();
}

function new_search(){
	for(i=0;i<document.getElementsByTagName('select').length;i++){
		document.getElementsByTagName('select')[i].selectedIndex=0;
	}	
}

function einblenden(id, onPopup){
	if(document.getElementById("masse_"+id).style.display=="none"){
		if(!onPopup){
			document.getElementById("masse_"+id).style.display="inline";
			document.getElementById("einblenden_"+id).innerHTML="Maße ausblenden";
		}
	}
	else{
		document.getElementById("masse_"+id).style.display="none";
		document.getElementById("einblenden_"+id).innerHTML="Maße einblenden";
	}
}

function getPosition(arrayName,arrayItem)
{
    for(var i=0;i<arrayName.length;i++){ 
       if(arrayName[i]==arrayItem)
            return i;
    }
}

function galerie_load_image(imagesrc){
	document.getElementById("galerie_bild_aktuell").src=imagesrc;
}
var aktiv;
function galerie_slideshow_play(max, bilder){
	var aktuell=document.getElementById("galerie_bild_aktuell").src.split("/");
	var i=getPosition(bilder, aktuell[4])+1;
	var filesrc="./files/";
	var time=2500;
	aktiv=new Array(max);
	for(i;i<max;i++){
	var file=filesrc+bilder[i];
	aktiv[i]=window.setTimeout("galerie_load_image('"+file+"');",time);
	time+=2500;
	}
}

function galerie_slideshow_stop(){
	for(var i=0;i<aktiv.length;i++){
		window.clearTimeout(aktiv[i]);
	}
}

function disable_input(id){
	if(document.getElementById(id).disabled!=false){
		document.getElementById(id).disabled=false;
	}
	else{
		document.getElementById(id).disabled=true;
		document.getElementById(id).value='';
	}
}

function change_visibility(id,visibility){
	if(visibility=="visible"){
		document.getElementById(id).style.display="inline";
		document.getElementById(id).style.visibility="visible";
	}
	else{
		if(visibility==""){
			if(document.getElementById(id).style.visibility=="hidden"){
				document.getElementById(id).style.display="inline";
				document.getElementById(id).style.visibility="visible";
			}
			else{
				document.getElementById(id).style.visibility="hidden";
				document.getElementById(id).style.display="none";
			}
		}
		else{
			document.getElementById(id).style.visibility="hidden";
			document.getElementById(id).style.display="none";
		}
	}

}

  function is_string(input){
    return typeof(input)=='string';
  }


function expand(id){
	if(is_string(id)){
		if(document.getElementById(id).className!='expanded'){
			document.getElementById(id).className='expanded';
		}
		else{
			document.getElementById(id).className='expandable';
		}
	}
	else{
		var expanded=1;
		for(var i=0;i<id.length;i++){
			if(document.getElementById(id[i]).className=='expandable')expanded=0;
			document.getElementById(id[i]).className='expanded';
		}
		if(expanded){
			for(var i=0;i<id.length;i++){
				document.getElementById(id[i]).className='expandable';
			}
		}
	}
}

function neueZeile(id,anzahl){
	var table=document.getElementById(id);
	var anzahl_rows=table.rows.length-1;
	for(var i=0;i<anzahl;i++){
		var row=table.insertRow(-1);
		var cell=row.insertCell(-1);
		cell.innerHTML='<input type="text" name="serie_'+(anzahl_rows+i)+'" size="5" onchange="neueZeile(\'bestell_form_table\',1); neuePinselAnzahl(\'bestell_form_anzahl\',1);fillForm(\''+(anzahl_rows+i)+'\');" />';
		var cell=row.insertCell(-1);
		cell.innerHTML='<input type="text" name="groesse_'+(anzahl_rows+i)+'" size="3" onchange="neueZeile(\'bestell_form_table\',1); neuePinselAnzahl(\'bestell_form_anzahl\',1);fillForm(\''+(anzahl_rows+i)+'\');" />';
		var cell=row.insertCell(-1);
		cell.setAttribute('id','bestell_form_kurzbeschreibung_'+(anzahl_rows+i));
		var cell=row.insertCell(-1);
		cell.setAttribute('id','bestell_form_epreis_'+(anzahl_rows+i));
		var cell=row.insertCell(-1);
		cell.innerHTML='<input type="text" name="anzahl_'+(anzahl_rows+i)+'" size="3" onchange="neueZeile(\'bestell_form_table\',1); neuePinselAnzahl(\'bestell_form_anzahl\',1);berechnePreis(\''+(anzahl_rows+i)+'\');" />';
		var cell=row.insertCell(-1);
		cell.setAttribute('id','bestell_form_preis_'+(anzahl_rows+i));
		if(document.getElementById('bestell_form_rechland').value.length==0){
			var cell=row.insertCell(-1);
			cell.setAttribute('id','bestell_form_preismwst_'+(anzahl_rows+i));
		}
		table.rows[anzahl_rows+i].cells[0].childNodes[0].setAttribute('onchange','fillForm('+(anzahl_rows+i-1)+')');
		table.rows[anzahl_rows+i].cells[1].childNodes[0].setAttribute('onchange','fillForm('+(anzahl_rows+i-1)+')');
		table.rows[anzahl_rows+i].cells[4].childNodes[0].setAttribute('onchange','berechnePreis('+(anzahl_rows+i-1)+')');
	}

}

function neuePinselAnzahl(id,add){
	document.getElementById(id).value=parseInt(document.getElementById(id).value)+add;
}

function checkForm(id){
	var form=document.getElementById(id);
	var table=form.childNodes[1];
	var anzahl_rows=table.rows.length-2;
	for(var i=0;i<anzahl_rows;i++){
		var serie=document.getElementsByName('serie_'+i)[0].value;
		var groesse=document.getElementsByName('groesse_'+i)[0].value;
		var anzahl=document.getElementsByName('anzahl_'+i)[0].value;
		if(!(serie.length==0&&groesse.length==0&&anzahl==0)||anzahl_rows==1){
			if(isNaN(parseInt(anzahl)))return false;
			if((serie.length==0&&groesse.length==0&&anzahl.length>0)||(serie.length==0&&groesse.length>0&&anzahl.length==0)||(serie.length>0&&groesse.length==0&&anzahl.length==0)||(serie.length>0&&groesse.length>0&&anzahl.length==0)||(serie.length==0&&groesse.length>0&&anzahl.length>0)){
				return false;
			}
		}
	}
	return true;
}

function fillForm(nr,isSecond){
	if(isSecond===undefined||isSecond.isNaN){
		isSecond=0;
	}
	reset(nr);
	var serie=document.getElementsByName('serie_'+nr)[0].value;
	var groesse=document.getElementsByName('groesse_'+nr)[0].value;
	var bereich=document.getElementById('bestell_form_bereich').value;
	if(serie.length>0){
		var http;
		var text;
		if (window.XMLHttpRequest) {
		   http = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		   http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (http != null) {
			http.open("GET", "/deutsch/login/kosmetikpinsel/kundenportal/ajax.html?act=getText&serie="+serie+"&groesse="+groesse+"&bereich="+bereich, true);
			http.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
			http.onreadystatechange = function(){
			if (http.readyState == 4){
				text=http.responseText.split('|');
				
				if(text[0].length>0 && !isNaN(parseFloat(text[1]))){
					document.getElementById('bestell_form_kurzbeschreibung_'+nr).innerHTML=text[0];
					document.getElementById('bestell_form_epreis_'+nr).innerHTML=parseFloat(text[1]).toFixed(2).replace(".", ",");
				}
				berechnePreis(nr);
			}
		   };
		   http.send(null);
		}
		if(!(typeof(text)=='object'&&(text instanceof Array))&&isSecond==0){
			getGroessenBox(nr,serie,bereich);
		}

	}
}

function getGroessenBox(nr,serie,bereich){
	var http;
		if (window.XMLHttpRequest) {
		   http = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		   http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (http != null) {			
			http.open("GET", "/deutsch/login/kosmetikpinsel/kundenportal/ajax.html?act=getGroessen&serie="+serie+"&bereich="+bereich, true);
			http.onreadystatechange = function(){
			if (http.readyState == 4){
				var text=http.responseText.split('|');
				if(text.length>0&&text.length!=1 && text[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '')!='0'){
					var htmlText='<ul>';
					for(var i=0;i<text.length;i++){
						htmlText+='<li><a onclick="setGroesse('+nr+',\''+text[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '')+'\')">'+text[i]+'</a></li>';
					}
					htmlText+='</ul>';
					var box=document.getElementById('ajaxSelectGroesse');
					var groesse=document.getElementsByName('groesse_'+nr)[0];
					var pos=getPosition(groesse);
					pos['y']+=groesse.offsetHeight;
					box.style.position='absolute';
					box.style.top=pos['y']+'px';
					box.style.left=pos['x']+'px';
					box.style.zIndex=3;
					box.style.backgroundColor='white';
					box.style.border='1px solid black';
					box.style.display='inline';
					box.style.visibility='visible';
					box.innerHTML='<p>'+htmlText+'</p>';
				}
				else{
					if(text.length>0 && text[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '')!='0'){
						setGroesse(nr,text[0]);
					}
				}
			}
		   };
		   http.send(null);
		}		
}

function setGroesse(nr,text){
	document.getElementsByName('groesse_'+nr)[0].value=text;
	fillForm(nr,1);
	change_visibility('ajaxSelectGroesse');
	return false;
}

function getPosition(obj) {
  var pos = { x:0, y:0 };

  do {
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
  } while (obj = obj.offsetParent);

  return pos;
}

function berechnePreis(nr){
	var anzahl=document.getElementsByName('anzahl_'+nr)[0].value;
	
	if(!isNaN(parseFloat(document.getElementById('bestell_form_epreis_'+nr).innerHTML))&&anzahl>0&&document.getElementById('bestell_form_epreis_'+nr).innerHTML.length>0){
		document.getElementById('bestell_form_preis_'+nr).innerHTML=(Math.round(parseFloat(document.getElementById('bestell_form_epreis_'+nr).innerHTML.replace(",", "."))*parseInt(anzahl)*100)/100).toFixed(2).replace(".", ",");
		if(document.getElementById('bestell_form_rechland').value.length==0){
			document.getElementById('bestell_form_preismwst_'+nr).innerHTML=(Math.round(parseFloat(document.getElementById('bestell_form_epreis_'+nr).innerHTML.replace(",", "."))*parseInt(anzahl)*1.19*100)/100).toFixed(2).replace(".", ",");
		}
	}
}

function reset(nr){
	document.getElementById('bestell_form_kurzbeschreibung_'+nr).innerHTML='';
	document.getElementById('bestell_form_epreis_'+nr).innerHTML='';
	document.getElementById('bestell_form_preis_'+nr).innerHTML='';
	if(document.getElementById('bestell_form_rechland').value.length==0){
		document.getElementById('bestell_form_preismwst_'+nr).innerHTML='';
	}
}



