$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#5A564A"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#A29C8C"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#5A564A"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var nome = $("input#nome").val();
		if (nome == "") {
      $("label#nome_error").show();
      $("input#nome").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    } else if (!(valida_email_ajax(email))){
			$("label#email_error_1").show();
      		$("input#email").focus();
      return false;			
		}
	var telef = $("input#telef").val();	

		var obs = $("textarea#obs").val();
		if (obs == "") {
      $("label#obs_error").show();
      $("textarea#obs").focus();
      return false;
    }
		
		var dataString = 'nome='+ escape(nome) + '&email=' + escape(email) + '&telef=' + escape(telef) + '&obs=' + escape(obs);
		//alert (dataString);return false;
			//alert("entrou"+nome+"-"+email+"-"+obs);	
		$.ajax({
      type: "POST",
	 // encoding: "iso-8859-1",
      url: "/contactos/f_contactos.asp",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<table width='100%'><tr><td height='250'><p align='center'><table><tr><td width='48' height='48'><div class='checkmark'></div></td></tr></table></p><p align='center'>O seu formulário foi enviado com sucesso!<br/>Entraremos em contacto brevemente.</p></td></tr></table>")		
        .hide()
        .fadeIn(100, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

/********************** VALIDAÇÃO DE EMAIL *************************************************/

function valida_email_ajax(email)
{
	var result = /^.+\@.+\..+$/ ;

	if ((email == null) || (email.length == 0)) {
       	if (valida_email.arguments.length == 1) {
       		$("label#email_error_1").show();
			$("label#email_error").hide();
      		$("input#email").focus();
       		return false ;
       	} else {
       		if (!(valida_email.arguments[1] == true)) {
       			$("label#email_error_1").show();
				$("label#email_error").hide();
      		$("input#email").focus();
       		}
       		return (valida_email.arguments[1] == true);
       	}
    } else {
		if (!(result.test(email))) {
			$("label#email_error_1").show();
			$("label#email_error").hide();
      		$("input#email").focus();
		}
       return result.test(email);
    }
}


