function yfcMiniCalPrevMonth(){
  if (this.currField == undefined) return ;
  if (this.currMonth > 0) this.currMonth--;
  if (this.currMonth == 0) document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'hidden';
  document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 'visible';
  if (this.disableSelectbox){
    document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
  } 
  else{
    document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
  } 
  this.showMonth();
} 

function yfcMiniCalNextMonth(){
  if (this.currField == undefined) return ;
  if (this.currMonth < this.maxMonth) this.currMonth++;
  if (this.currMonth == this.maxMonth - 1) document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 
  'hidden';
  document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'visible';
  if (this.disableSelectbox){
    document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
  } 
  else{
    document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
  } 
  this.showMonth();
} 

function yfcMiniCalCloseCal(){
  var cal = document.getElementById('yfcMiniCalDiv');
  var iframed = document.getElementById('yfcMiniCalIFrame');
  var iframe = document.getElementById('yfcMiniCalIFrameIn');
  if ( ! cal || ! iframe || ! iframed) return ;
  cal.style.visibility = 'hidden';
  iframed.style.height = "0px";
  iframed.style.width = "0px";
  iframe.style.height = "0px";
  iframe.style.width = "0px";
  iframed.style.visibility = 'hidden';
  document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 'hidden';
  document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'hidden';
  this.displayed[this.currField] = 0;
  this.currField = undefined;
  delete(cal);
} 

function yfcMiniCalOpenCal(fldDiv,fld,startFld){
  var _fldDiv = document.getElementById(fldDiv);
  var cal = document.getElementById('yfcMiniCalDiv');
  var iframeDiv = document.getElementById('yfcMiniCalIFrame');
  var iframe = document.getElementById('yfcMiniCalIFrameIn');
  var field = this.fields[fld];
  if ( ! _fldDiv) return ;
  if ( ! cal) return ;
  if ( ! iframeDiv || ! iframe) return ;
  if ( ! field) return ;
  var absoluteLeft = getAbsoluteLeft(_fldDiv);
  var absoluteTop = getAbsoluteTop(_fldDiv);
  var left = _fldDiv ? absoluteLeft + 'px':0;
  var top = _fldDiv ? absoluteTop + 24 + 'px':0;
  cal.style.top = top;
  cal.style.left = left;
  iframeDiv.style.top = top;
  iframeDiv.style.left = left;
  iframeDiv.style.height = "197px";
  iframeDiv.style.width = "390px";
  iframe.style.height = "197px";
  iframe.style.width = "390px";
  cal.style.visibility = 'visible';
  if (navigator.userAgent.indexOf('Opera') == - 1){
    iframeDiv.style.visibility = 'visible';
  } 
  this.displayed[this.currField] = 0;
  this.currField = fld;
  this.displayed[fld] = 1;
  this.startField = startFld;
  this.showMonth(true);
  if (typeof(horizontalResizeManager) != 'undefined'){
    horizontalResizeManager.attach(iframeDiv,_fldDiv);
    horizontalResizeManager.attach(cal,_fldDiv);
  } 
  delete(cal);
} 

function getAbsoluteLeft(e){
  var p = 0;
  while (e.offsetParent){
    p += e.offsetLeft;
    e = e.offsetParent;
  } 
  return p;
} 

function getAbsoluteTop(e){
  var p = 0;
  while (e.offsetParent){
    p += e.offsetTop;
    e = e.offsetParent;
  } 
  return p;
} 

function yfcMiniCalUpdateArrows(){
  var pm = document.getElementById('yfcMiniCalPrevMonthDiv');
  var nm = document.getElementById('yfcMiniCalNextMonthDiv');
  if (this.currMonth == this.maxMonth - 1){
    nm.style.visibility = 'hidden';
    pm.style.visibility = 'visible';
  } 
  else if (this.currMonth == 0){
    nm.style.visibility = 'visible';
    pm.style.visibility = 'hidden';
  } 
  else{
    nm.style.visibility = 'visible';
    pm.style.visibility = 'visible';
  } 
  delete pm;
  delete nm;
} 

function yfcMiniCalShowMonth(jumpToMonth){
  var formNames = this.currField.split(this.formFieldSeparator);
  var currFieldDate = document.getElementById(whatfield).value;
  var currFieldDateParse = currFieldDate.split('/');
  var selMon = currFieldDateParse[0];
  var selMonForCheck = (selMon && selMon.indexOf("0") == 0) ? selMon.substring(1):selMon;
  var selDay = currFieldDateParse[1];
  var selDayForCheck = (selDay && selDay.indexOf("0") == 0) ? selDay.substring(1):selDay;
  var selYear = currFieldDateParse[2];
  if ( ! parseInt(selMonForCheck) || ! parseInt(selDayForCheck) || ! parseInt(selYear)){
    var todayDate = new Date();
    selMon = todayDate.getMonth();
    if (selMon < 10) 
    {
      selMon = "0" + selMon;
    }
    selDay = todayDate.getDate();
    if (selDay < 10) 
    {
      selDay = "0" + selDay;
    }
    selYear = todayDate.getFullYear() % 100;
    if (selYear < 10) 
    {
      selYear = "0" + selYear;
    }
  } 
  var startFieldDate = null;
  var startMon = null;
  var startDay = null;
  var startYear = null;
  if (this.startField){
    formNames = this.startField.split(this.formFieldSeparator);
    startFieldDate = document.getElementById(whatfield).value;
    currFieldDateParse = startFieldDate.split('/');
    startMon = currFieldDateParse[0];
    startDay = currFieldDateParse[1];
    startYear = currFieldDateParse[2];
  } 
  var nextMonth = this.currMonth + 1;
  if (jumpToMonth){
    if (selMon.length == 1) selMon = '0' + selMon;
    for (var m = 0;m < this.maxMonth;m++){
      if (this.months[m].mon != selMon || this.months[m].yr != selYear) continue ;
      if (this.disableSelectbox){
        this.currMonth = m;
        document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
      } 
      else{
        this.currMonth = ((startMon == selMon || startMon == null) ? m:m - 1);
        document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
      } 
      nextMonth = this.currMonth + 1;
      break ;
    }
  } 
  nextMonthTitleDiv = document.getElementById('yfcMiniCalNextMonthTitle');
  nextMonthTitleDiv.innerHTML = this.months[nextMonth].dropDownLabel;
  var sdow = this.months[this.currMonth].startDOW;
  var lday = this.months[this.currMonth].lastDay;
  var mon = this.months[this.currMonth].mon;
  var yr = this.months[this.currMonth].yr;
  var today = (this.currMonth == 0) ? this.today:0;
  var isSelMonth = (this.months[this.currMonth].mon == selMon);
  var isStartMon = (this.months[this.currMonth].mon == startMon);
  var isSelYear = (this.months[this.currMonth].yr == selYear);
  var isStartYear = (this.months[this.currMonth].yr == startYear);
  var href = '<a href="javascript:void(0);" onClick="yfcMiniCal.updateField(\'' + this.currField + '\',\'' + 
  mon + '\',\'';
  var md = 1;
  var nextMd = 1;
  for (i = 0;i < 42;i++){
    dayDiv = document.getElementById('yfcMiniCalDayDiv' + Number(i));
    dayDiv.className = 'yfcMiniCalDay' + ((md >= today || this.disableSelectbox) ? ((md > lday) ? 'Next':(((md == 
    startDay) && isStartMon && isStartYear) ? 'Start':(((md == selDay) && isSelMonth && isSelYear) ? 
    'Sel':''))):(this.disableSelectbox ? '':'Past'));
    if (i < sdow || md > lday){
      dayDiv.innerHTML = '&nbsp;';
    } 
    else{
      dayDiv.innerHTML = (md < today && ! this.disableSelectbox) ? md:href + ((md > 9) ? md:'0' + md) + '\',\'' + 
      yr + '\'); return false;">' + md + '</a>';
      md++;
    }
  } 
  sdow = this.months[nextMonth].startDOW;
  lday = this.months[nextMonth].lastDay;
  mon = this.months[nextMonth].mon;
  yr = this.months[nextMonth].yr;
  today = 0;
  isSelMonth = (this.months[nextMonth].mon == selMon);
  isStartMon = (this.months[nextMonth].mon == startMon);
  isSelYear = (this.months[nextMonth].yr == selYear);
  isStartYear = (this.months[nextMonth].yr == startYear);
  href = '<a href="javascript:void(0);" onClick="yfcMiniCal.updateField(\'' + this.currField + '\',\'' + 
  mon + '\',\'';
  md = 1;
  nextMd = 1;
  for (i = 0;i < 42;i++){
    dayDiv = document.getElementById('yfcMiniCalDayDivNext' + Number(i));
    dayDiv.className = 'yfcMiniCalDay' + ((md >= today || this.disableSelectbox) ? ((md > lday) ? 'Next':(((md == 
    startDay) && isStartMon && isStartYear) ? 'Start':(((md == selDay) && isSelMonth && isSelYear) ? 
    'Sel':''))):(this.disableSelectbox ? '':'Past'));
    if (i < sdow || md > lday){
      dayDiv.innerHTML = '&nbsp;';
    } 
    else{
      dayDiv.innerHTML = (md < today && ! this.disableSelectbox) ? md:href + ((md > 9) ? md:'0' + md) + '\',\'' + 
      yr + '\'); return false;">' + md + '</a>';
      md++;
    }
  } 
  this.updateArrows();
  delete(dayDiv);
} 

function yfcMiniCalInitCalendar(){
  var wkDiv = document.getElementById('yfcMiniCalWeekdays');
  var wkDivNext = document.getElementById('yfcMiniCalWeekdaysNext');
  if ( ! wkDiv || ! wkDivNext) return ;
  var day = 0;
  var newDay;
  while (day < 42){
    newDay = document.createElement('div');
    newDay.id = 'yfcMiniCalDayDiv' + Number(day);
    newDay.className = 'yfcMiniCalDay';
    wkDiv.appendChild(newDay);
    newDay = document.createElement('div');
    newDay.id = 'yfcMiniCalDayDivNext' + Number(day);
    newDay.className = 'yfcMiniCalDay';
    wkDivNext.appendChild(newDay);
    day++;
  } 
  newClr = document.createElement('br');
  newClr.className = 'ygcl';
  wkDiv.appendChild(newClr);
} 

function yfcMiniCalField(formName,fldName,depFldName,depInc){
  this.formName = formName;
  this.fldName = fldName;
  this.depFldName = depFldName;
  this.depInc = (depInc == undefined) ? 3:depInc;
  this.depSet = false;
} 

function yfcMiniCalAddField(formName,fldName,depFldName,depInc){
  this.fields[formName + this.formFieldSeparator + fldName] = new yfcMiniCalField(formName,fldName,depFldName,
  depInc);
} 

function yfcMiniCalUpdateField(fld,m,d,y){
  if (m > 9)
  {
    if (m.charAt(0) == '0')
    {
      m = m.substring(1,3);
    }
  }
  if ( ! this.fields[fld]) return;
  var formName = this.fields[fld].formName;
  var fldName = this.fields[fld].fldName;
  document.getElementById(fldName).value = m + '/' + d + '/' + y;
  if (document.getElementById(fldName).onchange) document.getElementById(fldName).onchange();
  var depFldName = this.fields[fld].depFldName;
  if ( ! depFldName || this.fields[fld].depSet){
    this.closeCal();
    document.getElementById(fldName).focus();
    return;
  } 
  var depDate = new Date(Number(y),m - 1,d);
  var tmp = document.getElementById(depFldName).value;
  var slash = tmp.indexOf('/');
  var lastSlash = tmp.lastIndexOf('/');
  var ry = (Number(tmp.substring(lastSlash + 1)) % 100);
  var rm = tmp.substring(0,slash);
  var rd = tmp.substring(slash + 1,lastSlash);
  var retDate = new Date(ry,rm - 1,rd);
  var validDate = false;
  if (ry >= 0 && parseInt(ry) >= 0 && rm >= 0 && parseInt(rm) >= 0 && rd >= 0 && parseInt(rd) >= 0) validDate = true;
  if ( ! validDate || retDate <= depDate){
    depDate.setDate(depDate.getDate() + this.fields[fld].depInc);
    m = depDate.getMonth() + 1;
    if (m < 10) 
    {
      m = "0" + m;
    }
    d = depDate.getDate();
    if (d < 10) 
    { 
      d = "0" + d;
    }
    y = depDate.getFullYear().toString();
    y = y.substr(2,2);
    document.getElementById(depFldName).value = m + '/' + d + '/' + y;
  } 
  document.getElementById(fldName).focus();
  this.closeCal();
} 

function yfcMiniCalMonth(mon,yr,startDOW,lastDay,dropDownLabel){
  this.mon = mon;
  this.yr = yr;
  this.startDOW = startDOW;
  this.lastDay = lastDay;
  this.dropDownLabel = dropDownLabel;
} 

function yfcMiniCalAddMonth(mon,yr,startDOW,lastDay,dropDownLabel){
  this.months.push(new yfcMiniCalMonth(mon,yr,startDOW,lastDay,dropDownLabel));
} 

function yfcMiniCalSetMonth(objId){
  this.currMonth = objId.selectedIndex;
  this.showMonth();
} 

function yfcToggleDisplay(fldDiv,fld,startFld){
  if (this.displayed[fld]){
    this.closeCal();
  } 
  else{
    this.openCal(fldDiv,fld,startFld);
  }
} 

function yfcMiniCalClass(){
  this.months = new Array();
  this.fields = new Array();
  this.currMonth = 0;
  this.maxMonth = 11;
  this.currField = undefined;
  this.today = 0;
  this.formFieldSeparator = '_';
  this.displayed = new Array();
  this.init = yfcMiniCalInitCalendar;
  this.prevMonth = yfcMiniCalPrevMonth;
  this.nextMonth = yfcMiniCalNextMonth;
  this.closeCal = yfcMiniCalCloseCal;
  this.openCal = yfcMiniCalOpenCal;
  this.toggleDisplay = yfcToggleDisplay;
  this.addField = yfcMiniCalAddField;
  this.addMonth = yfcMiniCalAddMonth;
  this.showMonth = yfcMiniCalShowMonth;
  this.setMonth = yfcMiniCalSetMonth;
  this.updateArrows = yfcMiniCalUpdateArrows;
  this.updateField = yfcMiniCalUpdateField;
} 

var yfcMiniCal = new yfcMiniCalClass();