//### 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 = '';
}

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 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);
	}
	
	//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');
		
		if (update_page) GO(fa);
		
		//GO(fa+'&action=shoppingcart_add&shoppingcart_product='+prod_id+'&quantity='+mv+'&scrollY='+scrollY+add_params);
	}
}


function VarChange(art_onpage) {
	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;
	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('viewImageByID.jsp?ArtID="+artid_obj.value+"&sizefolder=150&size=140')";
}


function form_Select_change(CatID, oldCatID, Hersteller, action) {
	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+'&'+
		'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+'&'+
		'mode='+mode);
}