/*
----------- FundSys.net(TM) Copyright SySys(R) Corp 2002 ------------
====================================================================
  Created By: CG
  Last Edited By: CG
  Inception Date: 2/14/2002
  Last Edited Date: 2/14/2002
  Description: Calendar object 
  Dependencies: Manage_dhtml_layer.js
                Manage_date_time.js
                Manage_form.js
===================================================================''*/
/*'''---------------------------------
'' Calendar(element, name, defaultDate, dateList, ShowQuarterLinks)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE
   Description: Creates a Calendar object 
   Parameters:
     element: [Object] is the form element the object should set upon selection
     name: [String] is the unique identifying name that will be use to create layers and forms 
     defaultDate: [String] sets the default month and Year the calendar will start at if blank will default to  the current Date.
     dateList: [String] a comma separated list of dates that should show up highlighted  
     ShowQuarterLinks: [Boolean] to show the Quarter Links above the close button
     
----------------------------------'''*/ 
function Calendar(element, name, defaultDate, dateList, ShowQuarterLinks){
  this.defaultDate = defaultDate; 
  this.element = element; 
  this.name = name; 
  if(defaultDate != '')//If no default date use system date
    this.pDefaultDate = new Date(defaultDate);
  else
    this.pDefaultDate = new Date();
  this.dateList = dateList; 
  this.pBaseYear = this.pDefaultDate.getYear(); //sets the default year to be selected
  if(this.pBaseYear < 1000) this.pBaseYear += 1900; //adds 1900 for NS6
  this.pBaseMonth = this.pDefaultDate.getMonth() + 1; //sets the default month to be selected  (Months are 0 - 11 0 = Jan, 11 = Dec --- so Add 1)
  this.pBaseDay = this.pDefaultDate.getDate(); //sets the default day to be selected  
  this.pOldDate = defaultDate; //Holds the defaultDate passed to the Function  
  this.ShowQuarterLinks = ShowQuarterLinks; 
    
  //defines the field that the calendar writes to (can be a hidden field)
  this.dateField = element;
    
  //Write Calendar DIV/Layer to page use the unique name 
  
  //normally use this line but the select areas for month and year cause the mouseleave event to be fired
  //document.write("<div id='div_calendar_" + this.name + "' style='width:158; height:102; display:none; position:absolute;' onmouseleave=\"setDelayTimeout('collapseDIV(\\\'div_calendar_" + this.name + "\\\')',200);\">");
  document.write("<div id='div_calendar_" + this.name + "' style='width:158; height:102; display:none; position:absolute; z-index:100'>");
  document.write("<layer name='layer_calendar_" + this.name + "' z-index='100' bgcolor='#FFFFFF'>");
  document.write("  <form name='frm" + this.name + "' id='frm" + this.name + " method='post' action=''>");
  document.write("      <table border='0' class='cal_background' width='1%'>");
  document.write("        <tr>");
  document.write("          <td align='center' class='cal_top' nowrap>");
  document.write("          <a class='fc_calcs' href='javascript:decrementMonth(obj" + this.name + ");'>&lt;</a>"); 
  document.write("          <select id='" + this.name + "tbSelMonth' name='" + this.name + "tbSelMonth' onchange=\"forceSelectedDate('document.getElementById(" + this.name + "tbSelMonth).value', '', '',true,obj" + this.name + ");\">");
  if (this.pBaseMonth == 1) document.write("          <option value='1' selected>January</option>");
  else  document.write("          <option value='1'>January</option>");
  if (this.pBaseMonth == 2) document.write("          <option value='2' selected>February</option>");
  else  document.write("          <option value='2'>February</option>");
  if (this.pBaseMonth == 3) document.write("          <option value='3' selected>March</option>");
  else  document.write("          <option value='3'>March</option>");
  if (this.pBaseMonth == 4) document.write("          <option value='4' selected>April</option>");
  else  document.write("          <option value='4'>April</option>");
  if (this.pBaseMonth == 5) document.write("          <option value='5' selected>May</option>");
  else  document.write("          <option value='5'>May</option>");
  if (this.pBaseMonth == 6) document.write("          <option value='6' selected>June</option>");
  else  document.write("          <option value='6'>June</option>");
  if (this.pBaseMonth == 7) document.write("          <option value='7 selected'>July</option>");
  else  document.write("          <option value='7'>July</option>");
  if (this.pBaseMonth == 8) document.write("          <option value='8' selected>August</option>");
  else  document.write("          <option value='8'>August</option>");
  if (this.pBaseMonth == 9) document.write("          <option value='9' selected>September</option>");
  else  document.write("          <option value='9'>September</option>");
  if (this.pBaseMonth == 10) document.write("          <option value='10' selected>October</option>");
  else  document.write("          <option value='10'>October</option>");
  if (this.pBaseMonth == 11) document.write("          <option value='11' selected>November</option>");
  else  document.write("          <option value='11'>November</option>");
  if (this.pBaseMonth == 12) document.write("          <option value='12' selected>December</option>");
  else  document.write("          <option value='12'>December</option>");
  document.write("          </select>");
  document.write("          <a class='fc_calcs' href='javascript:incrementMonth(obj" + this.name + ");'>&gt;</a>");
  document.write("          &nbsp;");
  document.write("          <a class='fc_calcs' href='javascript:decrementYear(obj" + this.name + ");'>&lt;</a>");  
  document.write("          <select id='" + this.name + "tbSelYear' name='" + this.name + "tbSelYear' onchange=\"forceSelectedDate('', '', 'document.getElementById(" + this.name + "tbSelYear).value',true,obj" + this.name + ");\">");
  document.write("          </select>");
  document.write("          <a class='fc_calcs' href='javascript:incrementYear(obj" + this.name + ");'>&gt;</a>");
  document.write("          </td>");
  document.write("        </tr>");
  document.write("        <tr>");
  document.write("          <td>");
  var myMonth;
  myMonth = fBuildCal(this.pBaseYear, this.pBaseMonth, 1); // Call fBuildCal Function to build day and month arrays
  document.write("<table border='0' cellpaddng='2'>")
  document.write("  <tr>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][0] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][1] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][2] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][3] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][4] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][5] + "</td>");
  document.write("    <td align='center' class='cal_daytitles'>" + myMonth[0][6] + "</td>");
  document.write("  </tr>");
  for (w = 1; w < 7; w++) {
    document.write("<tr>")
    for (d = 0; d < 7; d++) {
      document.write("<td align='left' valign='top' width='30' height='20' id='calCell" + w + d + "' class='cal_days' onMouseOver='fToggleColor(this, obj" + this.name + ");' onMouseOut='fToggleColor(this, obj" + this.name + ");' onclick='fSetSelectedDay(this, obj" + this.name + ");'>");
      if (!isNaN(myMonth[w][d])) {
        document.write("<div id='" + this.name + "calDateText" + w + d +"' style='cursor:pointer;cursor:hand' onMouseOver='fToggleColor(this, obj" + this.name + ");' onMouseOut='fToggleColor(this, obj" + this.name + ");' onclick='fSetSelectedDay(this, obj" + this.name + ");'>" + myMonth[w][d] + "</div>");
      } else {
        document.write("<div id='" + this.name + "calDateText" + w + d +"' onMouseOver='fToggleColor(this, obj" + this.name + ");' onMouseOut='fToggleColor(this, obj" + this.name + ");' onclick='fSetSelectedDay(this, obj" + this.name + ");'>&nbsp;</div>");
      }
      document.write("</td>")
    }
    document.write("</tr>");
  }
  document.write("</table>")
  document.write("          </td>");
  document.write("        </tr>");
  document.write("        <tr>");
  document.write("          <td>");
  document.write("            <table border='0' width='100%' cellpadding=1>");
  document.write("              <tr>");
  document.write("                <td align='center' class='cal_bottom'>");
  if(this.ShowQuarterLinks){
    document.write("                <a href=\"javascript:forceSelectedDate('3','31','',false,obj" + this.name + ");\">[Q1]</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:forceSelectedDate('6','30','',false,obj" + this.name + ");\">[Q2]</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:forceSelectedDate('9','30','',false,obj" + this.name + ");\">[Q3]</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:forceSelectedDate('12','31','',false,obj" + this.name + ");\">[Q4]</a>");
    document.write("                <br>");    
  }
  document.write("                <a href=\"javascript:toggleLayerDisplay('div_calendar_" + this.name + "');\">[close]</a>");
  document.write("                </td>");
  document.write("              </tr>");
  document.write("            </table>");
  document.write("          </td>");  
  document.write("        </tr>");
  document.write("      </table>");
  document.write("  </form>");
  document.write("</layer>");
  document.write("</div>");  
  fUpdateCal(this);
}//End Calendar Function
/*'''---------------------------------
'' DebugCalendar(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Writes Debug values to the screen when a calendar object is first built
   Parameters:
     calObject: Calendar Object
----------------------------------'''*/  
function DebugCalendar(calObject){
  var debug = '';
  debug +=  '<br><br><b>DEBUG:</b><hr color="ff0000">'
  debug += '<i>Calendar object:</i> obj' + calObject.name + '<br>';
  debug += '<i>Parameters:</i><br>';
  debug += '&nbsp;&nbsp;element = >>' + calObject.element.name + '<<<br>';
  debug += '&nbsp;&nbsp;name = >>' + calObject.name + '<<<br>';    
  debug += '&nbsp;&nbsp;defaultDate = >>' + calObject.defaultDate + '<<<br>';
  debug += '&nbsp;&nbsp;dateList = >>' + calObject.dateList + '<< <br>';
  debug += 'baseYear = ' + calObject.pBaseYear + '<br>';
  debug += 'baseMonth = ' + calObject.pBaseMonth + '<br>';
  debug += 'baseDay = ' + calObject.pBaseDay + '<br>';
  debug += 'OldDate = ' + calObject.pOldDate + '<br>';
  debug += 'dateField = ' + calObject.dateField.value + '<br>';
  debug += 'Year Drop Down Text and Values: <br>';
  //for(var i=0; i <= 10; i++){
  //  debug += '&nbsp;&nbsp&nbsp;&nbsp;Text = ' + eval('document.getElementById("' + calObject.name + 'tbSelYear").options[' + i + '].text') + '<br>';
  //  debug += '&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;Value = ' + eval('document.getElementById("' + calObject.name + 'tbSelYear").options[i].value') + '<br>';
  //}    
  debug += '<hr color="ff0000">'; 
  document.getElementById("calTestBlock").innerHTML = debug;
  return debug;
}//End DebugCalendar Function

/*---------------------------------
 populateYears(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/   
function populateYears(calObject){  //Show 5 years in each direction from current year
  var calObjYear = getElementObject(calObject.name + 'tbSelYear');
  calObjYear.options.length = 11;
  for(var i=0; i <= 10; i++){
    calObjYear.options[i].value = calObject.pBaseYear - 6 + i + 1;
    calObjYear.options[i].text = calObject.pBaseYear - 6 + i + 1;
    //select the current year
    if(calObject.pBaseYear == (calObject.pBaseYear - 6 + i + 1)){
      calObjYear.options[i].selected = true;
    }
  }
}// End populateYears

/*---------------------------------
 fBuildCal(iYear, iMonth, iDayStyle)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
   Dependencies: Manage_date_time.js:fGetDaysInMonth
----------------------------------*/    
function fBuildCal(iYear, iMonth, iDayStyle) {
  var aMonth = new Array();
  aMonth[0] = new Array(7);
  aMonth[1] = new Array(7);
  aMonth[2] = new Array(7);
  aMonth[3] = new Array(7);
  aMonth[4] = new Array(7);
  aMonth[5] = new Array(7);
  aMonth[6] = new Array(7);
  var dCalDate = new Date(iYear, iMonth-1, 1);
  var iDayOfFirst = dCalDate.getDay();
  var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
  var iVarDate = 1;
  var i, d, w;
  if (iDayStyle == 2) {
    aMonth[0][0] = "Sunday";
    aMonth[0][1] = "Monday";
    aMonth[0][2] = "Tuesday";
    aMonth[0][3] = "Wednesday";
    aMonth[0][4] = "Thursday";
    aMonth[0][5] = "Friday";
    aMonth[0][6] = "Saturday";
  } else if (iDayStyle == 1) {
    aMonth[0][0] = "Sun";
    aMonth[0][1] = "Mon";
    aMonth[0][2] = "Tue";
    aMonth[0][3] = "Wed";
    aMonth[0][4] = "Thu";
    aMonth[0][5] = "Fri";
    aMonth[0][6] = "Sat";
  } else {
    aMonth[0][0] = "Su";
    aMonth[0][1] = "Mo";
    aMonth[0][2] = "Tu";
    aMonth[0][3] = "We";
    aMonth[0][4] = "Th";
    aMonth[0][5] = "Fr";
    aMonth[0][6] = "Sa";
  }
  for (d = iDayOfFirst; d < 7; d++) {
    aMonth[1][d] = iVarDate;
    iVarDate++;
  }
  for (w = 2; w < 7; w++) {
    for (d = 0; d < 7; d++) {
      if (iVarDate <= iDaysInMonth) {
        aMonth[w][d] = iVarDate;
        iVarDate++;
      }
    }
  }
  return aMonth;
}// End fBuildCal
selectMonth
/*---------------------------------
 selectMonth(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/  
function selectMonth(calObject){
   var calObjMonth = getElementObject(calObject.name + 'tbSelMonth');
   calObjMonth.selectedIndex = calObject.pBaseMonth - 1;
}// End selectMonth

/*---------------------------------
 decrementMonth(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/  
function decrementMonth(calObject){
  var calObjMonth = getElementObject(calObject.name + 'tbSelMonth');
  if(calObjMonth.selectedIndex == 0){
    calObjMonth.selectedIndex = calObjMonth.length - 1;
    calObject.pBaseMonth = calObjMonth.selectedIndex + 1;
    decrementYear(calObject);
  }else{
    calObjMonth.selectedIndex -= 1;
    calObject.pBaseMonth = calObjMonth.selectedIndex + 1;
  }
  fUpdateCal(calObject);
}// End decrementMonth
/*---------------------------------
 incrementMonth(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/   
function incrementMonth(calObject){
  var calObjMonth = getElementObject(calObject.name + 'tbSelMonth');
  if(calObjMonth.selectedIndex == calObjMonth.length - 1){
    calObjMonth.selectedIndex = 0;
    calObject.pBaseMonth = calObjMonth.selectedIndex + 1;
    incrementYear(calObject);
  }else{
    calObjMonth.selectedIndex += 1;
    calObject.pBaseMonth = calObjMonth.selectedIndex + 1;
  }
  fUpdateCal(calObject);
}// End incrementMonth
/*---------------------------------
 decrementYear(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/ 
function decrementYear(calObject){
  var calObjYear = getElementObject(calObject.name + 'tbSelYear');
  calObjYear.selectedIndex -= 1;
  calObject.pBaseYear = calObjYear.options[calObjYear.selectedIndex].value;
  fUpdateCal(calObject);
}// End decrementYear
/*---------------------------------
 incrementYear(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/   
function incrementYear(calObject){
  var calObjYear = getElementObject(calObject.name + 'tbSelYear');
  calObjYear.selectedIndex += 1;
  calObject.pBaseYear = calObjYear.options[calObjYear.selectedIndex].value;
  fUpdateCal(calObject);
}// End incrementYear
/*---------------------------------
 Global Variables 
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: For Calendar Object
----------------------------------*/   
var objPrevElement = new Object();
var DayProperties = new Array(); 
/*---------------------------------
 fUpdateCal(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
   Dependencies: Manage_date_time.js:dateInList
----------------------------------*/    
function fUpdateCal(calObject) {
//alert('pBaseYear: ' + calObject.pBaseYear + '\npBaseMonth: ' + calObject.pBaseMonth);
  myMonth = fBuildCal(calObject.pBaseYear, calObject.pBaseMonth);
  objPrevElement.bgColor = "";
  var tmpObj = "";
  for (w = 1; w < 7; w++) {
    for (d = 0; d < 7; d++) {
      if (!isNaN(myMonth[w][d])){
        tmpObj = getElementObject(calObject.name + 'calDateText' + w + d);
        tmpObj.innerHTML = myMonth[w][d];
        //Indicate the dates that have data with blue
        if(dateInList((calObject.pBaseMonth + '/' + myMonth[w][d] + '/' + calObject.pBaseYear), calObject.dateList)){
          tmpObj.style.color = 'blue';
        }else{
          tmpObj.style.color = 'black';
        }
        //store the current color for toggle
        eval('DayProperties[myMonth[w][d]] = tmpObj.style.color');
      } else {
        tmpObj = getElementObject(calObject.name + 'calDateText' + w + d);
        tmpObj.innerHTML = '&nbsp;';
      }
    }
  }
  populateYears(calObject);//Call populateYears to fill the select drop down with the apropriate year ranges  
  selectMonth(calObject);//Call to change what month is selected in drop down
}// End fUpdateCal
/*---------------------------------
 fToggleColor(myElement, calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/   
function fToggleColor(myElement, calObject) {
  var toggleColor = "#ff0000";
  var tmpObj = ''; 
  if (eval('myElement.id == "' + calObject.name + 'calDateText"')) {
    if (myElement.color == toggleColor) {
      myElement.color = "";
    }else{
      myElement.color = toggleColor;
    }
  }else if (myElement.id == "calCell"){
    for (var i in myElement.children){
      if (eval('myElement.children[i].id == "' + calObject.name + 'calDateText"')){
        if (myElement.children[i].color == toggleColor){
          myElement.children[i].color = DayProperties[myElement.innerText]; //"";
        }else{
          myElement.children[i].color = toggleColor;
        }
      }
    }
  }
}// End fToggleColor
/*---------------------------------
 fSetSelectedDay(myElement, calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
   Dependencies: Manage_dhtml_layer.js:toggleLayerDisplay
   Dependencies: Manage_dhtml_layer.js:toggleLayerDisplay
----------------------------------*/    
function fSetSelectedDay(myElement, calObject){
  var tmp = getElementObject(myElement.id);
  if (tmp.id.substr(0,7) != "calCell") {
    if(!isNaN(parseInt(tmp.innerHTML))) {
      tmp.style.bgColor = "#c0c0c0";
        
      objPrevElement.bgColor = "#EFEFEF";
      calObject.pBaseDay = parseInt(tmp.innerHTML);
      objPrevElement = myElement;
      //push the date to the given form element
      eval('obj' + calObject.name + '.dateField.value = getSelectedDate(calObject);');
      var event;
      //Execute onChange event if exists
      event = eval('obj' + calObject.name + '.element.onchange');
      if(event != undefined){
        var newEvent = event.toString();
        newEvent = newEvent.substring(newEvent.indexOf('{') + 1,newEvent.indexOf('}')-1);
        newEvent = newEvent.replace('this.form.name',"'" + eval('obj' + calObject.name + '.element.form.name') + "'");
        eval(newEvent); 
      }  
      tmp = getElementObject('div_calendar_' + calObject.name);
      toggleLayerDisplay(tmp.id);
    }
  }
}// End fSetSelectedDay
/*---------------------------------
 forceSelectedDate(month,day,year,UpdateCal,calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
   Dependencies: Manage_dhtml_layer.js:toggleLayerDisplay
----------------------------------*/       
function forceSelectedDate(month,day,year,UpdateCal,calObject){
  var calObjMonth = getElementObject(calObject.name + 'tbSelMonth');
  var calObjYear = getElementObject(calObject.name + 'tbSelYear');
  if(year == '') year = eval('obj' + calObject.name + '.pBaseYear');
  if(month == '') month = eval('obj' + calObject.name + '.pBaseMonth');
  if(day == '') day = eval('obj' + calObject.name + '.pBaseDay');
  if(UpdateCal == true){
    calObject.pBaseMonth = calObjMonth.options[calObjMonth.selectedIndex].value;
    calObject.pBaseYear = calObjYear.options[calObjYear.selectedIndex].value;
    fUpdateCal(calObject);
  }else{
    //push the date to the given form element
    eval('obj' + calObject.name + '.dateField.value = \'' + month + '/' + day + '/' + year + '\'');
    calObject.pBaseMonth = month;
    calObject.pBaseYear = year;
    fUpdateCal(calObject);
    eval('toggleLayerDisplay(\'div_calendar_' + calObject.name + '\')');
  }    
}// End forceSelectedDate
/*---------------------------------
 getSelectedDate(calObject)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE6
   Description: Private Function for Calendar Object
----------------------------------*/ 
function getSelectedDate(calObject){
  return calObject.pBaseMonth + '/' + calObject.pBaseDay + '/' + calObject.pBaseYear;
}// End getSelectedDate

/*'''---------------------------------
'' setCalendarDate(calObject, newDate)
--------------------------------------
   Created By: SySys:cg
   Compatibility: IE
   Description: Can be used to update the starting month and year for an already created calendar object
   Parameters:
     calObject: Calendar Object
     newDate: New Date String
----------------------------------'''*/ 
function setCalendarDate(calObject, newDate){
  if (newDate != ''){
    var pDate = new Date(newDate);
    var month = pDate.getMonth() + 1;
    var year = pDate.getFullYear();
    calObject.pBaseMonth = month;
    calObject.pBaseYear = year;
    fUpdateCal(calObject);
  }
}// End setCalendarDate
 
