$(document).ready(function(){
    var nam = "";
    $(".linkMail").click(function(e){
        e.preventDefault();
        href = $(this).attr("href");
        var rel = $(this).attr("rel");
        nam = $(this).attr("data-nombre");
        var tipo = $(this).attr("data-tipo");
        if($(this).attr("data-mail")){
            var mail = $(this).attr("data-mail");
        }
    
       
            $(".contacto22 form div:first").html("<center><table style=\"font-size:11px;width:320px;\">"+
                "<tr><td colspan=\"2\">Le estas enviando un mail a <b>"+nam+"</b></td></tr>"+
                "<input type=\"hidden\" name=\"ide\" value=\""+rel+"\" /><input type=\"hidden\" name=\"tipo\" value=\""+tipo+"\" /><input type=\"hidden\" name=\"smail\" value=\""+mail+"\" />"+
                "<tr><td>Nombre y Apellido:</td><td width=\"220px\"><input maxlength=\"40\" type=\"text\" name=\"nombre\" style=\"width:100%;font-size:11px;font-family:Tahoma;\"/></td></tr>"+
                "<tr><td>E-mail:</td><td><input type=\"text\" name=\"mail\" maxlength=\"80\" style=\"width:100%;font-size:11px;font-family:Tahoma;\"/></td></tr>"+
                "<tr><td>Telefono:</td><td><input type=\"text\" maxlength=\"20\" name=\"telefono\" style=\"width:100%;font-size:11px;font-family:Tahoma;\"/></td></tr>"+
                "<tr><td colspan=\"2\">Mensaje:</td></tr><tr><td colspan=\"2\"><textarea name=\"mensaje\" rows=\"5\" cols=\"30\" style=\"width:320px;font-size:11px;font-family:Tahoma;\"></textarea></tr></td>"+
                "</table></center>");
            $(".shadow").fadeIn();
            $(".contacto22").fadeIn();
    });

    $(".bcontacto").live('click',function(){
        if(checkDatos()){
        $.post("/inc/sendMail.php",{
            id: $("input[name=ide]").val(),
            tipo: $("input[name=tipo]").val(), //si es aviso o minisitio
            destinatario: $("input[name=smail]").val(), //solo si es minisitio, se envia al mail que se desea
            nombre: $("input[name=nombre]").val(),
            avisonombre: nam,
            mensaje: $("textarea[name=mensaje]").val(),
            mail: $("input[name=mail]").val(),
			telefono: $("input[name=telefono]").val(),
            challenge:$("input[name=recaptcha_challenge_field]").val(),
            response:$("input[name=recaptcha_response_field]").val()
        },
        function(data){
            //alert("MENSAJE ENVIADO CON EXITO!!!!11!!!1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
          if(data!="-1"){
               alert("E-mail enviado con éxito");
               $(".shadow").fadeOut();
               $(".contacto22").fadeOut();
           }else{
               alert("Ha introducido mal las palabras en el captcha");
           }
           //
        });
        }
    });
    $(".cerrar").click(function(){
        $(".shadow").fadeOut();
        $(".contacto22").fadeOut();
    });
});
function checkDatos(){
    var error = "";
    if($("input[name=nombre]").val() == ""){
        error = error + "El nombre es obligatorio\n";
    }
    if($("input[name=mensaje]").val() == ""){
        error = error + "El mensaje es obligatorio\n";
    }

    if($("input[name=mail]").val() != ""){
        
        if($("input[name=mail]").val().match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/) != null){
            error = error + "El e-mail parece no ser válido\n";
        }
    }
    if($("input[name=mail]").val() == "" && $("input[name=telefono]").val() == ""){
        error = error + "El telefono es obligatorio\n";
    }
    if(error != ""){
        alert(error);
        return false;
    }else{
        return true;
    }
}
