// JavaScript Document zur Steuerung der Navigation
// bei Deaktivierung von Javascript ist die Navigation voll funktionsfaehig

function openPopup(theUrl, theWindowName, theWidth, theHeight){
    lWindowLook = "height="+theHeight+",width="+theWidth+",status=yes,toolbar=no,menubar=yes,scrollbars=yes,location=no,resizable=yes,top=1,left=1";
    var lWin = window.open(theUrl, theWindowName , lWindowLook);
}





/* -- -------- oeffnet neues Fenster fuer den Start des sdi.mapClient------- */
/* --------------------------------------------------------------------------------------------- */
function callMapClient(mapclient, url, type, action) {
                completeUrl = mapclient+"?service_url="+url+"&service_type="+type+"&action="+action+"&layers=preselection:off";
                window.open(completeUrl ,'Mapclient', "height=600,width=800,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,dependent=yes,top=1,left=1");
}



/* --Funktion zur Steuerung der abgespeicherten Suchanfragen -->Suchanfrage starten/ --> Suchanfrage loeschen -- */
/* -------------------------------------------------------------------------------------------------------------- */

function controlStoredQueries (theAction, theTarget){

	if (document.getElementById('storedQueryForm')){
		var theActionValue = theAction;
			if (theActionValue != ""){
				document.getElementById('storedQueryForm').action = theActionValue
				}
			document.getElementById('storedQueryForm').target=theTarget;
			document.getElementById('storedQueryForm').submit();
		}
}


/* ----------------------Funktion zur Steuerung des einstellen und loeschen von Templates  ---------------------- */
/* -------------------------------------------------------------------------------------------------------------- */
function controlTemplateQueries(theAction){
    document.getElementById('executeType').value = theAction
    document.getElementById('saveTemplateForm').submit();
}


/* --Funktion zur Steuerung der Formularelemete bei Aufruf einer abgespeicherten Suchanfrage----------------------*/
/* -------------------------------------------------------------------------------------------------------------- */

function controlSearchFieldsByOnload(){

		var serviceType = false;
		var dataType = false;
		var applicationType = false;

		if (document.getElementsByName)	{

				if(document.getElementsByName('hierarchyLevel')){

						var theObj = document.getElementsByName('hierarchyLevel');

								for (var i=0; i < theObj.length; i++ ){

										if ((theObj[i].value == "csw:dataset") && (theObj[i].checked == true)){
											dataType = true;
											}

										if ((theObj[i].value == "csw:service") && (theObj[i].checked == true)){
											serviceType = true;
										}

										if ((theObj[i].value == "csw:application") && (theObj[i].checked == true)){
											applicationType = true;
										}
							}

							if (dataType && !serviceType){

								controlDataType(true);
								controlTopicCategorieFields(false);
								controlGeoSerachField(true);

								}

							if (!dataType && serviceType){

							    if(!applicationType){
								    controlDataType(false);
								    controlTopicCategorieFields(true);
								    controlGeoSerachField(false);
							    }else{
								    controlDataType(true);
								    controlTopicCategorieFields(true);
								    controlGeoSerachField(true);
							    }


								}
					}
			}

		serviceType = false;
		dataType = false;
		applicationType = false;
	}

/* --Funktion zur Steuerung der Suchanfragen -->Suchanfrage starten/ --> Suchanfrage loeschen ------------------- */
/* -------------------------------------------------------------------------------------------------------------- */

function controlSearchFields(theObj){

		var theObjValue =  theObj.value;
		var theTypObj = document.getElementsByName(theObj.name);

		var counter = 0;
		var hits = 0
		var checkedFields =0;
		var theFieldArray = new Array();

		while(counter < theTypObj.length){
			if (theTypObj[counter].checked == true){
				 checkedFields = checkedFields+1;
				 theFieldArray[checkedFields] = theTypObj[counter].value//
				}
			counter++;
	    }


        if (checkedFields <= 1)	{

            if (theFieldArray[checkedFields] == "csw:dataset") {// Daten
                    controlDataType(true);
                    controlTopicCategorieFields(false);
                    controlGeoSerachField(true);
                }


            if (theFieldArray[checkedFields] == "csw:service"){// Dienste;
                    controlDataType(false);
                    controlTopicCategorieFields(true);
                    controlGeoSerachField(false);
                }

            if (theFieldArray[checkedFields] == "csw:application"){// Applicationen;
                    controlDataType(true);
                    controlTopicCategorieFields(false);
                    controlGeoSerachField(true);
                }


            if (theFieldArray[checkedFields] == null){// nichts ausgewählt
                controlDataType(true);
                controlTopicCategorieFields(true);
                controlGeoSerachField(true);
                }

        }

        if (checkedFields == 2)	{ // Kombinatorik testen

            if ((theFieldArray[1] == "csw:service") || (theFieldArray[2] == "csw:service") ){// Dienste;

                    controlDataType(false);
                    controlTopicCategorieFields(false);
                    controlGeoSerachField(true);
            }else{
                    controlDataType(true);
                    controlTopicCategorieFields(false);
                    controlGeoSerachField(true);
            }


        }

        if (checkedFields == 3)	{ // alle anstellen
            controlDataType(false);
            controlTopicCategorieFields(false);
            controlGeoSerachField(true);
        }

}

function controlSelectedFields(theHlvObject, theFormElement, theHlvMessage, theDateMessage){

    var theHlvObject = document.getElementsByName(theHlvObject);
    var counter = 0;
    var theHlvUncheckedCounter = 0;
    var theMessageString = '';

    while(counter < theHlvObject.length){
        if(theHlvObject[counter].checked == true){
            theHlvUncheckedCounter = theHlvUncheckedCounter+1;
        }
        counter++;
    }

    var theDateCounter = 0;
    if((document.getElementById('tillMonth')) && (document.getElementById('tillYear')) && (document.getElementById('toMonth')) && (document.getElementById('toYear'))){
        for(var i=0; i<document.getElementById('tillMonth').options.length; i++){
            if(document.getElementById('tillMonth').options[i].selected == true){
                if(document.getElementById('tillMonth').options[i].value != '-1'){
                    theDateCounter = theDateCounter + 1;
                }
            }

        }
        for(var i=0; i<document.getElementById('tillYear').options.length; i++){
            if(document.getElementById('tillYear').options[i].selected == true){
                if(document.getElementById('tillYear').options[i].value != '-1'){
                    theDateCounter = theDateCounter + 1;
                }
            }

        }
        for(var i=0; i<document.getElementById('toMonth').options.length; i++){
            if(document.getElementById('toMonth').options[i].selected == true){
                if(document.getElementById('toMonth').options[i].value != '-1'){
                    theDateCounter = theDateCounter + 1;
                }
            }

        }
        for(var i=0; i<document.getElementById('toYear').options.length; i++){
            if(document.getElementById('toYear').options[i].selected == true){
                if(document.getElementById('toYear').options[i].value != '-1'){
                    theDateCounter = theDateCounter + 1;
                }
            }

        }

    }

    //Box werfen
    if((theHlvUncheckedCounter == 0) || (theDateCounter > 0 && theDateCounter < 4 )){

        if(theHlvUncheckedCounter == 0){
            theMessageString =  theHlvMessage + "\n";
        }if(theDateCounter > 0 && theDateCounter < 4 ){
            theMessageString = theMessageString + theDateMessage;
        }
        alert(theMessageString);
        return false;
    }else{
        return true;
        //document.getElementById(theFormElement).submit();
    }

}


/* --  Funktion Aussschalten der Diensteselektionsliste bei Datentyp Daten   -- */
/* ---------------------------------------------------------------------------- */
var theArray = new Array();

function controlDataType(theStatus){
		var theSelectTag = document.getElementById('serviceType');
		var theOptionTag = theSelectTag.options;
		theSelectTag.disabled = theStatus;

		if (theArray.length == 0){
			theArray = storeServiceTypeList();
		}

		for (var i=0; i < theOptionTag.length; i++ ){
				if (theOptionTag[i].selected == true){
					theOptionTag[i].value = theArray[i];
					}
		}

		if (theStatus == true){
				for (var i=0; i < theOptionTag.length; i++ ){
						if (theOptionTag[i].selected == true){
							theOptionTag[i].value = "";
							}
				}

			}

		if (theStatus == false){
			}
	}




/* --  Funktion Speichert die Werte aus der Selektionsliste der Dienstearten   -- */
/* ------------------------------------------------------------------------------ */
function storeServiceTypeList(){

	var theSelectTag = document.getElementById('serviceType');
	var theOptionTag = theSelectTag.options
	var theOptionValues = new Array();

	for (var i=0; i < theOptionTag.length; i++ ){
			theOptionValues[i] = theOptionTag[i].value;					}

	return theOptionValues;
}




/* --  Funktion holt sich den Feld-Wert für Geo-Suche und fügt diesen der erweiterten Such hinzu  -- */
/* ------------------------------------------------------------------------------------------------ */

function transferGeoSearchValue(){

	if (parent.queryGeoFrame){
		var theFrame= parent.queryGeoFrame;

		if (theFrame.document.getElementById('geographicDescriptionCodeField')){
			var theField = theFrame.document.getElementById('geographicDescriptionCodeField');
    		document.getElementById('geographicDescriptionCode').value = theField.value;
		}
	}
}

/* --  Funktion zur Aus-/Einschaltung der Fachkategorien in der erweiterten Suche ----------------- */
/* ------------------------------------------------------------------------------------------------ */

function controlTopicCategorieFields(theStatus){

	if (document.getElementsByName('topicCategory')){

		var theField = document.getElementsByName('topicCategory');
		var counter = 0

		while(counter < theField.length){

			if (theStatus == false){

				theField[counter].disabled = theStatus // aktiviert die Checkboxen

				} else {

				theField[counter].disabled = theStatus; // graut die Checkboxen aus
				theField[counter].checked = false;// deselektiert die Checkboxen
				}
			counter++;
			}
		}
}

/* --  Funktion zur Aus-/Einschaltung der Geo Suche ----------------------------------------------- */
/* ------------------------------------------------------------------------------------------------ */

function controlGeoSerachField(theStatus){
		if (document.getElementById('geographicDescriptionCode')){

			var theField = document.getElementById('geographicDescriptionCode');
			if (theStatus == false){

				theField.value = "";
				theField.disabled = true;
				theField.className = "disabledField";

				} else {

				theField.disabled = false;
				theField.className = "enabledField";

				}
		}
}

/* --  Funktion zur Ein- und Aschalten der vorbelegten Feldwerte ------------------- */
/* --------------------------------------------------------------------------------- */

function controlFieldValues(){
	if (document.getElementsByTagName("input")){

		var theInputTags = document.getElementsByTagName("input");

			for (i=0; i< theInputTags.length; i++){

					if (theInputTags[i].getAttribute("type") == "text"){
							theInputTags[i].onfocus = function() {
									focusFieldValue(this);
								}
							theInputTags[i].onblur = function() {
									blurFieldValue(this);
								}

						}
				}
		}
}


/* --  Funktion zur Ein- und Aschalten der vorbelegten Feldwerte ------------------- */
/* --------------------------------------------------------------------------------- */

function controlTextAreaValues(){
	if (document.getElementsByTagName("textarea")){

		var theInputTags = document.getElementsByTagName("textarea");

		for (i=0; i< theInputTags.length; i++){

			theInputTags[i].onfocus = function() {
					focusFieldValue(this);
				}
			theInputTags[i].onblur = function() {
					blurFieldValue(this);
			}
		}
	}
}

var theStoredValue;
/* --  Funktion loescht den belegten Wert eines Eingabefelds bei Focus ----------------------- */
/* ------------------------------------------------------------------------------------------- */
function focusFieldValue(theObj){
	theStoredValue = theObj.value;
	theObj.value = ""; // Eingabewert löschen;
	}

/* --  Funktion fuegt den Wert bei Verlassen des Feldes wieder ein  --------------------------- */
/* --------------------------------------------------------------------------------------------- */
function blurFieldValue(theObj){
	if (theObj.value == ""){
		//theObj.value = theStoredValue; // Eingabewert wieder auffüllen;
		theStoredValue = "";
		}
}

/* --  Container-Funktion zum Handeln verschiedener Subfunktionen in der erweiterten Suche  ---- */
/* --------------------------------------------------------------------------------------------- */

function controllExtendedSearch(theUrl){

	if (document.getElementById){
			controlSearchFieldsByOnload();			
			//pushGeoString(theGeoString);
			sendParamsToIMS(theUrl)


		}
	}

/* --  Funktion fuellt das GeoSuchFeld im queryGeoFrame  -------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
function pushGeoString(theGeoString){

	if (parent.queryGeoFrame){
			var theFrame= parent.queryGeoFrame;

			if (theFrame.document.getElementById('geographicDescriptionCodeField')){
				var theField = theFrame.document.getElementById('geographicDescriptionCodeField');

				theField.value = theGeoString;

			}
	 }
}

/* --  Funktion schickt einen Aufruf aus der erweiterten Suche an das IMC------------------------ */
/* --------------------------------------------------------------------------------------------- */
function sendParamsToIMS(theUrl){


    parent.mapFrame.frames['MapFrameSet'].frames['IMCMapFrame'].location.href=theUrl;
	//var IMCMapFrame = parent.mapFrame.MapFrameSet.IMCMapFrame;

		//alert(IMCMapFrame);

		//alert(theXa1+"\n"+theXa2+"\n"+theYa1+"\n"+theYa2+"\n"+theXb1+"\n"+theXb2+"\n"+theYb1+"\n"+theYb2);

}

/* --  Funktion schickt einen Aufruf aus der Trefferseite fuer das Loeschen der Metadaten------- */
/* --------------------------------------------------------------------------------------------- */
function deleteMetadata(theUrl, theMessage){
    var Check = confirm(theMessage);

        if (Check) {
            location.href = theUrl
            Check = false;
        } else if (!Check){
            return;
        }
}

/* --  Funktion zur Ermittlung der Browser-Fensterhöhe ----------------------------------------- */
/* --------------------------------------------------------------------------------------------- */

function checkScreenHeight(){

	if (window.innerHeight){	// MOZ & Co.
			//alert("MOZ: "+ window.innerHeight)
			return window.innerHeight;
		} else if (document.all.indexBody && document.all.indexBody.offsetHeight){ // IE
			//alert("IE: "+ document.all.indexBody.offsetHeight);
			return document.all.indexBody.offsetHeight;
		} else return 0;
}


function updateContentWrapperHeight(theEvent){

	if (document.getElementById("contentWrapper")){

		var theHeaderHeight = 110;

		var theObj = document.getElementById("contentWrapper")
		var theHeight = checkScreenHeight()-theHeaderHeight;

			if (theHeight < 530){
				theHeight = 530

				};
		theHeight = theHeight.toString();
		theHeight = theHeight+'px';

			if (window.innerHeight){
				theObj.style.height = theHeight;
			}

			if ((theEvent == "resize") && document.all.indexBody && document.all.indexBody.offsetHeight){ // IE
				//location.reload();
			}
		}

}

/* --  Editor MD Preview ----------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */

		// Funktion stellt die MD der Preview - Seite auf unsichtbar

		function hideTable(){
		 	var theTables;
			theTables = document.getElementsByTagName('table');
			for (var i=0; i<theTables.length;i++){
				theTables[i].style.display = "none";
			}
		}

		// Funktion zeigt MD der in der Preview an

		function showMD(theMD){
			var theTable;
			hideTable();
			theTable = document.getElementById(theMD);
			theTable.style.display = "block";
		}


		function highlightLink(theObj){
			var theLinks = document.getElementById('queryTab');
			var theOtherLinks
			theLinks = theLinks.getElementsByTagName('a');


			for (var i=0; i<theLinks.length; i++){
					theLinks[i].onclick = function(){
						theOtherLinks = this.parentNode.parentNode;
						theOtherLinks = theOtherLinks.getElementsByTagName('LI')
						for (var z=0; z<theOtherLinks.length; z++){	// stellt den Hintergrund wieder auf default
							theOtherLinks[z].style.backgroundColor = "#EBF1FD"
						}
						this.parentNode.style.backgroundColor ="#D3DDF3";
					}
			}

		}

/* --  checks, if AnyText was selected or none ---------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
function checkAnyTextEvent(theObj){
    var lAnyTextVisOperator = document.getElementById('AnyTextVisOperator');
    var lAnyTextVisField = document.getElementById('AnyTextVisField');
    var lRegularVisField = document.getElementById('RegularVisField');

    var lFuzzySearch = document.getElementById('fuzzySearch');

    if((lAnyTextVisOperator) && (lAnyTextVisField) && (lRegularVisField)){
        if(theObj.checked == true){
            document.getElementById('TITLE').value='';
            document.getElementById('SUBJECT').value='';
            document.getElementById('ORGANISATIONNAME').value='';
            lAnyTextVisOperator.style.display='block';
            lAnyTextVisField.style.display='block';
            lRegularVisField.style.display='none';
        }else if(theObj.checked == false){
            document.getElementById('ANYTEXT').value='';
            lAnyTextVisOperator.style.display='none';
            lAnyTextVisField.style.display='none';
            lRegularVisField.style.display='block';
        }

    }

}



