﻿var advocacyForms = {
    SubmitForm: function() {
        var form = $('#advocacyForm');
        var formid = form[0];
        if (!formValidate(formid)) {
            //reattach submit action
            $.each(['advocacyForm'], function(idx, item) {
                var $form = $('#' + item);
                $form.one('submit', function(ev) {
                    advocacyForms.SubmitForm();
                    ev.preventDefault();
                    return false;
                });
            });
            return false;
        }
        var values = {};
        var myarr = $('#advocacyForm').getFormValues();
        $.each($('#advocacyForm').serializeArray(), function(i, field) {
            values[field.name] = field.value;
            if (field.name == 'prefix') {
                values['title'] = field.value;
            }
        });
        myarr.alert_id = $('#advocacyForm').find(':input#alert_id').val();
        myarr.previewmode = $('#advocacyForm').find(':input#previewmode').val();
        $.ajax({
            type: "POST",
            url: "/WebServices/PublicationService.asmx/SubmitAdvocacyToConvio?",
            data: "{'email_address':'" + myarr.email_address + "'," + "'alert_id':'" + myarr.alert_id + "'," + "'first_name':'" + myarr.first_name + "'," + "'last_name':'" + myarr.last_name + "'," + "'prefix':'" + myarr.prefix + "'," + "'street1':'" + myarr.address + "'," + "'city':'" + myarr.city + "'," + "'state':'" + myarr.state + "'," + "'zip':'" + myarr.zip + "'," + "'previewmode':'" + myarr.previewmode + "'}",

            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                //var obj = jQuery.parseJSON(data.d);
                //$(photogallery.gallery).html(data.d);
                if (data.d == true && myarr.previewmode == "false") {
                    createCookie("WCSAdvocacy_" + myarr.alert_id, "true");
                }
                $('#advocacyForm').hide();
                $('#advocacyThankYou').show();
            }
        });
    },

    formPlaceholder: function() {
        $('#advocacyForm input[placeholder]').each(function() {
            var defaultVal = $(this).attr('placeholder');

            // initialize
            if ($(this).val() == '') {
                $(this).closest('li').addClass('placeholder');
                $(this).val(defaultVal);
            }

            $(this).focus(function() {
                if ($(this).val() == defaultVal) {
                    $(this).closest('li').addClass('placeholder');
                    $(this).val('');
                }
            })

            .blur(function() {
                if (($(this).val() == '') || ($(this).val() == defaultVal)) { // if value is unchanged restore
                    $(this).closest('li').addClass('placeholder');
                    $(this).val(defaultVal);
                } else {
                    $(this).closest('li').removeClass('placeholder'); // if value is set remove placeholder
                }
            })

            // for select boxes
            $("#advocacyForm option").each(function() { // need to add test for when this field is reset.
                $(this).parent().addClass('placeholder');
            });

            $("#advocacyForm select").change(function() {
                $(this).removeClass('placeholder');
            });
        });
    },

    formInitialize: function(form) {
        advocacyForms.formPlaceholder();
        form.find(':submit').click(function(ev) {
            setFormValidation("advocacyForm", this);
            advocacyForms.SubmitForm();
            ev.preventDefault();
            return false;
        });
    }

}


jQuery.fn.getFormValues = function() {
    var formvals = {};
    jQuery.each(jQuery(':input', this).serializeArray(), function(i, obj) {
        if (formvals[obj.name] == undefined) formvals[obj.name] = obj.value;
        else if (typeof formvals[obj.name] == Array) formvals[obj.name].push(obj.value);
        else formvals[obj.name] = [formvals[obj.name], obj.value];
    });
    return formvals;
}

var donationForms = {
    formNoteOverlay: function() {
        $('a.note').hover(function() {
            $(this).children('.ndesc').show();
        }, function() {
            $(this).children('.ndesc').hide();
        });
    },


    donationFormToggle: function() {
        $('.form-page-one').addClass('active');
        $('.next-btn').click(function() {
            $('.form-page-one').removeClass('active').slideUp('medium', function() {
                $('.form-page-two').addClass('active').slideDown();
            });
        });
        //$('.form-page-two').show();
    },

    // manage form placeholder text		
    formDefaultValue: function() {
        $('#corridor input').each(function() {
            var defaultVal = $(this).attr('placeholder');

            // initialize
            if ($(this).val() == '') {
                //$(this).val(defaultVal);
                $(this).closest('li').addClass('placeholder');
            }

            $(this).focus(function() {
                if ($(this).val() == defaultVal) {
                    $(this).closest('li').addClass('placeholder');
                    $(this).val('');
                }
            })

            .blur(function() {
                if (($(this).val() == '') || ($(this).val() == defaultVal)) { // if value is unchanged restore
                    $(this).closest('li').addClass('placeholder');
                    $(this).val(defaultVal);
                } else {
                    $(this).closest('li').removeClass('placeholder'); // if value is set remove placeholder
                }
            })

            // for select boxes
            $("#corridor option").each(function() { // need to add test for when this field is reset.
                $(this).parent().addClass('placeholder');
            });

            $("#corridor select").change(function() {
                $(this).removeClass('placeholder');
            });


        });
    }
}
