//Global Event Handlers
document.onmouseover = ClearStatus;
document.onmouseout = ClearStatus;
window.onload = StartBlink;

//Clears link statuses from displaying in the page footer
function ClearStatus()
{
	window.status='';
	return true;
}

//Locate <BLINK> and </BLINK> tags to within the document
//and modify their visibility based on their current visibility
function Blink()
{
	var blinkTags = document.all.tags("BLINK");
	
	for(var x = 0; x < blinkTags.length; i++)
	{
		blinkTags[i].style.visibility = blinkTags[i].style.visibility == "" ? "hidden" : ""; 
	}
}

//Used to set up and initialize an interval timer for Blink() function
function StartBlink() 
{
	if(document.all)
	{
		setInterval("Blink()",1000);
	}
}

//Changes the display type of a table located in the
//default.aspx page that alerts the user when a pop-up
//window has been blocked client-side.
function JSFX_FloatTopLeft()
{
	var startX = 10, startY = 380;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	var px = document.layers ? "" : "px";
	
	function ml(id)
	{
		var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
		if(el == null){return;}
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
		el.x = startX; el.y = startY;
		return el;
	}
	
	window.stayTopLeft=function()
	{
		var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		ftlObj.y = (pY > startY) ? pY : startY;
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 20);
	}

	window.stayTop=function()
	{
		var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		ftlObj2.y = (pY > 0) ? pY : 0;
		ftlObj2.sP(ftlObj2.x, ftlObj2.y);
	}
   	
	ftlObj = ml("divStayTopLeft");
	ftlObj2 = ml("divWait");
}

function ChangePopUpMessageDisplay(displayType)
{
	document.getElementById('tblPopUpMessage').style.display = displayType;
	if(displayType == 'block')
	{
		document.getElementById('tblPopUpMessage').focus();
	}
}

//Handles CallBackObject class errors
function Cbo_Error(status, statusText, responseText)
{
	alert(responseText);
}

//Gets a reference to an object and then creates
//the stayTop function which will get the current
//top Y position of the page and set this as the 
//object's Y position by calling setPosition
//on the object.
//timeOut: Sets an optional Timeout on the object.
//Pass in 0 for timeOut if it is not desired.
function InitFloatObject(id, x, y, timeOut)
{
	floatObj = InitElement(id, x, y);
	
    window.stayTop=function(x)
	{
		var topY = (navigator.appName.indexOf("Netscape") != -1) ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		floatObj.y = (topY > y) ? topY : y;
		floatObj.setPosition(floatObj.x, floatObj.y);
		setTimeout("stayTop()", timeOut);
	}
   	
   	return floatObj;
}

//Gets a reference to a control and sets up a function
//on that control called setPosition (which can be called
//at any time to set the left and top positioning.
function InitElement(controlId, startX, startY)
{
	var px = document.layers ? "" : "px";
	var element = document.getElementById ? document.getElementById(controlId) : document.all ? document.all[controlId] : document.layers[controlId];
	
	if(element == null)
	{
		return;
	}
	
	if(document.layers)
	{
		element.style = element;
	}
	
	element.setPosition=function(x,y)
	{
		this.style.left = x + px;
		this.style.top = y + px;
	}
	
	//Start up at desired x, y positions
	element.x = startX; 
	element.y = startY;
	
	return element;
}

//Used to add client side functionality to PeterBlum
//controls when they attempt to set focus on a control
//														  
//You must set the following parameter in the code behind!
//PeterBlum.VAM.Globals.Page.SetFocusFunctionName =  _    
//"YOUR CUSTOM CLIENT SIDE FUNCTION NAME"				  
//														  
//Finds the parentNode of the field passed in and if that 
//parentNode is a table it will scroll the parentNode into
//view instead of the field where the error occured	  
function ScrollToHeader(oField)
{
	var bParentFound = false;
		
	for(x = 1; bParentFound == false; x++)
	{
		var oParent = oField;
		
		for(y = 0; y < x; y++)
		{
			oParent = oParent.parentNode;
		}
		
		if(oParent.nodeName == "TABLE")
		{
			if(oParent.id.indexOf('tbl') != -1)
			{
				bParentFound = true;
				
				var oTableHeader = VAM_GetById('' + oParent.id);
				oTableHeader.scrollIntoView(true);
			}	
		}
	}
	
	return true;
}

//Opens a new browser window
function OpenWindow(url, name, width, height)
{
	if (document.all)
	{
		var xMax = screen.width, yMax = screen.height;
	}
	else
	{
		if (document.layers)
		{
			var xMax = window.outerWidth, yMax = window.outerHeight;
		}
		else
		{
			var xMax = 640, yMax=480;
		}
	}
				
	if (width || height)
	{						
		var xOffset = (xMax - width)/2;
		var yOffset = (yMax - height)/2;
	}
	else
	{
		var xOffset = 0;
		var yOffset = 0;
		width = xMax - 10;
		height = yMax - 65;
	}
	
	var popUp = window.open(url, name,'width='+width+', height='+height+',dependent=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
	
	return popUp;
}		

//Sets checked property to false on all radio buttons
//contained in a radio button group
function ClearRadioButtonGroup(controlGroup)
{	
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].checked = false;
	}
}

//Loops on all radio buttons contained in a radio button
//group and returns true if any of the radio buttons
//are checked
function RadioButtonGroupIsChecked(controlGroup)
{
	var valid = false;
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		if (controlArray[i].checked)
		{
			valid = true;
			break;
		}
	}
	return valid;
}

//Disables all radio buttons in a radio button group 
function DisableRadioButtonGroup(controlGroup)
{
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].disabled = true;
	}
}

//Enables all radio buttons in a radio button group 
function EnableRadioButtonGroup(controlGroup)
{
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].disabled = false;
	}
}

//Sets the display property on a control
function ChangeDisplay(controlId, displayType)
{
	document.getElementById(controlId).style.display = displayType;
}

//Loops on all checkboxes contained in a checkbox
//group and returns true if any of the checkboxes
//are checked
function CheckBoxGroupIsChecked(controlName, startValue, endValue, incrementValue)
{
	var valid = false;
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		if (document.getElementById(controlName + i).checked)
		{
			valid = true;
			break;
		}
	}
	return valid;
}

//Loops on all checkboxes contained in a checkbox
//group and returns the number of checkboxes that
//are checked
function GetNumCheckBoxesChecked(controlId, startValue, endValue, incrementValue)
{
	var numChecked = 0;
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		if (document.getElementById(controlId + i).checked)
		{
			numChecked = numChecked + 1;
		}
	}
	return numChecked;
}

//Sets checked property to true on a control
function CheckControl(controlId)
{
	document.getElementById(controlId).checked = true;
}

//Sets checked property to false on a control
function UnCheckControl(controlId)
{
	document.getElementById(controlId).checked = false;
}	

//Sets disabled property to false on a control
function EnableControl(controlId)
{
	document.getElementById(controlId).disabled = false;
}	

//Sets disabled property to true on a control
function DisableControl(controlId)
{
	document.getElementById(controlId).disabled = true;
}				

//Sets the checked property to false on a group of controls
function UncheckControls(controlId, startValue, endValue, incrementValue)
{
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlId + i).checked = false;
	}
}

//Sets the disabled property to false on a group of controls
function EnableControls(controlId, startValue, endValue, incrementValue)
{
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlId + i).disabled = false;
	}
}

//Sets the disabled property to true on a group of controls	
function DisableControls(controlId, startValue, endValue, incrementValue)
{	
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlId + i).disabled = true;
	}
}

//Sets the value property of a control
function ChangeControlValue(controlId, value)
{
	document.getElementById(controlId).value = value;
}

//Sets the innerText property of a control
function ChangeControlInnerText(controlId, innerText)
{
	document.getElementById(controlId).innerText = innerText;
}

//Sets the innerHtml property of a control
function ChangeControlInnerHtml(controlId, innerHtml)
{
	document.getElementById(controlId).innerHtml = innerHtml;
}

//Removes all in-valid characters from a string
String.prototype.StripNonNumeric = function() 
{ 
    var sValidChars = '0123456789'; 
    var sFormattedString = ''; 
 
    for(x = 0; x < this.length; x++) 
    { 
		if(sValidChars.indexOf(this.charAt(x)) > -1) 
        { 
			sFormattedString += this.charAt(x); 
        } 
    } 
              
    return sFormattedString; 
}

//Removes all in-valid characters from a string
String.prototype.FormatFloat = function() 
{ 
    var sValidChars = '0123456789.'; 
    var sFormattedFloat = ''; 
 
    for(x = 0;x < this.length; x++) 
    { 
		if(sValidChars.indexOf(this.charAt(x)) > -1) 
        { 
			sFormattedFloat += this.charAt(x); 
        } 
    } 
              
    return sFormattedFloat; 
}

//Format a float datatype to currency
function FormatCurrency(fNumber) 
{
	var sNumber = fNumber.toString().replace(/\$|\,/g,'');
	
	if(isNaN(sNumber))
	{
		sNumber = "0";
	}
	
	var bSign = (sNumber == (sNumber = Math.abs(sNumber)));
	fNumber = Math.floor(sNumber * 100 + 0.50000000001);
	var sCents = (fNumber % 100).toString();
	sNumber = Math.floor(fNumber/100).toString();
	
	if((sCents * 1) < 10)
	{
		sCents = "0" + sCents;
	}
	
	for (var i = 0; i < Math.floor((sNumber.length-(1+i))/3); i++)
	{
		sNumber = sNumber.substring(0,sNumber.length-(4*i+3))+','+ sNumber.substring(sNumber.length-(4*i+3));
	}
	
	return (((bSign)?'':'-') + '$' + sNumber + '.' + sCents);
}

function FilterNumeric(evt, dec)             
{
     evt = (evt) ? evt : ((window.event) ? event : null); 
     if (evt)                                           
     {                                                    
          var keyCode = (evt.charCode) ? evt.charCode :    
          ((evt.keyCode) ? evt.keyCode :                   
          ((evt.which) ? evt.which : 0));                  
           
          if((keyCode == 8) || (keyCode == 9) || (keyCode == 46 && dec) || (keyCode >= 48 &&  keyCode <= 57) ){return};
          if (window.event){evt.returnValue = false;}else{evt.preventDefault()}; 
     } 
}


//Round a number to iDecimalPlaces (defaults to 2)
function RoundNumber(fNumber,iDecimalPlaces) 
{
	iDecimalPlaces = (!iDecimalPlaces ? 2 : iDecimalPlaces);

	var sValue, sTemp;

	var sNumber = Math.round(fNumber * Math.pow(10,iDecimalPlaces)) / Math.pow(10,iDecimalPlaces);
	sNumber = sNumber.toString();
	sTemp = sNumber.substr(sNumber.indexOf(".") + 1);

	if(sTemp.length < iDecimalPlaces) 
	{
  		while(sTemp.length < iDecimalPlaces) 
		{
    		sTemp += "0";
    		sNumber += "0";
		}
	}

	return sNumber;
}

//Adds commas to a string to display as currency value
function AddCommas(fNumber)
{
	fNumber = fNumber.toString();
	
	var arrNumbers = fNumber.split('.');
	var sDollars = arrNumbers[0];
	var sCents = arrNumbers.length > 1 ? '.' + arrNumbers[1] : '';
	var rgx = /(\d+)(\d{3})/;
	
	while (rgx.test(sDollars)) 
	{
		sDollars = sDollars.replace(rgx, '$1' + ',' + '$2');
	}
	
	return sDollars + sCents;
}

function GetScrollX() {
    if (window.pageXOffset) {
        return window.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft) {
        return document.documentElement.scrollLeft;
    }
    else if (document.body) {
        return document.body.scrollLeft;
    }
    return 0;
}

function GetScrollY() {
    if (window.pageYOffset) {
        return window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
    }
    else if (document.body) {
        return document.body.scrollTop;
    }
    return 0;
}

function ValSummaryScrollToView(button)
{
	var ev = button.onclick.toString();
	ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
	var functionPrefix = "var offsetX = GetScrollX(); var offsetY = GetScrollY(); "
	var functionPostfix = "; window.scrollTo(offsetX, offsetY);"
	var newFunction = functionPrefix + ev + functionPostfix;
	var func = new Function(newFunction);
	eval("button.onclick = func;");


}


