//
// Bazillyo's Spiffy DHTML Popup Calendar Control - beta version 2.0 Release Candidate 1
// ©2001 Spiffy Controls - email me bazillyo@yahoo.com or
// see website for copyright information http://www.geocities.com/bazillyo/spiffy/calendar/index.htm
// 62NDS: modified for our use.. (and fixed 2 bugs)
//
// GLOBAL variables

var scIE=((navigator.appName == "Microsoft Internet Explorer") || ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion)>=5)));
var scNN6=((navigator.appName == "Netscape") && (parseInt(navigator.appVersion)>=5));
var scNN=((navigator.appName == "Netscape")&&(document.layers));

var img_Close=new Image();

img_Close.src="/img/cal/btn_close_small.gif";

var focusHack;

function makeArray0() {
 for (i = 0; i<makeArray0.arguments.length; i++)
  this[i] = makeArray0.arguments[i];
}

function spiffyCalManager() {

 this.lastSelectedDate=new Date();
 this.calendars=new makeArray0();
 this.matchedFormat="";
 this.DefBtnImgPath='/img/cal/';

 // methods  ----------------------------------------------------------------------
 this.getCount= new Function("return this.calendars.length;");

 function addCalendar(objWhatCal) {
  var intIndex = this.calendars.length;
  this.calendars[intIndex] = objWhatCal;
 }
 this.addCalendar=addCalendar;


 function hideAllCalendars(objExceptThisOne) {
  var i=0;
  for (i=0;i<this.calendars.length;i++) {
   if (objExceptThisOne!=this.calendars[i]) {
    this.calendars[i].hide();
   }
  }
 }
 this.hideAllCalendars=hideAllCalendars;
}

var calMgr = new spiffyCalManager();


//================================================================================
// Calendar Object

function ctlSpiffyCalendarBox(strVarName, strFormName, strYYName, strMMName, strDDName, strBtnName, strDefaultValue) {

 var msNames     = new makeArray0('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
 var msDays      = new makeArray0(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 var msDOW       = new makeArray0('S','M','T','W','T','F','S');


 var blnInConstructor=true;
 var strBtnW;
 var strBtnH;
 var strBtnImg;

 var dteToday=new Date;
 var dteCur=new Date;

 var scX=4; // default where to display calendar
 var scY=4;

 // Defaults
 var strDefBtnImgPath=calMgr.DefBtnImgPath;

 /* PROPERTIES =============================================================
  *
  */
 // Generic Properties
 this.varName=strVarName;
 this.enabled=true;
 this.visible=false;
 this.displayLeft=false;
 this.displayTop=false;
 // Name Properties
 this.formName=strFormName;
 this.yyName=strYYName;
 this.mmName=strMMName;
 this.ddName=strDDName;
 this.btnName=strBtnName;

 // look
 this.btnImgWidth=strBtnW;
 this.btnImgHeight=strBtnH;

 strBtnImg=strDefBtnImgPath+'calendar.gif';
 strBtnW = '33';
 strBtnH = '16';

 // Date Properties

 this.dayBox = function() {
    return eval('document.'+this.formName+'.'+this.ddName);
 }

 this.monthBox = function() {
    return eval('document.'+this.formName+'.'+this.mmName);
 }

 this.yearBox = function() {
    return eval('document.'+this.formName+'.'+this.yyName);
 }

 this.getSelectedDate = function () {
  if ((typeof this.formName !='undefined') && (typeof this.yyName !='undefined') && (!blnInConstructor)) {
   dteCur.setFullYear(this.yearBox().value);
   dteCur.setMonth(this.monthBox().value - 1);
   dteCur.setDate(this.dayBox().value);
   dteToday = dteCur;  
  }  
 }

 // behavior Properties
 this.JStoRunOnSelect='';
 this.JStoRunOnClear='';
 this.JStoRunOnCancel='';
 this.hideCombos=true;


 /* METHODS ===============================================================
  *
  */

 function makeCalendar(intWhatMonth,intWhatYear) {
  var strOutput = '';
  var intStartMonth=intWhatMonth;
  var intStartYear=intWhatYear;
  var temp;
  var intLoop;
  var strTemp='';

  dteCur.setMonth(intWhatMonth);
  dteCur.setFullYear(intWhatYear);
  dteCur.setDate(dteToday.getDate());
  dteCur.setHours(0);dteCur.setMinutes(0);dteCur.setSeconds(0);dteCur.setMilliseconds(0);
  // special case for form not to be inside table in Netscape 6
  if (scNN6) {
    strOutput += '<form name="spiffyCal"><table border=0 width=195 class="cal-Table" cellspacing=0 cellpadding=3>';
  } else { 
    strOutput += '<table border=0 width=195 class="cal-Table" cellspacing=0 cellpadding=3><form name="spiffyCal">';
  }

  var thisMonth = intWhatMonth + 12 * intWhatYear;
   
  temp = thisMonth - 1;
  var mm = dteToday.getMonth();
  var yy = dteToday.getFullYear();
  mm = (yy * 12 + mm) - 1;  
  
  strOutput += '<tr><td width=14% class="cal-HeadCell">&nbsp;</td><td class="cal-HeadCell" align="center" colspan=5>';
  if (temp >= mm) {
    strOutput += '<a href="javascript:'+this.varName+'.toMonth(' + temp + ');" class="cal-DayLink"><img src="' + strDefBtnImgPath + 'btn_left.gif" width=16 height=16 alt="prev" border=0></a>';
  }
  strOutput += '<select class="cal-ComboBox" name="cboMonth" onChange="'+this.varName+'.changeMonth();">';

  for (intLoop=mm; intLoop < mm + 36; intLoop++) {
   strTemp = (intLoop == thisMonth) ? ' selected' : '';
   temp = Math.floor(intLoop / 12);
   strOutput += '<option value=' + intLoop + strTemp +'>' + msNames[intLoop % 12] + ' ' + temp + '</option>';
  }

  temp = thisMonth + 1;
  strOutput += '</select><a href="javascript:'+this.varName+'.toMonth(' + temp + ');" class="cal-DayLink"><img src="' + strDefBtnImgPath + 'btn_right.gif" width=16 height=16 alt="next" border=0></a></td>';
  strOutput += '<td align=right width="14%" class="cal-HeadCell" valign=top><a href="javascript:'+this.varName+'.hide();"><img name="calbtn2" src="'+strDefBtnImgPath+'btn_close_small.gif" border=0 width=12 height=10></a></td></tr>';
  strOutput += '<tr>';


  firstDay = new Date(intWhatYear,intWhatMonth,1);
  startDay = firstDay.getDay();

  msDays[1] = (((intWhatYear % 4 == 0) && (intWhatYear % 100 != 0)) || (intWhatYear % 400 == 0)) ? 29 : 28;

  for (intLoop=0; intLoop<7; intLoop++) {
   strOutput += '<td width="14%" align="center" valign="middle" class="cal-DowCell">'+ msDOW[intLoop] +'<\/td>';
  }

  strOutput += '</tr><tr>';

  var intColumn = 0;
  var intLastMonth = intWhatMonth - 1;
  var intLastYear = intWhatYear;

  if (intLastMonth == -1) { intLastMonth = 11; intLastYear-=1;}

  for (intLoop=0; intLoop<startDay; intLoop++, intColumn++) {
   strOutput += this.getDayLink(true,(msDays[intLastMonth]-startDay+intLoop+1),intLastMonth,intLastYear);
  }

  for (intLoop=1; intLoop<=msDays[intWhatMonth]; intLoop++, intColumn++) {
   strOutput += this.getDayLink(false,intLoop,intWhatMonth,intWhatYear);
   if (intColumn == 6) {
    strOutput += '</tr><tr>';
    intColumn = -1;
   }
  }

  var intNextMonth = intWhatMonth+1;
  var intNextYear = intWhatYear;

  if (intNextMonth==12) { intNextMonth=0; intNextYear+=1;}

  if (intColumn > 0) {
   for (intLoop=1; intColumn<7; intLoop++, intColumn++) {
    strOutput +=  this.getDayLink(true,intLoop,intNextMonth,intNextYear);
   }
  } else {
   strOutput = strOutput.substr(0,strOutput.length-4); // remove the <tr> from the end if there's no last row
  }
  strOutput += '</tr>';

  if (scNN6) {
   strOutput += '</table></form>';
  } else {
   strOutput += '</form></table>';
  }
  dteCur.setDate(1);
  dteCur.setHours(0);dteCur.setMinutes(0);dteCur.setSeconds(0);dteCur.setMilliseconds(0);

  dteCur.setMonth(intStartMonth);
  dteCur.setFullYear(intStartYear);
  return strOutput;
 }
 this.makeCalendar=makeCalendar;


 // writeControl -------------------------------------
 //
 function writeControl() {
  var strHold='';
  var strTemp='';

  if (!scIE) {
   strTemp=' href="javascript:calClick();return false;" ';
  } else {
   strTemp='';
  }
  strHold+='<a class="so-BtnLink"'+strTemp+' onclick="';

//  strHold+=this.varName+'.show();return false;">';
  strHold+=this.varName+'.show();"><img align=center border=0 name=' + this.btnName + ' src="' + strBtnImg +'" width='+ strBtnW +' height='+ strBtnH +'></a>';
  document.write(strHold);
 }
 this.writeControl=writeControl;


 // show -------------------------------------
 //
 function show() {
  var strCurSelDate = calMgr.lastSelectedDate;

  if (!this.enabled) { return }
  calMgr.hideAllCalendars(this);
  if (this.visible) {
    this.hide();
  } else {
// put these next 2 lines in when the tiny cal btns seem to randomly disappear
//    if (document.images['calbtn2']!=null ) document.images['calbtn2'].src=img_Close.src;

   // get correct position of date btn
   if ( scIE ) {
    if (this.displayLeft) {
     scX = getOffsetLeft(document.images[this.btnName])-192+ document.images[this.btnName].width ;
    } else {
     scX = getOffsetLeft(document.images[this.btnName]);
    }
    if (this.displayTop) {
     scY = getOffsetTop(document.images[this.btnName]) -138 ;
    } else {
     scY = getOffsetTop(document.images[this.btnName]) + document.images[this.btnName].height + 2;
    }
   }
   else if (scNN){
    if (this.displayLeft) {
     scX = document.images[this.btnName].x - 192+  document.images[this.btnName].width;
    } else {
     scX = document.images[this.btnName].x;
    }
    if (this.displayTop) {
     scY = document.images[this.btnName].y -134;
    } else {
     scY = document.images[this.btnName].y + document.images[this.btnName].height + 2;
    }
   }
   // hide all combos underneath it
   if (this.hideCombos) {toggleCombos('hidden');}

   // pop calendar up to the correct month and year if there's a date there
   // otherwise pop it up using today's month and year
   dteCur.setDate(dteToday.getDate());
   dteCur.setMonth(dteToday.getMonth());
   dteCur.setFullYear(dteToday.getFullYear());
   dteCur.setHours(0);dteCur.setMinutes(0);dteCur.setSeconds(0);dteCur.setMilliseconds(0);

   this.getSelectedDate();
   domlay('spiffycalendar',1,scX,scY,this.makeCalendar(dteCur.getMonth(),dteCur.getFullYear()));
   this.visible=true;
   }
 }
 this.show=show;


 // hide -------------------------------------
 //
 function hide() {
  domlay('spiffycalendar',0,scX,scY);
  this.visible = false;
  if (this.hideCombos) {toggleCombos('visible');}
 }
 this.hide=hide;


 // changeDay -------------------------------------
 //
 function changeDay(intWhatDay) {
  dteCur.setDate(intWhatDay);
  dteCur.setHours(0);dteCur.setMinutes(0);dteCur.setSeconds(0);dteCur.setMilliseconds(0);

  if (!blnInConstructor) {
    this.yearBox().value=dteCur.getFullYear();
    this.monthBox().value=dteCur.getMonth() + 1;
    this.dayBox().value=dteCur.getDate();
    this.hide();
    if (this.JStoRunOnSelect!=null)
      eval(unescape(this.JStoRunOnSelect));
  }  
 }
 this.changeDay=changeDay;


 function updateCalendar2(yyyymm) {
   dteCur.setFullYear(Math.floor(yyyymm / 12));
   dteCur.setMonth(yyyymm % 12);
   domlay('spiffycalendar',1,scX,scY,this.makeCalendar(dteCur.getMonth(),dteCur.getFullYear()));
 }
 this.updateCalendar2=updateCalendar2;


 // toMonth -------------------------------------
 //
 function toMonth(newMonth) {
    this.updateCalendar2(newMonth);
 }
 this.toMonth=toMonth;

 // changeMonth -------------------------------------
 //
 function changeMonth() {
  var idx;   
  if (scIE) {
    idx = document.forms["spiffyCal"].cboMonth.selectedIndex;
    this.updateCalendar2(document.forms["spiffyCal"].cboMonth.options[idx].value);
  } else if (scNN) {
    idx = document.spiffycalendar.document.forms["spiffyCal"].cboMonth.selectedIndex;
    this.updateCalendar2(document.spiffycalendar.document.forms["spiffyCal"].cboMonth.options[idx].value);
  }
 }
 this.changeMonth=changeMonth;


 function getDayLink(blnIsGreyDate,intLinkDay,intLinkMonth,intLinkYear) {
   var templink;
   templink='<td align="center" width="14%" class=';
   if (blnIsGreyDate) {
      templink += '"cal-GreyDate">' + intLinkDay;
   } else if (isDayToday(intLinkDay)) {
      templink += '"cal-DayCell"><a class="cal-TodayLink" onmouseover="self.status=\' \';return true" href="javascript:'+this.varName+'.changeDay(' + intLinkDay + ');">' + intLinkDay + '</a>';
   } else {
      templink += '"cal-DayCell"><a class="cal-DayLink" onmouseover="self.status=\' \';return true" href="javascript:'+this.varName+'.changeDay(' + intLinkDay + ');">' + intLinkDay + '</a>';
   }
   return templink + '</td>';
 }
 this.getDayLink=getDayLink;


 // EXTRA Private FUNCTIONS ===============================================================

 function toggleCombos(showHow){
  var i; var j;
  var cboX; var cboY;
  for (i=0;i<document.forms.length;i++) {
   for (j=0;j<document.forms[i].elements.length;j++) {
    if (document.forms[i].elements[j].tagName == "SELECT") {
     if (document.forms[i].name != "spiffyCal") {
      cboX = getOffsetLeft(document.forms[i].elements[j]);
      cboY = getOffsetTop(document.forms[i].elements[j]);
       if ( ((cboX>=scX-15) && (cboX<=scX+200)) && ((cboY>=scY-15) && (cboY<=scY+145)) )
        document.forms[i].elements[j].style.visibility=showHow;
       //Check for right hand side overlapping.
       cboX = cboX + parseInt(document.forms[i].elements[j].style.width);
       cboY=cboY+15;//cbo height (default)
       if ( ((cboX>=scX+15) && (cboX<=scX+200)) && ((cboY>=scY-15) && (cboY<=scY+145)) )
        document.forms[i].elements[j].style.visibility=showHow;
     }
    }
   }
  }
 }


 function isDayToday(intWhatDay) {
  return ((dteCur.getFullYear() == dteToday.getFullYear()) && (dteCur.getMonth() == dteToday.getMonth()) && (intWhatDay == dteToday.getDate()));
 }

 calMgr.addCalendar(this);

 blnInConstructor=false;
}



// Utility functions----------------------------------


function getOffsetLeft (el) {
 var ol = el.offsetLeft;
 while ((el = el.offsetParent) != null)
  ol += el.offsetLeft;
 return ol;
}


function getOffsetTop (el) {
 var ot = el.offsetTop;
 while((el = el.offsetParent) != null)
  ot += el.offsetTop;
 return ot;
}

function calClick() {
 window.focus();
}

function domlay(id,trigger,lax,lay,content) {
 /*
  * Cross browser Layer visibility / Placement Routine
  * Done by Chris Heilmann (mail@ichwill.net)
  * http://www.ichwill.net/mom/domlay/
  * Feel free to use with these lines included!
  * Created with help from Scott Andrews.
  * The marked part of the content change routine is taken
  * from a script by Reyn posted in the DHTML
  * Forum at Website Attraction and changed to work with
  * any layername. Cheers to that!
  * Welcome DOM-1, about time you got included... :)
  */
 // Layer visible
 if (trigger=="1"){
  if (document.layers) document.layers[''+id+''].visibility = "show"
  else if (document.all) document.all[''+id+''].style.visibility = "visible"
  else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
  }
 // Layer hidden
 else if (trigger=="0"){
  if (document.layers) document.layers[''+id+''].visibility = "hide"
  else if (document.all) document.all[''+id+''].style.visibility = "hidden"
  else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
  }
 // Set horizontal position
 if (lax){
  if (document.layers){document.layers[''+id+''].left = lax}
  else if (document.all){document.all[''+id+''].style.left=lax}
  else if (document.getElementById){document.getElementById(''+id+'').style.left=lax+"px"}
  }
 // Set vertical position
 if (lay){
  if (document.layers){document.layers[''+id+''].top = lay}
  else if (document.all){document.all[''+id+''].style.top=lay}
  else if (document.getElementById){document.getElementById(''+id+'').style.top=lay+"px"}
  }
 // change content

 if (content){
 if (document.layers){
  sprite=document.layers[''+id+''].document;
  // add father layers if needed! document.layers[''+father+'']...
  sprite.open();
  sprite.write(content);
  sprite.close();
  }
 else if (document.all) document.all[''+id+''].innerHTML = content;
 else if (document.getElementById){
  //Thanx Reyn!
  rng = document.createRange();
  el = document.getElementById(''+id+'');
  rng.setStartBefore(el);
  htmlFrag = rng.createContextualFragment(content)
  while(el.hasChildNodes()) el.removeChild(el.lastChild);
  el.appendChild(htmlFrag);
  // end of Reyn ;)
  }
 }
}


//---------------------------------------

