function showAjaxForm(url) {
    $.get(url, function(data) {
        closeForm();
        var overlay = $("<div class='overlay'></div>");
        var popup = $(data);
        
        overlay.hide().appendTo("body").fadeTo(400, 0.35);
        popup.hide().appendTo("body").fadeIn(400);
        
        initForm($("form#submitForm"));
    });
}

function closeForm() {
    $(".popupContainer").fadeOut(400, function() {
        $(this).remove();
    });
    $(".overlay").fadeOut(400, function() {
        $(this).remove();
    }); 
}

function initForm(form) {
    $.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        }
    );
    $.validator.addMethod(
        "forbidden",
        function(value, element, regexp) {
            var sites = regexp.split(",");
            var valid = true;
            for (var i = 0; i < sites.length; i++)
                valid = valid && value.indexOf(sites[i]) == -1;
            return this.optional(element) || valid;
        }
    );
    $.validator.addClassRules({
        phone: {
            regex: "^[0-9()+-]+$"
        },
        corporateEmail: {
            email: true,
            forbidden: "gmail.com,yahoo.com,hotmail.com,live.com"
        }
    });

    form.validate();     
    form.bind('submit', function() {
        if ($(this).valid())
            $(this).ajaxSubmit({
                success: function(responseText) {
                    var response = eval("(" + responseText + ")");
                    alert(response.message);
                    if (!response.error)
                        closeForm();
                }
            });
        return false;
    });
    form.find("input.submit").removeAttr("disabled");
    $(".popupContainer .close").click(closeForm);

    form.find("input:first").focus();
}
