﻿function onSelectChange(selectEl) {
	onOptionsChange(selectEl.id.substring(6));	
}

function onAmountChange(amountEl) {
	onOptionsChange(amountEl.id.substring(6));
}

function onCountMinus(countEl) {
	onCountChangeMinus(countEl.id.substring(12));
	onOptionsChange(countEl.id.substring(12));
}

function onCountPlus(countEl) {
	onCountChangePlus(countEl.id.substring(12));
	onOptionsChange(countEl.id.substring(12));
}

function onCountChangeMinus(id){
	if (document.getElementById( "amount" + id ).value>0)
	{document.getElementById( "amount" + id ).value=document.getElementById( "amount" + id ).value-1;}
	else
	{document.getElementById( "amount" + id ).value=0;}
}

function onCountChangePlus(id){
	document.getElementById( "amount" + id ).value=+(document.getElementById( "amount" + id ).value)+1;
}

function onOptionsChange(id){
	var selectEl=document.getElementById( "select" + id );
	var price = getElemByVal( selectEl,selectEl.value ).getAttribute("xprice");
	var amount = document.getElementById( "amount" + id ).value;
	document.getElementById( "price" + id ).innerHTML = price;
	var costEl = document.getElementById( "cost" + id )
	if ( costEl ) { costEl.innerHTML = amount * price; }
}
function getElemByVal(parent,value){
	var els = parent.getElementsByTagName("*");
	var elsLen = els.length;
	var El=null;
	for ( var i = 0; i < elsLen ; i++ ) {
		if (els[i].value == value) {
			El = els[i]; 
			i = elsLen;
		}
	}
	return El;
}
function removeElemByClass(className){
	var els = document.body.childNodes;
	var elsLen = els.length;
	var El=null;
	for ( var i = 0; i < elsLen ; i++ ) {
		if (els[i].className == className) {
			els[i].parentNode.removeChild(els[i]);
		}
	}
}
function addToCartEnjoy( buttonEl ){
	var id=buttonEl.id.substring(6);
	var selectEl=document.getElementById( "select" + id );
	var optionsEl=getElemByVal( selectEl,selectEl.value );
	var price = optionsEl.getAttribute("xprice");
	var prodId = optionsEl.getAttribute("xprodId");
	var amountEl = document.getElementById( "amount" + id );
	addToCart(19501, prodId, price, amountEl.value);
	var msgEl = document.getElementById( "msgcart" ).cloneNode(true);
	var pos = findPosx( buttonEl );
	msgEl.className="msgcartMsg";
	msgEl.style.left = ( pos.x - 40 );
	msgEl.style.top = ( pos.y - 80 );
	msgEl.style.visibility = "visible";
	document.body.appendChild( msgEl );
}
function myscrInit(){
	var els = document.getElementsByTagName("input");
	var elsLen = els.length;
	for ( var i = 0; i < elsLen ; i++ ) {
		if ( els[i].id.substring(0,6) == "amount" ) {
			onOptionsChange(els[i].id.substring(6));
		}
	}
	if ( navigator.userAgent.indexOf("Opera") == -1 ) {
		if ( navigator.userAgent.indexOf("Firefox") == -1 ) {
			els = document.getElementsByTagName("ul");
			var ulLen = els.length;
			for ( var k = 0; k < ulLen ; k++ ) {
				if ( els[k].className == "enjoymenu" ) {
					var hEls = els[k].getElementsByTagName("li");
					var elsLen = hEls.length;
					for ( i = 0; i < elsLen; i++ ) {
						hEls[i].onmouseover = function() { this.className += " jshover"; }
						hEls[i].onmouseout = function() { this.className = this.className.replace(" jshover", ""); }
					}
				}
			}
		}
	}
}
function addmyscrInitOnLoad() {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = myscrInit;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			myscrInit();
		};
	}
}
function findPosx( a ) {
	var b = {};
	b.x = 0;
	b.y = 0;
	if (a.offsetParent) {
		while (a.offsetParent) {
			b.y += a.offsetTop;
			b.x += a.offsetLeft;
			a = a.offsetParent;
		}
	} else {
		if (a.x)
			b.x += a.x;
		if (a.y)
			b.y += a.y
	}
	return b;
}
function msgCartClick(ans,el){
	var msgEl = null;
	var xEl=el; 
	while(msgEl == null){
		xEl = xEl.parentNode;
		if(xEl.id == "msgcart"){ msgEl = xEl; }
	}
	xEl.parentNode.removeChild(xEl);
	if ( ans == "yes" ){ window.location = "/internet-magazin?mode=cart"; }
}
addmyscrInitOnLoad();
