var cmpProducts = new Array();

Array.prototype.indexOf = function(a) {
	for (var b = 0; b < this.length; b++) {
		if (this[b] === a) return b;
	}
	return false;
}
Array.prototype.contains = function(a) {
	for (var b = 0; b < this.length; b++) {
		if (this[b] === a) return true;
	}
	return false;
}
Array.prototype.remove = function(a) {
	this.splice(a, 1);
	return this.length;
}

function chgSortLocation(a) {
	var b = a.options[a.selectedIndex].value;
	if (b != '') window.location = b;
}

function chgCmpProducts(a) {
	if (a.checked) {
		if (cmpProducts.length >= 4) { 
			alert('Es können maximal 4 Produkte verglichen werden.'); 
			a.checked = false;
			return;
		}
		if (!cmpProducts.contains(a.value)) cmpProducts.push(a.value);
		return;
	}
	if (!cmpProducts.contains(a.value)) return;
	cmpProducts.remove(cmpProducts.indexOf(a.value));
}

function chgCmpLocation(a) {
	if (!cmpProducts.contains(a) && cmpProducts.length == 0) {
		cmpProducts.push(a);
		var c;
		if (c = document.getElementById('cmp_' + a)) c.checked = true;
		return;
	}
	if (!cmpProducts.contains(a) && cmpProducts.length < 4) cmpProducts.push(a);
	if (cmpProducts.length <= 1) { 
		alert('Bitte wählen Sie mindestens 2 Produkte.'); 
		return;
	}
	window.location = script_path + '/produktvergleich/' + cmpProducts.join('-');
}


function HelpText() {
	this.current = null;
}

HelpText.prototype.AddEvent = function(el, ev, hdl) {
	if (el.addEventListener) {
		el.addEventListener(ev, hdl, null);
		return true;
	}
	el['on'+ev] = hdl;
}

HelpText.prototype.GetPos = function(o) {
	var al	= 0;
	var at	= 0;
	while (o) {
		al	+= o.offsetLeft;
		at	+= o.offsetTop;
		if (o.clientTop)	at	+= o.clientTop;
		if (o.clientLeft)	al	+= o.clientLeft;
		o = o.offsetParent;
	}
	return [al, at];
}

HelpText.prototype.Show = function(n) {
	p = this.GetPos(n);
	this.CreateBox(n);
	n.div.style.left= p[0] + 30 + 'px';
	n.div.style.top	= p[1] + 'px';
	n.div.style.visibility = 'visible';
}

HelpText.prototype.Hide = function(n) {
	n.div.style.visibility = 'hidden';
}

HelpText.prototype.CreateBox = function(n) {
	if (n.div) return;
	n.div = document.createElement('div');
	n.div.className				= 'helptext';
	if (n.id && helptexts && helptexts[n.id]) {
		n.div.innerHTML = helptexts[n.id];
	}
	else {
		n.div.appendChild(document.createTextNode(text));
	}
	n.title = '';
	document.getElementsByTagName('body')[0].appendChild(n.div);
}


HelpText.prototype.Go = function() {
	var imgs = document.getElementsByTagName('img');
	for (var i = 0; i < imgs.length; i++) {
		if (imgs[i].className == 'helptext') {
			this.AddEvent(imgs[i], 'mouseover',	function() { helptext.Show(this); })
			this.AddEvent(imgs[i], 'mouseout',	function() { helptext.Hide(this); })
		}
	}
}


