﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_TR_Updated, cc_resx_TR_Update, cc_resx_TR_Apply_Now, cc_resx_TR_Saving */

//  
//  Author: Kevin Burkitt
//  Date:   2010-04-15
//  
//  Join Page Helper Functions
//
cc.form = function() {

    return {

        postInProgress: false,

        init: function(target) {

            jQuery('.cc-form', target).unbind('submit').submit(function() {
                return cc.form.formSubmit('#' + jQuery(this).attr('id'));
            });
            jQuery('#info-form', target).unbind('submit').submit(function() {
                return cc.form.formSubmit('#' + jQuery(this).attr('id'));
            });

            jQuery('#info-form .day', target).mask("9999");
            jQuery('#info-form .month', target).mask("99");
            jQuery('#info-form .year', target).mask("99");

            jQuery('#paypal-form', target).unbind('submit').submit(function() {
                return cc.form.paypalFormSubmit('#paypal-form');
            });

            jQuery('#applyVip', target).unbind('submit').submit(function() {
                return cc.form.applyVipFormSubmit('#applyVip');
            });

            // Ajax Forms
            jQuery('.cc-ajax-form', target).unbind('submit').submit(function() {
                return cc.ajax.formAjaxSubmit(jQuery(this));
            });

            jQuery('.cc-admin-send-pm', target).unbind('click').click(function() {
                cc.ui.colorbox('/send-admin-message/', function() {
                    // Init Response Form for Ajax Submit
                    jQuery('#admin-mail-form').unbind('submit').submit(function() {
                        return cc.userprofile.sendMailSubmit('#admin-mail-form');
                    });
                });
            });

            return false;
        },

        formSubmit: function(formId) {

            if (typeof cc.form.postInProgress !== 'undefined' && cc.form.postInProgress === true) {
                return false;
            }
            cc.form.postInProgress = true;

            var form = formId;
            var submitUrl = jQuery(form).attr('action');
            var nextUrl = jQuery(form).attr('cc:next');

            cc.ajax.formSubmit(
                form,
                {
                    target: form,
                    dataType: 'json',
                    url: submitUrl
                },
                function(data, statusText) {
                    if (typeof data.success === 'undefined' || data.success !== true) {
                        // Error!
                        if (data.errors.length > 0) {
                            for (var i = 0; i < data.errors.length; i++) {
                                cc.form.showFormError(data.errors[i].id, data.errors[i].message);
                            }

                            jQuery('.error:first').focus();
                        }
                        return;
                    }

                    if (typeof data.reload !== 'undefined' && data.reload === true) {
                        location.reload(true);
                        return;
                    }

                    if (typeof data.redirect !== 'undefined' && data.redirect !== '') {
                        location.href = data.redirect;
                        return;
                    }

                    if (typeof nextUrl !== 'undefined' && nextUrl !== '') {
                        location.href = nextUrl;
                        return;
                    }
                },
                function() { cc.form.postInProgress = false; }
            );

            return false;
        },

        showFormError: function(target, errorMsg) {
            if (target === 'formerror') {
                jQuery('.formerror').html(errorMsg);
                jQuery('.formerror').show();
            } else {
                var bubble = jQuery('<div />').attr('id', 'error' + target).attr('class', 'error-bubble').hide().html(errorMsg);

                jQuery('#' + target).addClass('error').focus(
                    function() {
                        jQuery('#error' + target).show();
                    }).blur(function() {
                        jQuery('#error' + target).hide();
                    }).after(bubble).change(
                        function() {
                            jQuery(this).removeClass('error');
                            jQuery('#error' + target).remove();
                            jQuery(this).unbind('focus');
                        }
                    );
            }
        },

        paypalFormSubmit: function(formId) {

            if (typeof cc.form.postInProgress !== 'undefined' && cc.form.postInProgress === true) {
                return false;
            }
            cc.form.postInProgress = true;

            var form = formId;
            var submitUrl = jQuery(form).attr('action');

            jQuery('#submit').html(cc_resx_TR_Saving);
            cc.ajax.formSubmit(
                form,
                {
                    target: form,
                    dataType: 'json',
                    url: submitUrl
                },
                function(data, statusText) {
                    if (typeof data.success === 'undefined' || data.success !== true) {
                        if (typeof data.message === 'undefined' || data.message !== true) {
                            jQuery('#paypalResult').html(data.message);
                            jQuery('#paypalResult').fadeOut(2000);
                            jQuery('#paypalEmail').focus();
                        }
                    }
                    else {
                        jQuery('#paypalResult').html(cc_resx_TR_Updated);
                    }

                    jQuery('#paypalResult').stop(true, true).fadeIn(100, function() {
                        jQuery('#paypalResult').fadeOut(2000);
                    });
                    jQuery('#submit').html(cc_resx_TR_Update);
                },
                function() { cc.form.postInProgress = false; }
            );

            return false;
        },

        applyVipFormSubmit: function(formId) {

            if (typeof cc.form.postInProgress !== 'undefined' && cc.form.postInProgress === true) {
                return false;
            }
            cc.form.postInProgress = true;

            var form = formId;
            var submitUrl = jQuery(form).attr('action');

            jQuery('#submit').html(cc_resx_TR_Please_wait);
            cc.ajax.formSubmit(
                form,
                {
                    target: form,
                    dataType: 'json',
                    url: submitUrl
                },
                function(data, statusText) {
                    if (typeof data.success === 'undefined' || data.success !== true) {
                        if (typeof data.message === 'undefined' || data.message !== true) {
                            jQuery('#applyResult').html(data.message);
                            jQuery('#applyResult').stop(true, true).fadeIn(100, function() {
                                jQuery('#applyResult').fadeOut(4000);
                            });
                        }
                    } else {
                        if (typeof data.redirect !== 'undefined' && data.redirect !== '') {
                            location.href = data.redirect;
                            return;
                        }
                    }

                    jQuery('#submit').html(cc_resx_TR_Apply_Now);
                },
                function() { cc.form.postInProgress = false; }
            );

            return false;
        }

    };

} ();

