/*
** Abilitation Common JavaScript Library (CommonLib.js)
**
** Written by: Neil Martin
** Date: 8th Decemmber 2004
*/


function getDialogWidth( w ) {
  return (w < 0) ? (screen.availWidth / Math.abs(w)) : (screen.availWidth < (w / 0.9)) ? (screen.availWidth * 0.9) : w;
}

function getDialogHeight( h ) {
  return (h < 0) ? (screen.availHeight / Math.abs(h)) : (screen.availHeight < (h / 0.9)) ? (screen.availHeight * 0.9) : h;
}

function getDialogLeft( x, dlgWidth ) {
  return (x >= 0) ? x : (screen.availWidth - dlgWidth) / Math.abs(x);
}

function getDialogTop( y, dlgHeight ) {
  return (y >= 0) ? y : (screen.availHeight - dlgHeight) / Math.abs(y);
}


function positionWindow( x, y, iWidth, iHeight ) {
  var width = getDialogWidth( iWidth );
  var height = getDialogHeight( iHeight );
  var left = getDialogLeft( x, width );
  var top = getDialogTop( y, height );

  self.moveTo( left, top );
  self.resizeTo( width, height );
}


function loadWindow( szURL, szName, x, y, iWidth, iHeight, bScrollBars, bResizable ) {
  var width = getDialogWidth( iWidth );
  var height = getDialogHeight( iHeight );
  var left = getDialogLeft( x, width );
  var top = getDialogTop( y, height );

  var szFeatures = 'menubar=no, status=no, toolbar=no,';
  szFeatures = szFeatures + 'left=' + left.toString() + ', ';
  szFeatures = szFeatures + 'top=' + top.toString() + ', ';
  szFeatures = szFeatures + 'width=' + width.toString() + ', ';
  szFeatures = szFeatures + 'height=' + height.toString() + ', ';
  if (bScrollBars) {
    szFeatures = szFeatures + 'scrollbars=yes, ';
  } else {
    szFeatures = szFeatures + 'scrollbars=no, ';
  }
  if (bResizable) {
    szFeatures = szFeatures + 'resizable=yes';
  } else {
    szFeatures = szFeatures + 'resizable=no';
  }
  
  var win = window.open( szURL, szName, szFeatures );
  win.focus();
  return win;
}


/*
** Set/reset the focus of the window
*/
function setWindowFocus() {
	window.focus();
}



/*
** Provides a simple mechanism for switching the text of an input
** text box to a prompt string - useful when labelling space is restricted
*/
function switchInputBoxText(object, baseText, eventName)
{
	var s = object.value.replace(/^\s+|\s+$/, '');	// Trim() leading/trailing space
	if (eventName == "onblur")
	{
		if (s == "")
		{
			object.value = baseText;
		}
	}
	else
	{
		if (s == baseText)
		{
			object.value = "";
		}
	}
}



/*
** Provide support for client-sixe text box insertion
*/
function storeCaret( oText )
{
   if (oText.createTextRange)
   {
      oText.caretPos = document.selection.createRange().duplicate();
   }
}

function insertAtCaret (oText, str)
{
   if ((oText.createTextRange) && (oText.caretPos))
   {
      var caretPos = oText.caretPos;
      caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? str + ' ' : str;
   }
   else
   {
      oText.value  = oText.value + str;
   }
}



/*
** Provide friendy date strings
*/

// Returns a date string in the specifed format
//
// Monday, 5th June 2005      dddd, dth mmmm yyyy
// Mon 02 Aug 04              ddd dd mmm yy
// 1/2/04                     d/m/yy
// 01/04/2004                 dd/mm/yy
//
function getFullDate( formatString )
{
   var d = new Date();
   var daysOfWeek = new Array( "Sunday", "Monday", "Tuesday", "Wednesday",
                               "Thursday", "Friday", "Saturday");
   var monthsOfYear = new Array( "January", "February", "March",
                                 "April", "May", "June",
                                 "July", "August", "September",
                                 "October", "November", "December" );
   var day = d.getDate();
   var day2 = (day < 10) ? "0" + day.toString() : day.toString();
   var dayOfWeek = daysOfWeek[d.getDay()];
   
   var th = ((day == 1) || (day == 21) || (day == 31)) ? "st" :
      ((day == 2) || (day == 22)) ? "nd" :
      ((day == 3) || (day == 23)) ? "rd" : "th";
   
   var month = d.getMonth();
   var month2 = (month < 10) ? "0" + month.toString() : month.toString();
   var monthOfYear = monthsOfYear[month];
   
   var year = d.getFullYear();
   var year2 = year.toString().substr(2,2);
   
   var result = formatString;
   result = result.replace( /(\W|^)dth(\W|$)/i, "$1" + day.toString() + th + "$2");
   result = result.replace( /(\W|^)dddd(\W|$)/i, "$1" + dayOfWeek + "$2");
   result = result.replace( /(\W|^)ddd(\W|$)/i, "$1" + dayOfWeek.substr(0,3) + "$2" );
   result = result.replace( /(\W|^)dd(\W|$)/i, "$1" + day2 + "$2" );
   result = result.replace( /(\W|^)d(\W|$)/i, "$1" + day + "$2" );
   
   result = result.replace( /(\W|^)mmmm(\W|$)/i, "$1" + monthOfYear + "$2" );
   result = result.replace( /(\W|^)mmm(\W|$)/i, "$1" + monthOfYear.substr(0,3) + "$2" );
   result = result.replace( /(\W|^)mm(\W|$)/i, "$1" + month2 + "$2" );
   result = result.replace( /(\W|^)m(\W|$)/i, "$1" + month + "$2" );

   result = result.replace( /(\W|^)yyyy(\W|$)/i, "$1" + year + "$2" );
   result = result.replace( /(\W|^)yy(\W|$)/i, "$1" + year2 + "$2" );

   return result;               
}


// Return a client geeting of the form Good Morning/Aftern0on/Evening
function getClientGreeting()
{
   var s = "Good Evening";
   var now = new Date();
   var t = now.getHours();
   if (t < 12) s = "Good Morning";
   else if (t < 18) s = "Good Afternoon";
   return s;
}


/*
** Abilitation.Web.UI.WebControls.ExpandPanel.cs
*/
function expandPanel(ctrlId) {
	var ctrl = document.getElementById( ctrlId );
	if (ctrl) {
		if ((ctrl.expandState) && (ctrl.content)) {
			// Default to the expanded state...
			if (ctrl.expandState.value == '1') {
				ctrl.expandState.value = '0';
				ctrl.content.style.display = 'none';
				if (ctrl.icon) {
					ctrl.icon.setAttribute('src', ctrl.expandIconUrl);
					ctrl.icon.setAttribute('alt', 'Expand');
					ctrl.icon.setAttribute('title', 'Expand');
					ctrl.icon.onmouseover = function() { ctrl.icon.src = ctrl.expandHoverIconUrl; }
					ctrl.icon.onmouseout = function() { ctrl.icon.src = ctrl.expandIconUrl; }
				}
				if (ctrl.suffix) {
					ctrl.suffix.innerHTML = ctrl.suffixCollapsed;
				}
				if (ctrl.footer)
				{
					ctrl.footer.style.display = 'none';
				}
			} else {
				ctrl.expandState.value = '1';
				ctrl.content.style.display = '';	// will resolve to default setting in all browsers
				if (ctrl.icon) {
					ctrl.icon.setAttribute('src', ctrl.collapseIconUrl);
					ctrl.icon.setAttribute('alt', 'Collapse');
					ctrl.icon.setAttribute('title', 'Collapse');
					ctrl.icon.onmouseover = function() { ctrl.icon.src = ctrl.collapseHoverIconUrl; }
					ctrl.icon.onmouseout = function() { ctrl.icon.src = ctrl.collapseIconUrl; }
				}
				if (ctrl.suffix) {
					ctrl.suffix.innerHTML = ctrl.suffixExpanded;
				}
				if (ctrl.footer)
				{
					ctrl.footer.style.display = '';
				}
			}
		}
	}
}


/*
** Wire the following function the the pages body element to prevent
** the page from being navigated by use of the browser's back button.
**
**		document.body.onload = preventBackButton;
*/

function preventBackButton() {
	if (history.length > 0) history.go(+1);
}
