﻿/******************************************************************************
* Summary: Main script for AAS. Contains various functions that are used
*          across multiple pages of the site and some basic event handling.
*****************************************************************************/

/******************************************************************************
* Variables
*****************************************************************************/
var autoHref = 0; 
var _scrollTop = 0; 
var postbackElement;

var FocusControlId;
var NoScrollLinkID;
var BehaviorID;

var helpPopUp;
var bResize=true;

/******************************************************************************
* Summary: Call this function from the Cotent pages to display Calendar 
*          window for selecting a date
*****************************************************************************/
function GetDate(oCtlId)
{
  var oCtlId = new calendar2(document.getElementById('ctl00_cphMain_' + oCtlId));
  oCtlId.year_scroll = true;
  oCtlId.time_comp = false;
  oCtlId.popup(); 
}

/******************************************************************************
* Summary: Cancels the return key "submit" functionality
*****************************************************************************/
function stopRKey(evt) { 
    var evt = (evt) ? evt : ((event) ? event : null); 
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
    if ((evt.keyCode == 13) && (node.type=="text")){return false;} 
}

/******************************************************************************
* Summary: Stops browser scrolling when an internal link is used (for JAWS).
*****************************************************************************/
function NoScrollLink(target, clearHash) { 
    anchor = target.substr(1);
    _scrollTop = document.documentElement.scrollTop;
    //document.body.style.overflow = 'hidden';
    
    if(clearHash != null && clearHash == true)
    {
        location.hash = '#';
    }
    autoHref = 1;
    location.hash = anchor;
}

/******************************************************************************
* Summary: Resets users scroll position when an internal link is used (for JAWS).
*****************************************************************************/
function ScrollHandler(e) { 
   if (autoHref == 1) { 
        autoHref = 0;
        if(CompatibleBrowser() == 0)
        {
            location.hash = '';
        }
        if (window.event) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        if (e && e.preventDefault && e.stopPropagation) {
            
            e.preventDefault();
            e.stopPropagation();
        }
        window.scrollTo(document.documentElement.scrollLeft, _scrollTop);
   }
   //document.body.style.overflow = 'visible';
}

/******************************************************************************
* Summary: Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
*****************************************************************************/
function GetIEVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

/******************************************************************************
* Summary: Returns true is the browser is "newer" and compatible with certain features
*****************************************************************************/
function CompatibleBrowser()
{
  var retVal = 1;
  var ver = GetIEVersion();

  if ( ver > -1 )
  {
    if ( ver <= 6.0 ) 
      retVal = 0;
  }
  return retVal;
}    

/******************************************************************************
* Summary: Ensures that certain date fields are actually dates.
*****************************************************************************/
function CheckDates(val1, val1Name, val2, val2Name)
{
    if(CheckDate(val1, val1Name) == 0 || CheckDate(val2, val2Name) == 0)
    {
        return false
    }
    return true;
}

/******************************************************************************
* Summary: Ensures that a certain date field is actually a date.
*****************************************************************************/
function CheckDate(val, valName)
{
    if(val.trim() == '')
    {
        return true;
    }
    if(isNaN(Date.parse(val)))
    {
        alert(valName + ' is not a valid date.');
        return false;
    }
    return true;
}

/******************************************************************************
* Summary: Cross-browser compatiable method of setting disabled button styles.
*          Some browsers don't support CSS selectors, this is an alternative (JQuery)
*****************************************************************************/
function SetDisabledButtonStyle()
{
    //use JQuery to get a handle on all disabled page buttons.
    //then set the disabled button style
    var arraySubmitButtons = $("input[@disabled][@class=button]").get(); 
    $(arraySubmitButtons).each(function(){ 
        $(this).removeClass(); 
        $(this).addClass('button-disabled'); 
    });  
    
    var navigationButtons = $("input[@disabled][@class=button-grid-navigation]").get(); 
    $(navigationButtons).each(function(){ 
        $(this).removeClass(); 
        $(this).addClass('button-grid-navigation-disabled'); 
    });
}

/******************************************************************************
* Summary: Cross-browser compatiable method of setting required field styles.
*          Some browsers don't support CSS selectors, this is an alternative (JQuery)
*****************************************************************************/
function SetRequiredFieldStyle()
{
    var requiredSpan = $("[@class=label-required]").get(); 
    $(requiredSpan).each(function(){ 
        $(this).prepend("<font color='#ff0000'>*</font>"); 
    });         
}    

/******************************************************************************
* Summary: Cross-browser compatiable method of setting disabled field styles.
*          Some browsers don't support CSS selectors, this is an alternative (JQuery)
*****************************************************************************/
function SetDisabledFieldStyle()
{
    var disabledFields = $("input[@disabled][@class=textbox]").get(); 
    $(disabledFields).each(function(){ 
        $(this).removeClass(); 
        $(this).addClass('label'); 
    });   
    var readOnlyFields = $("input[@readonly][@class=textbox]").get(); 
    $(readOnlyFields).each(function(){ 
        $(this).removeClass(); 
        $(this).addClass('label'); 
    });            
}

/******************************************************************************
* Summary: Closes the bottom message bar if it was rendered and the user 
*          pressed the "close" button.
*****************************************************************************/
function CloseBottomMessageBar()
{
    var msgPanel = $get('ctl00_pnlFilesDownloaded');
    
    if(msgPanel != null)
    {
        msgPanel.style.display = 'none';
    }
}

/******************************************************************************
* Summary: AJAX begin request event handler.
*****************************************************************************/
function beginRequest(sender, args) {
    postbackElement = args.get_postBackElement();
}

/******************************************************************************
* Summary: Event handler fired when a page has completed an asyn post-back
*****************************************************************************/
function pageLoadedAsync(sender, args)
{
    SetDisabledButtonStyle();
    SetDisabledFieldStyle();

    if(typeof(postbackElement) === "undefined" || postbackElement.id != 'ctl00_tvMenu')
    {
        SetRequiredFieldStyle();
    }
}

/******************************************************************************
* Summary: Called to display the SSRepotr
*****************************************************************************/
function ShowReport(reportURL,reportName)
{
    window.open(reportURL,reportName,"toolbar=no,width=700,height=600,scrollbars=yes");
    return false;
}

/******************************************************************************
* Summary: ClientSide function to set CheckedListBox Items checked state
*****************************************************************************/
function ToggleCheckedListBoxSelection(ctlId,bChecked)
{
 var cblItem = document.getElementById(ctlId);
  for(i=0;i<=cblItem.rows.length-1;i++)
 {
    cblItem.rows[i].cells[0].childNodes[0].checked = bChecked;
 }
 return false;
}

/******************************************************************************
* Handle the calling of a modal popup; 
*****************************************************************************/
function CallModalPopup(sender, args)
{
    var modalPopup = $find(BehaviorID);
    modalPopup.show();
    NoScrollLink(NoScrollLinkID);
    
    /* Find the control to make the default and put focus on it*/
    var modalFocusControl = document.getElementById(FocusControlId);
//    alert(FocusControlId);
//    alert(modalFocusControl)
    if(modalFocusControl != null){modalFocusControl.focus()};
}

/******************************************************************************
* Handle the calling of a help popup; 
*****************************************************************************/
function CallPaneHelp(url) 
{
	var sw = screen.width*0.24;
	var sWD="toolbar=0,status=0,menubar=0,resizable=1,left="+(screen.width-sw)+",top=0,width="+(sw-12) + ",height="+(screen.height-30);
	bResize=false;
	top.window.resizeTo(screen.width-sw,screen.height);
	top.window.moveTo(0,0);
	helpPopUp = window.open(url,'EEMHelp',sWD);
	helpPopUp.focus();
}

/******************************************************************************
* Wire up some basic event handlers for the window and document
*****************************************************************************/
document.onkeypress = stopRKey; 
String.prototype.trim = function() { this.replace(/^\s+|\s+$/, ''); };
window.onscroll = ScrollHandler;      



