function toggleDisplay(id)
{
	var element = window.document.getElementById(id);
	if(element.style.display == "none") {
		element.style.display = "block";
	} else {
		element.style.display = "none";
	}
}

function erwAddConstraint(id, value)
{
	var name = "clear-filter-constraint[" + id + "]";
	erwDeleteInput(name);
	name = "filter-constraint[" + id + "]";
	erwAddInput(name, value);
}

function addConstraint(id, value)
{
	var name = "clear-filter-constraint[" + id + "]";
	deleteInput(name);
	name = "filter-constraint[" + id + "]";
	addInput(name, value);
}

function addConstraintMultiple(id, value)
{
	var i = 0;
	var found;
	name = "filter-constraint[" + id + "]";

	for(i=0, found = false; !found; ++i) {
		var inputName = name + "[" + i + "]";
		var input = document.getElementById(inputName);
		if(input == null) {
			addInput(inputName, value);
			found = true;
		}
	}
}

function erwDeleteConstraint(id)
{
	var name = "filter-constraint[" + id + "]";
	erwDeleteInput(name);
	name = "clear-filter-constraint[" + id + "]";
	erwAddInput(name, id);
}

function deleteConstraint(id)
{
	var name = "filter-constraint[" + id + "]";
	deleteInput(name);
	name = "clear-filter-constraint[" + id + "]";
	addInput(name, id);
}

function toggleConstraint(chk, id, value)
{
	var cb = document.getElementById(chk);
	if(cb.checked) {
		addConstraint(id, value);
	  } else {
		deleteConstraint(id);
	}
}

function addInput(name, value)
{
	extAddInput("searchform-content", name, value);
}

function erwAddInput(name, value)
{
	extAddInput("detail-search-form", name, value);
}

function extAddInput(formname, name, value)
{
	var knoten = document.getElementById(name);
	if(knoten != null) {
		knoten.value = value;
	} else {
		var form = document.getElementById(formname);
		knoten = document.createElement("input");
		knoten.type = "hidden";
		knoten.id = name;
		knoten.name = name;
		knoten.value = value;
		form.appendChild(knoten);
	}
}

function deleteInput(name)
{
	extDeleteInput('searchform-content', name);
}

function erwDeleteInput(name)
{
	extDeleteInput('detail-search-form', name);
}

function extDeleteInput(formname, name)
{
	var knoten = document.getElementById(name);
	if(knoten != null) {
		var form = document.getElementById(formname);
		form.removeChild(knoten);
	}
}

function deleteInputMultiple(name)
{
	var i = 0;
	var found;
	var form = document.getElementById("detail-search-form");

	for(i=0, found = false; !found; ++i) {
		inputName = name + "[" + i + "]";
		var input = document.getElementById(inputName);
		if(input != null) {
			form.removeChild(input);
		} else {
			found = true;
		}
	}
}

function formSubmit()
{
	extFormSubmit("searchform-content");
}

function erwFormSubmit()
{
	extFormSubmit("detail-search-form");
}

function extFormSubmit(formname)
{
	/*
	var knoten = document.createElement("input");
	knoten.type = "hidden";
	knoten.name = "doSearch";
	knoten.value = true;

	var form = document.getElementById(formname);
	form.appendChild(knoten);
	 */

	extAddInput(formname, "doSearch", true);
	var form = document.getElementById(formname);

	form.submit();
}

function openSaveDialogue()
{
	if(isAngemeldet()) {
		if(document.getElementById('savedialogue').style.display == 'none') {
			document.getElementById('savedialogue').style.display = 'block';
		} else {
			document.getElementById('savedialogue').style.display = 'none';
		}
	}
}

function openSaveAboDialogue()
{
	if(isAngemeldetForMatching() && isOkForMatching()) {
		if(document.getElementById('saveabodialogue').style.display == 'none') {
			document.getElementById('saveabodialogue').style.display = 'block';
		} else {
			document.getElementById('saveabodialogue').style.display = 'none';
		}
	}
}

function detailOriginChanged()
{
	var chk = document.getElementById("detail-origin-international").checked;

	document.getElementById("detail-origin-gemany-state").disabled = chk;
	document.getElementById("detail-origin-germany-plz").disabled = chk;
	document.getElementById("detail-origin-germany-place").disabled = chk;

	document.getElementById("detail-origin-international-nation").disabled = !chk;
	document.getElementById("detail-origin-international-plz").disabled = !chk;
	document.getElementById("detail-origin-international-place").disabled = !chk;
}

function erwSucheSenden()
{
	// Suchbegriff:
	var begriff = document.getElementById("detail-search-search").value;
	if(document.getElementById("detail-search-options").value == "all") {
		// and Suche
	}
	addInput('searchform-content-search', begriff);

	// Bereich:
	var bereiche = 0;
	if(document.getElementById("detail-search-website").checked) bereiche |= 1;
	if(document.getElementById("detail-search-exhibitors-products").checked) {
		if(document.getElementById("detail-hits-type-1").checked) bereiche |= 2;
		if(document.getElementById("detail-hits-type-2").checked) bereiche |= 4;
		if(document.getElementById("detail-hits-type-3").checked) bereiche |= 8;
	}
	if(document.getElementById("detail-search-branches").checked) bereiche |= 16;
	if(document.getElementById("detail-search-events").checked) bereiche |= 32;
	addInput('searchform-content-category', bereiche);

	// Aussteller-/Produktoptionen
	if(document.getElementById("detail-search-exhibitors-products").checked) {
		if(document.getElementById("detail-origin-international").checked) {
			// international
			deleteConstraint(2);
			deleteConstraint(3);
			deleteConstraint(4);
			deleteConstraint(15);
			if(document.getElementById("detail-origin-international-nation").value != "false") {
				addConstraint(2, document.getElementById("detail-origin-international-nation").value);
			}
			if(document.getElementById("detail-origin-international-place").value.length > 0) {
				addConstraint(3, document.getElementById("detail-origin-international-place").value);
			}
		} else {
			// Deutschland
			deleteConstraint(2);
			deleteConstraint(3);
			deleteConstraint(4);
			deleteConstraint(15);

			addConstraint(2, "D");
			if(document.getElementById("detail-origin-gemany-state").value != "false") {
				addConstraint(15, document.getElementById("detail-origin-gemany-state").value);
			}
			if(document.getElementById("detail-origin-germany-plz").value.length > 0) {
				addConstraint(4, document.getElementById("detail-origin-germany-plz").value);
			}
			if(document.getElementById("detail-origin-germany-place").value.length > 0) {
				addConstraint(3, document.getElementById("detail-origin-germany-place").value);
			}
		}

		if(document.getElementById("detail-groups-search-selected").checked) {
			// "Produktgruppen Untergruppen";
			var gruppen = document.getElementsByName("chk-inner-options-hits-subgroups");
			var i;

			for(i=0; i<gruppen.length; ++i) {
				if(gruppen[i].checked) {
					addConstraint(1, gruppen[i].value);

					var untergruppen = document.getElementsByName("chk-" + gruppen[i].id);
					var j;
					for(j=0; j<untergruppen.length; ++j) {
						if(untergruppen[j].checked) {
							addConstraint(1, untergruppen[j].value);
						}
					}
				}
			}
		}

		if(document.getElementById("inner-options-hits-criteria-none").checked) {
			deleteConstraint(5);
			deleteConstraint(6);
		} else if(document.getElementById("inner-options-hits-criteria-news").checked) {
			deleteConstraint(5);
			addConstraint(6, "J");
		} else if(document.getElementById("inner-options-hits-criteria-oem").checked) {
			deleteConstraint(6);
			addConstraint(5, "J");
		} else if(document.getElementById("inner-options-hits-criteria-both").checked) {
			addConstraint(5, "J");
			addConstraint(6, "J");
		}
	}

	if(document.getElementById("detail-search-website").checked) {
		if(document.getElementById("inner-options-website-file-all").checked) deleteConstraint(12);
		if(document.getElementById("inner-options-website-file-html").checked) addConstraint(12, "html");
		if(document.getElementById("inner-options-website-file-pdf").checked) addConstraint(12, "pdf");
	}

	addInput('neue', true);
	addInput('reiter', 1);
	formSubmit();
}

//
// Listenoptionen
//
function addMerkInput(i, name)
{
	var input = document.getElementById(name + "-" + i);
	var name = "merk_" + name + "[" + i + "]";
	if(input==null) {
		addInput(name, "");
	} else {
		addInput(name, input.value);
	}
}

function trefferNichtMerken(i, merkid)
{
	var name = "rm_merk[" + i + "]";
	addInput(name, merkid);
}

function sucheNichtMerken(i, suchid)
{
	var name = "rm_srch[" + i + "]";
	addInput(name, suchid);
}

function changeHPP(num)
{
	var knoten = document.createElement("input");
	knoten.type = "hidden";
	knoten.name = "hitcount";
	knoten.value = num;

	var form = document.getElementById("searchform-content");
	form.appendChild(knoten);

	doSubmit();
}

function changeSort(order)
{
	var knoten = document.createElement("input");
	knoten.type = "hidden";
	knoten.name = "sortby";
	knoten.value = order;

	var form = document.getElementById("searchform-content");
	form.appendChild(knoten);

	doSubmit();
}

//
// searchouter
//

function showMeintenSie(srch)
{
	window.document.forms["searchform-content"]["searchform-content-search"].value = srch;
	doSubmit();
}

//
// Leftcol
//
function showCountry()
{
	var international = document.getElementById("international");
	if(international.checked) {
		document.getElementById("federal-countries").style.display = "none";
		document.getElementById("countries").style.display = "block";
	} else {
		document.getElementById("federal-countries").style.display = "block";
		document.getElementById("countries").style.display = "none";
	}
}

function setHerkunft()
{
	var plz = document.getElementById("zip");
	var ort = document.getElementById("place");

	if(plz.value=="") {
		deleteConstraint(4);
	} else {
		addConstraint(4, plz.value);
	}

	if(ort.value=="") {
		deleteConstraint(3);
	} else {
		addConstraint(3, ort.value);
	}

	var international = document.getElementById("international");
	if(international.checked) {
		deleteConstraint(15);
	} else {
		addConstraint(2, "D");
	}

	doSubmit();
}

//Matchmaking-Krams

$(document).ready(function()
{
	$("a.more-open").attr("href", "javascript://").click(function()
	{
		$(this).parent().fadeOut('fast', function()
		{
			$(this).next().fadeIn();
		});
	});

	$("a.more-close").attr("href", "javascript://").click(function()
	{
		$(this).parent().fadeOut('fast', function()
		{
			$(this).prev().fadeIn();
		});
	});
});

function mmOpenLayer(ID, DomPosition)
{
	var mmOpenLayerOffset = 420;
	var top;
	top = ($("#"+DomPosition).offset().top-mmOpenLayerOffset)+"px";
	$("#"+ID).css('top', top);
	$("#"+ID).css('zIndex', 5000);
	$("#"+ID).fadeIn('normal');
}

function mmCloseLayer(ID)
{
	layer = $("#"+ID).fadeOut('normal');
	$("#"+ID).css('zIndex', 0);
}

function mmOpenModalPopup(headContent,bodyContent)
{
	var xOffset = 200;
	var yOffset = -150;
	$("#popup-content").html('<span>'+headContent+'</span>'+'<p>'+bodyContent+'</p>');
	var posX = xOffset;
	var posY = window.getScrollXY()[1]+Math.floor((screen.height/2)-($("#mm-popup").height()/2)+yOffset);
	
	$("#mm-popup").css({
						'top': posY+'px',
						'left': posX+'px'
						});
	$("#mm-modal").css({
						'width':window.size().width+'px',
						'height':window.size().height+'px'
						});
	$("#mm-modal").show();
	$("#mm-popup").fadeIn('normal');
}

function mmCloseModalPopup() {
	$("#mm-popup").fadeOut('normal');
	$("#mm-modal").hide();
}

function mmOpenPopup(html, DomPosition)
{
	$("#popup-content").html(html);
	mmOpenLayer('mm-popup', DomPosition);
}

function mmClosePopup()
{
	mmCloseLayer('mm-popup');
}

