// v8.9.12


// ----------------------------------------------------------------------------
// Global variables
// ----------------------------------------------------------------------------


var isOpera = ( navigator.userAgent.indexOf('Opera') != -1 ); 

var isIE = ( !isOpera && navigator.userAgent.indexOf('MSIE') != -1 ); 


// ----------------------------------------------------------------------------
// Strings
// ----------------------------------------------------------------------------


function InStr( haystack, needle )
{
    var i;
    
	for ( i = 0; i < Len( haystack ); i++ )
	{
		if ( needle == Mid( haystack, i, Len( needle ) ) )
		{
			return i;
		}
	}
	
	return -1;
}

function Len( s )
{  
    return String(s).length;  
}

function Mid( sourceText, startAtIndex, lengthToExtract )
{
    if ( startAtIndex < 0 || lengthToExtract < 0 ) 
		return "";

    var sourceTextLength = String(sourceText).length;
    
    var endAtIndex;
    
    if ( startAtIndex + lengthToExtract > sourceTextLength )
        endAtIndex = sourceTextLength;
    else
        endAtIndex = startAtIndex + lengthToExtract;

    return String(sourceText).substring( startAtIndex, endAtIndex );
}

function Trim( s ) 
{
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}

	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}
	
	return s;
}

String.prototype.Copy = function(stringLength) { var s = '', i = 0; while (i++ < stringLength) { s += this; } return s; }

String.prototype.ZeroFill = function(stringLength) { return '0'.Copy(stringLength - this.length) + this; }

Number.prototype.ZeroFill = function(stringLength) { return this.toString().ZeroFill(stringLength); }


// ----------------------------------------------------------------------------
// Numbers
// ----------------------------------------------------------------------------


/*
 * Converts a numeric value to a string value. If the argument is not a number, 
 * then the argument itself is returned unmodified.
 *
 * Example: ConvertNumberToString( 1234.5678, 3 ) == "1,234.567"
 * Example: ConvertNumberToString( 1234.5678, 0 ) == "1,234"
 */
function FormatNumber( n, precision ) { return ConvertNumberToString(n,precision); }
function ConvertNumberToString( n, precision ) 
{
    // Trim and remove commas
	n += "";
	n = Trim(n.split(",").join(""));

	var formatted = n;
	
	if ( ! isNaN( n ) )
	{
	    var a = ""; // before decimal place
		var b = ""; // after decimal place
		var isNegative = false;
	
		// Is this a negative number?
		
		if ( n < 0 )
		{
			isNegative = true;
			n = Math.abs(n);
		}
    	
		// Determine the portion preceding the decimal place
		
		a = Math.floor( n );
	
		a += "";
		if ( a.length <= 3 )
		{
			a = ( a == '' ? '0' : a );
		}
		else 
		{
			var mod = a.length % 3;
			var output = ( mod == 0 ? '' : ( a.substring( 0, mod ) ) );
			for ( i = 0; i < Math.floor( a.length/3 ); i++ ) 
			{
				if ( mod == 0 && i == 0 )
					output += a.substring( mod+3*i, mod+3*i+3 );
				else
					output += ',' + a.substring( mod+3*i, mod+3*i+3 );
			}
			a = output;
		}
	
		// Determine the portion following the decimal place
		
		n += ""; // convert number to a string
		var decimalPosition = n.indexOf( "." );
		if ( decimalPosition > -1 )
		{
			b = n.substr( decimalPosition + 1, precision )
		}
		
		if ( precision > 0 )
		{
			var len = b.length;
			for ( var i = 0; i < precision - len; i++ )
				b += "0";
			
			b = "." + b;
		}
		
		// Return the formatted value

		formatted = a + b;
		if ( isNegative )
			formatted = "-" + formatted;
	}
	
	return formatted;
}

/*
 * Converts a standard-format numeric string to a floating-point number.
 *
 * Example: ConvertToNumber( "1,234.567" ) == 1234.567
 */
function ConvertToNumber( s ) { return ConvertStringToNumber(s); }
function ConvertStringToNumber( s ) 
{
    if ( s.substring(0,1) == "$" )
        s = s.substring(1,s.length-1);

	var n = parseFloat( s.split(",").join("") );
	
	if ( isNaN( n ) )
		return 0;
	else
		return n;
}


// ----------------------------------------------------------------------------
// Dates
// ----------------------------------------------------------------------------


var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

Date.prototype.Format = function(f)
{
    if (!this.valueOf())
        return '&nbsp;';

    var d = this;

    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|d|hh|h|nn|ss|a\/p|tt)/gi,
        function($1)
        {
            switch ($1.toLowerCase())
            {
            case 'yyyy': return d.getFullYear();
            case 'mmmm': return monthNames[d.getMonth()];
            case 'mmm':  return monthNames[d.getMonth()].substr(0, 3);
            case 'mm':   return (d.getMonth() + 1).ZeroFill(2);
            case 'dddd': return dayNames[d.getDay()];
            case 'ddd':  return dayNames[d.getDay()].substr(0, 3);
            case 'dd':   return d.getDate().ZeroFill(2);
            case 'd':    return d.getDate().ZeroFill(1);
            case 'hh':   return ((h = d.getHours() % 12) ? h : 12).ZeroFill(2);
            case 'h':    return ((h = d.getHours() % 12) ? h : 12).ZeroFill(1);
            case 'nn':   return d.getMinutes().ZeroFill(2);
            case 'ss':   return d.getSeconds().ZeroFill(2);
            case 'a/p':  return d.getHours() < 12 ? 'AM' : 'PM';
            case 'tt':  return d.getHours() < 12 ? 'AM' : 'PM';
            }
        }
    );
}

function IsLeapYear(year) 
{
   if (year % 4 == 0) {
      if (year % 100 == 0) {
         return (year % 400 == 0);
      } else {
         return (true);
      }
   }
   return (false);
}

function DaysInMonth(month, year) 
{
   switch (month) {
      case  0: return 31;  // january
      case  1:             // february
         if (IsLeapYear(year)) {
            return 29;
         } else {
            return 28;
         }
      case  2: return 31;  // march
      case  3: return 30;  // april
      case  4: return 31;  // may
      case  5: return 30;  // june
      case  6: return 31;  // july
      case  7: return 31;  // august
      case  8: return 30;  // september
      case  9: return 31;  // october
      case 10: return 30;  // november
      case 11: return 31;  // december
   }
}

function DateAdd(datepart, number, date) 
{
   var d = new Date(date);
   switch (datepart) {
      // millisecond
      case "ms":
         return new Date(Date.parse(d) + (number));

      // second
      case "s":
      case "ss":
         return new Date(Date.parse(d) + (number*1000));

      // minute
      case "n":
      case "mi":
         return new Date(Date.parse(d) + (number*1000*60));

      // hour
      case "hh":
         return new Date(Date.parse(d) + (number*1000*60*60));

      // day
      case "d":
      case "dd":
         return new Date(Date.parse(d) + (number*1000*60*60*24));

      // week
      case "wk":
      case "ww":
         return new Date(Date.parse(d) + (number*1000*60*60*24*7));

      // month
      case "m":
      case "mm":
         var i = 0;
         var maxcurr = DaysInMonth(d.getMonth(), d.getFullYear());
         var mm = (d.getMonth() + number) % 12;
         if (mm < 0) mm += 12;
         var yy = d.getFullYear() + Math.floor((number + d.getMonth()) / 12);
         var maxnext = DaysInMonth(mm, yy);
         if (maxnext < d.getDate()) {
            i = (maxnext - d.getDate());
         }
         if (d.getDate() == maxcurr) {
            i = (maxnext - maxcurr);
         }
         return new Date(
            d.getFullYear(),
            d.getMonth()+number,
            d.getDate()+i,
            d.getHours(),
            d.getMinutes(),
            d.getSeconds());

      // quarter
      case "q":
      case "qq":
         var i = 0;
         var maxcurr = DaysInMonth(d.getMonth(), d.getFullYear());
         var mm = (d.getMonth() + number*3) % 12;
         if (mm < 0) mm += 12;
         var yy = d.getFullYear() + Math.floor((number*3 + d.getMonth()) / 12);
         var maxnext = DaysInMonth(mm, yy);
         if (maxnext < d.getDate()) {
            i = (maxnext - d.getDate());
         }
         if (d.getDate() == maxcurr) {
            i = (maxnext - maxcurr);
         }
         return new Date(
            d.getFullYear(),
            d.getMonth()+number*3,
            d.getDate()+i,
            d.getHours(),
            d.getMinutes(),
            d.getSeconds());

      // year
      case "y":
      case "yy":
      case "yyyy":
         var i = 0;
         if (d.getMonth() == 1) {
            if (d.getDate() == 29) {
               if (!IsLeapYear(d.getFullYear() + number)) {
                  i = -1;
               }
            }
            if (d.getDate() == 28) {
               if (!IsLeapYear(d.getFullYear())) {
                  if (IsLeapYear(d.getFullYear() + number)) {
                     i = 1;
                  }
               }
            }
         }
         return new Date(
            d.getFullYear()+number,
            d.getMonth(),
            d.getDate()+i,
            d.getHours(),
            d.getMinutes(),
            d.getSeconds());
   }
}
  
Date.prototype.WeekStart = function()
{
    return DateAdd( "d", - (this.getDay()%7), this );
}

Date.prototype.WeekEnd = function()
{
    return DateAdd( "d", 6, this.WeekStart() );
}

Date.prototype.MonthStart = function()
{
    var start = new Date(this);
    start.setDate( 1 );
    return start;
}

Date.prototype.MonthEnd = function()
{
    return DateAdd( "d", -1, DateAdd( "m", 1, this.MonthStart() ) );
}

Date.prototype.YearStart = function()
{
    var start = new Date(this);
    start.setMonth( 0 );
    start.setDate( 1 );
    return start;
}

Date.prototype.YearEnd = function()
{
    return DateAdd( "d", -1, DateAdd( "y", 1, this.YearStart() ) );
}
 

// ----------------------------------------------------------------------------
// Input control values
// ----------------------------------------------------------------------------


function SetElementValue( controlID, value )
{
    var c = document.getElementById( controlID );
    
    if ( c != null )
    {
        c.value = value;
    }
}

function GetElementValue( controlID )
{
    var c = document.getElementById( controlID );
    
    if ( c != null )
    {
        return c.value;
    }
    else
    {
        return null;
    }
}

function GetElementText( controlID )
{
    var c = document.getElementById( controlID );
    
    if ( c != null )
    {
        return c.options[c.selectedIndex].text;
    }
    else
    {
        return null;
    }
}

/*
 * Automatically formats the input control value to a standard numeric format.
 *
 * Example: <input type=text onchange="AutoFormatNumber(this,2)">
 */
function AutoFormatNumber( control, precision )
{
	var v = control.value;
	
	if ( v.length > 0 )
	{
		if ( v.toUpperCase() == "N/AV" )
			control.value = "N/AV";
		
		else if ( v.toUpperCase() == "N/AP" )
			control.value = "N/AP";
		
		else
			control.value = ConvertNumberToString(v,precision);
	}
}

/*************************************************************************
 * Automatically formats a telephone number to use a standard format. 
 * The control value "2506120071" becomes "(250) 612-0071". This function 
 * applies only to a string with a length of 10. 
 *
 * Example: <input type=text onchange="AutoFormatPhone(this)">
 */
function AutoFormatPhone( control )
{
	var p = control.value;
	
	if ( p.length == 10 )
	{
		var area = p.substr( 0, 3 );
		var prefix = p.substr( 3, 3 );
		var number = p.substr( 6, 4 );
		var np = "(" + area + ") " + prefix + "-" + number;
		
		control.value = np;
	}
}

/*
 * Automatically trims the text value in an input control. 
 *
 * Example: <input type=text onchange="TrimControlValue(this)">
 */
function AutoTrimValue( control )
{
	var v = control.value;
	
	if ( v.length > 0 )
	{
	    control.value = Trim(v);
    }
}

/*************************************************************************
 * Client-side validation function for numeric range checking. Implements
 * the evaluation function for my NumberRangeValidator web server control.
 */
function NumberRangeValidatorIsValid( source, args )
{
	var isValid = true;
	
	var value = ValidatorGetValue( source.controltovalidate );
	
	if ( value.length > 0 )
	{
		if ( value.toUpperCase() != "N/AV" && value.toUpperCase() != "N/AP" )
		{
			var x = ConvertToNumber( value );
			isValid = ( source.minimumvalue <= x && x <= source.maximumvalue );
        }
    }
	
	return isValid;
}


// ----------------------------------------------------------------------------
// Checkboxes
// ----------------------------------------------------------------------------


function CheckAll() 
{ 
	var frm = document.forms[0];
    
    var i;
    
	for (i=0; i<frm.elements.length; i++)
	{
		if ( frm.elements[i].id.indexOf('IsSelected') > -1 )
		{
			frm.elements[i].checked = true;
		}
	}
}

function UncheckAll() 
{ 
	var frm = document.forms[0];
    
    var i;
    
	for (i=0; i<frm.elements.length; i++)
	{
		if ( frm.elements[i].id.indexOf('IsSelected') > -1 )
		{
			frm.elements[i].checked = false;
		}
	}
}

/******************************************************************************
 * If an "IsSelected" checkbox is checked then prompt the user for confirmation
 * to proceed.
 */
function ConfirmDeleteIfChecked( ) 
{ 
    var frm = document.forms[0];
    
    var i;
    
	for ( i=0; i<frm.length; i++ ) 
	{
		// Look for our checkboxes only
		if ( frm.elements[i].id.indexOf( 'IsSelected' ) > -1 ) 
		{
			if ( frm.elements[i].checked )  
			{
				return confirm ('Are you sure you want to delete the selected item(s)?')
			}
		}
	}
}


// ----------------------------------------------------------------------------
// Radio buttons
// ----------------------------------------------------------------------------


/*************************************************************************
 * Returns the index of the selected radio button or -1 if no button is 
 * selected. 
*/
function GetRadioSelectedIndex(buttonGroupName) 
{ 
    var buttonGroup = document.getElementsByName(buttonGroupName);
    
    if ( buttonGroup[0] ) 
    { 
        // The button group is an array (one button is not an array)
        for (var i=0; i<buttonGroup.length; i++) 
        {
            if (buttonGroup[i].checked) 
            {
                return i
            }
        }
    } 
    else 
    {
        if ( buttonGroup.checked ) 
        { 
            return 0; 
        } 
    }
   
    // No radio button is selected
    return -1;
} 

/*************************************************************************
 * Returns the value of the selected radio button or "" if no button is 
 * selected.
 */
function GetRadioSelectedValue(buttonGroupName) 
{
    var i = GetRadioSelectedIndex(buttonGroupName);
    
    if (i == -1) 
    { 
        return "";
    } 
    else 
    { 
        var buttonGroup = document.getElementsByName(buttonGroupName);
        
        if (buttonGroup[i]) 
        {   
            // Make sure the button group is an array (not just one button)
            return buttonGroup[i].value;
        } 
        else 
        {   
            // The button group is just the one button, and it is checked
            return buttonGroup.value;
        }
    }
}

/*************************************************************************
 * Enables/disables an entire radio button group.
 */
function DisableRadioGroup( buttonGroupName, disable ) 
{
    var buttonGroup = document.getElementsByName(buttonGroupName);
    
	if ( buttonGroup.tagName == 'SELECT' )
	{
		buttonGroup.disabled = disable;
	}
	else if ( buttonGroup[0] ) 
	{ 
	    // The button group is an array (one button is not an array)
		for ( var i = 0; i < buttonGroup.length; i++ )
		{
			buttonGroup[i].disabled = disable;
			
			if ( buttonGroup[i].checked )
		        buttonGroup[i].checked = false;
		}
	}
	else
	{
	    // The button is a single element
		buttonGroup.disabled = disable;
		
		if ( buttonGroup.checked )
		    buttonGroup.checked = false;
	}
}


// ----------------------------------------------------------------------------
// Browser windows
// ----------------------------------------------------------------------------


/*************************************************************************
 * Opens a new window for the given URL.
 */
function openWindow( URL ) 
{
	return window.open( URL );
}

/*************************************************************************
 * Opens a new window for the given URL. The window is centered on the 
 * screen.
 */
function openWindowCenter( url, name, width, height, attributes ) 
{
    if ( attributes != null && attributes.length > 0 )
        attributes += ",";
    
    if ( attributes != null )
	    attributes += "height=" + height + ",width=" + width;
	else
	    attributes = "height=" + height + ",width=" + width;
	
	if ( window.screen ) 
	{
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

		attributes += ",left=" + xc + ",screenX=" + xc;
		attributes += ",top=" + yc + ",screenY=" + yc;
	}
	
	var newwindow = window.open( url, name, attributes );
	if ( !newwindow.opener ) newwindow.opener = self;
	if (window.focus) {newwindow.focus()}
		
	return newwindow;

}

/*************************************************************************
 * Opens a new window for a report display. The window fills up as much 
 * available space as possible.
 */
function openWindowFullScreen( url, name, attributes ) 
{
	if ( window.screen ) 
	{
		var ah = screen.availHeight;
		var aw = screen.availWidth;

		attributes += ",width=" + aw;
		attributes += ",height=" + ah;
		
		newwindow = window.open( url, name, attributes );
		if (window.focus){newwindow.focus()}
	}
	return newwindow;
}

/*************************************************************************
 * Opens a new window to preview a block of HTML text.
 */
function openWindowPreviewHtml( title, html ) 
{
	var buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>" + title + "</title>\n"+
		"<link href='../skin/Engine.css' type='text/css' rel='Stylesheet'>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n"+
		"<div style='margin:4px'>\n"+
		html + "\n" +
		"</div>\n"+
		"</body>\n" +
		"</html>\n" );

	var popup = openWindowCenter( "", title, 400, 600, "scrollbars=yes," );
	popup.opener = self;
	
	var calc_doc = popup.document;
	calc_doc.open();
	calc_doc.write( buffer );
	calc_doc.close();
	
	popup.focus();
}
