function add_ersatz_to_cart() {
	if (document.forms['replacementform'] && document.forms['replacementform'].elements['article_act_count']) {
			document.forms['replacementform'].elements['article_act_count'].value=1;
			window.setTimeout("document.forms['replacementform'].submit();", 100);
	}
}


function add_to_cart() {
  var c = 1*document.forms['frm_fuss_detail'].elements["article_act_count"].value;
  if (!c || c<1 || c>99) {
    alert(lang['gueltige_zahl']);
    return false;
  } else {
      if (document.forms['konfiguration'] && document.forms['konfiguration'].elements['article_act_count']) {
      	  var hdderror = false;
      	  var hdderror_incat = 0;
		  for(var cat = maxhdds_array.length-1; cat >= 0; cat--){
		  	if(hddcount_array[cat] > maxhdds_array[cat]){
		  		hdderror = true;
		  		hdderror_incat = cat;
		  	}
		  }
      	  if(hdderror){
      	  	document.location.hash = "hddkategorie_"+hdderror_incat;
      	  }else{
          	document.forms['konfiguration'].elements['article_act_count'].value=c;
          	window.setTimeout("document.forms['konfiguration'].submit();", 100);
          }
      }
  }
}


function euro(e, mit_euro) {
 var r = Math.round(e*100).toString();
 r = r.substr(0, r.length-2) + lang['nachkomma'] + r.substr(r.length-2, r.length);
 if (r.substr(0, 1) != "-" && r.length > 6)
   r = r.substr(0, r.length-6) + lang['tausender'] + r.substring(r.length-6, r.length);
 if (r == lang['nachkomma']+"0")
   r = "0"+lang['nachkomma']+"00";
 if (mit_euro)
   r = r + " €";
 return (r);
}


function setFussListe(bcount, bprice, bshipping, land) {
  if(bcount!=null) $("article_sum_count").firstChild.data = bcount;
  $("article_sum_price").firstChild.data = euro(bprice);
  $("warenkorb_gesamt").firstChild.data = euro(bprice, 1);
  $("warenkorb_versand_land").firstChild.data = land;
  $("warenkorb_versand").firstChild.data = euro(bshipping, 1);
  $("warenkorb_summe").firstChild.data = euro(bprice + bshipping, 1);
}


function setFussDetail(abaseprice, adiff, acount, atitle, aisnew, oldprice) {
  var adiff_color = "#000000";
  var act_price = abaseprice + adiff;

  if (adiff > 0) {
      adiff = "+" + euro(adiff, 1);
      adiff_color = "#2B7E31";
  } else if (adiff < 0) {
      adiff = euro(adiff, 1);
      adiff_color = "#D13939";
  } else {
      adiff = "";
  }
  $("article_act_diff").firstChild.data = adiff;
  $("article_act_diff").style.color = adiff_color;
  $("article_act_price").firstChild.data = euro(act_price, 1);
  $("article_act_price").value = Math.round(act_price*1.19);

  if (acount!=null) {
      // "Init"
      $("article_base_price").firstChild.data = euro(abaseprice, 1)
      $("article_act_count").value = acount;
      $("article_title").firstChild.data = atitle;
      $("action_text").firstChild.data = aisnew ? (lang['warenkorb_aendern']) : lang['warenkorb_reinlegen'];
  }
  
  if(oldprice!=null && oldprice != 0){
  	$("article_old_price").value = oldprice;
  }
}


function setFussErsatz(abaseprice, adiff, atitle) {
  var adiff_color = "#000000";
  var act_price = abaseprice + adiff;

  if (adiff > 0) {
      adiff = "+" + euro(adiff, 1);
      adiff_color = "#2B7E31";
  } else if (adiff < 0) {
      adiff = euro(adiff, 1);
      adiff_color = "#D13939";
  } else {
      adiff = "";
  }
  $("ersatz_act_diff").firstChild.data = adiff;
  $("ersatz_act_diff").style.color = adiff_color;
  $("ersatz_act_price").firstChild.data = euro(act_price, 1);
  $("ersatz_act_price").value = Math.round(act_price*1.19);
	
	if(atitle!=null) {
		// "Init"
		$("ersatz_base_price").firstChild.data = euro(abaseprice, 1)
		$("ersatz_article_title").firstChild.data = atitle;
		$("ersatz_action_text").firstChild.data = lang['warenkorb_reinlegen'];
	}
}


function swap_gallery(id) {
	var gallerydiv = $("gallery_div");
    if (id>0) {
        $("gallery_div_iframe").src="about:blank";
        setTimeout('document.getElementById("gallery_div_iframe").src="/gallery.php?id='+id+'"', 500);
        gallerydiv.setStyle("top:100px;");
        gallerydiv.show();
    }else{
        gallerydiv.setStyle("top:-450px;");
    	gallerydiv.hide();
    }

}

/** POPUP und Galerie funktionen */
function closegallery(){
	var popup = $("popup");
	var popupoverlay = $("popupoverlay");

	popup.hide();
	popupoverlay.hide();
}

function opengallery(popuptitle, angeboteid){
	var popup = $("popup");
	var popupoverlay = $("popupoverlay");

	$("popup_title").update(popuptitle);
	
	var globalWidth = document.viewport.getWidth();
	var globalHeight = document.viewport.getHeight();
	var newLeftPos = globalWidth/2 - (popup.getWidth()/2);
	var newTopPos = globalHeight/2 - (popup.getHeight()/2);

	newTopPos = newTopPos > 0 ? Math.round(newTopPos) : 0;
	
	popup.setStyle("left:"+newLeftPos+"px");
	popup.setStyle("top:"+newTopPos+"px");
	
	popupcontent = $('popup_content');
	popupcontent.update("&nbsp;");
	popuploader = $('popup_loader');
	popupoverlay.show();
	popup.show();
	
	new Ajax.Request('/gallery_new.php', {
		  method: 'get',
		  parameters: {action: "show_gallery",id: angeboteid},
		  onCreate: function(){
			popuploader.show();  
		  },
		  onSuccess: function(transport) {
			popuploader.hide();
		    popupcontent.update(transport.responseText);
		  }
		});

	
}


function galleryChangePic(element, bildpfad){
	var bild = $("bild_gross");
	bild.src = bildpfad;

	var element = $(element);
	element.setOpacity(1);
	var geschwister = element.siblings();
	geschwister.each(function(e) {
		  e.setOpacity(0.6);
	});

	
	
}


function open_shipping() {
	window.open("/popup_shipping.php","rect_popup","width=519,height=438,left=320,top=100");
}

function open_directmail() {
	e = $("directmail_email");
	email = e?e.value!="E-Mail-Adresse"?e.value:"":"";
	window.open("/popup_directmail_step1.php?email="+email,"rect_popup","width=519,height=438,left=320,top=100");
}

function open_faq() {
	window.open("/popup_faq.php?chapter=mwst","rect_popup","width=519,height=438,left=320,top=100");
}
function open_versandfrei() {
	window.open("/popup_versandfrei.php","rect_popup","width=519,height=438,left=320,top=100");
}

function open_service() {
	window.open("/popup_service.php","rect_popup","width=519,height=438,left=320,top=100");
}

function open_vorkasserabatt() {
	window.open("/popup_vorkasserabatt.php","rect_popup","width=519,height=438,left=320,top=100");
}

function open_erstbestellerrabatt() {
	window.open("/popup_erstbestellerrabatt.php","rect_popup","width=519,height=438,left=320,top=100");
}

function open_faq_rectid() {
	window.open("/popup_faq.php?chapter=rectid","rect_popup","width=519,height=438,left=320,top=100");
}


function setFussKasse(step) {
    for (var i = 1; i<=4; i++) {
        var oimg = $("img_step"+i);
        var new_img_src = "/images/step" + i + "_" + (i==step ? "o" : "b") + ".gif";
        if (oimg.src != new_img_src ) {
	            oimg.src = new_img_src;
        }
    }
}


function toshop() {
    return $("mycbid").checked
}


function autoinit(){
	if(typeof(init) == "function") init();
}


function $(e) {
	return document.getElementById(e);
}


function px2int(Pixel) {
	 return 1*Pixel.substr(0, Pixel.length - 2);
}

var steps = 3;
function slideUpDiv(eleId, originalHeight){
	var ele = $(eleId);
	var cur_height = px2int(ele.getStyle("height"));
	if(Object.isUndefined(originalHeight)) originalHeight = cur_height;
	if(cur_height - steps > 0){
		ele.setStyle("overflow: hidden;");
		ele.setStyle("height: "+(cur_height-steps)+"px;");
		window.setTimeout("slideUpDiv('"+eleId+"',"+originalHeight+");",20);
	}else{
		ele.hide();
		ele.setStyle("height: "+originalHeight+"px;");
	}
}

var steps_down = 3;
var slidingDownElements = new Array();
function slideDownDiv(eleId, targetHeight, currentHeight){
	var ele = $(eleId);
	if((Object.isUndefined(targetHeight) && ele.readAttribute("moving") != 1) || Object.isNumber(targetHeight)){
		ele.writeAttribute("moving","1");
		if(Object.isUndefined(targetHeight) || targetHeight == 0){
			targetHeight = ele.getHeight();
		}
		
		if(Object.isUndefined(currentHeight)){
			currentHeight = 0;
			new_height = 0;
			ele.setStyle("height: "+new_height+"px;");
			ele.setStyle("display: block;");
			ele.setStyle("overflow:hidden;");
		}else{
			new_height = currentHeight + steps_down;
			ele.setStyle("height: "+new_height+"px;");
		}
		
		if(new_height < targetHeight){
			window.setTimeout("slideDownDiv('"+eleId+"',"+targetHeight+","+new_height+")",20);
		}else{
			ele.writeAttribute("moving",false);
		}
	}
}

function openCloseDIV(theID, moveID) {
  var oNF = document.getElementById(theID);
  var moveEle = document.getElementById(moveID);
  if (!oNF || !oNF.style) return false;
  var minHeight = 1* oNF.getAttribute("minHeight");
  var maxHeight = 1* oNF.getAttribute("maxHeight");
  var step = 5;
  var move = 1 * oNF.getAttribute("move");
  if (move == 1) { // Öffnen
    if(oNF.style.display == 'none') oNF.style.display = 'block';
    if (px2int(oNF.style.height) < maxHeight-step) {
      oNF.style.height = "" + (px2int(oNF.style.height) + step) + "px";
      if(moveEle){ moveEle.style.top = "" + (px2int(moveEle.style.top) + step) + "px";}
      //window.setTimeout("openCloseDIV('"+theID+"','"+moveID+"')", 20)
      window.setTimeout("openCloseDIV('"+theID+"','"+moveID+"')", 10)
    } else {
      oNF.style.height = "" + maxHeight + "px";
      if(moveEle){ moveEle.style.top = "" + (px2int(moveEle.style.top) + step) + "px";}
      oNF.setAttribute("move", 0);
    }
  } else { // Schließen
    if (px2int(oNF.style.height) > minHeight+step) {
      oNF.style.height = "" + (px2int(oNF.style.height) - step) + "px";
      if(moveEle){ moveEle.style.top = "" + (px2int(moveEle.style.top) - step) + "px";}
      //window.setTimeout("openCloseDIV('"+theID+"','"+moveID+"')", 20)
      window.setTimeout("openCloseDIV('"+theID+"','"+moveID+"')", 10)
    } else {
      if(oNF.style.display == 'block') oNF.style.display = 'none';
      oNF.style.height = "" + minHeight + "px";
      if(moveEle){ moveEle.style.top = "" + (px2int(moveEle.style.top) - step) + "px";}
      oNF.setAttribute("move", 1);
    }
  }
}


function StdPopup(d) {
	window.open(d,"rect_popup","width=519,height=438,left=320,top=100");
}

function check_rectid() {
    var rectid = $("rectid").value.toUpperCase();
    $("rectidpixel").src = "/images/pixel.gif?search=" + encodeURIComponent(rectid);
    rectid = rectid.replace(/^\s*(RECT )?(DT|WS|TS|RS|MB)?(-| )?(.+?)\s*$/g, '$4');
    rectid = rectid.replace(/RECT /, '')
    if (isNaN(rectid)) {
	      switch (1*rectid.substr(0, 1)) {
	          case 1 : rectid = "DT-" + rectid; break;
	          case 2 : rectid = "WS-" + rectid; break;
	          case 3 :
	          case 4 :
	          case 5 :
	          case 6 : rectid = "TS-" + rectid; break;
	          case 8 : rectid = "RS-" + rectid; break;
	          case 9 : rectid = "MB-" + rectid; break;
	      }
    }
    if (rectid == "RECT-ID" || rectid == "") {
				open_faq_rectid();
        return false;
    } else {
        search = isNaN(rectid) ? rectnames : rectids;
        if (search.indexOf("|"+rectid+"|") == -1) {
            alert(lang['unbekannte_rectid']);
            return false;
        } else {
            $("rectid").value = rectid;
            return true;
        }
    }
}


function doSearch(){
	//alert("doSearch()");
	//$('searchType').setValue('ajaxsearch');
	formular.request({
		method:'POST',
		encoding: 'iso-8859-1',
		parameters: {action:'ajaxsearch'},
		onCreate: function(){
			$('searchIndicator').show();
			$('searchResult').hide();
		},
		onSuccess: function(transport){
			$('searchResult').update(transport.responseText);
			$('searchIndicator').hide();
			$('searchResult').show();
		}
	});
}

function getResultCount(){
	//$('searchType').setValue('ajaxsearch');
	formular.request({
		method:'POST',
		parameters: {fltgetcount:1, action:'ajaxsearch'},
		onCreate: function(){
			//$('resultCount').update("loading...");
		},
		onSuccess: function(transport){
			$('resultCount').update(transport.responseText);
			$('resultCountBox').show();
		}
	});
};


function open_tech_details(ids){
	var idarray = ids.split(",");
	var selectedids = new Array();
	for(var i=0;i<idarray.length;i++){
		//alert(idarray[i]);
		if(document.getElementById(idarray[i])){
			if(document.getElementById(idarray[i]).checked){
				selectedids.push(idarray[i]);
			}
		}
		if(document.getElementById("chk_"+idarray[i])){
			if(document.getElementById("chk_"+idarray[i]).checked && document.getElementById(idarray[i]).value*1 != 0){
				selectedids.push(idarray[i]);
			}
		}

		
	}
	selectedids = selectedids.join(",");
	//window.open('/popup_show_tech_details.php?ids='+ids+'&selectedids='+selectedids,'rect_popup','width=690,height=455,left=320,top=100,resizable');
	window.open('/detail.php?action=show_tech_details&ids='+ids+'&selectedids='+selectedids,'rect_popup','width=690,height=455,left=320,top=100,resizable');
	return false;
}




function showInfoOverlay(elementid) {

	var infodiv = document.getElementById(elementid);

	infodiv.style.visibility = "visible";
	return true;
}
function hideInfoOverlay(elementid) {

	document.getElementById(elementid).style.visibility = "hidden";
	return true;
}

function setRecommendation(){
	document.forms["konfiguration"].reset();
	
	var konfform = document.forms["konfiguration"];
	for(var i=0;i<konfform.elements.length;i++){
		var akt_element = konfform.elements[i];

		if(akt_element.type == "text" && akt_element.value != 0){
			akt_element.style.visibility = "visible";
		}else if(akt_element.type == "text" && akt_element.value == 0){
			akt_element.style.visibility = "hidden";
		}
	}
	
	calc();
	window.document.images["img_recommendation"].src = img_empf_checked.src;
	window.document.getElementById("angebot_konfiguration_basis_text").style.background = "#FFF";
	window.document.getElementById("angebot_konfiguration_empfehlung_text").style.background = "#EAEEF1";
}

function setBasic(){
	//
	var konfform = document.forms["konfiguration"];
	var upgradekategorie = new Array();

	var currentCat = "";
	var oldCat = "";
	var currentCatMinPrice = 9999;

	var killnexttext = false;
	for(var i=0;i<konfform.elements.length;i++){

		var akt_element = konfform.elements[i];

		if(akt_element.type == "text"){
			akt_element.value=0;
			if(killnexttext){
				akt_element.style.visibility = "hidden";
				killnexttext = false;
			}
		}
		if(akt_element.type == "checkbox"){
			akt_element.checked = false;
			killnexttext = true;
		}

		if(akt_element.name.search("upgrade") != -1){

			currentCat = akt_element.name;
			if(currentCat == oldCat){
				//alert(currentCatMinPrice+" - "+akt_element.getAttribute("preis"));
				
				if(akt_element.getAttribute("preis") < currentCatMinPrice){
					currentCatMinPrice = eval(akt_element.getAttribute("preis"));
					akt_element.checked = true;
				}
			}else{
				currentCatMinPrice = eval(akt_element.getAttribute("preis"));
				akt_element.checked = true;
			}
			oldCat = currentCat;
		}
	}
	calc();
	window.document.images["img_basis"].src = img_basis_checked.src;
	window.document.getElementById("angebot_konfiguration_basis_text").style.background = "#F2F3ED";
	window.document.getElementById("angebot_konfiguration_empfehlung_text").style.background = "#FFF";
}

function setVisibility(checker, target_id) {
	if(checker.checked) {
		document.getElementById(target_id).value = "1";
		document.getElementById(target_id).style.visibility = "visible";
		document.getElementById(target_id).select();
	} else {
		document.getElementById(target_id).style.visibility = "hidden";
		document.getElementById(target_id).value = "0";
	}
}