$(document).ready(function(){

$("#frmContacto").append('<div id="ajax" style="clear:both"></div>');

$("#frmContacto").submit(function(){
    var error = false;
    var mensaje = '';
    $("input.boton").attr('disabled', 'true');

    if ($("#txtNombre").val().length == 0)
        mensaje += doLi('Introduzca el nombre');

    if ($("#txtEmail").val().length > 0){
        if (!validarEmail($("#txtEmail").val()))
            mensaje += doLi('Su email no parece valido');
    }
    else
        mensaje += doLi('Introduzca el Email');

    if ($("#txtTelefono").val().length == 0)
        mensaje += doLi('Introduzca el telefono');

    if ($("#txtComentarios").val().length == 0)
        mensaje += doLi('Introduzca el comentario');

    if (mensaje.length > 0){
        error = true;
        $("#ajax").html('<ul>' + mensaje + '<ul>').slideDown();
        $("input.boton").removeAttr('disabled');
    }
    else{
        if ($("#ajax").css('display') == 'block')
            $("#ajax").slideUp();

        $("#btnEnviar").val('Espere...');

        $.post(
            'http://es.lacarrasca.es/contacto.php',
            {
                nombre:$("#txtNombre").val(),
                telefono:$("#txtTelefono").val(),
                email:$("#txtEmail").val(),
                ciudad:$("#txtCiudad").val(),
                provincia:$("#txtProvincia").val(),
                pais:$("#txtPais").val(),
                personas:$("#txtPersonas").val(),
                conocio:$("#txtConocio").val(),
                comentarios:$("#txtComentarios").val()

            },
            function(data){
                if (data == 'ok'){
                    clearForm($("#frmContacto"));
                    $("#ajax").html('Su mensaje se ha enviado correctamente. Nos pondremos en contacto con usted en caso de ser necesario. Gracias por utilizar nuestros servicios.').slideDown();
                    $("#btnEnviar").val('Hecho');
                }
                else{
                    $("input.boton").removeAttr('disabled');
                    $("#btnEnviar").val('Enviar consulta');
                    $("#ajax").html(data).slideDown();
                }
            }
        );
        
    }

    return false;
});

});



function validarEmail(value) {
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(value);
}

function clearForm(myForm) {
      // recorremos todos los campos que tiene el formulario
      $(':input', myForm).each(function() {
        var type = this.type;
        var tag = this.tagName.toLowerCase();
        //
        if (type == 'text' || type == 'password' || tag == 'textarea')
          this.value = "";
        // checkboxes and radios need to have their checked state cleared
        // but should *not* have their 'value' changed
        else if (type == 'checkbox' || type == 'radio')
          this.checked = false;
        // select elements need to have their 'selectedIndex' property set to -1
        // (this works for both single and multiple select elements)
        else if (tag == 'select')
          this.selectedIndex = -1;
      });
}




function doLi(thing){
    return '<li>' + thing + '</li>';
}



