matrixStatusLockedText = 'Bitte w&auml;hle eine Gr&ouml;&szlig;e und Farbe aus';

function matrixStatus(text) {
	x = xGetElementById('status');
	x.value = text;
}

function matrixStatusLock() {
	x = xGetElementById('status');
	matrixStatusLockedText = x.value;
}

function matrixStatusDefault() {
	matrixStatus(matrixStatusLockedText);
}

function matrixStatusAndPrice(text, price) {
	matrixStatus(text);
	matrixProductPrice(price);
}

function matrixProductPrice(price) {
	x = xGetElementById('product_price');
	x.value = price + ' ' + 'EUR';
}

function setProduct(id) {
	matrixStatusLock();
	document.forms.order['product[id]'].value = id;
}

function checkOrder(form) {
	var errors = Array();
	
	if (isNaN(parseInt(form['product[id]'].value)) || parseInt(form['product[id]'].value) <= 0) {
	  errors.push('Bitte w&auml;hle eine Gr&ouml;&szlig;e und Farbe aus.');
	}
	if (isNaN(parseInt(form['quantity'].value)) || parseInt(form['quantity'].value) == 0) {
	  errors.push('Bitte gib die gew&uuml;schte Anzahl an Artikeln als Zahl zwischen 1 und 99 ein!');
	}
	
	if (errors.length > 0) {
	  alert_text = '';
	  for (var i = 0; i < errors.length; i++) {
	    alert_text += errors[i] + '\n';
	  }
	  alert(alert_text);
	  return false;
	}
	
	return true;
}

function xGetElementById(e, f) {
	if (document.layers) {
		f = (f) ? f : self;
		if (f.document.layers[e]) {
			return f.document.layers[e];
		}
		for (var W=0; i<f.document.layers.length; W++) {
			return getElement(e,f.document.layers[W]);
		}
	}
	if (document.all) {
		return document.all[e];
	}
	return document.getElementById(e);
}

function xGetElementOfEvent(e) {
	return (e) ? e.target : window.event.srcElement;
}

function toggleDisplay(name) {
	if ($(element)) $(element).toggle();
}

function display(name, type) {
	if (type == null) type = 'block';
	element = xGetElementById(name);
	if (element != null) {
		element.style.display = type;
	}
}

function hide(name) {
	if ($(element)) $(element).hide();
}

/******* product details *********/

function show_last_clickimage() {
	var id = $F('last_click_image');
	if (id) { show_image(id) }
}

function choose(artno,size,color,price,saleprice,stock_str,image,sa_id,size_id,stock_count,act_id,available) {
	$('size').value = size;
	$('color').value = color;
	$('stock_str').value = stock_str;
	$('stock').value = stock_count;
	$('price').value = price;
	if (saleprice == 0) saleprice = price;
	$('saleprice').value = saleprice;
	$('artno').value=artno;
	$('colorboxborder').hide();
	if ($('size_id').value) {
		$('size'+$('size_id').value).className='matrixsizebox in';
	}
	if ($('size'+size_id)) {
		$('size'+size_id).className='matrixsizebox act';
	}
	$('size_id').value = size_id;
	
	if ($F('act_id_tmp') && $('matrixact'+$F('act_id_tmp'))) {
		$('matrixact'+$F('act_id_tmp')).hide();
	}
	$('act_id_tmp').value=0;
	if (act_id) {
		$('matrixact'+act_id).show();
		$('act_id_tmp').value = act_id;
	}
	$('sized_article_id').value = sa_id;
	
	show_image(image,1);
	check_count_value();
	if ($('available')) {
		if (available && !stock_count) {
			$('available').show();
			$('available_at').innerHTML=available;
		} else {
			$('available').hide();
		}
	}
	// für alternative Währungen
	var p=$('price_1');
	if (p) {
		p.value=price;
		p=$('price_2');
		if (p) p.value=price;

		p=$('price_3');
		if (p) p.value=saleprice;
		p=$('price_4');
		if (p) p.value=saleprice;
		
		update_currency(current_rate);
	}
}

function is_callcenter() {
	return (parent && parent.frames[0] && parent.frames[0].name == 'callcenter_header');
}

function refresh_html() {
	if ($('size_container')) $('size_container').innerHTML = $F('size');
	if ($('color_container')) $('color_container').innerHTML = $F('color');
	$('artno_container').innerHTML=$F('artno');
	$('stock_container').innerHTML = $F('stock_str');
	
	if ($('stock').value > 0 || is_callcenter()) {
		$('wkbtnin').hide();
		$('wkbtnact').show();
		if (is_callcenter() && $('is_callcenter'))
			$('is_callcenter').value='1';
	} else {
		$('wkbtnact').hide();
		$('wkbtnin').show();
	}
	
	var cent = parseInt($F('price'));
	if (cent) {
		$('price_container').innerHTML = cent2euro(cent)+' &euro;';
	}		
	var anzahl = parseInt($F('countx'));
	var cent_ges = cent * anzahl;
	if (cent_ges) {
		$('price_ges_container').innerHTML = cent2euro(cent_ges)+' &euro;';
	}
	
	var salecent = parseInt($F('saleprice'));
	if (salecent > 0 && salecent < cent) {
		$('sale_price_container').innerHTML = cent2euro(salecent)+' &euro;';
		var cent_ges = salecent * anzahl;
		if (cent_ges) {
			$('sale_price_ges_container').innerHTML = cent2euro(cent_ges)+' &euro;';
		}
		$('price_container').setStyle({ fontWeight: 'normal', textDecoration: 'line-through', color:'#8B8B8B' });
		$('price_ges_container').setStyle({ fontWeight: 'normal', textDecoration: 'line-through', color:'#8B8B8B' });
		$('summe_2').setStyle({ textDecoration: 'line-through', color:'#8B8B8B' });
		$('gprice_1').setStyle({ textDecoration: 'line-through', color:'#8B8B8B' });
		$('sale_price_ges_container').show(); $('sale_price_container').show();
		$('summe_4_outer').show(); $('gprice_3_outer').show();
	} else {
		$('price_container').setStyle({ fontWeight: 'bold', textDecoration: 'none', color:'#CC6699' });
		$('price_ges_container').setStyle({ fontWeight: 'bold', textDecoration: 'none', color:'#CC6699' });
		$('summe_2').setStyle({ textDecoration: 'none', color:'#CC6699' });
		$('gprice_1').setStyle({ textDecoration: 'none', color:'#CC6699' });
		$('sale_price_ges_container').hide(); $('sale_price_container').hide();
		$('summe_4_outer').hide(); $('gprice_3_outer').hide();
	}
}

function addwk() {
	if ((typeof(check_cookie) != 'function') || ((typeof(check_cookie) == 'function') && check_cookie())) {
		if ($F('sized_article_id') == 0 || $F('sized_article_id') == '') return errorwk();
		$('count_wk').value = Math.max(1, $F('countx'));
		$('sized_article_id_wk').value = $F('sized_article_id');
		$('shoppingcart_add').submit();
	}
}

function readCookie(name) {
	if (name == "") return ""; // Ohne Name gibt es auch keinen Wert
	var dc = document.cookie;
	
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return ""; // Wenn der Name (Prefix) ohne vorgestelltes ; nicht am Zeilenanfang steht, dann ist er Teil eines anderen Cookies und somit nicht was wir suchen.
	} else {
		begin += 2 // Plus 2 damit der Index nicht mehr auf dem ; steht
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) { // Wenn kein ; vorhanden ist, dann handelt es sich um den letzten Wert im Cookie-String. Somit ist das Ende des Cookie-Strings auch gleich das ende des gesuchten Wertes.
		end = dc.length;
	}
	var value = unescape(dc.substring(begin + prefix.length, end)); // Der Wert des gesuchten Cooki wird als Teilstring aus dem gesamten Cookie-Strings extrahiert.

	if (value == ";") { // bug with IE
		return ""
	}
	
	return value;
}

function setCookie(name, value, expires, path, domain, secure) {
	if (expires) {
		if (typeof(expires)=='object')
			expires='; expires='+expires.toGMTString();
		else
			expires='; expires='+expires;
	} else
		expires='';
	document.cookie= name+'='+escape(value) +
			expires +
			'; path=/'+(domain ? '; domain='+domain : '') +
			(secure ? '; secure' : '');
}

function cookie_enabled() {
	setCookie('cookie_test', 1);
	var s = readCookie("cookie_test");
	if(s != null && s != "" && s != ";") {
		return true
	} else {
		return false
	}
}

function show_leg_length_box(size_id, color_id) {
	if ($('size_color_tmp').value) {
		$('size_select_box_'+$('size_color_tmp').value).hide();
	}
	$('size_select_box_'+size_id+'_'+color_id).show();
	$('size_color_tmp').value = size_id+'_'+color_id;
	eval("show_first_leg_length_"+size_id+"_"+color_id+"()");
}

/*********************************/

function mouseover_button_left(obj, img, col) {
	if (!col) col='rot';
	obj.firstChild.src='/shared/pics/shop/button/left_'+col+'_hover' + img + '.jpg';
	var n = get_next_div_sibling(obj);
	n = get_next_div_sibling(n);
	n.firstChild.src='/shared/pics/shop/button/right_'+col+'_hover.jpg';
}

function mouseout_button_left(obj, img, col) {
	if (!col) col='rot';
	obj.firstChild.src='/shared/pics/shop/button/left_'+col + img + '.jpg';
	var n = get_next_div_sibling(obj);
	n = get_next_div_sibling(n);
	n.firstChild.src='/shared/pics/shop/button/right_'+col+'.jpg';
}

function mouseover_button_right(obj, img, col) {
	if (!col) col='rot';
	obj.firstChild.src='/shared/pics/shop/button/right_'+col+'_hover.jpg';
	var n = get_prev_div_sibling(obj);
	n = get_prev_div_sibling(n);
	n.firstChild.src='/shared/pics/shop/button/left_'+col+'_hover' + img + '.jpg';
}

function mouseout_button_right(obj, img, col) {
	if (!col) col='rot';
	obj.firstChild.src='/shared/pics/shop/button/right_'+col+'.jpg';
	var n = get_prev_div_sibling(obj);
	n = get_prev_div_sibling(n);
	n.firstChild.src='/shared/pics/shop/button/left_'+col+ img + '.jpg';
}

function mouseover_button_middle(obj, img, col) {
	if (!col) col='rot';
	var prev = get_prev_div_sibling(obj);
	prev.firstChild.src='/shared/pics/shop/button/left_'+col+'_hover' + img	+ '.jpg';
	
	var n = get_next_div_sibling(obj);
	n.firstChild.src='/shared/pics/shop/button/right_'+col+'_hover.jpg';
}

function mouseout_button_middle(obj, img, col) {
	if (!col) col='rot';
	var prev = get_prev_div_sibling(obj);
	prev.firstChild.src='/shared/pics/shop/button/left_'+col+ img + '.jpg';
	
	var n = get_next_div_sibling(obj);
	n.firstChild.src='/shared/pics/shop/button/right_'+col+'.jpg';
}

function get_next_div_sibling(obj) {
	var n = obj.nextSibling;
	while(n.nodeType != 1) {
		n = n.nextSibling
	}
	return n;
}

function get_prev_div_sibling(obj) {
	var n = obj.previousSibling;
	while(n.nodeType != 1) {
		n = n.previousSibling
	}
	return n;
}


// nur Zahlen im Input
function check_for_integer(obj) {
	var v=$F(obj).replace(/\D/g, '');
	$(obj).value=v;
}

// Fliesskommazahl (3,5 und 3.5)
// optional: Anzahl Nachkommastellen   onkeyup="check_for_float(this, 2);" 
function check_for_float(obj) {
	var v=$F(obj);
	var val;
	if (arguments.length>1) {
		eval('val=v.match(/\\d+[,\\.]?\\d{0,'+arguments[1]+'}/);');
	} else {
		val=v.match(/\d+[,\.]?\d*/);
	}
	$(obj).value=val;
}

// Fliesskommazahl (3,5 und 3.5), auch negative erlaubt (- als erstes Zeichen)
// optional: Anzahl Nachkommastellen   onkeyup="check_for_float(this, 2);" 
function check_for_neg_float(obj) {
	var v=$F(obj);
	var val;
	if (arguments.length>1) {
		eval('val=v.match(/-?\\d*[,\\.]?\\d{0,'+arguments[1]+'}/);');
	} else {
		val=v.match(/-?\d*[,\.]?\d*/);
	}
	$(obj).value=val;
}

function zerofill(txt, len) {
	if (!len) len=2;
	if (typeof(txt)=='number') txt=txt.toString();
	var l=txt.length;
	var zero='0000000000';
	return zero.substring(10-len+l)+txt;
}

// auf Datum prüfen; macht aus "30.12.08" -> "30.12.2008" und aus "30.12." -> "30.12.YYYY" mit YYYY=aktuelles Jahr
function check_for_date(obj) {
	$(obj).style.backgroundColor='white';
	var val = $F(obj);
	if (val=='') return;
	var pattern=/^(\d{1,2})\.(\d{1,2})\.?(\d\d)?(\d\d)?$/;
	val = $F(obj);
	var tm=new Date();
	var res = pattern.exec(val);
	if (res && res.length>2) {
		var m=res[2];
		var t=31;
		var j=(res[3] ? (res[4] ? res[3]+res[4] : '19'+res[3]) : tm.getFullYear());
		if (m == 4 || m == 6 || m == 9 || m == 11) t--;
		if (m == 2) {
			t-=3;
			if (j % 4 == 0) t++;
			if (j % 100 == 0) t--;
			if (j % 400 == 0) t++;
		}
		if (res[1]<1 || res[1]>t) {
			$(obj).style.backgroundColor='yellow';
			return false;
		}
		if (m<1 || m>12) {
			$(obj).style.backgroundColor='yellow';
			return false;
		}
		
		$(obj).value=zerofill(res[1])+'.'+zerofill(m)+'.'+j;
		return true;
	}
	$(obj).style.backgroundColor='yellow';
	return false;
}

// prueft, ob in dem Feld der Default-Wert drin steht; wenn ja, wird das Feld geleert
function checkDefault(obj, def) {
	if ($(obj) && $F(obj) == def)
		$(obj).value='';
}

// Funktionen der Produkt-Detail-Seite
function show_image(id,remember) {
	if (!id) return;
	$$('div.mainimage').each(function(x) { x.hide(); });
	if ($('mainimage'+id)) $('mainimage'+id).show();
	if (remember) {
		var lci = $F('last_click_image');
		if (lci && $('small_image_'+lci)) {
			$('small_image_'+lci).removeClassName('active');
		}
		if (id && $('small_image_'+id)) $('small_image_'+id).addClassName('active');
		
		$('last_click_image').value = id;
	}
}

function check_count_value() {
	var anzahl_str = $F('countx');
	var bad_int = anzahl_str.search(/\D/);
	
	var anzahl = parseInt(anzahl_str);
	var stock = parseInt($F('stock'));
	if (stock > max_items) {
		stock = max_items;
	}
	if ((isNaN(anzahl_str) == true || anzahl <= 0 || anzahl > stock) && stock > 0 || anzahl_str == '' || bad_int >= 0) {
		if (anzahl > stock && !(isNaN(anzahl_str) == true)) {
			$('anzahl_error_msg').innerHTML=prodstr1+' '+stock;
		} else {
			$('anzahl_error_msg').innerHTML=prodstr2;
		}
		$('anzahl_error').show();
		$('wkbtnact').hide();
		$('wkbtnin').show();	
		return false;
	} else {
		$('anzahl_error').hide();
		if ($('price_matrix')) {
			$('price_matrix').style.border='2px solid white';
		}
		refresh_html();
		// alternative Währung
		var p=$('count_2');
		if (p) {
			p.value=anzahl;
			p=$('count_4');
			if (p) p.value=anzahl;
			update_currency(current_rate);
		}
	}
}

function errorwk() {
	if ($F('sized_article_id') > 0 && $F('stock') == 0) {
		alert(prodstr3);
		if ($('price_matrix')) {
			$('price_matrix').style.border='2px solid red';
		}
	} else {
		alert(prodstr4);
		if ($('price_matrix')) {
			$('price_matrix').style.border='2px solid red';
		}
	}
}

function check_cookie() {
	if (cookie_enabled()) {
		return 1;
	} else {
		alert(prodstr5);
		return 0;
	}
}
