function ProcessMenuClientClick(Item)
{
/*
If the NoEvent category is set for an item then we tell our updated version of the
menu control to drop down the menu window (cancel the standard action of closing 
the drop down and executing some command)
*/
    if (Item.Category != "NoEvent")
	{	
		if (Item.Value != null)
		{
			eval ( Item.Value );
		}	
	}
	else
	{
		Item.CancelAction = true;
	}
}

// Provides a random query string parameter that can be used to prevent
// caching of modal pop-up windows.
function GetRandomQuerystringParameter() {
	var str = (new Date()).getTime().toString(); 
	var revstr='';
    for (var i = str.length-1; i>=0; i--) {
         revstr += str.charAt(i);         
    }
    var rand = "rx=" + revstr;
    return rand;
}

function restoreScroll(div_ID, hdnScrPos_ID)
{
/* 
	This function is used to ensure that the BillList control (which uses
	a DIV tag to put scroll bars on) is re-scrolled to the right position
	after postback.
   
	This will require a hidden form field called "hdnScrPos", and the div name.  
	The DIV must call storeScroll on it's scroll event.
	
	Note that the last line in this script calls restoreScroll.  I had 
	difficulties getting this to work using syntax to add to the 
	window.onload event handler, but it seems to work as is.
 */
  
 var divScroll = document.getElementById(div_ID);
 var hiddenField = document.getElementById(hdnScrPos_ID);
 
 if (divScroll != null)
 {
	if (divScroll.scrollTop != hiddenField.value) 
	{
	    //Setting a timeout of 1 ms forces the inner contents to fully render BEFORE
	    //we try to set the scroll position of the outer div.  Why this works?  We don't fully understand.
	    //For questions, talk to MK or PT.
	    setTimeout("restoreScrollWait('" + div_ID + "'," + hiddenField.value + ")",1);
	}
 } 
}

function restoreScrollWait(div_ID, val)
{
    var divScroll = document.getElementById(div_ID);
    divScroll.scrollTop = val;
}

function storeScroll(div_ID, hdnScrPos_ID)
{
 var divScroll = document.getElementById(div_ID);
 var hiddenField = document.getElementById(hdnScrPos_ID);
 
 hiddenField.value = divScroll.scrollTop;
 
} 

function scrollToBottom(div_ID, hdnScrPos_ID) {
 var divScroll = document.getElementById(div_ID);
 divScroll.scrollTop = divScroll.scrollHeight - divScroll.clientHeight;
 storeScroll(div_ID, hdnScrPos_ID);
}

// bring element el into view within div 
function makeVisible(div_ID, el_ID, hdnScrPos_ID)
{
	var div = document.getElementById(div_ID);
	var el = document.getElementById(el_ID);
	var hiddenField = document.getElementById(hdnScrPos_ID);
	
	if ( (div == null) || (el == null) || (hiddenField == null) ) return;

	var elRc = el.getBoundingClientRect();
	var divRc = div.getBoundingClientRect();
	
	if ( (elRc == null) || ( divRc == null) ) return;
	
	// Since we haven't restored the scroll, the element's relative position is not
	// where it "would" be
	var top = elRc.top - hiddenField.value;
	var bottom = elRc.bottom - hiddenField.value;
//alert('elRc: Top = ' + elRc.top + '\r\nbottom=' + elRc.bottom + '\r\nDivRc : top=' + divRc.top + '\r\nbottom=' + divRc.bottom);
	if (bottom < divRc.top) 
	{
		el.scrollIntoView(true);
		hiddenField.value = div.scrollTop;
	}
	else 
	{
		if (top > divRc.bottom) 
		{
			el.scrollIntoView(false);
			hiddenField.value = div.scrollTop;
		}
	}
}


// This is a generic "collapsible panel callback" that can be used on pages
// that have resizeable images ... all panels will be resized on panel state change
function changeImageURL(pnl, state) 
{
	var img;
	
	for (img in arrResizeableImages) {
		ResizeImage( img );		
	}
}

/* 
 * PopupWindow(URL, name, Scrollbars, width, height, top, left)	
 */
function PopupWindow(URL, name, scrollbars, width, height, top, left) {
	var options = 'toolbar=no,location=no,status=no,menubar=no,resizeable=no,';
	options += 'width=' + width + ',height=' + height;
	if (top>-1) {
		options += ',top=' + top;
	}
	if (left>-1) {
		options += ',left=' + left;
	}
	if (scrollbars) {
		options += ',scrollbars=yes';
	}
	else
	{
		options += ',scrollbars=no';
	}
	
	window.open(URL, name , options);

}

function setFormFocus() {
	if (document.forms.length > 0) 
	{
		var field = document.forms[0];

		for (i = 0; i < field.length; i++) 
		{
			if ( (field.elements[i].type == "text") || 
				(field.elements[i].type == "textarea") || 			     
				(field.elements[i].type.toString().charAt(0) == "s")) 
			{
				try {
					document.forms[0].elements[i].focus();
					break;
				}
				catch (ex)
				{
					// No worries, just don't set the focus.  It may be that this control is
					// hidden from the browser... keep going!
				}
			}
		}
	}
}

function setFormFocusOnField(fieldName) {
		try {
			document.forms[0].elements[fieldName].focus();					
		}
		catch (ex)
		{
			// No worries, just don't set the focus.  It may be that this control is
			// hidden from the browser... keep going!
		}
}


function GetDivSizeAndUpdateUrl(divClientID, iFrameClientID, loadingImageClientID, displayFormName, controlPath)
{
    var containerDiv = document.getElementById( divClientID );
	var iframe = document.getElementById(iFrameClientID);
	
	if (containerDiv == null)
	{
		alert('containerDiv is null');
		return;
	}
	if (iframe== null)
	{
		alert('iframe is null');
		return;
	}
	 
	
	var divDimensions = containerDiv.getBoundingClientRect();
	var divWidth = divDimensions.right - divDimensions.left;
	var divHeight = divDimensions.bottom - divDimensions.top;

    //Shrink by 8 pixels to compensate for padding & borders
	var iWidth = divWidth - 8;
	var iHeight = divHeight - 8;

    var iframeSrcPath = controlPath;
	iframeSrcPath += '&width=' + iWidth + '&height=' + iHeight + '&loadingImage=' + loadingImageClientID + '&referenceFormName=' + displayFormName;
	
	//iframe.style.zIndex = 0;
	iframe.src = iframeSrcPath;
}

// Test if the passing in text string is a valid number.  If the includeDecimal value is true, allow the decimal point as well
// -- NOTE
// This is NOT localization friendly.  If we want to support languages such as French, we will need to rework this function.
// --
function IsNumeric(sText, includeDecimal)
{
    
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    
    if (includeDecimal==true)
    {
        ValidChars += ".";
    }

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;

}

// Tests whether or not the given email address is valid
// Matches: e@eee.com or eee@e-e.com or eee@ee.eee.museum 
// Non-Matches: .@eee.com or eee@e-.com or eee@ee.eee.eeeeeeeeee
function IsValidEmailAddress(address) 
{
    var emailReg = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[0-9a-zA-Z]{2,9})$/
    return emailReg.test(address);
}

function SetWindowSize(adjust)
{
    // quirk - as long as the window is small enough, scrollheight will
    // return the actual size of the window content. However, if the window 
    // is larger than the content, scrollheight will return the window size. 
    // To get around this, we set the window's initial size to a small number
    // so that the scrollheight will always return the size of the content
    window.dialogHeight = '1px';
    
    // get the height of the content
    var height = document.body.scrollHeight;
    
    // adjust height for IE 6 and older.
    // The adjustment is because it seems that in IE6
    // the returned scrollHeight is not always perfect
    // and must therefore be 'tweaked' a bit to fit perfectly. 
    // This doesn't seem to be an issue in IE7
    if (navigator.appVersion.indexOf("MSIE")!=-1)
    {
        var temp = navigator.appVersion.split("MSIE")
        var version = parseFloat(temp[1])
         
        if (version < 7)
        {
            height = height + adjust;
        }
    }
    
    // resize to the size of the content. 
    window.dialogHeight = height + "px";
}

// adds additional trim functionality to a string
String.prototype.trim = 
    function() 
    {
        // Strip leading and trailing white-space
        return this.replace(/^\s*|\s*$/g, "");
    }


