function ShowACWindowHelp() {

	var height = setLayerState('block', 'acWindowHelp');
	setFrameState('block', height);
	setLayerState('none', 'acWindow');
}


function ShowACWindow() {

	var height = setLayerState('block', 'acWindow');
	setFrameState('block', height);
	setLayerState('none', 'acWindowHelp');
	updateCounter();
}

function filterCats() {

	var all_merchants = document.getElementById("mer_list_all");
	var target = document.getElementById("acFMerchants");
	var sel = document.getElementById("catSel");
	target.options.length = 0;
	if (sel.value == "") {
		for (var i = 0; i < all_merchants.options.length; i++) {
			target.options[i] =
					new Option(all_merchants.options[i].text, all_merchants.options[i].value);
		}
	}
	else {
		var ids = sel.value.split(",");
		var last = -1;
		for (var i = 0; i < all_merchants.options.length; i++) {
			if (inArray(all_merchants.options[i].value, ids)) {
				last++;
				target.options[last] =
						new Option(all_merchants.options[i].text, all_merchants.options[i].value);
			}
		}
	}
	updateCounter();
}


function inArray(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
		if (haystack[i] == needle) {
			return true;
		}
	}
	return false;
}

function setTopMerchants(id_list) {

	//var source = document.getElementById("mer_list");
	var source = document.acForm.acFMerchants;
	var target = document.acForm.acTMerchants;
	moveMerchantsList(source, target, 50, id_list);
	//if (sortBox(target, "asc")) {}
	updateCounter();
	return false;
}

function HideACWindow() {

	setLayerState('none', 'acWindowHelp');
	setLayerState('none', 'acWindow');
	setLayerState('none', 'acFrame');
}

function setFrameState(display, height) {

	var fra = document.getElementById('acFrame');
	fra.style.top = document.documentElement.scrollTop + 30 + 'px';
	fra.style.height = height;
	fra.style.display = display;
}

function setLayerState(display, id) {

	var layer = document.getElementById(id);
	layer.style.top = document.documentElement.scrollTop + 30 + 'px';
	layer.style.display = display;
	return layer.offsetHeight;
}

function addMerchantsList(list) {

	ShowACWindow();
	// Formulardaten holen
	if (document.iForm.iEmail != null)
		document.acForm.tEmail.value = document.iForm.iEmail.value;
	if (document.iForm.iFreq != null)
		document.acForm.tFreq.value = document.iForm.iFreq.value;
	if (document.iForm.iName != null)
		document.acForm.tName.value = document.iForm.iName.value;
	if (document.iForm.iVorname != null)
		document.acForm.tVorname.value = document.iForm.iVorname.value;
	if (document.iForm.iAnrede != null)
		document.acForm.tAnrede.value = document.iForm.iAnrede.value;
	var source = document.acForm.acFMerchants;
	var target = document.acForm.acTMerchants;
	moveMerchantsList(source, target, 50, list);
	//if (sortBox(target, "asc")) {}
	updateCounter();
	return false;
}

function subscribeMerchant(text, id, preset) {

	// Formulardaten holen
	if (preset) {
		if (! testValues("i"))
			return false;
		if (document.iForm.iEmail != null)
			document.acForm.tEmail.value = document.iForm.iEmail.value;
		if (document.iForm.iFreq != null)
			document.acForm.tFreq.value = document.iForm.iFreq.value;
		if (document.iForm.iName != null)
			document.acForm.tName.value = document.iForm.iName.value;
		if (document.iForm.iVorname != null)
			document.acForm.tVorname.value = document.iForm.iVorname.value;
		if (document.iForm.iAnrede != null)
			document.acForm.tAnrede.value = document.iForm.iAnrede.value;
		if (document.iForm.iC4u != null)
			document.acForm.tC4u.checked = document.iForm.iC4u.checked;
	}
	ShowACWindow();
	var source = document.acForm.acFMerchants;
	var target = document.acForm.acTMerchants;
	moveMerchants(source, target, 50, text, false);
	//if (sortBox(target, "asc")) {}
	updateCounter();
	return false;
}

function subscribeSelectedMerchants() {

	document.body.style.cursor = 'wait';
	var source = document.acForm.acFMerchants;
	var target = document.acForm.acTMerchants;
	moveMerchants(source, target, 50, '', true);
	updateCounter();
	//if (sortBox(target, "asc")) {}
	document.body.style.cursor = 'default';
}

function unSubscribeSelectedMerchants() {

	document.body.style.cursor = 'wait';
	var target = document.acForm.acFMerchants;
	var source = document.acForm.acTMerchants;
	moveMerchants(source, target, 0, '', true);
	updateCounter();
	//if (sortBox(target, "asc")) {}
	document.body.style.cursor = 'default';
}

function storeList() {

	var target = document.acForm.acTMerchants;
	if (target.options.length == 0) {
		alert("Um den Gutscheinwecker zu abonnieren, müssen Sie mindestens einen Anbieter dafür " +
				"auswählen. Markieren Sie dazu Ihre gewünschten Anbieter in der Liste der " +
				"verfügbaren Anbieter (links) und fügen Sie diese durch klicken auf den Pfeil " +
				"der Liste der Abonnierten zu.");
		return false;
	}
	if (! testValues("t"))
		return false;
	var list = "";
	for (var i = 0; i < target.options.length; i++) {
		if (list != "") {
			list += ",";
		}
		list += target.options[i].value;
	}
	document.acForm.acTMerList.value = list;
	return true;
}

function testValues(prefix) {

	var msg = "";
	if (document.getElementById(prefix + "Anrede").value == "n") {
		msg += "Bitte wählen Sie eine Anrede.\n\n";
	}
	if (document.getElementById(prefix + "Name").value == "" ||
			document.getElementById(prefix + "Vorname").value == "") {
		msg += "Bitte geben Sie Ihren Vor- und Nachnamen ein.\n\n";
	}
	if (document.getElementById(prefix + "Email").value == "") {
		msg += "Bitte geben Sie Ihre E-Mail-Adresse ein.";
	}
	if (msg != "") {
		alert(msg);
		return false;
	}
	return true;
}

// --------------------------- --------------------------------------------------------------------
function updateCounter() {

	document.getElementById('acFCount').firstChild.data =
			document.acForm.acFMerchants.options.length;
	document.getElementById('acTCount').firstChild.data =
			document.acForm.acTMerchants.options.length;

}

function moveMerchants(source, target, max_target_items, text, sel) {

	var too_many = false;
	for (var i = 0; i < source.options.length; i++) {
		if (moveThis(source.options[i], text, sel)) {
			if (max_target_items > 0 && target.options.length >= max_target_items) {
				too_many = true;
			}
			else {
				currOption = new Option(source.options[i].text, source.options[i].value, false,
						false);
				addMerchant(currOption, target);
				removeMerchant(i, source);
				i--;
			}
		}
	}
	if (too_many) {
		alert("Sie können höchstens " + max_target_items + " Anbieter abonnieren.");
	}
}

function moveMerchantsList(source, target, max_target_items, list) {

	var ids = list.split(',');
	var too_many = false;
	for (var i = 0; i < source.options.length; i++) {
		for (var j = 0; j < ids.length; j++) {
			if (source.options[i].value == ids[j]) {
				if (max_target_items > 0 && target.options.length >= max_target_items) {
					too_many = true;
				}
				else {
					currOption = new Option(source.options[i].text, source.options[i].value, false,
							false);
					addMerchant(currOption, target);
					removeMerchant(i, source);
					i--;
				}
			}
		}
	}
	if (too_many) {
		alert("Sie können höchstens " + max_target_items + " Anbieter abonnieren.");
	}
}

function moveThis(currOption, text, sel) {

	if (sel) {
		return currOption.selected;
	}
	else {
		return (currOption.text == text);
	}
}

function addMerchant(addOption, box) {
	
	var insertBefore = inBox(box, addOption.value, addOption.text);
	if (insertBefore == false) {
		return false;
	}
	if (isIE) {
		box.add(addOption, insertBefore[1]);
	}
	else {
		try {
			box.add(addOption, insertBefore[0]);
	    }
    	catch(ex) {
			box.add(addOption, null);
		}
    }
}

function removeMerchant(i, box) {
	box.options[i] = null;
}

function inBox(box, testValue, testText) {

	var retValue = new Array(null, box.options.length);
	for (var i = 0; i < box.options.length; i++) {
		if (box.options[i].text.toLowerCase() > testText.toLowerCase() && retValue[0] == null) {
			retValue[0] = box.options[i];
			retValue[1] = i; 
		}
		if (box.options[i].value == testValue) {
			return false;
		}
	}
	return retValue;
}

function sortBox(box, direction) {

	if (box.options.length == 1) {
		return true;
	}
	for (var r = box.options.length - 1; r > 0; r--) {
		for (var i = 0; i < r; i++) {
			var t1 = box.options[i].text;
			var t2 = box.options[i + 1].text;
			if (((direction == 'asc') && (t1 > t2)) || ((direction == 'desc') && (t1 < t2))) {
				var tmp_text = box.options[i].text;
				var tmp_value = box.options[i].value;
				box.options[i].text = box.options[i + 1].text;
				box.options[i].value = box.options[i + 1].value;
				box.options[i + 1].text = tmp_text;
				box.options[i + 1].value = tmp_value;
			}
		}
	}
	return true;
}

function removeOptgroup(number) {
	var el = document.getElementsByTagName("optgroup")[number];
	el.parentNode.removeChild(el);
	return false;
}

// =========================== ====================================================================
function ToggMer(id) {
	var _div = document.getElementById(id+'D');
	var _img = document.getElementById(id+'I');
	if (_div.style.display == 'block') {
		_div.style.display = 'none';
		_img.style.backgroundImage = 'url(img/none.gif);';
	}
	else {
		_div.style.display = 'block';
		_img.style.backgroundImage = 'url(img/block.gif);';
	}
}

function hide(id) {
	var element = document.getElementById(id);
	var linkShow = document.getElementById(id+'A');
	var linkShow2 = document.getElementById(id+'C');
	var linkHide = document.getElementById(id+'B');
	var linkHide2 = document.getElementById(id+'D');
	element.style.display = 'none';
	linkShow.style.display = 'block';
	linkShow2.style.display = 'block';
	linkHide.style.display = 'none';
	linkHide2.style.display = 'none';

	var drop = document.getElementById(id+'I');
	if (drop && document.getElementById(id+'P')) {
		document.getElementById(id+'P').removeChild(drop);
	}
	return false;
}

function surfto(form) {
	var myindex=form.s_anbieter.selectedIndex
	location=form.s_anbieter.options[myindex].value;
}
