var webhosting_view_controller = $.extend( generic_view_controller, {

    /*******************************
     * helper function that helps to
     * validate that field data exists
     * before making DWR call
     *******************************/
    isValid : function (fieldData) {
        if (fieldData !== undefined  && fieldData != null
                && fieldData != "" && fieldData != "null") {
            return true;
        } else {
            return false;
        }
    },

    /*******************************
     * DWR call to update which website
     * is set as the 'Primary' for
     * eCards and H&F Solution
     *******************************/
    updatePrimaryWebsite : function (siteId, assocId) {
        if (wvc.isValid(siteId) && wvc.isValid(assocId)) {
            WebhostingAdminServices.updatePrimaryWebsite(siteId, assocId, function(ret){
                $("[name=siteSelectRadio]").removeAttr("checked");
                $("#"+ret).attr("checked", "checked");
            });
        }
    },

    /*******************************
     * Create the binding for editing
     * the Primary Email Address
     *******************************/
    bindEditEmailAddress : function() {
        
        $('#email_edit').click( function () {            
            wvc.editEmailAddress();
        });
    },

    /*******************************
     * Call to update the Webhosting
     * Primary Email Address
     *******************************/
    editEmailAddress: function() {
        var emailAddress = $('#email1').val();

        var test = $('#generic_dialog').dialog({
                zIndex: 1200,
                autoOpen: false,
                width: 300,
                modal: true,
                show:'drop',
                hide:'drop'
        })
        .load(
            "/webhosting/admin/editEmail?emailAddress="+ emailAddress + "&decorator=ajax",
            function() {
                var width = 566;
                var style = 'videoBox';

                var title = $('div#hiddenemailupdate').attr('title'); 
                var update = $('div#hiddenhfupdate').attr('title');
                var close = $('div#hiddenhfclose').attr('title');

                var buttons = {};                
                buttons[update] = function(){
                    var newEmailAddr = $('#email').val();

                    if(wvc.checkemail(newEmailAddr)) {
                        var dialogHandle = $(this);
                        if (newEmailAddr.indexOf("@usana.com") != -1) {
                            var assocId = $("#customerId").val();
                            WebhostingAdminServices.isOwnerOfEmailAddress(assocId,newEmailAddr,
                                function(isSuccess){
                                    if (isSuccess) {
                                        var siteId = $('div#hiddensiteid').attr('title');
                                        WebhostingAdminServices.updateEmailAddress(siteId, newEmailAddr, function(emailAddr){
                                            $("#email1").val(emailAddr);
                                            dialogHandle.dialog("close");                                            
                                        });                                        
                                    } else {
                                        var invalid = $('div#hiddeninvalid').attr('title');
                                        $('.wh_invalidEmail').html("<em><font color='red'>&nbsp;(" + invalid + ")</font></em>");
                                    }
                                });
                        } else {
                            var siteId = $('div#hiddensiteid').attr('title');
                            WebhostingAdminServices.updateEmailAddress(siteId, newEmailAddr, function(emailAddr){
                                $("#email1").val(emailAddr);
                                dialogHandle.dialog("close");
                            });                            
                        }
                    }
                };
                buttons[close] = function(){$(this).dialog("close");};

                $(this).dialog("option", "title", update+" "+title)
                        .dialog("option", "resizable", false)
                        .dialog("option", "width", width)
                        .dialog("option", "height", 225)
                        .dialog("option", "buttons", buttons)
                        .dialog('open');
                $(".ui-widget-overlay").addClass("lightBoxOverlay");
            }
        );

        return false;
    },
      
    /*******************************
     * Validate the format of an
     * Email Address
     *******************************/
    checkemail: function (str){
        $('.wh_invalidEmail').html(""); // reset to empty
        var testresults;        
        var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (/\S/.test(str)) {
            if (filter.test(str)) {
                testresults=true;
            } else {
                var invalid = $('div#hiddeninvalid').attr('title');
                $('.wh_invalidEmail').html("<em><font color='red'>&nbsp;(" + invalid + ")</font></em>");                
                testresults=false;
            }
        } else {
            testresults = true;
        }

        return testresults;
    },

    /*******************************
     * Call to update the Webhosting
     * Primary Email Address
     *******************************/
    displayVideoPage: function (videoId, eventId, mediaType, videoUrl, distId) {

        $('#videopage_dialog').dialog({
                autoOpen: false,
                width: 300,
                modal: true,
                show:'drop',
                hide:'drop',
                beforeClose: function(event, ui) {  // this is necessary to track videos when the dialog containing the video is closed.
                    tracking.trackUsage();
                }
        })
        .load(
            "/ecard/loadVideoModal",
            {decorator:'ajax', id: videoId, eventId: eventId, mediaType:mediaType, videoUrl: videoUrl, distId: distId},
            function() {
                var width = 566;
                var style = 'videoBox';

                // set the width of the modal dialog depending on the type
                if (mediaType == 'playlist'){
                    width = 780;
                    style = "playlistBox";
                }

                $(this).dialog("option", "title", "Usana Video")
                        .dialog("option", "dialogClass", style)
                        .dialog("option", "resizable", false)
                        .dialog("option", "width", width)
                        .dialog("option", "height", 350)
                        .dialog("option", "buttons", {})
                        .dialog('open');
                $(".ui-widget-overlay").addClass("lightBoxOverlay");
            }
        );
    }
});

var wvc = webhosting_view_controller;
