openedBoxes = new Array();


//### prototypes ###
/*** working, but not needed ***/
Array.prototype.contains = function(a) {
	res = false;
	for (i=0; i<this.length; i++) {
		if (this[i]==a) {
			res = true;
			break;
		}
	}
	
	return res;
}
//### prototypes - end ###


//### standard-functions ###
function GE(obj_name) {
	var res = document.getElementById(obj_name);
	if (res==null) res = document.getElementsByName(obj_name)[0];
	
	return res;
}

function GEs(name) {
	var res = document.getElementsByName(name);
	if (res==null || res.length<1) res = document.getElementsByTagName(name);
	
	return res;
}

function GO(loc) {
	GO(loc, true);
}
function GO(loc, enc) {
	document.location.href = (enc==true? encodeURI(loc): loc);
}



function objExists(obj_name) {
	return (GE(obj_name)!=null);
}


function boxhead_over(obj) {
	obj.className = "boxhead_ovr";
}


function boxhead_out(obj) {
	obj.className = "boxhead";
}

function ShopSelectionMouseOver(obj, mode) {
	obj.className = 'curr_shop'; 
	if (mode==1) GE("corner_left").style.background = 'url(\'images/menu_top/v4_links_white.jpg\') repeat-x scroll';
	if (mode==2) GE("corner_right").style.background = 'url(\'images/menu_top/v4_rechts_white.jpg\') repeat-x scroll';
}

function ShopSelectionMouseOut(obj, mode) {
	obj.className = (mode==1)? "start_shop": "other_shop"; 
	if (mode==1) GE("corner_left").style.background = 'url(\'images/menu_top/v4_links_red.jpg\') repeat-x scroll';
	if (mode==2) GE("corner_right").style.background = 'url(\'images/menu_top/v4_rechts_blue.jpg\') repeat-x scroll';
}

function objDisplayShow(obj_id) {
	GE(obj_id).style.display = 'block';
}

function objDisplayHide(obj_id) {
	GE(obj_id).style.display = 'none';
}


function toggleDisplay(obj_id) {
	ob = GE(obj_id);
	ob.style.display = (ob.style.display=='none')? '': 'none';
}


function openBox(obj_id, e) {
	if (!e) e = window.event;
	
	objDisplayShow(obj_id);
	if (!openedBoxes.contains(obj_id)) openedBoxes.push(obj_id);
	
	if (e.stopPropagation) e.stopPropagation()
	else e.cancelBubble = true;
	
	return false;
}

function closeAllBoxes(obj_id_part) {
	for (i=0; i<openedBoxes.length; i++) {
		if (obj_id_part!=null) {
			if(openedBoxes[i]!="" && openedBoxes[i].indexOf(obj_id_part)>-1) {
				objDisplayHide(openedBoxes[i]);
				openedBoxes[i] = "";
			}
	 	} else {
	 		if(openedBoxes[i]!="") {
				objDisplayHide(openedBoxes[i]);
				openedBoxes[i] = "";
			}
	 	}
	}
	
}

function OpenAndCloseThisBox(obj_id_part, e) {
	if (!e) e = window.event;
	if(GE(obj_id_part).style.display == 'block'){
		GE(obj_id_part).style.display = 'none';
	}else {
		 openBox(obj_id_part, e);
	}
}

function CloseKat (obj_id, e){
	if (!e) e = window.event;
	GE(obj_id_part).style.display = 'none';
}

function ShowArrow (obj_id, pic_down, pic_up){
	if(GE(obj_id).src.indexOf(pic_down) > -1){
		GE(obj_id).src = pic_up;
	}else {
		GE(obj_id).src = pic_down;
	}
}

document.onclick = function(e) {
	if(!e) e = window.event;
	var obj = 	e.target ? e.target : e.srcElement ? e.srcElement : null;
	var firedobj = obj;
	
	for (i=0; i<openedBoxes.length; i++) {	
		
		while (firedobj.tagName!="BODY" && firedobj.id!=openedBoxes[i]) {
			//firedobj = (firedobj.parentElement) ? firedobj.parentNode : firedobj.parentElement;
			firedobj = firedobj.parentNode;
		}
		
		if(openedBoxes[i]!="" && openedBoxes[i]!=firedobj.id) {
			objDisplayHide(openedBoxes[i]);
			openedBoxes[i] = "";
		}
	}
}


function AddEvent(obj, evt_name, fn_name) {
	if (obj.addEventListener) {
		//### Firefox, Opera, Safari
		obj.addEventListener(evt_name, fn_name, false);
	} else if (obj.attachEvent) {
		//### IE
		obj.attachEvent("on"+evt_name, fn_name);
	} else {
		try {
			eval("obj.on"+evt_name+" = fn_name");
		} catch(e) {}
	}
}


function getCookieValue(c_name) {
	a = document.cookie;
	ac = a.split("; ");
	r = "";
	
	for (i=0; i<ac.length; i++) {
		if (c_name==ac[i].substr(0, ac[i].search('='))) {
			r = ac[i].substr(ac[i].search('=')+1, ac[i].search(';'));
			if (r=='') r = ac[i].substr(ac[i].search('=')+1, ac[i].length);
			
			break;
		}
	}
	
	return r;
}


function checkCookie() {
	//cd = new Date();
	//cd.setMinutes(cd.getMinutes()+1);
	
	//# test-cookie should expire on end of session, not in just 1 min.
	//document.cookie = "c_test=ok; expires="+cd.toGMTString();
	document.cookie = "c_test=ok; path=/";
	
	if (getCookieValue("c_test")!="ok") objDisplayShow("cookieNotSupported_div");
}


function MengeChange(prod_on_page, update_page) {
	f = GE("artikel");													//# Form-Objekt
	p = GE("ArtID" + prod_on_page).value;								//# ArtikelID
	m = GE("menge" + prod_on_page);										//# Menge
	k_min_v = GE("MinWeightValue" + prod_on_page).value;				//# Mindestmenge
	
	mv = m.value;
	fa = f.action;
	scrollY = (isIE)? document.body.scrollTop: window.pageYOffset;
	
	//### zusatz-parameter mit abschicken ###
	add_params = "";
	input_obs = GEs("input");
	for (i=0; i<input_obs.length; i++) {
		if (input_obs[i].name.indexOf("MengeChangeParam")>-1) {
			param_name = input_obs[i].name.split("_")[1];
			param_value = input_obs[i].value;
			add_params += "&" + param_name + "=" + param_value;
		}
	}
	
	
	if(mv==0) {
		if (confirm("Wollen Sie dieses Produkt aus dem Warenkorb löschen?")) {
			ShoppingCart.deleteProduct(p);
			ShoppingCart.saveCookie();
			
			getData(js_domain+'templates/wkorb_klein_ajax.jsp', 'ShoppingCart_small');

			if (update_page) GO(fa);
			
			//GO(fa+'&action=shoppingcart_delete&shoppingcart_product='+p+'&quantity=0&scrollY='+scrollY+add_params);
		} else {
			m.value = 0;
		}
		
		return(false);
	}
	
	
	//if(isNaN(parseFloat(mv))) m.value = 0;
	if(isNaN(parseInt(mv)))m.value = k_min_v;
	
	if(parseInt(mv)<parseInt(k_min_v)) {
		m.value = parseInt(k_min_v); 
		mv = parseInt(k_min_v);
		alert("Die Mindestmenge für dieses Produkt beträgt "+ m.value +" Gramm. \n Die Menge wurde auf die Mindestmenge geändert.");		
	}
	
	//if(mv>0) {
	if(parseInt(mv)>=parseInt(k_min_v)) {
		if (objExists("load_div")) toggleDisplay("load_div");
		
		var new_cart_entry = new ShoppingCartEntry();
		new_cart_entry.setProductID(p);
		new_cart_entry.setProductQuantity(mv);
		new_cart_entry.setProductAlternate(true);
		
		ShoppingCart.addProduct(new_cart_entry);
		ShoppingCart.saveCookie();

		getData(js_domain+'templates/wkorb_klein_ajax.jsp', 'ShoppingCart_small');
		//alert("asdsad");
		if (objExists("FastOrderShoppingCart")) 
			getData(js_domain+'templates/fastorder_shoppingcart_ajax.jsp', 'FastOrderShoppingCart');
		
		if (update_page) GO(fa);
		
		//GO(fa+'&action=shoppingcart_add&shoppingcart_product='+prod_id+'&quantity='+mv+'&scrollY='+scrollY+add_params);
	}
	
	if(mv > 0){
		showProductBox(p, mv);
	}
}


function showProductBox(artikelnummer, menge) {
	if (typeof GE('ShowProd')!='undefined') {
		
		if (artikelnummer == 1) {
			GE('ShowProd').style.display = 'none';
		} else {
			
			if (GE('ShowProd').style.display == 'block') {
				toggleDisplay('ShowProd');
				//GE('ProdPicSmall').style.background = "url(\"templates/getproductpic.jsp?artikel="+artikelnummer+"\") no-repeat center";
				GE('ProductQuantity').value = menge;
				toggleDisplay('ShowProd');
			} else {
				//GE('ProdPicSmall').style.background = "url(\"templates/getproductpic.jsp?artikel="+artikelnummer+"\") no-repeat center";
				GE('ProductQuantity').value = menge;
				toggleDisplay('ShowProd');
			}
			
			window.setTimeout("showProductBox(1,1)",10000);
		}
		
	}
}

function VarChange(art_onpage) {
	VarChange(art_onpage,0);
}
function VarChange(art_onpage,lieferanten_artikelnummer) {
	tmp_lieferanten_artikelnummer = lieferanten_artikelnummer;
	frm = document.artikel;
	obj = eval("frm.Variation" + art_onpage);
	artid_obj = eval("frm.ArtID" + art_onpage);
	menge_obj = eval("frm.menge" + art_onpage);
	detail_link_obh = document.getElementById("detail_link_" + art_onpage);
	
	artid_obj.value = obj.options[obj.selectedIndex].value;
	if(artid_obj.value.indexOf('-')>-1){
		arr_ = artid_obj.value.split("-");
		artid_obj.value = arr_[0];
		tmp_lieferanten_artikelnummer = arr_[1];
	}
	menge_obj.value = eval("frm.menge_"+art_onpage+"_"+artid_obj.value).value;
	
	det_hr = detail_link_obh.href;
	det_hr = det_hr.replace(det_hr.substring(det_hr.indexOf("&Artikel")+1, det_hr.length), "Artikel=" + artid_obj.value);
	detail_link_obh.href = det_hr;
	
	GE("prodpic" + art_onpage).style.backgroundImage = "url('productpics/w_140/h_140/"+(tmp_lieferanten_artikelnummer>0? "la_"+tmp_lieferanten_artikelnummer: "") + "/"+artid_obj.value+".jpg')";
}


function form_Select_change(CatID, oldCatID, Hersteller, action, inFirstCatID) {
	cat_val = 0;
	
	try {
		cat_ob = GE("form_Cat3");
		cat_val = cat_ob.options[cat_ob.selectedIndex].value;
	} catch(e) {}
	
	use_catid = CatID;
	if (cat_val>0) use_catid = cat_val;
	
	inCatID = (cat_val>0 || (oldCatID>0 && oldCatID!=CatID))? 1: 0;
	
	
	her_val = Hersteller;
	try {
		her_ob = GE("form_Hersteller");
		her_val = her_ob.options[her_ob.selectedIndex].value;
	} catch(e) {}
	
	
	sort_ob = GE("form_sortBy");
	sort_val = sort_ob.options[sort_ob.selectedIndex].value;
	
	pop_ob = GE("form_ProdsOnPage");
	pop_val = pop_ob.options[pop_ob.selectedIndex].value;
	
	//prem_ob = GE("form_PremiumFilter");
	//prem_filt = prem_ob.options[prem_ob.selectedIndex].value;
	
	mode = GE("mode").value;
	main_cat = GE("MainCat").value;
	
	
	GO('?p=produktliste&'+
		''+(action!=''?'action='+action+'&':'')+''+
		'MainCat='+main_cat+'&'+
		'inCatID='+inCatID+'&'+
		'CatID='+use_catid+'&'+
		'oldCatID='+(oldCatID>0? oldCatID: CatID)+'&'+
		'form_Hersteller='+her_val+'&'+
		'sortBy='+sort_val+'&'+
		'form_ProdsOnPage='+pop_val+'&'+
		//'form_PremiumFilter='+prem_filt+'&'+
		'inFirstCatID='+inFirstCatID+'&'+
		'mode='+mode);
}

function Bewertung() {
var myLinks = document.getElementsByTagName('A');	 
   var ratingIndex = 0; 
   for (i=0; i<myLinks.length; i++) {
       if(myLinks[i].className.substr(0,6)  == "rating") {
         myLinks[i].myActualRating = document.createElement('SPAN');
         myLinks[i].myHiddenInput = document.createElement('INPUT');
         myLinks[i].myHiddenInput.type = 'hidden';
         myLinks[i].myHiddenInput.value = 'none';
         myLinks[i].myHiddenInput.name = myLinks[i].className + ratingIndex;    
         ratingIndex++;    
         myLinks[i].appendChild(myLinks[i].myActualRating);
         myLinks[i].appendChild(myLinks[i].myHiddenInput);
         myLinks[i].onmousemove = function (e) {
            obj = this;
            this.absXPos = obj.offsetLeft;
            while (obj.offsetParent) {
               obj = obj.offsetParent;
               this.absXPos += obj.offsetLeft;
            }
            var e = e || window.event;
            var relPos = e.clientX || e.pageX;
            this.myActualRating.style.width = Math.round((relPos - this.absXPos)/4)*4 + 'px';
         }
         myLinks[i].onmouseover = myLinks[i].onmousemove;
         myLinks[i].onmouseout = function () {
            this.myActualRating.style.width = 0;
         }
         myLinks[i].onclick = function () {
             this.onmouseout = function () {
               this.myActualRating.style.width = (this.myHiddenInput.value * 20) + '%';
             };
             this.myHiddenInput.value = Math.round(this.myActualRating.offsetWidth*125/this.offsetWidth);
             if(this.myHiddenInput.value > 120)this.myHiddenInputValue = 115;
             var wertStern =  (parseInt(this.myHiddenInput.value/20)).toString();
             this.myHiddenInput.value = wertStern;
             var nameStern = this.myHiddenInput.name;  
          	 return false;
         }
      }
   }
}

function addProductlistFilters(filter_checked){
	
	the_filter_check = filter_checked + "_ini";
	the_filter_memory = GE('form_productlist_filters');
	the_filter_value = the_filter_memory.value;
	var filter_fertig = '';
	
	if(the_filter_value.indexOf('f'+filter_checked+':')>-1){
		//##HB# den Filter entfernen ##
		GE(the_filter_check).checked = "";
		filter_fertig = the_filter_value.replace('f'+filter_checked+':','');
		the_filter_memory.value = filter_fertig;
		runData('templates/produktliste_filter.jsp?form_productlist_filters='+filter_fertig);
	}else{
		
		//##HB# den Filter anfügen ##
		GE(the_filter_check).checked = "checked";
		filter_fertig = the_filter_value + 'f'+filter_checked+':';
		the_filter_memory.value = filter_fertig;
		runData('templates/produktliste_filter.jsp?form_productlist_filters='+filter_fertig);
		GE(the_filter_check).checked = "checked";
	}
}

function OpenLightboxOverlay(ToOpeningBox){
	toggleDisplay('lightbox-overlay');
	GE('lightbox-overlay').style.width=screen.width;
	GE('lightbox-overlay').style.height=screen.height;
	
	toggleDisplay(ToOpeningBox);
	GE(ToOpeningBox).style.position="fixed";
	GE(ToOpeningBox).style.zIndex=91;
	GE('ToBeConcludedBox').value = ToOpeningBox;
}

function CloseLightboxOverlay(){
	ToBeConcludedBox = GE('ToBeConcludedBox').value;
	toggleDisplay(ToBeConcludedBox);
	toggleDisplay('lightbox-overlay');
}

function OpenDragNDrop (productID){
	toggleDisplay('ToggleDisplay_' + productID);
	OpenLightboxOverlay('DragAndDropBox');
	dragstart(GE('ToggleDisplay_' + productID));
}

function CloseDragNDrop (productID){
	toggleDisplay('ToggleDisplay_' + productID);
	CloseLightboxOverlay();
}


//Das Objekt, das gerade bewegt wird.
var dragobjekt = null;

// Position, an der das Objekt angeklickt wurde.
var dragx = 0;
var dragy = 0;

// Mausposition
var posx = 0;
var posy = 0;


function draginit() {
  document.onmousemove = drag;
  document.onmouseup = dragstop;
}


function dragstart(element) {
  dragobjekt = element;
  dragx = posx - dragobjekt.offsetLeft;
  dragy = posy - dragobjekt.offsetTop;
}


function dragstop() {
  dragobjekt=null;
}


function drag(ereignis) {
  posx = document.all ? window.event.clientX : ereignis.pageX;
  posy = document.all ? window.event.clientY : ereignis.pageY;
  if(dragobjekt != null) {
    dragobjekt.style.left = (posx - dragx) + "px";
    dragobjekt.style.top = (posy - dragy) + "px";
  }
}

//Browserweiche
IE = document.all&&!window.opera;
DOM = document.getElementById&&!IE;

// Variablen für die Mausposition
var mouseX = 0; // x-Koordinate
var mouseY = 0; // y-Koordinate


/* Body-Objekt des IE ermitteln (abhängig, ob IE im Quirks- oder Standardmode*/
function getBody(){
  // Entsprechendes Bodyobjekt zurückgeben (CSS1Compat = Standardmode
  return (window.document.compatMode == "CSS1Compat") ?
          window.document.documentElement : window.document.body || null;
}

var ieBody = null;

// Funktion zum Initialisierung, die in onload aufgerufen wird
function init(){
  document.onmousemove = getMousePos; // Mausposition ermitteln

  // Falls es sich um einen IE handelt
  if(IE)
    ieBody = getBody(); // Bodyobjekt für IE ermitteln
}

// Funktion zum Ermitteln der Mausposition
function getMousePos(ereignis){
  // Aktuelle Mauskoordinaten bei Mausbewegung ermitteln
  mouseX = (IE) ? window.event.clientX : ereignis.pageX;
  // Mauskoordinate im Body auch nach Scrollen (Wert ist evtl. höher im IE)
  mouseY = (IE) ? (window.event.clientY + ieBody.scrollTop) : ereignis.pageY;
  // Mauskoordinate im Fenser (ohne Scrollen im IE)
  //mouseY = (IE) ? window.event.clientY : ereignis.pageY;

  window.status = "Mauskoordinaten (x, y): (" + mouseX + ", " + mouseY +")";
}
