window.onresize = setResizeColorsettings;
window.onload = setLoadColorsettings;

//mozilla 1.5 css bug
if(navigator.appName == "Netscape"){
         var userAgent = navigator.userAgent;
	var result = userAgent.search(/Firefox.+/);
	if (result == -1){
         document.write("<STYLE TYPE='text/css' MEDIA='screen'>.infoimg {position: static;}</STYLE>");
         }
}
//**************************************************************************
// bildoptimierung bei veraenderter hg-farbe
//***************************************************************************

function setResizeColorsettings(){
	var myColor = document.bgColor;
	if(myColor != "#fffffe" && navigator.appVersion.indexOf("MSIE") != -1){
		document.getElementById("main").style.borderLeftWidth = 0 + "em";
		var myWidth = document.getElementById("main").offsetWidth;
		if(myWidth < 1400){
			document.getElementById("main").style.width = 83 + "em";
		}
		else{
			document.getElementById("main").style.width = 110 + "em";
		}
		document.getElementById("navigation").style.marginLeft = 0 + "em";
		document.getElementById("navigation").style.width = 15 + "em";
	}
}

function setLoadColorsettings(){
	var myColor = document.bgColor;
	if(myColor != "#fffffe" && navigator.appVersion.indexOf("MSIE") != -1){


		document.getElementById("navigation").style.marginLeft = 0 + "em";
		document.getElementById("navigation").style.width = 15 + "em";
		document.getElementById("main").style.borderLeftWidth = 0 + "em";
		var myWidth = document.getElementById("main").offsetWidth;
		if(myWidth < 1400){
			document.getElementById("main").style.width = 83 + "em";
		}
		else{
			document.getElementById("main").style.width = 110 + "em";
		}

	}
	//aufruf ueberpruefung form-elements
	checkFormElements();
}



//******************************************************************************
// formularscripte
//******************************************************************************

function resetSelect(whichForm,whichObj,whichID) {
	if(whichObj.value=='tt.mm.jjjj')whichObj.value='';
	document.forms[whichForm].elements[whichID].options[0].selected = true;
}

function resetTime(whichForm,formEl1,formEl2) {
	document.forms[whichForm].elements[formEl1].value = "tt.mm.jjjj";
	document.forms[whichForm].elements[formEl2].value = "tt.mm.jjjj";
}

//****************************************************************************
// Scripte ris-muenchen.de, z.T. modifiziert
//****************************************************************************

// Source file: /utils/checkdate.js

/**
   Prüft Datumswerte clientseitig auf Korrektheit (inkl. Beachtung von Schaltjahren usw.).
   Akzeptiert werden Datumswerte vom 1.1.1900 - 31.12.2999.
   cuw 29.9.05: Für Muster 'tt.mm.jjjj' soll keine Fehlermeldung ausgegeben werden.

   function checkdate()
     true - Datumswert ist ok.
     false - Datumswert ist nicht ok.

   @author  sseifert
 */

function checkDate(pValue) {
  var iPos1 = 0;
  var iPos2 = 0;

  if (pValue == "tt.mm.jjjj") {
    return true;
  }

  for (i=0; i<pValue.length; i++) {
    switch (pValue.charAt(i)) {
      case "0":
      case "1":
      case "2":
      case "3":
      case "4":
      case "5":
      case "6":
      case "7":
      case "8":
      case "9":
        break;
      case ".":
        if (iPos1==0)
          iPos1 = i;
        else if (iPos2==0)
          iPos2 = i;
        else
          return false;
        break;
      default:
        return false;
    }
  }

  if (iPos1==0 || iPos1>2 || iPos2==0 || iPos2-iPos1<2 || iPos2-iPos1>3
      || pValue.length-iPos2<2 || pValue.length-iPos2>5)
    return false;

  var iDay = pValue.substring(0,iPos1);
  var iMonth = pValue.substring(iPos1+1,iPos2);
  var iYear = pValue.substring(iPos2+1,pValue.length);

  if (iDay<1 || iDay>31 || iMonth<1 || iMonth>12 || iYear<1900 || iYear>2999)
    return false;

  if(iMonth==2){
  	if (iDay>29)
        return false;
    var fSchaltjahr = (iYear%4==0 && (iYear%200!=0 || iYear==2000))
    if (!fSchaltjahr && iDay > 28)
        return false;
  }

  if ((iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11) && iDay>30){
  	return false;
  }
  return true;
}

function checkTime(pValue) {
  var iPos1 = 0;

  for (i=0; i<pValue.length; i++) {
    switch (pValue.charAt(i)) {
      case "0":
      case "1":
      case "2":
      case "3":
      case "4":
      case "5":
      case "6":
      case "7":
      case "8":
      case "9":
        break;
      case ":":
        if (iPos1==0)
          iPos1 = i;
        else
          return false;
        break;
      default:
        return false;
    }
  }

  if ( iPos1 != 2 || pValue.length > 5)
    return false;

  var iStunde = pValue.substring(0,iPos1);
  var iMinute = pValue.substring(iPos1+1,pValue.length);


  if (iStunde<1 || iStunde>24 || iMinute<0 || iMinute>59)
    return false;
  return true;
}


/*
  übprüft alle Formular-Elemente der Form "SUCHE", die mit txtVon
  beginnen auf einen gültigen Datumswert.

  Im Fehlerfall wird Fokus gesetzt, FM und return false

  nötige Funktionen:
    - checkDate()
    - convertDate()
    - startsWith()
    - istleer()
*/

function checkDateFieldsInForm(whichForm,txtVon)
{

    var iCount = document.forms[whichForm].elements.length;

    for( var i = 0; i < iCount; i++ )
    {
        var elName = document.forms[whichForm].elements[i].id;
        if( startsWith(elName.toString(),txtVon) )
        {
         document.forms[whichForm].elements[i].value = convertDate(document.forms[whichForm].elements[i].value);
         var s = document.forms[whichForm].elements[i].value;

         // s = trim( s );

          if( s.length == 0 )
            continue;

            if( checkDate( s ) == false )
            {
              alert( 'Bitte ein gültiges Datum eingeben ');
              document.forms[whichForm].elements[i].focus();
              return false;
            }
        }
    }

    return true;
}
// Source file: /utils/checkdate.js

/**
   Prüft Datumswerte clientseitig auf Korrektheit (inkl. Beachtung von Schaltjahren usw.).
   Akzeptiert werden Datumswerte vom 1.1.1900 - 31.12.2999.

   function checkdate()
     true - Datumswert ist ok.
     false - Datumswert ist nicht ok.

   @author  sseifert
 */

function convertDate(pValue) {

  if((pValue.length > 5) && (pValue.length < 10))
  {
  	tag = "01";
  	monat = "01";
  	jahr = "2000";

  	if (pValue.indexOf('.') > 0){
  		if(pValue.indexOf('.') == 1){
		  tag = "0" + pValue.substring(0,1);
		 } else {
		  tag = pValue.substring(0,2);
		 }
  		pValue = pValue.substring(pValue.indexOf('.') + 1);
  	}
  	if (pValue.indexOf('.') > 0){
  		if(pValue.indexOf('.') == 1){
		  monat = "0" + pValue.substring(0,1);
		 } else {
		  monat = pValue.substring(0,2);
		 }
  		pValue = pValue.substring(pValue.indexOf('.') + 1);
  	}

  	jahr = pValue;
  	jetzt = new Date();
  	var y = jetzt.getYear();
  	if(y < 1000){
  	 y = y +1900;
  	}
  	if(y > 2000){
  		y = 2000;
  	}
  	var x = y.toString().substring(0,2);

  	if(jahr.length < 4){
                if (jahr.valueOf()>70&&jahr.valueOf()<=99)
                  x = 19;
  	  	pValue = tag + "." + monat + "." + x + jahr.valueOf();
  	} else{
  	  	pValue = tag + "." + monat + "." + jahr.valueOf();
  	}

  }

  return pValue;
}

// Source file: /utils/trim.js

/**
   Entfernt alle führenden und abschließenden Leerzeichen aus einem String.

   @author  sseifert
 */

function trim(pValue) {
  var pNewValue = "";
  var iStartPos = 0;
  var iEndPos = pValue.length;

  for (i=0; i<pValue.length; i++) {
    if (pValue.substr(i,1)!=" ") {
      iStartPos = i;
      break;
    }
  }
  for (i=pValue.length-1; i>=0; i--) {
    if (pValue.substr(i,1)!=" ") {
      iEndPos = i;
      break;
    }
  }

  return pValue.substr(iStartPos,iEndPos-iStartPos+1);
}

  function startsWith( s1, s2 )
  {

    if( s1.length < s2.length )
      return false;

     for( var i = 0; i < s2.length; i++)
     {
       var c1 = s1.charAt(i);

       var c2 = s2.charAt(i);

       if( c1 != c2 )
        return false;
     }
     return true;
  }

function istleer( s )
  {
    for( var i = 0; i < s.length; i++)
    {
      var c = s.charAt(i);
      if( (c != ' ') && ( c != '\n') && ( c != '\t') )
        return false;
    }
    return true;
  }

//formularcheck onsubmit (ueberprueft korrekte datumseingabe)
//aufruf im form-tag mit onSubmit="return suche('ANTRAGTREFFERLISTE')"

function suche(whichForm){
    //keine wahlperiode ausgewaehlt?
	if(document.forms[whichForm].selWahlperiode.options[0].selected){
		if( istleer(document.forms[whichForm].txtVon.value) && !istleer(document.forms[whichForm].txtBis.value) || !istleer(document.forms[whichForm].txtVon.value) && istleer(document.forms[whichForm].txtBis.value)){
     		alert('Bitte geben Sie den Zeitraum vollständig an!');
     		return false;
    	}
    	if( checkDateFieldsInForm(whichForm, 'txtVon') == false || checkDateFieldsInForm(whichForm, 'txtBis') == false){
      	return false;
		}
	}
}


//********************************************************************
//functions detailsuche
//*******************************************************************

function checkFormElements(){
	if(document.forms[0]){
		myFElength = document.forms[0].elements.length;
	 	FEvalues = new Array();
	 	for(i=0;i<myFElength;i++){
	 	if(document.forms[0].elements[i].type == "text"){
	 		FEvalues[i] = document.forms[0].elements[i].value;
	 		}
	 	}
	}
}

function kriterienLoeschen()
{

    for(i=0;i<myFElength;i++){
	 	if(document.forms[0].elements[i].type == "text"){
	 		document.forms[0].elements[i].value = FEvalues[i];
	 		}
	 	if(document.forms[0].elements[i].type == "select-one"){
	 		document.forms[0].elements[i].options[0].selected = true;
	 		}
		if(document.forms[0].elements[i].type == "textarea"){
	 		document.forms[0].elements[i].value = "";
	 		}
    }

}

function openSchlagwortauswahl(whichForm) {
  var sThemaId = document.forms[whichForm].selThema.selectedIndex;
  var sThema = document.forms[whichForm].selThema.options[sThemaId].value;
  var sSelected = "";
  if (document.forms[whichForm].txtSchlagwoerter.value.length > 0)
      sSelected = document.forms[whichForm].txtSchlagVal.value;
  if(sThema!="")
  {
    //url muss noch angepasst werden
	wnd = window.open("http://www.ris-muenchen.de/RII/RII/ris_schlagworte.jsp?thema="+sThema+"&selected="+sSelected,"","directories=0,location=0,menubar=0,resizable=yes,scrollbars=1,status=0,toolbar=0,width=640,height=480,dependent=no");
    wnd.focus();
  }
  else{
  	alert("Bitte wählen Sie zunächst ein Thema aus");
  }
}

function resetSchlagwoerter(whichForm){
	document.forms[whichForm].txtSchlagwoerter.value = "";
}

