/*********************************
 **** LOAD BALANCING SESSION *****
 *********************************/
var _lbs = setInterval(function() {
	var datum = new Date();
	var param = Date.UTC(datum.getFullYear(), datum.getMonth(), datum.getDay(), datum.getHours(), datum.getMinutes(), datum.getSeconds());
	new Ajax.Request('/ajax/index.html?t_timestamp=' + param , {method: 'get'});
}, 1 * 60 * 1000);

/* Warenkorb Funktionen */

var wkto;

if (!document.all) {
	Event.observe(window, 'scroll', function(ev) {
		if (!$('darkroom')) return false;
		if ($('darkroom').style.display != 'none') {
			$('darkroom').style.top = (document.documentElement.scrollTop || document.body.scrollTop) + 'px';
			$('darkroom').style.left = (document.documentElement.scrollLeft || document.body.scrollLeft) + 'px';
		}
	});
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function fadeOut(vis, options) {
	if (vis) {
		var arrayPageSize = getPageSize();
		$('darkroom').style.height = (arrayPageSize[1] + 'px');
		$('darkroom').style.display = "block";
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "hidden";
		}
	} else {
		$('darkroom').style.display = "none";
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "visible";
		}
	}
}

function centerElement(obj, center) {
	var height;
	if (center == null) center = true;
  var x1,y1,x2,y2;
  if (self.innerHeight) {	x1 = self.innerWidth;	y1 = self.innerHeight; }												// all except Explorer
  else if (document.documentElement && document.documentElement.clientHeight)										// Explorer 6 Strict Mode
	{	x1 = document.documentElement.clientWidth; y1 = document.documentElement.clientHeight; }
  else if (document.body) {	x1 = document.body.clientWidth; y1 = document.body.clientHeight; }	// other Explorers
  if (self.pageYOffset) {	x2 = self.pageXOffset; y2 = self.pageYOffset; }												// all except Explorer
  else if (document.documentElement && document.documentElement.scrollTop) 											// Explorer 6 Strict Mode
	{ x2 = document.documentElement.scrollLeft; y2 = document.documentElement.scrollTop; }
	else if (document.body) { x2 = document.body.scrollLeft; y2 = document.body.scrollTop; }			// other Explorers
	obj = $(obj);
	height = (y2 + (y1 - obj.offsetHeight) / 2);
	if (height < 40) height = 40;
	if (center == true) obj.style.top = height + 'px';
	obj.style.left = (x2 + (x1 - obj.offsetWidth) / 2) + 'px';
}


function checkNumber(num, std) {
	num = num.split('.')[0];
	num = num.split(',')[0];
	num = num.replace(/[^0123456789]/g, '');
	num = parseInt(num, 10);
	if (num > 0.9) {
		return num;
	}
	return std;
}

function handleLogout() {
	
	if (!$('WKAnzPos')) return true;
	
	if (parseInt($('WKAnzPos').innerHTML) == 0) return true;
	
	new Ajax.Updater('layer_logout', '/ajax/logout/index.html', {
		onComplete : function(tp) {
			fadeOut(true);
			$('layer_logout').show();
			$('eklselect').style.visibility = '';
			centerElement('layer_logout', true);	
		}
	});
	
	return false;
	
}
								
function addWK(artnr, menge) {
	if (wkto) {
    clearTimeout(wkto);
    wkto = null; 
  }
	if (!artnr) {
		var params = '';
		var suffix = '';
		var arts = document.getElementsByTagName('input');
		var anz = 0;
		var menge = 0;
		var timeout = 2000;
		for (var i = 0; i < arts.length; i++) {
			if (arts[i].name.indexOf('t_Menge_') == 0 && arts[i].value > '') {
				suffix = arts[i].name.substr(8);
				arts[i].value = checkNumber(arts[i].value, 0);
				params += 't_Artikel_' + suffix + '=' + $F('t_Artikel_' + suffix) + '&t_Menge_' + suffix + '=' + $F(arts[i]) + '&';
				anz++;
				menge = $F(arts[i]);
				$(arts[i]).value = '';
			}
		}
		if (anz == 1){
			params += 't_Anz=' + menge + '&';
		}
		else{
			params += 't_AnzProd=' + anz + '&';
		}
		new Ajax.Request('/ajax/updatewk/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=mergeWK&' + params, {
			async: true,
			onSuccess: function(tp) {
				// Message anzeigen
				$('wkmsg').innerHTML = tp.responseText.split('\n')[0];
				
				if (tp.responseText.split('\n')[1].split('|')[2] == 'TRUE') {
					Element.show('addedExisting');
					timeout = 5000;
				} else {
					Element.hide('addedExisting');
				}
				
				Element.show('helplayer1');
				centerElement('helplayer1');
				wkto = setTimeout(function() {
					Element.hide('helplayer1');
				}, timeout);
				
				// WK-Anzeige updaten
				$('WKAnzPos').innerHTML = tp.responseText.split('\n')[1].split('|')[0];
				$('WKTotal').innerHTML = tp.responseText.split('\n')[1].split('|')[1];
			}
		});
	} else {
		new Ajax.Request('/ajax/updatewk/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=addArtikelToWK&t_Artikel=' + artnr + '&t_Menge=1', {
			async: true,
			onSuccess: function(tp) {
				// Message anzeigen
				$('wkmsg').innerHTML = tp.responseText.split('\n')[0];
				
				if (tp.responseText.split('\n')[1].split('|')[2] == 'TRUE') {
					Element.show('addedExisting');
					timeout = 5000;
				} else {
					Element.hide('addedExisting');
				}
				
				Element.show('helplayer1');
				centerElement('helplayer1');
				wkto = setTimeout(function() {
					Element.hide('helplayer1');
				}, timeout);
				
				// WK-Anzeige updaten
				$('WKAnzPos').innerHTML = tp.responseText.split('\n')[1].split('|')[0];
				$('WKTotal').innerHTML = tp.responseText.split('\n')[1].split('|')[1];
			}
		});
	}
}

function setWarenkorbPersistent(YESorNO) {
	if (YESorNO == 'yes' || YESorNO == 'no') {
		new Ajax.Request('/ajax/standard/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=WKpersistent&t_WarenkorbPersistent=' + YESorNO, { async: true } );
	} else {
		// Falsche Option mitgegeben.
	}
}

function setWKpersistence() {
	if ($('persistentWK').value == '1') {
		$('persistentWKButton').checked = false;
		$('persistentWK').value = '';
		new Ajax.Request('/ajax/standard/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=persistWK&t_WarenkorbPersistent=no', { async: true } );
	} else {
		$('persistentWKButton').checked = true;
		$('persistentWK').value = '1';
		new Ajax.Request('/ajax/standard/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=persistWK&t_WarenkorbPersistent=yes', { async: true } );
	}
}

function setPersistentWK(lever) {
	if (!lever || ! (lever == 'yes' || lever == 'no') ) return false;

	var antilever;
	
	if (lever == 'yes') antilever = 'no';
	if (lever == 'no') antilever = 'yes';

	var url = '/ajax/standard/index.html?i2cms_i2connect=i2c_container.i2connect()&t_Methode=persistWK&t_WarenkorbPersistent=';

	url = url + lever;
		
	new Ajax.Request(url, { 
		async: true,
		onSuccess: function(tp) {
			$('wk_persist_' + lever).style.display = 'none';
			$('wk_persist_' + antilever).style.display = '';
		}
	} );	
}

function addEKL(loopid) {
	if (loopid == '') {
		var params = '';
		var suffix = '';
		var arts = document.getElementsByTagName('input');
		var anz = 0;
		var menge = 0;
		for (var i = 0; i < arts.length; i++) {
			if (arts[i].name.indexOf('t_Menge_') == 0 && arts[i].value > '') {
				suffix = arts[i].name.substr(8);
				arts[i].value = checkNumber(arts[i].value, 0);
				params += 't_Artikel_' + suffix + '=' + $F('t_Artikel_' + suffix) + '&t_Menge_' + suffix + '=' + $F(arts[i]) + '&';
				anz++;
				menge = $F(arts[i]);
				$(arts[i]).value = '';
			}
		}
		params += 't_AnzProd=' + anz + '&';
		document.location.href = '/myshop/einkaufsliste/index.html?i2cms_i2connect=i2c_ekl.i2connect()&t_Method=updateContainer&t_CacheType=1&t_FromCon=EKLTEMP&t_Insert=true&' + params;
	} else {
		if (!menge && $('t_Artikel' + loopid) && $('t_Menge' + loopid)) {
			menge = $F('t_Menge' + loopid);
			loopid = $F('t_Artikel' + loopid);
		}
		
		if (!menge) menge = 1;

		document.location.href = '/myshop/einkaufsliste/index.html?i2cms_i2connect=i2c_ekl.i2connect&t_Method=addArtikelToContainer&t_Artikel=' + loopid + '&t_Menge=' + menge + "&t_EKLID=EKLTEMP&t_CacheType=1&t_Insert=true";
		
	}
}

function changeMengeWK(menge, zeile) {
	$('t_Menge_' + zeile).value = menge;
	$('warenkorb').submit();	
};

function toggleStaffel(artikelnr, loop) {
	
	var content = $('splayer1');
	
	var url = '/ajax/staffelpreise/index.html?t_ArtikelNr=' + artikelnr;
		
	document.body.style.cursor = "progress";
	
	new Ajax.Request(url, {
		async: true,
		onSuccess: function(data) {
			document.body.style.cursor = "";
			$('spmsg').innerHTML = data.responseText;
			Element.show(content);
			centerElement('splayer1');
			wkto = setTimeout(function() {
				Element.hide('splayer1');
			}, 10000);
    }
	});
}
