
/*****************************************************************************************
Trim 
*/

  String.prototype.Trim = function () 
  {
    return (this.replace(/\s+$/,"").replace(/^\s+/,""));
  }
  ;
// single  Leerzeichen ;
  String.prototype.sTrim = function () 
  {
    return(this.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,""));
  }
  ;

// alle leerzeichen raus;
  String.prototype.wsTrim = function () 
  {
    return (this.replace(/\s+/g,""));
  }
  ;
  
  String.prototype.lTrim = function () 
  {
    return (this.replace(/^\s+/,""));
  }
  ;
  
  
  String.prototype.rTrim = function() 
  {
    return (this.replace(/\s+$/,""));
  }
  ;

/***************************Ende String *********************************************/

/* Formularfunktionen*/
 function dbg ( where,what )
 {
	 if (  typeof( where ) == 'string' )
	      where = document.getElementById ( where );
    if (  typeof(where) != 'object' || where == null ) return;
	where.value = what;
	return;
 }
 function delOption ( opt )
 {
    if (  typeof( opt ) == 'string' )
	      opt = document.getElementById ( opt );
    if (  typeof(opt) != 'object' || opt == null ) return;
	for ( i=opt.length-1;i>0;i--) // erster Eintrag leer wird nicht gelöscht
    {
      opt.options[i] = null;
    }
	if ( opt.options[0].value != "-1" ) 
	{
		newEntry = new Option("Bitte wählen Sie","-1", false, false);
		opt.options[0] = newEntry;
		opt.selectedIndex = 0;
	}
 }
 function addOption (opt,what)
 {
    if (  typeof( opt ) == 'string' )
	      opt = document.getElementById ( opt );
    if (  typeof(opt) != 'object' || opt == null ) return;
	newEntry = new Option(what, what, false, false);
    opt.options[opt.options.length] = newEntry;
  
    
 }
 function fillfield ( field,content)
 {
	if (  typeof( field ) == 'string' )
	      field = document.getElementById ( field );
    if (  typeof(field) != 'object' || field == null ) return;
	field.value = content;

 }
 function addOption_arr (opt, what, col )
 {
	 var i;
    if (  typeof( opt ) == 'string' )
	      opt = document.getElementById ( opt );
    if (  typeof(opt) != 'object' || opt == null ) return;

	 for ( i=0;i<what.length;i++)
	 {
	    if ( what[i][col] != null ) 
		{
			newEntry = new Option(what[i][col], what[i][col], false, false);
        	opt.options[opt.options.length] = newEntry; 
		}
	 }
 }
 
 function enable (field ) 
 {
    if (  typeof( field ) == 'string' )
	      field = document.getElementById ( field );
    if (  typeof(field) != 'object' || field == null ) return;
    field.enabled = true;
	field.disabled = false;
	field.style.backgroundColor = "";
 }
 function disable (field ) 
 {
    if (  typeof( field ) == 'string' )
	      field = document.getElementById ( field );
    if (  typeof(field) != 'object' || field == null ) return;
	
	field.enabled  = false;
	field.disabled = true;	  
    field.style.backgroundColor = "#cccccc";
 }
 function delField (field ) 
 {
    if (  typeof( field ) == 'string' )
	      field = document.getElementById ( field );
    if (  typeof(field) != 'object' || field == null ) return;
	
	field.value   = "";
	
 }
 
  function unselOption (opt ) 
 {
    if (  typeof( opt ) == 'string' )
	      opt = document.getElementById ( opt );
    if (  typeof(opt) != 'object' || opt == null ) return;
	var ibreak=false;
	
	for ( i=0;i<opt.options.length ;i++)
	{
		buf = new String (opt.options[i].text);
		if ( buf.Trim() == "" || buf.Trim() == "&nbsp;")
		{
			opt.options.selectedIndex = i;
			break;
		}
			
	}
	
 }


 function setFocus(field)
 {
	 if (  typeof( field ) == 'string' )
	      field = document.getElementById ( field );
     if (  typeof(field) != 'object' || field == null ) return;

	 field.focus()
 }
function makeDate ( dateStr ) 
{
   var datePat = /^(\d{1,2})(\/|.)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		return(null);
		
	}
    day = matchArray[1]; // parse date into variables
	month = matchArray[3];
	year = matchArray[4];
	ret = new Date ();
	ret.setDate ( parseInt(day) );
	ret.setMonth( parseInt(month) - 1 );
	ret.setYear ( parseInt(year));
	ret.setHours( 0 );
	ret.setMinutes( 0);
	ret.setSeconds (0);
	ret.setMilliseconds(0);
	return ret;
}

function isValidDate(dateStr)
{

	var datePat = /^(\d{1,2})(\/|.)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		return("Das Datum ist nicht im gültigem Format.")
		
	}
	day = matchArray[1]; // parse date into variables
	month = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) 
	{ // check month range
		return("Der Monat muss zwischen 1 und 12 liegen.");
		
	}
	if (day < 1 || day > 31) 
	{
		return("Der Tag muss zwischen 1 und 31 liegen.");
		
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		return("Dieser Monat ( "+month+" ) hat keine 31 Tage !")
		
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
		return("Februar " + year + " hat nicht " + day + " Tage!");
		
	    }
	}
	return '';  // date is valid
}
//  End -->

function my_validateForm ( )
{
	
  var i,p,q,nm,test,num,min,max,errors=document.errors,args=my_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  { 
    test=args[i+2]; 
    val=MM_findObj(args[i]);
    if (val) 
	   { 
	      nm=val.name; 
          if (  args[i+1]  != '' ) 
               ntext = args[i+1];
          else
               ntext = nm; 
	      if ((val=val.value)!="") 
		  {
             if (test.indexOf('isEmail')!=-1) 
			 { 
               p=val.indexOf('@');
               if (p<1 || p==(val.length-1)) 
			        errors+='- '+ntext+' soll eine E-Mail- Adresse enthalten.\n';
             } 
			 else 
				 if (test.indexOf('isDate')!=-1) 
				 { 
				     buf = isValidDate( val );
					 if ( buf != '' ) 
					      errors+='- ' + ntext + ": " + buf  + '\n';
				 } 
				 else
				 if (test!='R') 
				 { 
					 num = parseFloat(val);
					 if (isNaN(val)) 
						  errors+='- '+ntext+' sollte eine Nummer enthalten.\n';
					 if (test.indexOf('inRange') != -1) 
					 { 
						 p=test.indexOf(':');
						 min=test.substring(8,p); max=test.substring(p+1);					 
						 if (num<min || max<num) 
							   errors+='- '+ntext+' sollte eine Nummer zwischen  '+min+' und  '+max+' enthalten.\n';
					 } 
				 } 
		 } 
		else 
		 if (test.charAt(0) == 'R') 
				errors += ntext+' muss angegeben werden.\n'; 
		 }
   } 
    if (errors) 
	    alert('Folgende Probleme traten auf:\n'+errors);
   document.MM_returnValue = (errors == '');
}

/***********Ende Formularfunktionen******************************************/

/********Testfunction****************************/
function say_hello ()
{
	alert( "hello World" );
}

/********Ende Testfunktion **********************************/

/****************************************************************************************
*  dbq - Database Query with AJAX 
****************************************************************************************/
 
 function init_AJAX_default (rh)
    {
     
     var req;
     var parent;
     
      // Mozilla, Opera, Safari sowie Internet Explore
     
     try
       { 
           req = new XMLHttpRequest();
         
       }
     catch (error)
       { 
       try
          { 
	          req = new ActiveXObject("Microsoft.XMLHTTP");
    	     
	      }
       catch ( error1 )
          {
            try 
            {
               req  = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch ( error2 )
            {
               return ( false );
            }
          }
        }
      if ( req == null )
      {
          return ( false );
         
      }
      var parent = this;
     
      this.setResultHandler(rh);     
      this.xmlhttp = req;
       req.onreadystatechange = 
    function (  )
      {
          
          var self = req;
          var test = parent;
          if (self.readyState == 4)
             {
                if (self.status == 200)
			    {
                   result = parent.parseData();
                   mbuf = self.responseText;
				   if ( result )
                        parent.resultHaendler(parent.theCommands,parent.theData,mbuf,parent.connection);
				   else
				        parent.resultHaendler(null,null,mbuf,parent.connection);
                } 
			    else 
			    {
                   alert("Fehler beim Abrufen der XML Daten");
                }
             } 

      }
      //this.xmlhttp.onreadystatechange = handleHttpState;
     // actualxmlhttp = this;
      return ;
    }
 
 
    function init_AJAX (hHttpSt)
    {
     
    
     
      // Mozilla, Opera, Safari sowie Internet Explore
     
     try
       { 
           this.xmlhttp = new XMLHttpRequest();
         
       }
     catch (error)
       { 
       try
          { 
	          this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	     
	      }
       catch ( error1 )
          {
            try 
            {
               this.xmlhttp  = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch ( error2 )
            {
               return ( false );
            }
          }
        }
      if ( this.xmlhttp == null )
      {
          return ( false );
         
      }
      var test = this;
      this.xmlhttp.onreadystatechange = hHttpSt;
      actualxmlhttp = this;
      return this.xmlhttp;
    }


    function  send_AJAX ( buf,script )
    {
       var test = this;
        if ( this.xmlhttp != null) 
        {
	       //  alert ('vor senden von :' + buf + ' an ' + script );
		     this.xmlhttp.open('POST', script);
		     this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
             //xmlhttp.send(buf);
			 if ( buf != null && buf.Trim () != '' )
				 buf = buf + '&conn=' + escape (this.connection ) // aus Sicherheitgründen wird nur ein String
				 // übermitteltm, der dann Serverseitig interpretiert wird. 
		     this.xmlhttp.send(buf);
             return true;
            
        }
        else 
        {
             return false;
        }
    }
    
  
function getTag ( tag,arr )
{
	    var len = this.xmlDocument.getElementsByTagName(tag).length;
        for(var i=0; i < len; i++)
           {
		        var base = this.xmlDocument.getElementsByTagName(tag)[i];
		        arr[i] = new Object;
		        parseRec ( base,arr[i] );
           }

}
function parseData()
    {
       var test = this;
       this.response_text  = this.xmlhttp.responseText;
       
       this.xmlDocument  = this.xmlhttp.responseXML ;
       this.kill();
       if (this.xmlDocument != null && this.response_text != '')
       {
           if ( this.theData == null ) 
           {
             this.theData = new Array();
           }
		   
		   this.getTag("data",this.theData );
		   if ( this.theCommands == null ) 
		   {
			   this.theCommands = new Array();
		   }
		   this.getTag("command",this.theCommands );
		   
           return true;
       }
       else
       {
        return false;
       }
    }
 function killArrs ()
 {
	 killArr(this.theData);
	 killArr(this.theCommands);
 }
 function killArr(arr)
    {
        if ( arr == null)
           return;
        for ( i=0; i< arr.length;i++)
        {
            arr[i]= null;
        }
        arr = null;
    }

 function parseRec ( base,rec )
    {
   	    var actual;
   	    var fldnam;
   	    var fldvalue;
   	    actual = base.firstChild;
   	    while (actual!= null)
   	    {
   	        fldnam   = actual.nodeName;
    		
		    fldvalue = actual.textContent;
            if ( fldvalue == null ) 
		        fldvalue = actual.text;
		    rec [fldnam] = fldvalue;
		    actual = actual.nextSibling;
   	    }
    		
     
    }

function setResultHandler ( rh ) 
{
  this.resultHaendler = rh;
}

function DBQ (connection)
{
/*  Attributes */
    
    this.connection = connection;

    this.xmlDocument = null;
    this.response_text = null;

    this.theData = new Array();
    this.xmlhttp = null;
    
/* Methods */


    this.init= init_AJAX_default;
    this.init_local = init_AJAX;
    this.send= send_AJAX;

    this.parseData = parseData;
    this.kill = killArrs;
    this.parseRec = parseRec;
    this.resultHaendler = null;
    this.setResultHandler = setResultHandler;
	this.getTag = getTag;

    
}
/*********************************************Ende dbq - Database Query with AJAX */




/*************Formularfunktionen Event - Handling ********************************/

function forcechanged(buffer,fieldname,fieldvalue)
{
   tell_server (buffer, fieldname, fieldvalue ) ;
}

function changed (buffer, field, serverIsInterested )
{
	if(field.id=="fexterneid") buffer["fpraemie"] = document.getElementById("fpraemie").value;
	
    tell_server (buffer, serverIsInterested[field.name]?field.name:"",field.value ) ;
	buffer[field.name] = field.value;
}

var busy = false;
function tell_server ( buffer, fieldname,fieldvalue )
{
	if ( busy ) return;
	busy = true;
	if ( fieldname == null || fieldname == ""   )
	    return ;
    buf = "";
	and = "";
	for (b in buffer )
	{
		buf = buf + and + b + "=" + escape(buffer[b]) ;
		and = "&";
	}
	buf = "key=" + fieldname + "&value=" + escape(fieldvalue) +  and + buf;
	//alert ( "intresting: "  + buf );
	script = "controller.php";
	thedbq = new DBQ ( fieldname );
	thedbq.init(prs);
	if ( ! thedbq.send ( buf, script ))
	{
		window.status = "AJAX wird nicht unterstützt" ;
		busy=false;
		return false;
	}
	else
	{ 
	  busy = false;
	  return true;
	}
}

/***********Ende Formularfunktionen Event - Handling ********************************/
