﻿// JScript File

$(document).ready(function(){

    //*****
    //Hack for Firefox/Opera/Safari to avoid freezing buttons
    //on browser <BACK> due to side effects of below script.
    window.onunload = function(){};
    //*****

    /******This script blocks multiple button clicks*****/
    var _formSubmitted = false;
    
    singleClickHandler = function(){
      var _validFlag=true;
      
      //if validator exists, check if page is valid
      if(typeof(Page_IsValid)!="undefined") 
      {
        _validFlag=Page_IsValid;
      }
      
      if(_validFlag)
      {
        if (_formSubmitted)
        {
            return false;
        }

        _formSubmitted = true;  
      }            
    }

    $("form").bind("submit", singleClickHandler);
    $(".SingleClickLink").bind("click", singleClickHandler);

    /******end of This script blocks multiple button clicks*****/
    
  /****Accordian Menu*****/
  
  //show until DOM is ready to prevent FOUC
  if ($('#SideMenu').is(":hidden")) {
        $('#SideMenu').show();
  }
  
  //fix for IE 6 stacking images on global home page
  if ($('.Globalimgslides').is(":hidden")) {
        $('.Globalimgslides').show();
  }
  
  //handle header click
    var closeNavFirstFlag=true;
    
    $('#SideMenu a.menuBar_nav-first').click(function() { 
            var link = jQuery(this);
            var _href=link.attr("href");
            var _parent=$(this).parent()
            var _currentTabIndex = _parent.attr("tabIndex");
            
            if(enableHeaderAccordian && closeNavFirstFlag && (_currentTabIndex!=null && _currentTabIndex==0)) 
            {
              closeNavFirstFlag=false;
              return true;
            }else
            {
	              closeNavFirstFlag=true;
	              location.href = link.attr("href");
	              return false;
	            }
        });   	 
        
        //handle my account header click when not logged in
        $('#SideMenu div#menuHeader1Auth').click(function() { 
        var link=$(this).find('a');
        location.href = link.attr("href");
	        return false;
      });

    //handle second layer click
    var closeNavSecondFlag=true;
    
    $('#SideMenu a.menuBar_nav-second').click(function() { 
            
            var link = jQuery(this);
            var _href=link.attr("href");
            var _parent=$(this).parent();
            var _expanded = _parent.attr("aria-expanded");
            if(closeNavSecondFlag && (_expanded!=null && _expanded=="true")) 
            {
              closeNavSecondFlag=false;
              return true;
            }else
            {
	              closeNavSecondFlag=true;
	              location.href = link.attr("href");
	              return false;
	            }
        });	  

    //handle second layer(Selected) click
    var closeNavSecondSelFlag=true;
    $('#SideMenu a.menuBar_nav-secondSel').click(function() { 
            
            var link = jQuery(this);
            var _href=link.attr("href");
            
            var _parent=$(this).parent();
            var _expanded = _parent.attr("aria-expanded");
            if(closeNavSecondSelFlag && (_expanded!=null && _expanded=="true")) 
            {
              closeNavSecondSelFlag=false;
              return true;
            }else
            {
	              closeNavSecondSelFlag=true;
	              location.href = link.attr("href");
	              return false;
	            }
        });	  	          
    /****end of Accordian Menu*****/
    
    /****For whole middle column****/
  //show until DOM is ready to prevent FOUC
  if ($('#HomePageCenter').is(":hidden")) {
        $('#HomePageCenter').show();
  }
  
    //
    // force all password fields to not allow password saving (TFS4455 - Plynt #2)
    $('input[type=password]').attr('autocomplete', 'off');

});


var MessagePanelHelper = new function () {

    var regexNoTags = /<(?![!/]?(a|b|i|u)[>\s])[^>]*>/g; // remove all html markup except br,a,b,i and u
    var oldValidationSummaryOnSubmit;
    var jMessagePanel;
    var jMessagePanelEmpty;
    var jMessageBox;
    
    this.ClearAlerts = function () {
        jMessagePanel.html('');
        jMessagePanelEmpty.show();
    }

    this.AddAlert = function (alertData) {
        var jAlert = $('<div />');

        jAlert.text(alertData.alertText);
        if (alertData.showIcon) jAlert.addClass("icon");
        if (alertData.alertType) jAlert.addClass(alertData.alertType);
        if (alertData.alertItems) {
            var jMenu = $('<ul />');
            for (i = 0; i < alertData.alertItems.length; i++) {
                var jItem = $("<li />").text(alertData.alertItems[i]);
                jMenu.append(jItem);
            }
            jAlert.append(jMenu);
        }

        jMessagePanel.append(jAlert);
        jMessagePanelEmpty.hide();
    }
    
    this.MessageBox = function(text, title) {
    
        // use the ui-dialog if located
        if (jMessageBox && jMessageBox.length)
        {
            // ensure the dialog is closed so it will open at the correct coordinates
            jMessageBox.dialog("close");
            
            // set the content
            jMessageBox.html(text);
            if (title)
                jMessageBox.dialog("option", "title", title);
                
            // open it as modal with fixed width
            jMessageBox.dialog("option", "modal", true);
            jMessageBox.dialog("option", "width", 470);
            jMessageBox.dialog("option", "height", 'auto');
            jMessageBox.dialog("open");
        }
        else // no ui-dialog, use standard alert box
        {
            alert(jElement.attr('title'));
        }
    }

    this.Hook_ValidationSubmission = function() {
        if (typeof (ValidationSummaryOnSubmit) != "undefined") {
            oldValidationSummaryOnSubmit = ValidationSummaryOnSubmit;
            ValidationSummaryOnSubmit = Validation_OnSubmit;
        }
    }

    Validation_OnSubmit = function (validationGroup) {

        if (typeof (Page_ValidationSummaries) == "undefined" ||
            typeof (Page_Validators) == "undefined")
            return;

        MessagePanelHelper.ClearAlerts();
        var handled = false;
        if (!Page_IsValid) {
            for (var sums = 0; sums < Page_ValidationSummaries.length; sums++) {
                var summary = Page_ValidationSummaries[sums];
                summary.style.display = "none";
                if (summary.showsummary != "False" && typeof (summary.headertext) == "string") {

                    var text = summary.headertext.replace(regexNoTags, "");
                    if (text.length > 0) {
                        var items = new Array();

                        for (i = 0; i < Page_Validators.length; i++) {
                            if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string") {
                                var itemText = Page_Validators[i].errormessage.replace(regexNoTags, '');
                                if (itemText.length > 0) {
                                    items.push(itemText);
                                }
                            }
                        }
                        MessagePanelHelper.AddAlert({
                            alertText: text,
                            alertType: "validation",
                            alertItems: items,
                            showIcon: true
                        });

                        handled = true;
                    }
                }
            }
        }
        
        if (!handled) {
            oldValidationSummaryOnSubmit();
        } else {
                jMessagePanel.append('<hr size=\'1\' id=\'MessagePanelHRTag\' class=\'horizontalRule\' />');
                window.scrollTo(0, 0);
        }
    } // Validation_OnSubmit

    $(document).ready(function () {
    
        jMessagePanel = $('#MessagePanel');
        jMessagePanelEmpty = $('#MessagePanelEmpty');
        jMessageBox = $('#MessagePanelAlert');
        jMessageBox.dialog({autoOpen: false, modal: true });
        // use 'ui-theme' as the namespace wrapper to avoid conflict with other ui- artifacts
        jMessageBox.parents('.ui-dialog:eq(0)').wrap('<div class="ui-theme"></div>');
        
    });
    
} // MessagePanelHelper

var CollapseHelper = new function() {

    this.MakeCollapsible = function(opts) {
    
        var jTarget = $(opts.selTarget);
        
        //
        // only enable if sideBar is present 
        if (opts.selMustExist && $(opts.selMustExist, jTarget).length == 0)
            return;
            
        // find the hide/show command elements
        var jShow = $(opts.selShow);
        var jHide = $(opts.selHide);

        // update controls base on hidden state
        function setSideBarState(hidden) {
        
            if (hidden) {
                jHide.hide();
                jShow.show();
                jTarget.hide();
            } else {
                jHide.show();
                jShow.hide();
                jTarget.show();
            }

            if (opts.cookieName)
                document.cookie = opts.cookieName + "=" + hidden + "; path=/";
        }
        
        jHide.click(function() {
            //
            // prepare to slide in the sidebar by animating the width 
            jTarget.animate( { width : 0 }, 400, function() {
                jTarget.width('auto');
                jTarget.hide();
            });

            setSideBarState(true);
        });
        
        jShow.click(function() {
            //
            // prepare to slide out the sidebar from hidden to normal width
            var width = jTarget.width();
            jTarget.width(0);
            jTarget.show();
            jTarget.animate( { width : width }, 400);

            setSideBarState(false);
        });

        // set the initial side bar state (show unless prior state was hidden)        
        setSideBarState(opts.cookieName && (getCookie(opts.cookieName) == "true"));
    }
}

//
// jQuery Control Action Helper Methods
var ControlActionHelpers = new function() {

    var eventName = 'clickAction';
    
    //
    // create a custom event handler for processing the dependency graph
    function action(jCheckbox, options, selArray) {
        
        var isChecked = jCheckbox.is(':checked') && !jCheckbox.is(':disabled');
        
        for(var dep in selArray)
        {
            var jDependents = $(selArray[dep].selDependents);
            var jValidators = $(selArray[dep].selValidators);
            var invertAction = selArray[dep].invertAction? selArray[dep].invertAction : false;
            var hasDatepicker = selArray[dep].hasDatepicker? selArray[dep].hasDatepicker : false;
            
            if (isChecked == !invertAction)
            {
                if (selArray[dep].disableOnAction)
                {
                    if (hasDatepicker)
                        jDependents.datepicker('enable');
                    jDependents.removeAttr('disabled');
                    jDependents.removeClass('disabled');
                    
                    if ($.browser.msie && $.browser.version < 7) {
                        jDependents.each(function() { // ie6 fix
                            if (this.type) $(this).removeClass('disabled_' + this.type);
                        });
                    }
                }
                    
                jValidators.each(function() {
                    ValidatorEnable(this, true);
                });
                
                //
                // restore any saved checked state
                jDependents.filter('input:checkbox, input:radio').each(function() {
                    if ($(this).attr('checked_save') > 0)
                        $(this).attr('checked', true);
                });

                if (selArray[dep].defaultCheck && !jDependents.is(':checked')) {
                    $(selArray[dep].defaultCheck).attr('checked', true);
                }
                
                if (selArray[dep].defaultText && !jDependents.val()){
                    jDependents.val(selArray[dep].defaultText);
                }
            }
            else
            {
                if (selArray[dep].disableOnAction)
                {
                    if (hasDatepicker)
                        jDependents.datepicker('disable');
                    jDependents.attr('disabled', 'disabled');
                    jDependents.addClass('disabled');
                    
                    if ($.browser.msie && $.browser.version < 7) {
                        jDependents.each(function() { // ie6 fix
                            if (this.type) $(this).addClass('disabled_' + this.type);
                        });
                    }
                }
                
                jValidators.each(function() {
                    ValidatorEnable(this, false);
                });
                
                //
                // save original check state
                jDependents.filter('input:checkbox, input:radio').each(function() {
                    $(this).attr('checked_save', $(this).is(':checked') ? 1 : 0);
                });
                
                if (selArray[dep].clearChecks)
                    jDependents.attr('checked', false);
            }
        
            //
            // fire the handler for all dependents
            jDependents.each(function() {
                $(this).triggerHandler(eventName);
            });
        };
    };
        
    this.RegisterCheckboxClickAction = function (selCheckBox, options, selArray)
    {
        var jCheckBox = $(selCheckBox);
        
        //
        // for radio buttons - change events are handled by the group and not the
        // individual control
        if (options.isGrouped)
        {
            var name = jCheckBox.attr('name');
            var jGroup = $('[name=\"'+name+'\"]');
            
            jGroup.click(function(){
                jCheckBox.triggerHandler(eventName);
            });
        }
        
        //
        // bind the custom event handler
        jCheckBox.bind(eventName, function() { action($(this), options, selArray); });
        
        //
        // bind the click handler so it triggers the custom event
        jCheckBox.click(function() { $(this).triggerHandler(eventName); });
        
        //
        // trigger the action to set the initial state of the controls 
        jCheckBox.triggerHandler(eventName);
    };
}

