/*
** 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';
  }
  return window.open( szURL, szName, szFeatures );
}



/*
** 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.getDay();
   var day2 = (day < 10) ? "0" + day.toString() : day.toString();
   var dayOfWeek = daysOfWeek[day];
   
   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" + d.getDate().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" + d.getDate() + "$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;
}