/*
	TIG Mailer - Version 0.1

    Dependencies: 
	+ prototype v. 1.5.1.1
	+ mailer.php

	
	html - elements to be set: 
	
	DIV 
	+ mail_processing (div) - shows animation while mail is processed
	+ mail_error (div) - contains error texts for validations, hidden
	+ mail_form (div) - contains <form>
	
	INPUT
	+ mail_from (value = title), onfocus = clearField(), onblur = restoreField()
	TEXTFIELD
	+ mail_text (value = title), onfocus = clearField(), onblur = restoreField()
	
	

*/


function mailLost(r)
{
	alert("Oh, das hat leider nicht funktioniert.\nBitte versuchen Sie es bald noch einmal.");
}

function mailSent(data)
{
  $("#mail_result").html(data);
	$("#mail_result").css({"display": "block"});
	$("p.hideIfMailSent").hide();
	$("#mail_processing").hide();
}

function sendMail()
{
  if ($("#mailtext").val() != $("#mailtext").attr("title")){
	var msg = $("#mailtext").val();
	var subj = "Anfrage via web";
	var from = $("#mailfrom").val();

	// msg = encodeURIComponent(msg);

	// var pars = "from=" + from + "&subject=" + subj + "&body=" + msg;

	var url="mailer.php";

  $.post("mailer.php", { from: from, subject: subj, body: msg },
     function(data){
       mailSent(data);
     }, mailLost);

	$("#mail_form").hide();
	$("#mail_error").hide();
	
	$("#mail_processing").css({"display": "block"});
  }
}

function checkMail(errDiv)
{
  errDiv = $(errDiv);
	errDiv.css({"display": "none"});

	var from = $("#mailfrom").val();

	if (validateEmail("#mailfrom")){
    sendMail();
  }
  else {
    errDiv.html("Bitte geben Sie eine gültige eMail-Adresse an. <br />Es gibt sonst keine Möglichkeit auf Antwort.");
    errDiv.slideDown('slow').focus();
  }
}

 $(function(){
   $("#form_mail_form").submit(function(event){
     checkMail("#mail_error");
     return false;
   });
 });
 