$(document).ready(function()
{
    common_options = { 
            clearForm : false,
            //type: 'POST',     
            dataType : 'json',                 
            success: function (data, status)
            {
                if (data)
                {
                    $('#error_div').hide();
                    $('#thanks_div').show();
                    $("#commentForm").get(0).reset();
                }
                else 
                {
                    $('#thanks_div').hide();
                    $('#error_div').show();
                }               
                //submitBtnEnabled(true);
            },
            notsuccess: errorAjax
    }

    var common_validator = {
        focusInvalid: false,
        focusCleanup: true,
        errorPlacement: function(error, element) {},
        highlight: function(element, errorClass)
        {
            var div_id = "#" + element.name + "_er";
            $(div_id).removeClass("noerror");
            $(div_id).addClass("myerror");
            $(element).addClass("input_myerror");
        },
        unhighlight: function(element, errorClass)
        {
            var div_id = "#" + element.name + "_er";
            $(div_id).removeClass("myerror");
            $(div_id).addClass("noerror");
            $(element).removeClass("input_myerror");
        }
    }

    var payment_validator = new cloneObject(common_validator);

    payment_validator.rules =
    {
        email:
        {
            required: true,
                minlength: 5,
                maxlength: 100,
            email: true
        },
        title:
        {
            required: true,
            rangelength: [3, 150]
        },
        amount:
        {
            required: true,
            min: 0.01
        }
    }

    var comment_validator = new cloneObject(common_validator);
    comment_validator.rules =
    {
        comment_name:
        {
            //required: true,
            minlength: 1,
            maxlength: 50
        },
        comment_email:
        {
                required: true,
                minlength: 5,
                maxlength: 100,
                email: true
        },
        comment_text:
        {
            required: true,
            maxlength: 2000
        }
    };

    comment_validator.submitHandler = function(form) 
    {
        $(form).ajaxSubmit(common_options);
    };

    $("#make_payment_form").validate(payment_validator);
    $("#commentForm").validate(comment_validator);

    $("#payment_form_frame").load(function (){
        location.href = $(this).contents().find("#url").val();
    });
});

function cloneObject(obj)
{
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = obj.constructor(); // changed

    for(var key in obj)
        temp[key] = cloneObject(obj[key]);
    return temp;
}
