$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"transparent"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"transparent"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"transparent"});
  });
  
  $("a#ToogleSidebar").click().toggle(function() {
                $('#sidebar').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow');
        }, function() {
                $('#sidebar').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow');
        }
        );
        $("a#ToogleSidebar_close").click().toggle(function() {
                $('#sidebar').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
        }, function() {
                $('#sidebar').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
        });
      
   $("a#ToogleSidebar_close").mouseover(function(){
		$("#ToogleSidebar_close_img").attr("src","typo3conf/ext/fritzbauer_mediabox/images/pfeil_links_rot.gif");
		});
   $("a#ToogleSidebar_close").mouseout(function(){
		$("#ToogleSidebar_close_img").attr("src","typo3conf/ext/fritzbauer_mediabox/images/pfeil_links_weiss.gif");
		});	    
        
    $("#openmedia").click().toggle(function() {
               
                $('#fbMediaBox').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow'); 
        }, function() {
                $('#fbMediaBox').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow'); 
                
        }
        );
        
    $("#mediaclose").click().toggle(function() {
    		 $('#fbMediaBox').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
        }, function() {
                $('#fbMediaBox').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
        });
    $("#mediaclose").mouseover(function(){
		$("#mediaclose_img").attr("src","typo3conf/ext/fritzbauer_mediabox/images/pfeil_links_rot.gif");
		});
   $("#mediaclose").mouseout(function(){
		$("#mediaclose_img").attr("src","typo3conf/ext/fritzbauer_mediabox/images/pfeil_links_weiss.gif");
		});	    

  $(".button").click(function() {
  get(document.getElementById('contact'))
	
    
		
    return false;
	});
});
      


   
   
   var http_request = false;
   function makeRequest(url, parameters) {
   //alert(url);
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
//alert(alertContents);
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }
	
	function alertContents() {
      	// validate and process form
		// first hide any error messages
    $('.error').hide();
		
      var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").fadeIn(600);
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").fadeIn(600);
      $("input#email").focus();
      return false;
    }
		var phone = $("input#phone").val();
		
		if (phone == "") {
      $("label#phone_error").fadeIn(600);
      $("input#phone").focus();
      return false;
    }
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('myspan').innerHTML = result; 
            
            if(result=="Again"){
            var sicherheitscode = $("input#sicherheitscode").val();
    var sicherheitscode2 = $("span#myspan").html();
    
    if (sicherheitscode == "" || sicherheitscode2 == "Again" || sicherheitscode2 == "") {
      $("label#sicherheitscode_error").fadeIn(600);
      $("input#sicherheitscode").focus();
    //  alert(result);
    //Get a reference to CAPTCHA image
			img = document.getElementById('imgCaptcha'); 
			//Change the image
			img.src = 'fileadmin/template/scripts/create_image.php?' + Math.random(); // Search for new image
			document.getElementById('sicherheitscode').value=''; //Reset input Captcha  after succes return 			
         
      return false;
      
    }
            }
        if(result!="Again"){
      //  alert("hurra");
        var sicherheitscode = $("input#sicherheitscode").val();
       var nameurl  = document.URL;
       var dataString = 'nameurl=' + nameurl + '&name='+ name + '&email=' + email + '&phone=' + phone+ '&sicherheitscode=' + sicherheitscode;
      //alert (dataString);//return false;
      var t;
      $.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
       error: function() {
              $('#contact_form').html("<div id='message'></div>");      
        $('#message').html("")
        .append("<p>Sicherheitscode falsch!</p>")
            },
      success: function() {
       $('#contact_form').html("<div id='message'></div>");    
  
        $('#message').html("")
        .append("<p>Ihre Nachricht wurde verschickt!</p>")
        .hide()
        .fadeIn(500,function() {
        t = setTimeout(function() {$('#sidebar').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow')}, 1500);
    });
    //$('#contact_form').html("<div id='message'>test</div>"); 
      }
     });
     //alert("hurra2");
            }
            
            
            
            
            
			} else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   
   function get(obj) {
      var getstr = "?" + "&sicherheitscode=" + encodeURI( document.getElementById("sicherheitscode").value) ;
      //alert(getstr);
      makeRequest('bin/get.php', getstr);
   }

// IMAGE REFRESHING
function refreshimg()
{
	//Get a reference to CAPTCHA image
	img = document.getElementById('imgCaptcha'); 
	//Change the image
	img.src = 'fileadmin/template/scripts/create_image.php?' + Math.random();
}

