
$(function () {
    $('.help-block').hide();
    $('#response').hide();
    var contacto = false, validateField, resetForm;

    resetForm = function () {
        $('#institucion').val('');
        $('#cargo').val('');
        $('#telefono').val('');
        $('#nombre').val('');
        $('#mail').val('');
        $('#comentarios').val('');
    }

    $('#ventasform').live('click', function () {
        resetForm();
        setTimeout(function () {
            $('#institucion_div').show();
            $('#cargo_div').show();
            $('#telefono_div').show();
            $('#plataforma_div').show();
            $('#ventasmsg').show();
            $('#contacomsg').hide();
        }, 100);
        contacto = false;
    });
    $('#contactoform').live('click', function () {
        resetForm();
        setTimeout(function () {
            $('#institucion_div').hide();
            $('#cargo_div').hide();
            $('#telefono_div').hide();
            $('#plataforma_div').hide();
            $('#ventasmsg').hide();
            $('#contacomsg').show();
        }, 100);
        contacto = true;
    });

    validateField = function (id) {
        var val = $("#" + id).val();

        if (id == 'mail') {
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
                val = reg.test(val) == false ? '' : val;
        }

        if (val == "") {
            $("#" + id + "_error").show();
            $("#" + id + "_div").addClass('error');
            $("#" + id).live('focus', function () {
                $("#" + id + "_error").hide();
                $("#" + id + "_div").removeClass('error');
            });
            return false;
        }
        return true;
    }

    $('#contactform').submit(function () {
        $('.help-block').hide();
        var ids = ['institucion', 'nombre', 'cargo', 'telefono', 'mail'], i, errores = false, dataString = '';
        for (i = 0; i < ids.length; i++) {
            if (!validateField(ids[i])) {
                errores = true;
            }
            else {
                errores = false;
            }
        }
        if (errores) {
            return false;
        }
        dataString = 'plataforma=' + $('#plataforma').val() + '&comentarios=' + $('#comentarios').val() + '&contacto=' + contacto;

        for (i = 0; i < ids.length; i++) {
            dataString += '&' + ids[i] + '=' + $("#" + ids[i]).val();
        }
        $.ajax({
            type:"POST",
            url:"http://cursa.me/contact.php",
            data:dataString,
            success:function () {
                $('#contactform').hide();
                $('#response').show();
                setTimeout(function () {
                    $('#modal-from-dom').modal('hide');
                    setTimeout(function () {
                        $('#contactform').show();
                        $('#response').hide();
                    }, 1000);
                }, 2100);
            }
        });
        return false;
    });
});
