/* EURO dd.mm.yyyy JAPAN yyyy/mm/dd */


var _calDateStringDelimiter = '\/';

var suppressLeadingZero = false;
var twoDigitYear = false;
var US = 0;
var EURO = 1;
var JAPAN = 2;
var dateConvention = US; 
var _VALID_DATE = 1; 
var _ILGLDATE_FLDCNT = -1;
var _ILGLDATE_MONTH = -2;
var _ILGLDATE_YEAR = -3;
var _ILGLDATE_DATE = -4;
var _ILGLDATE_DATERANGE = -5;

function validateDate(t)
{
  _calMM = 1;
  _calYYYY = 1970;
  var d="";
  for (i = 0; i < t.length; i++)
    if (t.charAt(i) < '0')
      d = d+_calDateStringDelimiter;
    else
    {
      if (i == 0) d = t.substr(0, 1);
      else d = d+t.substr(i, 1);
    }
  d = d.split(_calDateStringDelimiter);
  var mi = 0, di = 1, yi = 2;
  if (d.length != 3) return _ILGLDATE_FLDCNT;
  if (dateConvention == EURO)
  {
    mi = 1; di = 0;
  }
  else if (dateConvention == JAPAN)
  {
    yi = 0; mi = 1; di = 2;
  }
  if (d[mi].charAt(0) == '0') d[mi] = d[mi].substr(1,1);
  if (d[di].charAt(0) == '0') d[di] = d[di].substr(1,1);
  _calMM = parseInt(d[mi]);
  if (!((_calMM > 0) && (_calMM < 13)))
    if ((_calMM = stringToMonth(d[mi])) < 0)
      return _ILGLDATE_MONTH;
  _calYYYY = parseInt(d[yi]);
  if (isNaN(_calYYYY)) return _ILGLDATE_YEAR;
  if (twoDigitYear)
  {
    if (d[yi].length < 2) return _ILGLDATE_YEAR;
    if (_calYYYY < 50) _calYYYY += 2000;
    else if (_calYYYY > 49) _calYYYY += 1900;
    else return _ILGLDATE_YEAR;
  }
  if ((_calYYYY < 1900) || (_calYYYY > 2099)) return _ILGLDATE_YEAR;

  _calDD = parseInt(d[di]);
  if (isNaN(_calDD)) return _ILGLDATE_DATE;
  var ld = lastDateInMonth(_calMM-1,_calYYYY);
  if (_calDD > ld)
  {
    _calDD = ld;
    return _ILGLDATE_DATERANGE;
  }
  _calMM--;
  return 1;
}


function calError(errNo)
{
  var convention = '';
  var errStr = '';
  switch (dateConvention)
  {
    case US:
      convention = 'MM'+_calDateStringDelimiter+'DD'+_calDateStringDelimiter;
      if (twoDigitYear) convention += 'YY';
      else convention += 'YYYY';
      break;
    case EURO:
      convention = 'DD'+_calDateStringDelimiter+'MM'+_calDateStringDelimiter;
      if (twoDigitYear) convention += 'YY';
      else convention += 'YYYY';
      break;
    case JAPAN:
      if (twoDigitYear) convention = 'YY'+_calDateStringDelimiter;
      else convention = 'YYYY'+_calDateStringDelimiter;
      convention += 'MM'+_calDateStringDelimiter+'DD';
      break;
  }
  switch (errNo)
  {
    case _VALID_DATE:
      return 'Valid date.';
    case _ILGLDATE_FLDCNT:
      errStr = 'Invalid date. Too many or too few date fields.\n'; break;
    case _ILGLDATE_MONTH:
      errStr = 'Invalid month.\n'; break;
    case _ILGLDATE_YEAR:
      errStr = 'Invalid year.\n'; break;
    case _ILGLDATE_DATE:
      errStr = 'Invalid date.\n'; break;
    case _ILGLDATE_DATERANGE:
      errStr = 'Invalid date. Date out of range.\n'; break;
  }
  errStr += 'Date should be in the form of: '+convention;
  return (errStr);
}


function updateCalendar(y, m, d)
{
  document.getElementById(this.hilitedDay).style.backgroundColor = this.prevBackground;

  this.dt.setFullYear(y, m, d);
  this.selectedDate = d;
  this.intendedSelectedDate = d;
  this._calDD = this.dt.getDate();
  this._calMM = this.dt.getMonth();
  this._calYYYY = this.dt.getFullYear();
  this.buildCalendar();
}


function stringToMonth(s)
{
  var m = s.toLowerCase();
  if (m.indexOf('jan') >= 0) return 1;
  if (m.indexOf('feb') >= 0) return 2;
  if (m.indexOf('mar') >= 0) return 3;
  if (m.indexOf('apr') >= 0) return 4;
  if (m.indexOf('may') >= 0) return 5;
  if (m.indexOf('jun') >= 0) return 6;
  if (m.indexOf('jul') >= 0) return 7;
  if (m.indexOf('aug') >= 0) return 8;
  if (m.indexOf('sep') >= 0) return 9;
  if (m.indexOf('oct') >= 0) return 10;
  if (m.indexOf('nov') >= 0) return 11;
  if (m.indexOf('dec') >= 0) return 12;
  return -1;
}


function setSuppressLeadingZero(z)
{
  if (typeof z != 'boolean') return false;
  suppressLeadingZero = z;
  return true;
}


function setDateConvention(c)
{
  
  if (typeof c == 'number')
  {
    if (c == 0)
      dateConvention = US;
    else if (c == 1)
      dateConvention = EURO;
    else if (c == 2)
      dateConvention = JAPAN;
    else return false;
    return true;
  }
  else if (typeof c == 'string')
  {
    if (c.indexOf('US') >= 0)
      dateConvention = US;
    else if (c.indexOf('EURO') >= 0)
      dateConvention = EURO;
    else if (c.indexOf('JAPAN') >= 0)
      dateConvention = JAPAN;
    else return false;
    return true;
  }
  return false;
}


function setTwoDigitYear(t)
{
  if (typeof t != 'boolean') return false;
  twoDigitYear = t;
  return true;
}

function test()
{
  alert(dateConvention);
}


function hiliteCal(elem)
{
  if (elem.id == this.hilitedDay) return;
  if (elem.innerHTML.charAt(0) < '0') return;
  this.prevBackground = elem.style.backgroundColor;
  elem.style.backgroundColor = '#d0d0d0';
}

function deHiliteCal(elem)
{
  if (elem.id == this.hilitedDay) return;
  elem.style.backgroundColor = this.prevBackground;
}


function hiliteDay(elem)
{
  if (document.getElementById(this.hilitedDay))
    document.getElementById(this.hilitedDay).style.backgroundColor = this.prevBackground;
  this.hilitedDay = elem.id;
  elem.style.backgroundColor = '#f0f080';
}


function monthString(m)
{
  switch (m)
  {
    case 0: return('Jan');
    case 1: return('Feb');
    case 2: return('Mar');
    case 3: return('Apr');
    case 4: return('May');
    case 5: return('Jun');
    case 6: return('Jul');
    case 7: return('Aug');
    case 8: return('Sep');
    case 9: return('Oct');
    case 10: return('Nov');
    case 11: return('Dec');
  }
}


function lastDateInMonth(m, y)
{
  switch (m)	{
    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11: return 31;

    case 3:
    case 5:
    case 8:
    case 10: return 30;
  }
  if ((y % 4) == 0) return 29;
  else return 28;
}



function buildCalendar()
{
  var start = false;
  var done = false;
  var dateNo = 1;
  var w;
  var d;

tempDt = new Date();
/* Safari can't set date properly with constructor so... */
tempDt.setFullYear(this.dt.getFullYear(), this.dt.getMonth(), this.dt.getDate());

  lastDay = lastDateInMonth(tempDt.getMonth(), tempDt.getFullYear());
  tempDt.setDate(1);
// Next line is for IE5.2
  document.getElementById(this.name+'cMonth').innerHTML = "";
  document.getElementById(this.name+'cMonth').innerHTML = monthString(this.dt.getMonth())+" "+this.dt.getFullYear();
  for (w = 0; w < 6; w++)
  {
    for (d = 0; d < 7; d++)
    {
// Next line is for IE5.2
      document.getElementById(this.name+'w'+w+'d'+d).innerHTML = "";
      if (d >= tempDt.getDay()) start = true;
      if ((start) && (!done))
      {
        document.getElementById(this.name+'w'+w+'d'+d).innerHTML = dateNo;
        if (dateNo == this.selectedDate)
        {
          document.getElementById(this.name+'w'+w+'d'+d).style.backgroundColor = "#f0f080";
          this.hilitedDay = this.name+'w'+w+'d'+d;
        }
        dateNo++;
      }
      else
      {
        document.getElementById(this.name+'w'+w+'d'+d).innerHTML = "&nbsp;";
      }
      if (dateNo > lastDay) done = true;
    }
  }

  if (this.ie52)
  {
    var elm = document.getElementById(this.name);
    var wrapper= document.getElementById(this.name+'Wrapper');
    var w = elm.offsetWidth;
    var h = elm.offsetHeight;
    wrapper.style.width = w;
    wrapper.style.height = h;
    wrapper.style.overflow = 'hidden';
    return;
  }
//  stuff for hiding empty end week - doesn't work so well on the Mac
// The next line is in deference to Safari's weak innerHTML support
  var ch = document.getElementById(this.name+'w5d0').innerHTML.charAt(0);
  if ((ch < '0') || (ch > '9'))
  {
    this.dispCell = 'none';
    this.dispRow = 'none';
  }
  else
  {
    if (navigator.appName == "Netscape")
    {
      this.dispCell = 'table-cell';
      this.dispRow = 'table-row';
    }
    else
    {
      this.dispCell = 'block';
      this.dispRow = 'block';
    }
  }
  for (i = 0; i < 7; i++)
  {
    document.getElementById(this.name+'w5d'+i).style.display = this.dispCell;
  }
  document.getElementById(this.name+'week5').style.display = this.dispRow;
}	/* buildCalendar() */


function incrMonth(inc)
{
  this.selectedDate = this.intendedSelectedDate;
  this.dt.setDate(1);
  if (inc < 0)
  {
    if (this.dt.getMonth() == 0)
    {
      this.dt.setFullYear(this.dt.getFullYear() - 1);
      this.dt.setMonth(11);
    }
    else
    {
      if (this.selectedDate > lastDateInMonth(this.dt.getMonth()-1, this.dt.getFullYear()))
        this.selectedDate = lastDateInMonth(this.dt.getMonth()-1, this.dt.getFullYear());
      this.dt.setMonth(this.dt.getMonth() - 1);
    }
  }
  else
  {
    if (this.dt.getMonth() == 11)
    {
      this.dt.setFullYear(this.dt.getFullYear() + 1);
      this.dt.setMonth(0);
    }
    else
    {
      if (this.selectedDate > lastDateInMonth(this.dt.getMonth()+1, this.dt.getFullYear()))
        this.selectedDate = lastDateInMonth(this.dt.getMonth()+1, this.dt.getFullYear());
      this.dt.setMonth(this.dt.getMonth() + 1);
    }
  }
  this.dt.setDate(this.selectedDate);

  if (document.getElementById(this.hilitedDay))
    document.getElementById(this.hilitedDay).style.backgroundColor = this.prevBackground;
  this.hilitedDay = '';
  this.buildCalendar();
  if (this.textTarget)
    if (this.textTarget.id) this.updateTextTarget();
  this._calDD = this.dt.getDate();
  this._calMM = this.dt.getMonth();
  this._calYYYY = this.dt.getFullYear();
  if (this.onChangeFunc)
    eval(this.onChangeFunc);
}


function setDay(elem)
{
  var n = parseInt(elem.innerHTML);
  if ((n > 0) && (n < 32))
  {
    this.selectedDate = n;
    this.intendedSelectedDate = this.selectedDate;
    this.dt.setDate(n);
  }
  else return;
  this.hiliteDay(elem);
  if (this.textTarget)
    if (this.textTarget.id) this.updateTextTarget();
  this._calDD = this.dt.getDate();
  this._calMM = this.dt.getMonth();
  this._calYYYY = this.dt.getFullYear();
  if (this.onChangeFunc)
    eval(this.onChangeFunc);
}



function paintCalendar()
{
  var result = "            <table id='"+this.name+"' class='miniCalendar' cellspacing='0'\n";
  result += (this.teeny)?"                style='font-size:7pt; width: 100px'>\n":"                >\n";

  result += "" +
"              <tr>\n"+
"                <th onmousedown='"+this.name+".incrMonth(-1)' class='changeMonth'><</th>\n"+
"                <th colspan='5' id='"+this.name+"cMonth'>May 2004</th>\n"+
"                <th onmousedown='"+this.name+".incrMonth(1)' class='changeMonth')>></th>\n"+
"              </tr>\n"+
"              <tr>\n"+
"                <th>S</th>\n"+
"                <th>M</th>\n"+
"                <th>T</th>\n"+
"                <th>W</th>\n"+
"                <th>T</th>\n"+
"                <th>F</th>\n"+
"                <th>S</th>\n"+
"              </tr>\n";
  for (w = 0; w < 6; w++) {
    result += "             <tr id='"+this.name+"week"+w+"'>\n";
    for (d = 0; d < 7; d++) {
      result += " <td id='"+this.name+"w"+w+"d"+d+"' onmouseover='"+this.name+".hiliteCal(this)' \n"+
				"onmouseout='"+this.name+".deHiliteCal(this)'\n"+
				"onmousedown='"+this.name+".setDay(this)'>&nbsp;</td>\n";
    }

    result += "              </tr>\n";
  }
  result += "            </table>\n";
  return result;
}


function makeTeeny()
{
  this.teeny = true;
}



function onChange(func)
{
  this.onChangeFunc = func;
}





function dateToString()
{
  var mm = ""+(this.dt.getMonth()+1);
  var dd = ""+this.dt.getDate();
  var yyyy = ""+this.dt.getFullYear();
  var dstr = "";

  if (!suppressLeadingZero)
  {
    if ((parseInt(dd) < 10) && (dd.length == 1)) dd = '0'+dd;
    if ((parseInt(mm) < 10) && (mm.length == 1)) mm = '0'+mm;
  }

  if (twoDigitYear)
    yyyy = yyyy.substr(2);

  if (dateConvention == EURO)
    dstr = ""+dd+_calDateStringDelimiter+mm+_calDateStringDelimiter+yyyy;
  else if (dateConvention == JAPAN)
    dstr = ""+yyyy+_calDateStringDelimiter+mm+_calDateStringDelimiter+dd;
  else
    dstr = ""+mm+_calDateStringDelimiter+dd+_calDateStringDelimiter+yyyy;
  return (dstr);
}



function dateIntVal()
{
  var d = (this.dt.getFullYear() - 1900) * 10000;
  d += (this.dt.getMonth() * 100);
  d += this.dt.getDate();
  return d;
}


function setDate(m,d,y)
{
  document.getElementById(this.hilitedDay).style.backgroundColor = this.prevBackground;
  this.dt.setFullYear(y);
  this.dt.setMonth(m);
  this.buildCalendar();
  document.getElementById(this.hilitedDay).style.backgroundColor = this.prevBackground;
  this.dt.setDate(d);
  this.selectedDate = d;
  this.intendedSelectedDate = d;
  this.buildCalendar();
}


/* constructor */
function Cal(name)
{
  var ua = navigator.userAgent.toLowerCase();
  if ((ua.indexOf('msie 5') >= 0) && (ua.indexOf('mac') >= 0))
    this.ie52 = true;
  if (this.ie52)
  {
    if (!document.getElementById(name+'Wrapper'))
    {
      alert('IE5.2 div wrapper \"'+name+'Wrapper\" missing.');
      return false;
    }
  }

  if (!name) return false;
  this.name = name;

  this.paintCalendar = paintCalendar;
  this.buildCalendar = buildCalendar;
  this.dt = new Date();
  this.hiliteCal = hiliteCal;
  this.deHiliteCal = deHiliteCal;
  this.hiliteDay = hiliteDay;
  this.setDay = setDay;
  this.incrMonth = incrMonth;
  this.teeny = false;
  this.makeTeeny = makeTeeny;
  this.onChange = onChange;
  this.dateToString = dateToString;
  this.dateIntVal = dateIntVal;
  this.setDate = setDate;
  this.updateCalendar = updateCalendar;

  this.test = test;


  this.selectedDate = this.dt.getDate();
  this.intendedSelectedDate = this.selectedDate;

  this.prevBackground = '';
  this.hilitedDay = '';
  this.title = '';
  this.dispCell = '';
  this.dispRow = '';
  this._calMM = 0;
  this._calYYYY = 1970;
  this._calDD = 1;
}

/* handy functions for popping up calendars */
var activeCal = null;

function calCanGo(elem)
{
  activeCal = elem;
}


function calIsInUse()
{
  activeCal = null;
}

function killPopupCalendar()
{
  if (activeCal != null)
  {
    activeCal.style.display = 'none';

    if (is.ie && !is.IEmac)
      if (document.getElementById('mask'))
        document.getElementById('mask').style.display = 'none';
  }
}

var X=0, Y=0;
function getPos(elem)
{
  var localElem = elem;
  X = 0; Y = 0;
  while (localElem)
  {
    X += localElem.offsetLeft;
    Y += localElem.offsetTop;
    localElem = localElem.offsetParent;
  }
  return;
}


function resetCalDisplay()
{
  activeCal = null;
}


function popupCal(elem, targ, overrideX, overrideY)
{

  getPos(elem);
  elem.focus();
  var c = document.getElementById(targ+'Wrapper');
  if (activeCal == c)
  {
    return;
  }
  if (overrideX)
    c.style.left = overrideX;
  else
    c.style.left = X;
  if (overrideY)
    c.style.top = overrideY;
  else
    c.style.top = Y+(elem.offsetHeight);
  c.style.display='block';

  if (is.IEmac)
  {
    c.style.width = document.getElementById(targ).offsetWidth;
    c.style.height = document.getElementById(targ).offsetHeight;
  }
  else if (is.ie)
  {
    if (document.getElementById('mask'))
    {
      var m = document.getElementById('mask').style;
      m.width = document.getElementById(targ).offsetWidth+2;
      m.height = document.getElementById(targ).offsetHeight+2;
      m.left = c.style.left;
      m.top = c.style.top;
      m.display = 'block';
    }
  }


  activeCal = c;
}
