/**
 * General purpose/use JavaScript library file
 */

function popupWindow(popupURL,height,width,windowName) {
	if (height == null) height=400;
	if (width == null) width=750;
	if (windowName == null) windowName='PopupWin';
	popupWin = window.open(popupURL, windowName,'height='+height+',width='+width+',scrollbars=yes,resizable=yes');
	popupWin.focus();
}

function popupWindowHW (popupURL,height,width) {
	popupWindow (popupURL,height,width);
}

function trim (theString) {
	theString = theString.replace(/\s+$/g, "");
	theString = theString.replace(/^\s+/g, "");
	return theString;
}

// String Custom Methods
String.prototype.padLeft = function(padChar, padMax)
{
  result = "";
  for (padding = 0; padding < (padMax - this.length); padding++) {
    result += padChar;
  }

  return result + this;
}

String.prototype.trim = function()
{
  result = this;

  result = result.replace(/\s+$/g, "");
  result = result.replace(/^\s+/g, "");

  return result;
}

// Date Custom Methods
Date.prototype.format = function(formatString)
{
  if (!formatString.length) {
    formatString = 'r';
  }

  dateString = '';
  for (formatIndex = 0; formatIndex < formatString.length; formatIndex++) {
    currChar = formatString.charAt(formatIndex);
    switch (currChar) {
      case 'd':
        // Day of the month, 2 digits with leading zeros
        // 01 to 31
        dateString += this.getDate().toString().padLeft('0',2);
        break;

      case 'D':
        // A textual representation of a day, three letters
        // Mon through Sun
        shortDayNames = this.getShortDayNames();
        dateString += shortDayNames[this.getDay()];
        break;

      case 'j':
        // Day of the month without leading zeros
        // 1 to 31
        dateString += this.getDate().toString();
        break;

      case 'l':
        // A full textual representation of the day of the week
        // Sunday through Saturday
        longDayNames = this.getLongDayNames();
        dateString += longDayNames[this.getDay()];
        break;

      case 'S':
        // English ordinal suffix for the day of the month, 2 characters
        // st, nd, rd or th. Works well with j
        // NIY
        break;

      case 'w':
        // Numeric representation of the day of the week
        // 0 (for Sunday) through 6 (for Saturday)
        dateString += this.getDay().toString();
        break;

      case 'z':
        // The day of the year (starting from 0)
        // 0 through 365
        // NIY
        break;

      case 'z':
        // The day of the year (starting from 0)
        // 0 through 365
        // NIY
        break;

      case 'W':
        // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
        // Example: 42 (the 42nd week in the year)
        // NIY
        break;

      case 'F':
        // A full textual representation of a month, such as January or March
        // January through December
        longMonthNames = this.getLongMonthNames();
        dateString += longMonthNames[this.getMonth() + 1];
        break;

      case 'm':
        // Numeric representation of a month, with leading zeros
        // 01 through 12
        // NIY
        dateString += (this.getMonth() + 1).toString().padLeft('0',2);
        break;

      case 'M':
        // A short textual representation of a month, three letters
        // Jan through Dec
        shortMonthNames = this.getShortMonthNames();
        dateString += shortMonthNames[this.getMonth() + 1];
        break;

      case 'n':
        // Numeric representation of a month, without leading zeros
        // 1 through 12
        dateString += (this.getMonth() + 1).toString();
        break;

      case 't':
        // Number of days in the given month
        // 28 through 31
        // NIY
        break;

      case 'L':
        // Whether it's a leap year
        // 1 if it is a leap year, 0 otherwise.
        // NIY
        break;

      case 'Y':
        // A full numeric representation of a year, 4 digits
        // Examples: 1999 or 2003
        // Firefox returns 105 for this.getYear and IE returns 2005
        dateString += (this.getYear() < 1000 ? 1900+this.getYear() : this.getYear()).toString();
        break;

      case 'y':
        // A two digit representation of a year
        // Examples: 99 or 03
        // Firefox returns 105 for this.getYear and IE returns 2005
        dateString += (this.getYear() < 1000 ? 1900+this.getYear() : this.getYear()).toString().substr(2,2);
        break;

      case 'a':
        // Lowercase Ante meridiem and Post meridiem
        // am or pm
        dateString += (this.getHours() < 12) ? 'am' : 'pm';
        break;

      case 'A':
        // Uppercase Ante meridiem and Post meridiem
        // AM or PM
        dateString += (this.getHours() < 12) ? 'AM' : 'PM';
        break;

      case 'B':
        // Swatch Internet time
        // 000 through 999
        // NIY
        break;

      case 'g':
        // 12-hour format of an hour without leading zeros
        // 1 through 12
        if (this.getHours() == 0) {
          dateString += "12";
        }
        else {
          dateString += (this.getHours() > 12) ? (this.getHours() - 12).toString() : this.getHours().toString();
        }
        break;

      case 'G':
        // 24-hour format of an hour without leading zeros
        // 0 through 23
        dateString += this.getHours().toString();
        break;

      case 'H':
        // 24-hour format of an hour with leading zeros
        // 00 through 23
        dateString += this.getHours().toString().padLeft('0',2);
        break;

      case 'h':
        // 12-hour format of an hour with leading zeros
        // 00 through 23
        if (this.getHours() == 0) {
          dateString += "12";
        }
        else {
          dateString += (this.getHours() > 12) ? (this.getHours() - 12).toString().padLeft('0',2) : this.getHours().toString().padLeft('0',2);
        }
        break;

      case 'i':
        // Minutes with leading zeros
        // 00 to 59
        dateString += this.getMinutes().toString().padLeft('0',2);
        break;

      case 's':
        // Seconds, with leading zeros
        // 00 through 59
        dateString += this.getSeconds().toString().padLeft('0',2);
        break;

      case 'r':
        // RFC 2822 formatted date
        // Example: Thu, 21 Dec 2000 16:01:07 +0200
        // NIY
        break;

      default:
        dateString += currChar;
    }
  }

  return dateString
}

Date.prototype.getShortMonthNames = function()
{
  return new Array('',
                   'Jan',
                   'Feb',
                   'Mar',
                   'Apr',
                   'May',
                   'Jun',
                   'Jul',
                   'Aug',
                   'Sep',
                   'Oct',
                   'Nov',
                   'Dec'
                   );
}

Date.prototype.getLongMonthNames = function()
{
  return new Array('',
                   'January',
                   'February',
                   'March',
                   'April',
                   'May',
                   'June',
                   'July',
                   'August',
                   'September',
                   'October',
                   'November',
                   'December'
                   );
}

Date.prototype.getShortDayNames = function()
{
  return new Array('Sun',
                   'Mon',
                   'Tue',
                   'Wed',
                   'Thu',
                   'Fri',
                   'Sat'
                   );
}

Date.prototype.getLongDayNames = function()
{
  return new Array('Sunday',
                   'Monday',
                   'Tuesday',
                   'Wednesday',
                   'Thursday',
                   'Friday',
                   'Saturday'
                   );
}
