var user_delete_form = null;
var cur_step_form = null;

$(document).ready(function()
{
    var payment_validator = {
        submitHandler: function(form) 
        {
            $('#donation_email_hidden').val($('#donation_email').val());
            if ($("#amount_temp").val() > 2) {
                $("#amount").val(($("#amount_temp").val() - 1.75) / 1.035);
            } else {
                $("#amount").val($("#amount_temp").val());
            }
            $(form).submit(common_options);
        },
        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");
        }
    }

    payment_validator.rules =
    {
        donation_email:
        {
            required: true,
                minlength: 5,
                maxlength: 100,
            email: true
        },
        title:
        {
            required: true,
            rangelength: [3, 150]
        },
        amount_temp:
        {
            required: true,
            min: 0.01
        }
    }
    $("#donation_form").validate(payment_validator);
    $("#payment_form_frame").load(function (){ 
        if ($(this).contents().get(0).location.href.indexOf("payment") != -1)
        {
            $("#submit_signature_main").val($(this).contents().find('input[name=submit_signature]').val());
            $("#donation_form").attr('target', '');
            $("#donation_form").attr('action', $(this).contents().find("#merchant_id_url").html());
            $("#donation_form .submitbtn").trigger('click');
        }
    });

    common_validator.rules =
    {
        shiduch_status_id:
        {
            required: true
        },
        offer_message:
        {
            required: true,
            rangelength: [1, 50000]
        }
    };
    $("#offer_reply").validate(common_validator);

    common_validator.rules =
    {
        last_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        first_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        email:
        {
            required: true,
                minlength: 5,
                maxlength: 100,
            email: true
        },
        country_id:
        {
            required: true
        },
        region_id:
        {
            required: true
        },
        city_id:
        {
            required: true
        },
        access_permition_id:
        {
            required: true
        },
        password:
        {
            required: true,
            rangelength: [3, 50]
        },
        password2:
        {
            required: true,
            rangelength: [3, 50],
            equalTo: "#password"
        }
    };
    $("#regForm").validate(common_validator);

    common_validator.rules =
    {
        last_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        first_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        sex_id:
        {
            required: true
        },
        birthday:
        {
            required: true,
            format_date : true
        },        
        jewry_id:
        {
            required: true
        },
        height:
        {
            required: true
        },
        marital_status_id:
        {
            required: true
        },
        children:
        {
            required: true
        },
        country_id:
        {
            required: true
        },
        region_id:
        {
            required: true
        },        
        city_id:
        {
            required: true
        },
        images:
        {
            required: true
        }, 
        shabbat_now_id:
        {
            required: true
        }, 
        kashrut_now_id:
        {
            required: true
        }, 
        shabbat_future_id:
        {
            required: true
        }, 
        kashrut_future_id:
        {
            required: true
        },
        email_reg:
        {
            email: true
        },
        email_reg2:
        {
            email: true, 
            equalTo: "#email_reg"
        }
    };
    
    if ($("#phone_er").length == 1) {
        //common_validator.rules.phone = { required: true };
        common_validator.rules.email_reg = { required: true, email: true };
        common_validator.rules.email_reg2 = { required: true, email: true, equalTo: "#email_reg" };
        common_validator.rules.recommendation = { required: true };
    }

    //$("#regClientForm").validate(common_validator);

    if ($("#regClientForm").length > 0) $("#regClientForm").formwizard({ 
        formPluginEnabled: true,
        validationEnabled: true,
        focusFirstInput : true,
        textSubmit : $('#send_btn').html(),
        textNext : $('#next_btn').html(),
        textBack : $('#back_btn').html(),
        formOptions : common_options,
        validationOptions : common_validator
    });

    var user_edit_options = new cloneObject(common_options);
    user_edit_options.success = function (data, status)
    {
        var value = eval(data);
        submitBtnEnabled(true);
        if (value.result) {
            cur_step_form.find('tr').find('td').find('span.edit.select_value').each(function(){ 
                $(this).html($(this).parent().find('span.save').find('select option:selected').text());
            });

            cur_step_form.find('tr').find('td').find('span.edit.text_value').each(function(){ 
                $(this).html($(this).parent().find('span.save').find('input').val());
            });

            cur_step_form.find('tr').find('td').find('span.edit.textarea_value').each(function(){ 
                $(this).html(nl2br($(this).parent().find('span.save').find('textarea').val()));
            });

            cur_step_form.find('tr').find('td').find('span.edit.checkboxes_value').each(function(){ 
                var s = '';
                $(this).parent().find('span.save').find('input:checked').each(function(){
                    s = s + $(this).attr('text_value') + ', ';
                });
                s = s.slice(0, -2);
                $(this).html(s);
            });

            cur_step_form.find('tr').find('td').find('span.edit.radio_value').each(function(){ 
                var s = $('.current_status').html() + $("input[name=actual_shiduch_status_id]:checked").parents('label').text();
                if ($("input[name=actual_shiduch_status_id]:checked").val() == 0) {
                    $(this).parents('form').find('#with_user_id').val('');
                }
                if ($(this).parents('form').find('#with_user_id').val() != '') {
                    s = s + ' ' + $(this).parent().find('span.save').find('.add_words').val();
                    s = s + ' ' + $(this).parent().find('span.save').find('.add_words').html() + ' ' + $(this).parent().find('span.save').find('#quick_search_shiduch_status').val();
                }

                $('#set_by_name_for_output .small_tip_text').html($('#set_by_name_tip').html() + $('#cur_name').val());
                s = s + $('#set_by_name_for_output').html();
                $(this).html(s);
            });
  
            cur_step_form.find('.img_loading').hide();
            cur_step_form.find('.save').hide();
            cur_step_form.find('.edit').show();
        }
        if (value.url != '')
        {
            window.location = value.url;
        } else
        {
            show_message(value.result, value.msg);
        }
    };

    var user_edit_validator = new cloneObject(common_validator);
    user_edit_validator.submitHandler = function(form) { 
        submitBtnEnabled(false);
        cur_step_form = $(form);
        cur_step_form.find('.button_save').hide();
        cur_step_form.find('.img_loading').show();
        $(form).ajaxSubmit(user_edit_options);
    }

    user_edit_validator.rules =
    {
        audience:
        {
            required: true,
            minlength: 1,
            maxlength: 5000
        },
        finance:
        {
            required: true,
            minlength: 1,
            maxlength: 5000
        }
    };
    $("#user_edit_step_shadchan1").validate(user_edit_validator);

    user_edit_validator.rules =
    {
        last_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        first_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        sex_id:
        {
            required: true
        },
        birthday:
        {
            required: true,
            format_date : true
        },        
        country_id:
        {
            required: true
        },
        region_id:
        {
            required: true
        },        
        city_id:
        {
            required: true
        }
    };
    $("#user_edit_step1").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step2").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step3").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step4").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step5").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step6").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_images_load").validate(user_edit_validator);

    user_edit_validator.rules = {};
    $("#user_edit_step_status").validate(user_edit_validator);

    shiduch_offer_validator = {
        submitHandler: function(form) {
            $('<div>').dialog({
                modal: true,
                open: function () { 
                    //$(this).addClass('loading_div'); 
                    $(this).html($('#loading_img_text').html());
                    $("#shiduch_offer_div").remove();
                    $(this).attr('id', 'shiduch_offer_div'); 
                    $(this).load('/user/shiduch_confirm', 
                                 $("#send_to_email_form").serializeArray(), 
                                 function() {
                                    common_validator.rules = {};
                                    $("#send_to_email_form_after_confirm").submit(function(){
                                        $("#shiduch_offer_div").dialog("close");
                                    });
                                    $(".btn_not_offer_shiduch").click(function(){
                                        $("#shiduch_offer_div").dialog("close");
                                    });
                                    $("#send_to_email_form_after_confirm").validate(common_validator);
                                    $("#shiduch_offer_div").dialog({ position: 'center' });
                                 }
                                )
                },
                width: 950,
                title: $('#shiduch_offer').html()
            });
        },
        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");
        }
    }

    shiduch_offer_validator.rules =
    {
        send_to_email:
        {
            required: true,
                minlength: 5,
                maxlength: 100,
            email: true
        }
    }
    $("#send_to_email_form").validate(shiduch_offer_validator);

    common_validator.rules = {};
    $(".form_image_delete").each(function(){
        $(this).validate(common_validator);
    });

    common_validator.rules =
    {
        email:
        {
            required: true,
                minlength: 5,
                maxlength: 100,
            email: true
        },
        last_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        first_name:
        {
            required: true,
            minlength: 0,
            maxlength: 50
        },
        city_id:
        {
            required: true
        },
        password:
        {
            required: true,
            rangelength: [3, 50]
        },
        password2:
        {
            required: true,
            rangelength: [3, 50],
            equalTo: "#password"
        }
    };
    $("#profile_edit").validate(common_validator);

    var user_delete_options = new cloneObject(common_options);
    user_delete_options.success = function (data, status)
    {
        var value = eval(data);
        submitBtnEnabled(true);
        if (value.result) {
            if ($(".view_user_edit_with_tabs").length == 0) {
                user_delete_form.parent().parent().parent().remove();
                user_delete_form = null;
            } else {
                window.location = value.url;
            }
        }
        show_message(value.result, value.msg);
    };

    user_delete_validator = { submitHandler: function(form) { 
        submitBtnEnabled(false); 
        $(form).ajaxSubmit(user_delete_options); } 
    }

    approve_form_validator = new cloneObject(common_validator);

    addActionsToClasses();
});

