
// ------------------------------------------------------------ //
// Toggles the visibility of all image thumbnails on the screen //
// ------------------------------------------------------------ //
function ToggleImages()
{
	// Get the list of image elements
	var ctrlList = document.getElementsByName( "imgThumbnail" );
	if ( ctrlList.length == 0 ) return;
	
	// Determine if elements are currently visible
	var visible = ctrlList[0].style.display == "";
	
	// Loop through each element in the list and set visibility
	for ( var i=0; i<ctrlList.length; i++ )
	{
		if ( !visible )
		{
			ctrlList[i].style.display = "";
		}
		else
		{
			ctrlList[i].style.display = "none";
		}
	}
	
	// Get the list of span elements (hyperlink placeholder)
	ctrlList = document.getElementsByName( "spnThumbnail" );
	if ( ctrlList.length == 0 ) return;

	// Loop through each element in the list and set visibility
	for ( var i=0; i<ctrlList.length; i++ )
	{
		if ( visible )
		{
			ctrlList[i].style.display = "";
		}
		else
		{
			ctrlList[i].style.display = "none";
		}
	}
}

// ----------------------------------------------------------------------------- //
// Toggles the visibility of image thumbnails contained by the specified control //
// ----------------------------------------------------------------------------- //
function ToggleImages(parentCtrl)
{
	// Get the list of child elements
	//var parentCtrl = document.getElementById( parentCtrlID );
	if ( parentCtrl == null || parentCtrl.children == null || parentCtrl.children.length == 0 ) { alert("no elements" ); return; }

	var visible = false;
	
	// Determine if thumbnails are currently visible
	for ( var i=0; i<parentCtrl.all.length; i++ )
	{
		var ctrl = parentCtrl.all[i];
		if ( ctrl.name == "imgThumbnail" )
		{
			visible = ctrl.style.display == "";
		}
	}

	// Set visibility of elements
	for ( var i=0; i<parentCtrl.all.length; i++ )
	{
		var ctrl = parentCtrl.all[i];
		if ( ctrl.name == "imgThumbnail" )
		{
			ctrl.style.display = visible ? "none" : "";
		}
		else if ( ctrl.name == "spnThumbnail" )
		{
			ctrl.style.display = visible ? "" : "none";
		}
	}
}

// ----------------------------------------- //
// Toggles the visibility of the search pane //
// ----------------------------------------- //
function ToggleSearch()
{
	// Get references to page elements
	var img = document.getElementById( "imgSearch" );
	var tbl = document.getElementById( "tblSearch" );
	
	// Set visibility of elements
	if ( tbl.style.display == "" )
	{
		tbl.style.display = "none";
		img.src = "/global/images/but_arrowdown_up.gif";
		img.alt = "Show search pane";
	}
	else
	{
		tbl.style.display = "";
		img.src = "/global/images/but_arrowup_up.gif";
		img.alt = "Hide search pane";
	}
}

// ---------------------------------------------- //
// Toggles the visibility of the calendar control //
// ---------------------------------------------- //
function ToggleCalendar(ctrlName)
{
	// Get the control references
	var frm = document.getElementById( ctrlName + "FRAME" );
	var ctrl = document.getElementById( ctrlName );
	var btnUp = document.getElementById( ctrlName + "BTNUP" );
	var btnDown = document.getElementById( ctrlName + "BTNDN" );
	
	// Abort if we can't find the controls
	if ( !frm || !ctrl ) return;
	
	// Set visibility and position of the frame
	if ( frm.style.display == "" )
	{
		frm.style.display = "none";
		btnDown.style.display = "none";
		btnUp.style.display = "";
	}
	else
	{
		frm.style.display = "";
		btnDown.style.display = "";
		btnUp.style.display = "none";
		
		// Calculate coordinates for the frame
		var newX = FindPosX( ctrl) + ctrl.style.posWidth + 23;
		var newY = FindPosY( ctrl );
		
		// Determine if frame will overlap right of screen
		var overlapX = newX + 180 - document.body.offsetWidth;
		if ( overlapX > 0 ) newX = newX - 185;
		
		// Determine if frame will overlap bottom of screen
		var overlapY = newY + 180 - document.body.offsetHeight;
		if ( overlapY > 0 ) newY = newY - 160;
			
		frm.style.posLeft = newX;
		frm.style.posTop = newY;
		
		frm.focus();
	}
}

// ------------------------------------------------------------ //
// Hides the calendar control for the specified textbox control //
// ------------------------------------------------------------ //
function HideCalendar(ctrlName)
{
	var frm = document.getElementById( ctrlName + "FRAME" );
	var btnUp = document.getElementById( ctrlName + "BTNUP" );
	var btnDown = document.getElementById( ctrlName + "BTNDN" );
	frm.style.display = "none";
	btnDown.style.display = "none";
	btnUp.style.display = "";
}

// --------------------------------------------------- //
// Displays the specified page in a modal popup window //
// --------------------------------------------------- //
function DisplayPopup(url, height)
{
	var curDate = new Date();
	var features = "dialogHeight:" + height + "px; dialogWidth:900px; center:yes; resizable:no; status:no; scroll:yes";
	return window.showModalDialog( url + "&TimeStamp=" + curDate.toString(), null, features );
}

// ------------------------------ //
// Displays the record info popup //
// ------------------------------ //
function DisplayRecordInfo(popupURL, contactURL)
{
	var curDate = new Date();
	var features = "dialogHeight:250px; dialogWidth:400px; center:yes; resizable:no; status:no; scroll:yes";
	var userID = window.showModalDialog( popupURL + "&TimeStamp=" + curDate.toString(), null, features );
	if( userID > 0 ) window.location = contactURL + "&UserID=" + userID;
}

// ----------------------- //
// Displays the help popup //
// ----------------------- //
function DisplayHelp(popupURL)
{
	var curDate = new Date();
	var features = "height=400px, width=600px, center=yes, resizable=no, status=no, scrollbars=yes";
	var newWin = window.open( popupURL + "&TimeStamp=" + curDate.toString(), "HelpWindow", features );
	newWin.focus();
}

// ----------------------------------------------------- //
// Returns the X position for the specified page element //
// ----------------------------------------------------- //
function FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

// ----------------------------------------------------- //
// Returns the Y position for the specified page element //
// ----------------------------------------------------- //
function FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// ------------------------- //
// Querystring functionality //
// ------------------------- //
function PageQuery(q) 
{
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else 
		this.q = null;
		
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() 
	{ 
		return this.keyValuePairs; 
	}
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() 
	{ 
		return this.keyValuePairs.length; 
	} 
}

// -------------------------------------------------- //
// Returns the value of the specified querystring key //
// -------------------------------------------------- //
function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

// ------------------------------------------------- //
// Returns whether the specified value is an integer //
// ------------------------------------------------- //
function IsInt(value)
{
	return parseInt( value ) == ( value - 0 ); 
}

// -------------------------------------------------------------- //
// Returns whether the specified value is a floating-point number //
// -------------------------------------------------------------- //
function IsFloat(value)
{
	return parseFloat( value ) == ( value - 0 ); 
}