var browser=navigator.appName;
if ( browser != "Microsoft Internet Explorer" )
{
	HTMLElement.prototype.click = function() {
		try {
			/*
			var evt = this.ownerDocument.createEvent('MouseEvents');
			evt.initMouseEvent("click", true, true, this.ownerDocument.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
			this.dispatchEvent(evt);
			*/
			var result = true;
			if (this.onclick) result = this.onclick();
			if ( typeof(result) == 'undefined' || result ) {
				eval( this.href );
			}					
		} catch (e) {
			alert( e.message );
		}
	}
}
function FireLink( buttonToClick, eventObj )
{
	eventObj = eventObj || window.event;
	var tgt = eventObj.target || eventObj.srcElement;
	if ( eventObj.keyCode == 13)
	{
		eventObj.cancelBubble = true;
		eventObj.returnValue = false;
		document.getElementById( buttonToClick ).click();
	}
}
function CancelLink( buttonToClick, eventObj )
{
	eventObj = eventObj || window.event;
	var tgt = eventObj.target || eventObj.srcElement;
	if ( eventObj.keyCode == 27)
	{
		eventObj.cancelBubble = true;
		eventObj.returnValue = false;
		document.getElementById( buttonToClick ).click();
	}
}
function GetClientDate() {
	document.write(GetFormattedClientDate());
}
function GetFormattedClientDate(format) {
	var myFormat = ( format == null || typeof( format ) == "undefined" ) ? "MMMM dd, yyyy" : format;
	var months = new Array(13);
	months[1] = "January";
	months[2] = "February";
	months[3] = "March";
	months[4] = "April";
	months[5] = "May";
	months[6] = "June";
	months[7] = "July";
	months[8] = "August";
	months[9] = "September";
	months[10] = "October";
	months[11] = "November";
	months[12] = "December";

	var time = new Date();
	var lmonth = months[ time.getMonth() + 1 ];
	var date = time.getDate();
	var year = time.getFullYear();
	
	var ret = myFormat.replace( "MMMM", months[ time.getMonth() + 1 ] ).replace( "MM", ZeroPad( time.getMonth() + 1, 2, false ) ).replace( "M", time.getMonth() + 1 );
	ret = ret.replace( "dd", ZeroPad( time.getDate(), 2, false ) ).replace( "d", time.getDate() );
	ret = ret.replace( "yyyy", year );
	return ret;
}
function ZeroPad(value, length, decimals) {
    // Convert the number to a string
    var value_string = value.toString();
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");
	var pad_total = 0;
	var original_length = 0;
	
	if ( decimals ) {
		// Is there a decimal point?
		if (decimal_location == -1) {
			// If decimal_places is greater than zero, tack on a decimal point
			value_string += length > 0 ? "." : "";
		}
		else {
			// If yes, then only the extra decimal places will be padded with 0s
			original_length = value_string.length - decimal_location - 1;
		}
	}
	else {
		// Is there a decimal point?
		if ( decimal_location == -1 ) {
			// If no, then only the extra places will be padded with 0s
			original_length = value_string.length;
		}
		else {
			// If yes, then only the extra decimal places will be padded with 0s
			original_length = decimal_location - 1;
		}
	}

	// Calculate the number of decimal places that need to be padded with 0s
	pad_total = length - original_length;
	if (pad_total > 0) {
		// Pad the string with 0s
		for (var counter = 1; counter <= pad_total; counter++) {
			if ( decimals )
				value_string += "0";
			else
				value_string = "0" + value_string;
		}
	}

    return value_string
}

function ToggleView(div1Name, div2Name) {
	var div1 = document.getElementById(div1Name);
	var v = div1.style.display;

	if ( div2Name == null || typeof( div2Name ) == "undefined" ) {
		if ( v == "none" )
			div1.style.display = "";
		else
			div1.style.display = "none";		
	
	}
	else {
		var div2 = document.getElementById(div2Name);
		div1.style.display = div2.style.display;
		div2.style.display = v;
	}
}
function SetResultNavClass(lnkCurrent) {
	SetCurrentNavClass( lnkCurrent, "lnkResult" );
}
function SetCurrentNavClass(lnkCurrent, lnkName, totalItems, className, selectedClassName) {
	var _className = ( className == null || typeof( className ) == "undefined" ) ? "resultnav" : className;
	var _selectedClassName = ( selectedClassName == null || typeof( selectedClassName ) == "undefined" ) ? "resultnav-selected" : selectedClassName;
	var max = ( totalItems == null || typeof( totalItems ) == "undefined" ) ? 20 : totalItems;
	for ( var i = 1; i < max + 1; i++ ) {
		try {
			document.getElementById( lnkName + i ).className = _className;
		}
		catch ( e ) {
			//In case they skipped a section id...
		}
	}
	var lnk = document.getElementById(lnkCurrent)
	lnk.className = _selectedClassName;
	lnk.blur();
}
function ResultNav(divCurrent) {
	SetCurrentNav( divCurrent, "divResult" );
}
function SetCurrentNav(divCurrent, divName, totalItems) {
	var max = ( totalItems == null || typeof( totalItems ) == "undefined" ) ? 20 : totalItems;
	for ( var i = 1; i < max + 1; i++ ) {
		try {
			document.getElementById( divName + i).style.display = "none";
			//document.getElementById( divName + i).style.visibility = "hidden";
		}
		catch ( e ) {
			//alert( divName + i );
			//alert( e.description );
			//break;
		}
	}
	try {
		var div = document.getElementById(divCurrent)
		div.style.display = "";
		//div.style.visibility = "visible";
		if ( !isElementVisible( divCurrent ) ) div.scrollIntoView();
	}
	catch ( e ) {
		//alert( divName + i );
		//alert( e.description );
		//break;
	}
}
function ShowAllNav(divName) {
	for ( var i = 1; i < 10; i++ ) {
		try {
			document.getElementById( divName + i).style.display = "";
			//document.getElementById( divName + i).style.visibility = "visible";
		}
		catch ( e ) {}
	}
}
function DrilldownTable(sTable, bCollapse) {
	var collapse = ( bCollapse == null || typeof( bCollapse ) == "undefined" ) ? false : bCollapse;
	try {
		var table = document.getElementById( sTable );
		table.style.display = collapse ? "" : "none";

		var i = 1;
		var row = document.getElementById( sTable + "_row" + i );
		while ( row != null && typeof( row ) != "undefined" ) {
			row.style.display = collapse ? "none" : "";
			i++;
			row = document.getElementById( sTable + "_row" + i );
		}
	}
	catch ( e ) {
		//Exit out when error
	}	
}
function isElementVisible(id) { 
  var scroll = this.getScroll();
  var ele = document.getElementById( id );
  var ex = DL_GetElementLeft(ele);
  var ey = DL_GetElementTop(ele);
  return ex >= scroll.left && ex <= scroll.left + scroll.width && ey >= scroll.top && ey <= scroll.top + scroll.height; 
} 
function getScroll() {
	if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
		var ieBox = document.compatMode != "CSS1Compat";
		var cont = ieBox ? document.body : document.documentElement;
		return {
			left:	cont.scrollLeft,
			top:	cont.scrollTop,
			width:	cont.clientWidth,
			height:	cont.clientHeight
		};
	}
	else {
		return {
			left:	window.pageXOffset,
			top:	window.pageYOffset,
			width:	window.innerWidth,
			height:	window.innerHeight
		};
	}
}
//Following two fucntions are from www.webreference.com
function DL_GetElementLeft(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE
   var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy

      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent is not a table or the body, then...
            nLeftPos += eParElement.clientLeft; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nLeftPos += 1;             // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nLeftPos += nParBorder;       // append the border width to counter
            }
         }
      }
      nLeftPos += eParElement.offsetLeft;    // append left offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nLeftPos;                          // return the number calculated
}
function DL_GetElementTop(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nTopPos = eElement.offsetTop;         // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent a table cell, then...
            nTopPos += eParElement.clientTop; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nTopPos += 1;              // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nTopPos += nParBorder;        // append the border width to counter
            }
         }
      }

      nTopPos += eParElement.offsetTop;      // append top offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nTopPos;                           // return the number calculated
}
function EnableControlFromRadioButton( RadioButton, ControlName, ShowHide ) {
	EnabledClientControl( ControlName, document.getElementById( RadioButton ).checked, ShowHide );
}
function EnabledControlFromList( ListControl, Control, EnabledValue, ShowHide ) {
	var ctrl = document.getElementById( ListControl );
	if ( ctrl.tagName == "SELECT" ) {
		var enable = ( ctrl.value == EnabledValue );
		EnabledClientControl( Control, enable, ShowHide );
	}
	else {
		var col = ctrl.all;
		var disabled = true;
		if ( col != null ) {
			for ( var i = 0; i < col.length; i++ ) {
				if (col.item(i).tagName == "INPUT") {
					if ( col.item(i).checked ) {
						disabled = ( col.item(i).value != EnabledValue );
						break;
					}
				}
			}
			EnabledClientControl( Control, !disabled, ShowHide );
		}
	}
}
function BTRValidatorEnable(val, enable) {
    val.enabled = (enable != false);
    
    // Only want the validation to show when they submit.  It can 'hide' validation, but never show
    // new stuff just b/c it was 'triggered' on by something else.
    if ( !val.enabled ) {
		ValidatorValidate(val);
		ValidatorUpdateIsValid();
    }
}
function EnabledClientControl( Control, EnabledValue, ShowHide ) {
	var ctrl = document.getElementById( Control );
	if ( ctrl == null || typeof( ctrl ) == "undefined" ) return;
	var showHide = ( ShowHide == null || typeof( ShowHide ) == "undefined" ) ? true : ShowHide;

	if ( ctrl.tagName == "SPAN" && ctrl.getAttribute("controltovalidate") != null ) {
		//This is a validator...
		BTRValidatorEnable( ctrl, EnabledValue );

		//No children so exit...
		return;
	}
	else if ( ctrl.tagName == "INPUT" ) {
		ctrl.disabled = !EnabledValue;
		//No children so exit...
		return;
	}
	else {
		if ( showHide ) ctrl.style.display = ( EnabledValue ) ? "" : "none";
		//ctrl.style.visibility = ( EnabledValue ) ? "visible" : "hidden";

		var col = ctrl.all;
		if ( col != null ) {
			for ( var i = 0; i < col.length; i++ ) {
				if (col.item(i).tagName == "SPAN" && col.item(i).getAttribute("controltovalidate") != null ) {
					BTRValidatorEnable( col.item(i), EnabledValue );
			
					BTRValidationSummaryOnSubmit(); 
					//TODO: Would have liked to call this function but it locked up IE - wanted summary to 're-evaluate' and not show
					//error messages that should have been hidden (if validator was hidden). - Could maybe make my own function
					//that also checked to see if validator was enabled.
				}
			}
		}
	}
}
function BTRValidatorTrim(s) {
	 var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	 return (m == null) ? "" : m[1];
}

function GetDateFromControl( ControlName ) {
	 var control = document.getElementById( ControlName );
	 if ( control == null || typeof( control ) == "undefined" ) return null;
	 var myDate = BTRValidatorTrim( control.value );
	 var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
	 var m = myDate.match(yearLastExp);
	 if (m == null)
		  return null;
	 var day, month, year;
	 day = m[3];
	 month = m[1];
	 if (m[5].length != 4)
		return null; //for now FORCE four digit years - server side validates to 20xx for two digit years...
	 year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
	 month -= 1;
	 var date = new Date(year, month, day);
	 return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date : null;
}

function FormatCurrency(Number, Decimals, Symbol) {
  if ( isNaN(Number) ) return "n/a";

  var mySymbol = ( Symbol == null || typeof( Symbol ) == "undefined" ) ? "$" : Symbol;
  var s = RoundNumber(Number, Decimals) + "";
  
  var num = s * 1; 
  var negNum = ( num != Math.abs(num) );
  s = Math.abs(num) + "";
  
  var oRE = /(\d)(?=(\d\d\d)+(?!\d))/g; 
  var currency = mySymbol + (s.replace(oRE, "$1,"));
  return negNum ? "(" + currency + ")" : currency;
}

function RoundNumber(n, d)
{
	if ( isNaN(n) ) return "n/a";

	var decimalpts = d * 1; //number
	if ( decimalpts == 0 ) 
		return Math.round(n);
	else {
		var f = Math.pow(10, decimalpts);
		n = Math.round(n * f) / f;

		var numberstring = n + "";
		var decimalposition = numberstring.indexOf('.');
		var numberLength = numberstring.length;
		if ( decimalposition < 0 ) {
			numberstring += ".";
			for ( var i = 0; i < decimalpts; i++) {
				numberstring += "0";
			}
		}
		else {
			var decimalstring = numberstring.substring(decimalposition + 1);
			if ( decimalstring.length < decimalpts ) {
				for ( var i = 0; i < ( decimalpts - decimalstring.length ); i++) {
					numberstring += "0";
				}
			}
		}
		return numberstring;
	}
}
// TODO: Possible function to include:
// Prospective.Detail.js - RowVisible( CtrlName ), GetVal( CtrlName ) (gets numeric val), SetLabel( CtrlName, Value ), SetValue( CtrlName, Value ), pad(rounded_value, decimal_places) (last two functions would probably eliminate need for spinner js)
// Prospective.Output.js - SetCheckedState(CheckListCtrl, Checked)
// ****************************************************************************** //
// Remove below this line...just to show what was grabbed from WebUIValidation.js //
// in case they ever change implementation.										  //
// ****************************************************************************** //
/*
function GetFullYear(year) {
	return year + 1900;
}
function ValidatorTrim(s) {
	 var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	 return (m == null) ? "" : m[1];
}
*/