(function($) {
    var WAIT_CLASS = "wait";
    Dialog = function(dialogID, openID, closeID, submitID) {
        this.dialog = $("#" + dialogID);
        this.window = $(window);
        this.size = {
            w: this.window.width(),
            h: this.window.height()
        };
        $("<div id='bgDiv'></div>").hide().appendTo($("#wrapper1"));
        if (openID) {
            $("#" + openID).click(delegate(this, this.ShowDialog));
        }
        $("#" + closeID).click(delegate(this, this.CloseDialog));
        this.submit = $("#" + submitID).bind('click.dialog', delegate(this, this.SubmitDialog));
    }

    $.extend(Dialog.prototype,
    {
        DisableSubmit: function() {
            this.submit.addClass(WAIT_CLASS).unbind('click.dialog');
        },
        EnableSubmit: function() {
            this.submit.removeClass(WAIT_CLASS).bind('click.dialog', delegate(this, this.SubmitDialog));
        },
        ShowDialog: function() {
            this.window.bind("resize", delegate(this, function() {
                var height = this.window.height();
                var width = this.window.width();
                if (this.size.w != width || this.size.h != height) {
                    this.size = {
                        h: height,
                        w: width
                    };
                    this.BlinkClose();
                    this.BlinkShow();
                }
            }));
            this.FadeShow();
        },

        FadeShow: function() {
            this.Show();
            this.dialog.fadeIn(200);
        },

        FadeClose: function() {
            this.Close();
            this.dialog.fadeOut(200);
        },

        BlinkShow: function() {
            this.Show();
            this.dialog.show();
        },

        BlinkClose: function() {
            this.Close();
            this.dialog.hide();
        },

        Show: function() {
            $("#bgDiv").css({
                width: this.GetDocumentWidth(),
                /*height: $("body form:first-child").height()*/
                height: "9999px"
            }).show();
            this.dialog.css({
                top: ((document.documentElement.clientHeight - this.dialog.height() -
                    (14 + parseInt(this.dialog.css("padding-top"))) * 2) / 2)
                    + this.GetScrollTop(),
                left: ((document.documentElement.clientWidth - this.dialog.width() -
                    (14 + parseInt(this.dialog.css("padding-left"))) * 2) / 2)
                    + this.GetScrollLeft()
            });
        },

        Close: function() {
            $("#bgDiv").hide();
        },

        CloseDialog: function() {
            this.FadeClose();
            this.window.unbind("resize");
        },

        SubmitDialog: function() {
            $(this).submit();
        },

        GetScrollTop: function() {
            return document.documentElement.scrollTop > document.body.scrollTop ?
                document.documentElement.scrollTop : document.body.scrollTop;
        },

        GetScrollLeft: function() {
            return document.documentElement.scrollLeft > document.body.scrollLeft ?
                document.documentElement.scrollLeft : document.body.scrollLeft;
        },

        GetDocumentHeight: function() {
            return (document.documentElement.scrollHeight > document.body.scrollHeight) ?
                document.documentElement.scrollHeight : document.body.scrollHeight;
        },

        GetDocumentWidth: function() {
            return (document.documentElement.scrollWidth > document.body.scrollWidth) ?
                document.documentElement.scrollWidth : document.body.scrollWidth;
        }
    });

    function delegate(that, thatMethod) {
        return function() {
            return thatMethod.call(that);
        }
    }
})(jQuery);

(function($) {
    var FORM = 'fieldset#requestInfoForm';
    var ERRORS = 'div#errorsReqInfo';
    var OPENER = 'RequestInfoOpener';
    var EMAIL = '^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$';
    var PHONE = '^[0-9-()+ ]{5,20}$';
    var POSTALCODE = '(^[0-9\-]{5}[0-9\-]*$)|(^$)'; 
    var INVALID_LIST_VALUE = '-1';
    RequestInfoForm = function(DIALOG, CLOSER, fieldIDs, submitID) {
        var ids = fieldIDs;
        var dialog = new Dialog(DIALOG, null, CLOSER, submitID);
        var self = this;
        var GetVal = function(id) {
            return $('#' + id).val();
        }
        var GetPhoneType = function(id) {
            return $('input[type=radio][id*=' + id + ']:checked').val();
        }
        this.ShowLoadIndicator = function() {
            dialog.DisableSubmit();
        };
        this.HideLoadIndicator = function() {
            dialog.EnableSubmit();
        };
        this.Open = function(callback, title) {
            this.callback = callback;
            dialog.ShowDialog();
        }
        this.Close = function() {
            dialog.CloseDialog();
        }
    }
})(jQuery);

(function($) {
    RequestInfoDialog = function(requestInfoForm, opener, programID, formTitle, applicationSiteName) {
        var form = requestInfoForm;
        $(opener).click(function() {
            form.Open(function(data) {
                form.ShowLoadIndicator();
                var dataToSend = $.extend({}, data);
                if (programID) {
                    dataToSend.ProgramID = programID;
                }
                AjaxDataService.SaveRequestInfo(dataToSend, HandleSaved, HandleFailed);
            }, formTitle);
        });
        var HandleSaved = function() {
            if (typeof (pageTracker) != undefined) {
                pageTracker._trackEvent(applicationSiteName, 'Submit success', 'Request Info Form');
            }        
            form.HideLoadIndicator();
            form.Close();
        };
        var HandleFailed = function(ex) {
            var type = ex.get_exceptionType();
            form.HideLoadIndicator();
            alert(ex.get_message());
        };
    }
})(jQuery);

