/**
* John Resig Methode
*/
function addEvent( obj, type, fn )
{ 
   if (obj.addEventListener) {
      obj.addEventListener( type, fn, false );
   } else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
   }
}

function removeEvent( obj, type, fn )
{
   if (obj.removeEventListener) {
      obj.removeEventListener( type, fn, false );
   } else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, obj[type+fn] );
      obj[type+fn] = null;
      obj["e"+type+fn] = null;
   }
}



function checkFormular( pflichtfelder ) {

 for(i=0;i < pflichtfelder.length;i++) {
  if(document.forms[0][pflichtfelder[i]]) {
   if(document.forms[0][pflichtfelder[i]].value.length < 3 ) { // Mindestens 2 Zeichen
     
     document.forms[0].absenden.style.display = 'none';
     
     ladeIcon(3,'ladeIcon01'); // Fehler
     ladeIcon(0,'ladeIcon02'); // leer
     ladeIcon(0,'ladeIcon03'); // leer
     
     return false;
   }
  }
 
 }
 
 document.forms[0].absenden.style.display = 'block';
 return true;
 
}




function sendFormular( ) {
 ladeIcon(2,'ladeIcon02');
 ladeIcon(1,'ladeIcon03');
 
 var ausgabe = '';
 
 var emailFormular;
 emailFormular = document.kontaktformular;
 for(i=0;i<emailFormular.length;i++) {
  //alert(emailFormular[i].value);
  ausgabe = ausgabe + emailFormular[i].name + ":\n" + emailFormular[i].value + "<br />\n\n";
 }
 
 return sendMailForm ( ausgabe );
}



function start_typing () {
 ladeIcon(1,'ladeIcon01');
 clearTimeout(window.typingTimeout);
 window.typingTimeout = setTimeout('stop_typing();',1500); 
}

function stop_typing () {

 if(checkFormular(window.pflichtfelder)) {
  ladeIcon(2,'ladeIcon01');
 } else {
  ladeIcon(3,'ladeIcon01');
 }
 
 clearTimeout(window.typingTimeout);
 //check_formular();
 
}




function ladeIcon (status, obj) {
 var ladeIcon = new Array('', 'img/ajaxload_arrows.gif', 'img/okay.png', 'img/fehler.png'); // off, loading, okay, fehler
 
 if(document.getElementById(obj)) {
   // Falls die ID statt das Objekt uebergeben wurde
   obj = document.getElementById(obj);
 }
 
 if(obj) {
  
  if(obj.src.search(ladeIcon[status]) < 0) { // Falls das Icon noch nicht geladen wurde
   obj.src = ladeIcon[status];
  }
  
 }
 
}




// AJAX

function sendMailForm ( text ) {
 
 xmlhttp.open("POST", 'ajax/email_senden.ajax.php', true);
 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 
 xmlhttp.send('text=' + text);
 
 xmlhttp.onreadystatechange=function() {
   
   if(xmlhttp.readyState==4) {

    if(xmlhttp.responseText==1) {
     ladeIcon(2,'ladeIcon03');
	 
	 if(document.getElementById('gesendet')) {
		document.getElementById('gesendet').innerHTML = '<H1>Kontaktformular erfolgreich abgeschickt.</H1>';
	 }
    } else {
     ladeIcon(3,'ladeIcon03');
	 
	 if(document.getElementById('gesendet')) {
		document.getElementById('gesendet').innerHTML = document.getElementById('gesendet').innerHTML + '<br />Das Kontaktformular wurde nicht abgeschickt <img src="img/fehler.png" />';
	 }
    }
        
   }
  
 }
 
 
}
