$(function() {

  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#A08C62"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#A08C62"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#A08C62"});
  });
  
  $("a#ToogleSidebar").click().toggle(function() {
  		$('a.printlink').css({color:"#444444"});
  		$('#DRUCKVERSION .printlink').hover(function() { //mouseover
		$('#DRUCKVERSION a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#DRUCKVERSION a.printlink').css({color:"#444444"});
		});
		$('#anker .printlink').hover(function() { //mouseover
		$('#anker a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#anker a.printlink').css({color:"#444444"});
		});
			 
                $('#sidebar').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow');               
                
        },function() {
        	$('a.printlink').css({color:"#444444"});
        	$('#DRUCKVERSION .printlink').hover(function() { //mouseover
		$('#DRUCKVERSION a.printlink').css({color:"#97544D"});	
		},function() { //mouseout
		$('#DRUCKVERSION a.printlink').css({color:"#444444"});
		});
		$('#anker .printlink').hover(function() { //mouseover
		$('#anker a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#anker a.printlink').css({color:"#444444"});
		});
		
        	$('#sidebar').animate({
                        width: 'show',
                        opacity: 'show'
                }, 'slow');
        }); 
        $("a#ToogleSidebar_close img").imghover();   
        $("a#ToogleSidebar_close").click().toggle(function() {
        	$('a.printlink').css({color:"#ffffff"});
  		$('#DRUCKVERSION .printlink').hover(function() { //mouseover
		$('#DRUCKVERSION a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#DRUCKVERSION a.printlink').css({color:"#ffffff"});
		});	
		$('#anker .printlink').hover(function() { //mouseover
		$('#anker a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#anker a.printlink').css({color:"#ffffff"});
		}); 
		$('#empfehlung .printlink').hover(function() { //mouseover
		$('#empfehlung a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#empfehlung a.printlink').css({color:"#ffffff"});
		}); 
                $('#sidebar').animate({
                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
                
        },function() {
        	$('a.printlink').css({color:"#ffffff"});
  		$('#DRUCKVERSION .printlink').hover(function() { //mouseover
		$('#DRUCKVERSION a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#DRUCKVERSION a.printlink').css({color:"#ffffff"});
		});	
		$('#anker .printlink').hover(function() { //mouseover
		$('#anker a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#anker a.printlink').css({color:"#ffffff"});
		}); 
		$('#empfehlung .printlink').hover(function() { //mouseover
		$('#empfehlung a.printlink').css({color:"#97544D"});
		},function() { //mouseout
		$('#empfehlung a.printlink').css({color:"#ffffff"});
		});
                $('#sidebar').animate({

                        width: 'hide',
                        opacity: 'hide'
                }, 'slow');
        });

  $(".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();
	
       // $('a.printlink').css({color:"#ff6600"});
     
      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!="Success"){
            var sicherheitscode = $("input#sicherheitscode").val();
    var sicherheitscode2 = $("span#myspan").html();
   // alert(sicherheitscode2);
    if (sicherheitscode == "" || sicherheitscode2 != "Success" || 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_JM/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_jm.php",
      data: dataString,
      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_JM/scripts/create_image.php?' + Math.random();
}

