$(document).ready(form);

function form() {
    $("span.check > input").unbind("click").click(function () {
        if ($(this).is(":checked")) {
            $(this).parent().addClass("on");
        }
        else {
            $(this).parent().removeClass("on");
        }
    });

    $("div.form [label=1]").unbind("click").click(function () {
        if ($(this).attr("changed") != "1") {
            $(this).attr("default",$(this).val());
            $(this).val("");
        }
    });
    $("div.form [label=1]").unbind("change").change(function () {
        $(this).attr("changed","1");
        if ($(this).val() == "") $(this).attr("changed","0");
    });
    $("div.form [label=1]").unbind("blur").blur(function () {
        if ($(this).val() == "") $(this).val($(this).attr("default"));
    });
    $("div.form form a[mtype=submit]").unbind("click").click(function () {

        var form = $(this).closest("form");
        var valido = true;
        form.find("[req=1]").each(function () {
            if ($(this).attr("type") == "text") {
                var valor = $(this).val();
                if ($.trim(valor) == "" || ($(this).attr("label") == "1" &&$(this).attr("changed") != "1")) {
                    error($(this));
                    valido = false;
                }
                else bien($(this));
            }
            if ($(this).is("textarea")) {
                var valor = $(this).val();
                if ($.trim(valor) == "" || ($(this).attr("label") == "1" &&$(this).attr("changed") != "1")) {
                    error($(this));
                    valido = false;
                }
                else bien($(this));

            }
            if ($(this).is("select")) {
                var valor = $(this).find("option:selected").val();
                if ($.trim(valor) == "0") {
                    error($(this));
                    valido = false;
                }
                else bien($(this));
            }
            if ($(this).is("[type=checkbox]")) {
                if (!$(this).is(":checked")) {
                    error($(this));
                    valido = false;
                }
                else bien($(this));
            }
            if ($(this).attr("mtype") == "mail") {
                if (!is_mail($(this).val())) {
                    error($(this));
                    valido = false;
                }
                else bien($(this));
            }
        });
        if (valido) {
            if (form.attr("ajax") != "1")
                form.submit();
            else {
                $.ajax({
                    type: "post",
                    url: form.attr("action"),
                    data: form.serialize(),
                    success: function (r) {
                        $("div.cortina div.form div.col2").hide();
                        $("div.cortina div.form div.col3").hide();
                        $("div.cortina div.form div.col1 div.texto").text(r);
                    }
                });
            }
        }
    });

    var error = function(elem) {
        elem.css("border","red solid 1px");
        if (elem.is("select")) {
            elem.css("color","red");
        }
        if (elem.is("[type=checkbox]")) {
            elem.parent().css("border","red solid 1px");
        }
    }

    var bien = function(elem) {
        elem.css("border","");
        elem.css("color","");
        elem.parent().css("border","");
    }
}

function is_mail(txt)
{
    var s = txt;
    var filter=/^[A-Za-z]+[.]*[A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
    if (s.length == 0 ) return false;

    if (filter.test(s))
        return true;
    else
        return false;
    return false;
}