/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/**
 * Retrieve the coordinates of the given event relative to the center
 * of the widget.
 *
 * @param event
 *   A mouse-related DOM event.
 * @param reference
 *   A DOM element whose position we want to transform the mouse coordinates to.
 * @return
 *    A hash containing keys 'x' and 'y'.
 */
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;

  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };

    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x;
    y = event.pageY - pos.y;
  }

  return { x: x, y: y };
}
//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

function padout(number) { return (number < 10) ? '0' + number : number; }

var setDateField;
var names     = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var days      = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var dow       = ['Su','Mo','Tu','We','Th','Fr','Sa'];
var today     = new Date();
var gDay      = today.getDate();
var gMonth    = today.getMonth();
var gYear     = today.getFullYear();
var dateDiv;
var browser   = new Browser();

function getDateWindow(setFieldParam, e) {	
    setDateField = setFieldParam;
    if(setFieldParam.value=='') {
    	curDate=today;
    } else {
    	curDate = setFieldParam.value;
    	curDate = curDate.split('-');
    	curDate = new Date(curDate[0],curDate[1]-1,curDate[2]);
    }
    gDay = curDate.getDate();
    gMonth = curDate.getMonth();
    gYear = curDate.getFullYear();
    
    contentDiv=document.getElementById('Content');
    pos = getRelativeCoordinates(e,contentDiv);

	if (!browser.isIE) {
	  pos.x += contentDiv.scrollLeft;
	  pos.y += contentDiv.scrollTop;
	} else if (browser.version < 7) {
	  pos.x += 200;
	  pos.x -= contentDiv.scrollLeft;
	  pos.y -= contentDiv.scrollTop;	
	}

    dateDiv=document.getElementById('dateDiv');
    dateDiv.style.left=(pos.x+10)+'px';
    dateDiv.style.top=(pos.y)+'px';
    dateDiv.style.width='160px';
    dateDiv.innerHTML=Calendar(gMonth,gYear);
    dateDiv.style.visibility='visible';
}

function Calendar() {
    var output = '';

    output += '<table style="padding:0; margin:0; width:100%; background-color:#c0c0c0; border-style:ridge; border-width:2px; border-color:black;" >';
    output += '<tr><td style="padding:0; margin:0;">';
    output += '<select name="Month" style="font-family:Arial; font-size:11px;" onChange="gMonth=this.value; dateDiv.innerHTML=Calendar();">';

    for (month=0; month<12; month++) {
        if (month == gMonth) output += '<option value="' + month + '" selected>' + names[month] + '<\/option>';
        else                output += '<option value="' + month + '">'          + names[month] + '<\/option>';
    }

    output += '<\/select><\/td><td style="padding:0; margin:0;"><select name="Year" style="font-family:Arial; font-size:11px;" onChange="gYear=this.value; dateDiv.innerHTML=Calendar();">';

    for (year=1900; year<2101; year++) {
        if (year == gYear) output += '<option value="' + year + '" selected>' + year + '<\/option>';
        else              output += '<option value="' + year + '">'          + year + '<\/option>';
    }

    output += '<\/select><\/td><td style="padding:0; margin:0; width:10px; text-align:center; cursor:pointer;" onclick="dateDiv.style.visibility=\'hidden\';"><span style="padding:0px 1px 0px 2px; margin:0; border-style:solid; border-width:1px; border-color:#666; font-familiy:Verdana; font-size:11px; font-weight:bold;">x</span><\/td><\/tr><tr><td align=center colspan=3>';

    firstDay = new Date(gYear,gMonth,1);
    startDay = firstDay.getDay();

    if (((gYear % 4 == 0) && (gYear % 100 != 0)) || (gYear % 400 == 0))
         days[1] = 29; 
    else
         days[1] = 28;

    output += '<table style="width:100%; padding:0; border-style:solid; border-width:1px; border-color:#444;"><tr>';

    for (i=0; i<7; i++)
        output += '<td style="padding:0; background-color:#777; border-style:dotted; border-width:1px; border-color:#666; font-size:8px; font-family:Verdana; font-weight:bold;">' + dow[i] +'<\/td>';

    output += '<\/tr><tr align=center valign=middle>';

    var column = 0;
    var lastMonth = gMonth - 1;
    if (lastMonth == -1) lastMonth = 11;

    for (i=0; i<startDay; i++, column++)
        output += '<td style="height:20px; padding:0; background-color:#CCC; border-style:dotted; border-width:1px; border-color:#666;"><font size=-1 color="#808080" face="Arial">' + (days[lastMonth]-startDay+i+1) + '<\/font><\/td>';

    for (i=1; i<=days[gMonth]; i++, column++) {
        output += '<td style="height:20px; padding:0; background-color:#999; border-style:dotted; border-width:1px; border-color:#666; cursor:pointer;" onmouseover="this.style.backgroundColor=\'#666\';" onmouseout="this.style.backgroundColor=\'#999\';"onclick="changeDay(' + i + ')">' + '<font size=-1 face="arial" color="#000088">' + i + '<\/font>' +'<\/td>';
        if (column == 6) {
            output += '<\/tr><tr align=center valign=middle>';
            column = -1;
        }
    }

    if (column > 0) {
        for (i=1; column<7; i++, column++)
            output +=  '<td style="height:20px; padding:0; background-color:#CCC; border-style:dotted; border-width:1px; border-color:#666;"><font size=-1 color="#808080" face="Arial">' + i + '<\/font><\/td>';
    }

    output += '<\/tr><\/table><\/td><\/tr><\/table>';

    return output;
}

function changeDay(day) {
	setDateField.value = '' + gYear + '-' + padout(gMonth - 0 + 1) + '-' + padout(day);
	dateDiv.style.visibility='hidden';
}
