/*-----------------------------------------------------------------------------
** divCalendar.js:
*----------------------------------------------------------------------------*/
var calendarDate;
var calClose=false;
var calTarget;

function elePos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

function divCss() {
  var css='';
  css+='<style>\n';
  css+='.calBorder { position:absolute;border:#000 1px solid;padding:0px;background:#414149;_width:206px; }\n';
  css+='.calInner { border:#ADAACD 3px outset;padding:0px; }\n';
  css+='.calDate, .calTime { table-layout:fixed;width:200px;border-collapse:collapse; margin:0px; } \n';
  css+='.calBody { font-size:8pt;font-family:Arial,san-serif;text-align:center; }\n';
  css+='.cRow { height:18px;padding:0px;margin:0px; } \n';
  css+='</style>\n';
  return css;
}

function divOpenCalendar(o) {
  // Find the preceeding <input> element with the same parent as the button
  calTarget=o.previousSibling;
  while (calTarget && (calTarget.tagName!="INPUT")) 
    calTarget=calTarget.previousSibling;

  // Fetch it's date string or create a new date based on current datetime
  calendarDate = new Date();
  if (calTarget.value.length > 8) {
		var Months = {Jan:0,Feb:1,Mar:2,Apr:3,May:4,Jun:5,Jul:6,Aug:7,Sep:8,Oct:9,Nov:10,Dec:11};
		var curr = calTarget.value.split('-',3);
		var mon = curr[1];
		calendarDate.setDate(curr[0]);	
		calendarDate.setMonth(Months[mon],curr[0]);	
		calendarDate.setYear(curr[2]);
	}

  // Create the Calendar 
  var cal='';
  if (document.all && !window.opera) // Fix for IE 6.0 select z-index bug
    cal+='<iframe frameborder="0" width=206 height=195 style="position:absolute;"></iframe>\n';
  cal+= divCss();
  cal+='<div class="calBorder" onmouseout="divHideCalendar();">\n';
  cal+=' <div class="calInner" onmouseover="divShowCalendar();">\n';
  cal+='  <div id="calDateDiv" style="position:relative;border:#000 1px solid;border-width:1px 0 0 1px;">\n';
  cal+=     divDrawCalendar(calendarDate);
  cal+='  </div> <!-- calDateDiv -->\n';
  cal+='  <div id="calTimeDiv" style="position:relative;height:20px;padding:2px 2px 0 2px;">\n'; 
  cal+='    <div class="link" style="display:inline;float:left;height:20px;border:#56A 0px outset;text-align:center;color:#fff;text-weight:600;cursor:pointer" onclick="divClearCalendar()">&nbsp;Clear&nbsp;</div>\n';
  cal+='    <div class="link" style="display:inline;float:right;height:20px;border:#56A 0px outset;text-align:center;color:#fff;text-weight:600;cursor:pointer" onclick="divHideCalendar()">&nbsp;Close&nbsp;</div>\n';
  cal+='    <div style="clear:both;"></div>\n';
  cal+='  </div> <!-- calTimeDiv -->\n';
  cal+=' </div> <!-- inner border -->\n';
  cal+='</div> <!-- outer border -->\n';

  // Create an element in the document to put the calendar into
  posn = document.createElement('DIV');
  posn.id='calendarPopUpDiv';
  posn.style.position='relative';
  posn.style.zIndex=2147483646;  // Always on top
  posn.style.left=10+o.offsetLeft+'px';
  var postop=-10;
  var coords = elePos(o);
  if (document.body.clientHeight - coords[1] < 200)
    postop=1-(document.body.clientHeight - coords[1]);
  posn.style.top=postop+'px';
  posn.innerHTML=cal;
  o.parentNode.appendChild(posn);
}

function divDrawCalendar(dte) {
  var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

  // Dates
  year=dte.getFullYear();
  month=dte.getMonth();
  date=dte.getDate();
  day=dte.getDay();

  // Header
  var table="";
  table+='<table class="calDate" border="1" bordercolor="black">\n';
  table+='  <tbody class="calBody">\n';
  table+='  <tr style="height:18px">\n';
  table+='    <td class="cRow"><img src="/images/arrowdblleftss.gif" title="Back 10 years" onclick="divSetYear(-10)"></td>\n';
  table+='    <td class="cRow"><img src="/images/arrowleftss.gif" title="Back 1 year" onclick="divSetYear(-1)"></td>\n';
  table+='    <td class="cRow" id="tdYear" style="color:white" colspan="3">'+year+'</td>\n';
  table+='    <td class="cRow"> <img src="/images/arrowrightss.gif" title="Forward 1 year"  onclick="divSetYear(1)""></td>\n';
  table+='    <td class="cRow"><img src="/images/arrowdblrightss.gif" title="Forward 10 years" onclick="divSetYear(10)"></td>\n';
  table+='  </tr>\n';
  table+='  <tr style="height:18px">\n';
  table+='    <td class="cRow"><img src="/images/arrowdblleftss.gif" title="Back 4 months"  onclick="divSetMonth(-4)"></td>\n';
  table+='    <td class="cRow"><img src="/images/arrowleftss.gif" title="Back 1 month" onclick="divSetMonth(-1)"></td>\n';
  table+='    <td class="cRow"id="tdMonth"  style="color:white" colspan="3">'+monthNames[month]+'</td>\n';
  table+='    <td class="cRow"><img src="/images/arrowrightss.gif" title="Forward 1 month" onclick="divSetMonth(1)"></td>\n';
  table+='    <td class="cRow"><img src="/images/arrowdblrightss.gif" title="Forward 4 months" onclick="divSetMonth(4)"></td>\n';
  table+='  </tr>\n';
  table+='  <tr style="height:18px;color:#fff">\n';
  table+='    <td class="cRow">Su</td>\n';
  table+='    <td class="cRow">Mo</td>\n';
  table+='    <td class="cRow">Tu</td>\n';
  table+='    <td class="cRow">We</td>\n';
  table+='    <td class="cRow">Th</td>\n';
  table+='    <td class="cRow">Fr</td>\n';
  table+='    <td class="cRow">Sa</td>\n';
  table+='  </tr>\n';

  daysInMonth=[31,((!(year%4)&&((year%100)||!(year%400)))?29:28),31,30,31,30,31,31,30,31,30,31][month];
  startDay= new Date(year,month,1).getDay();
  for (var i=0;i<6;i++) {
    table+='  <tr style="height:18px;">\n';
    for (var j=1;j<=7;j++) {
      var cell=(i*7)+j-startDay;
      if (cell<1 || cell>daysInMonth) {
        cell="&nbsp;";
        args='';
      }
      else {
        args=' onmouseover="divHilite(event);" onmouseout="divHilite(event);" onclick="divSetDay(this)" style="cursor:pointer;';
        if (cell==dte.getDate())
          args+='background:#FF88FF;color:#000;';
        args+='"';
      }
      table+='    <td class="cRow"'+args+'>'+cell+'</td> \n';
    }
    table+='  </tr>\n';
  }
  table+='  </tbody>\n</table>\n';
  return table;
}

function divSetDay(o) {
  // Clear the old day (if any)
  tds=o.parentNode.parentNode.getElementsByTagName("td");
  for(var i=0;i<tds.length;i++)
    tds[i].style.backgroundColor="transparent";

  // Set new day
  calendarDate.setDate(o.firstChild.nodeValue);
  o.style.backgroundColor="#FF99FF";
  o.setAttribute("oldbg","#FF66FF");
  divReturnDate();
}

function divSetMonth(amount) { 
  year=calendarDate.getFullYear();
  month=calendarDate.getMonth();
  month+=parseInt(amount);
  if (month > 11) {
    year++;
    month-=12;
  } else if (month < 0) {
    year--;
    month+=12;
  }
  calendarDate.setFullYear(year);
  calendarDate.setMonth(month);
  document.getElementById('calDateDiv').innerHTML=divDrawCalendar(calendarDate);
}

function divSetYear(amount) {
  year=calendarDate.getFullYear();
  year+=parseInt(amount);
  calendarDate.setFullYear(year);
  document.getElementById('calDateDiv').innerHTML=divDrawCalendar(calendarDate);
}

function divReturnDate() {
  // Set the target input field's value to calendarDate
  var monthNames=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  calTarget.value=calendarDate.getDate()+'-'+monthNames[calendarDate.getMonth()]+'-'+calendarDate.getFullYear();

  // Close the calendar  
  calClose=true;
  divCloseCalendar();
}

function divShowCalendar() {
  calClose=false;
}

function divClearCalendar() {
  calTarget.value=' ';
  calendarDate = new Date();
}

function divHideCalendar() {
  calClose=true;
  setTimeout(divCloseCalendar,800);
}

function divCloseCalendar() {
  if (calClose) {
    var cal=document.getElementById("calendarPopUpDiv");
    if (cal)
      cal.parentNode.removeChild(cal);
  }
}

function divHilite(e) {
  if (!e)
    var e=window.event;
  var o=e.srcElement||e.target;
  switch (e.type) {
    case "mouseover" :
      o.setAttribute("oldbg",o.style.backgroundColor);
      o.setAttribute("oldfg",o.style.color);
      o.style.backgroundColor="#FFDD44";
      o.style.color="#000000";
     break;
    case "mouseout" :
      o.style.backgroundColor=o.getAttribute("oldbg");
      o.style.color=o.getAttribute("oldfg");
     break;
  }
}
