//<SCRIPT> // <- dummy script tag used to enable InterDev syntax coloring
//------------------------------------------------------------------------
// File:    imkdatepicker.js
// Author:  Al Yanchak
//
// Copyright© 2002, i-MARK Inc. All Rights Reserved
//
// WARNING: The information in this file is protected by copyright law
// and international treaty provisions. Unauthorized reproduction or
// distribution of this file, or any portion of it, may result in severe
// criminal and civil penalties, and will be prosecuted to the maximum
// extent possible under the law.  Further, you may not reverse engineer,
// decompile, or disassemble the file.
//------------------------------------------------------------------------

var theCalendarWindow;
var theCalendar;
var theCalendarStyle =  "body {background-color: #ffffff; color: #000000; font-family: Arial; font-size: 10pt;} " +
                        "td {background-color: #ffffff; color: #000000; font-family: Arial; font-size: 10pt;} " +
                        ".calNav a {color: #000000; text-decoration: none;} " +
                        ".calHdr {color: #000000; font-family: Arial; font-size: 10pt; font-weight: bold;} " +
                        ".calGrid {background-color: #c0c0c0;} " +
                        ".calWeekend {background-color: #d3d3d3; color: #000000; font-family: Arial; font-size: 10pt;} " +
                        ".calWeekend a {color: #000000; text-decoration: none;} " +
                        ".calWeekend a:hover {color: #ff0000; font-weight: bold; text-decoration: underline;} " +
                        ".calWeekday {background-color: #ffffff; color: #000000; font-family: Arial; font-size: 10pt;} " +
                        ".calWeekday a {color: #000000; text-decoration: none;} " +
                        ".calWeekday a:hover {color: #ff0000; font-weight: bold; text-decoration: underline;}";


function BuildDatePicker(month, year, returnObject)
{
  var oldCalendar = null;
  if(typeof(theCalendar) != "undefined")
    oldCalendar = theCalendar;
  
	theCalendar = new imkDatePicker(month, year, returnObject);
	theCalendar.show();
	
	// set selection may be overriden to provide custom behavior,
	// reset the pointer here when switching throuhg months
	if(oldCalendar)
	  theCalendar.setSelection = oldCalendar.setSelection;
}

function imkShowDatePicker(returnObject, evt)
{
	var width  = 250;
	var height = 200;
	var xpos = evt.screenX + 50;	
	var ypos = evt.screenY;
	
	var xoverrun = (xpos + width + 50) - screen.width;
	if(xoverrun > 0)
	  xpos -= (xoverrun);
	  
	var yoverrun = (ypos + width + 50) - screen.height;
	if(yoverrun > 0)
	  ypos -= (yoverrun);

	theCalendarWindow = window.open("", "Calendar", "width=" + width + 
	                                    ",height=" + height + ",top=" + ypos +
	                                    ",left=" + xpos +",status=no,resizable=no");
	theCalendarWindow.opener = self;
	theCalendarWindow.focus();

  var d = new Date();
	BuildDatePicker(d.getMonth(), d.getFullYear(), returnObject);
}


function imkDatePicker(month, year, returnObject)
{
  // public methods
  this.show             = imkDatePicker_show;
  this.setSelection     = imkDatePicker_setSelection;
  
  // private methods
  this._getDaysInMonth  = imkDatePicker_getDaysInMonth;
  this._getMonthName    = imkDatePicker_getMonthName;
  this._writeMonth      = imkDatePicker_writeMonth;
  this._writeDay        = imkDatePicker_writeDay;
  
  // private data
  this._doc         = theCalendarWindow.document;
  this._month       = month;
  this._year        = year;
  this._retObject   = returnObject;
  this._monthNames  = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  this._monthDays   = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  this._style       = theCalendarStyle; 
}

function imkDatePicker_show()
{
  var nextMonth = this._month + 1;
  var nextYear  = this._year;
  if(nextMonth == 12)
  { 
    nextMonth = 0; 
    nextYear++;
  }
  
  var prevMonth = this._month - 1;
  var prevYear  = this._year;
  if(prevMonth == -1)
  { 
    prevMonth = 11; 
    prevYear--;
  }

  this._doc.open();
  
  this._doc.write("<html>");
  this._doc.write("<head>");
  this._doc.write("<title>Calendar</title>");
  this._doc.write("<style type='text/css'>" + this._style + "</style>");
	this._doc.write("</head>");
	
	this._doc.write("<body>");

  // navigation buttons
	this._doc.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
	this._doc.write("<tr><td align='left' class='calNav'>[<a href=\"" +
		              "javascript:window.opener.BuildDatePicker(" + prevMonth + ", " + 
		               prevYear + ", '" + this._retObject + "');\">&lt;</a>]</td>");
	this._doc.write("<td align='center' class='calNav'><b>" +
	                this._getMonthName(this._month) + 
	                "&nbsp;" +
	                this._year +
	                "</b></td>");
  this._doc.write("<td align='right' class='calNav'>[<a href=\"" +
		              "javascript:window.opener.BuildDatePicker(" + nextMonth + ", " + 
		              nextYear + ", '" + this._retObject + "');\">&gt;</a>]</td>");
  this._doc.write("</td></tr></table><br />");		              
  
  // the calendar
  this._writeMonth(this._month, this._year);
  
  this._doc.write("</body></html>");
	this._doc.close();
}

function imkDatePicker_setSelection(date)
{
  var obj = eval("document." + this._retObject);
  obj.value = "" + (this._month + 1) + "/" + date + "/" + this._year;
}

function imkDatePicker_getDaysInMonth(month, year)
{
  // check for leap year
  if(month == 1 && (year % 4) == 0)
  {
    if((year % 100) == 0 && (year % 400) != 0)
	    return this._monthDays[month];
	  else
	    return this._monthDays[month] + 1;
  }
  else
  {
		return this._monthDays[month];
	}
}

function imkDatePicker_getMonthName(month)
{
  return this._monthNames[month];
}

function imkDatePicker_writeMonth(month, year)
{
  this._doc.write("<table border='0' width='100%' cellspacing='0' cellpadding='0'>" +
                  "<tr><td class='calGrid'>");
  this._doc.write("<table border='0' width='100%' cellspacing='1' cellpadding='0'>");
  this._doc.write("<tr>" +
                  "<td class='calHdr' width='15%' align='center'>Sun</td>" +
                  "<td class='calHdr' width='14%' align='center'>Mon</td>" +
                  "<td class='calHdr' width='14%' align='center'>Tue</td>" +
                  "<td class='calHdr' width='14%' align='center'>Wed</td>" +
                  "<td class='calHdr' width='14%' align='center'>Thu</td>" +
                  "<td class='calHdr' width='14%' align='center'>Fri</td>" +
                  "<td class='calHdr' width='15%' align='center'>Sat</td>" +
                  "</tr>");
                  
  var d = new Date();
  d.setDate(1);
  d.setMonth(this._month);
  d.setYear(this._year);
  
  var firstDay = d.getDay();
  var daysInMonth = this._getDaysInMonth(this._month, this._year)                 
  var date = 1;
  while(date <= daysInMonth)
  {
    this._doc.write("<tr>");
    for(var day = 0; day < 7; day++)
    {
      if((day < firstDay && date == 1) || date > daysInMonth)
      {
        this._writeDay(day, 0)
      }
      else
      {
        this._writeDay(day, date);
        date++
      }
    }
    this._doc.write("</tr>");
  }
  this._doc.write("</table></td></td></table>");
}

function imkDatePicker_writeDay(day, date)
{
  var str_dateLink = "<a href='#' onclick='self.opener.theCalendar.setSelection(" + 
                      date + "); self.close();'>" + date + "</a>";

  this._doc.write("<td class='" +
                  ((day == 0 || day == 6) ? "calWeekend" : "calWeekday") +
                  "' align='center'>" +
                  ((date != 0) ? str_dateLink : "&nbsp;") +
                  "</td>");
}
//Used for Time Picker
  var imagePath='/images/';
  
  var ie=document.all;
  var dom=document.getElementById;
  var ns4=document.layers;
  var bShow=false;
  var textCtl;

  function setTimePicker(t) {
    textCtl.value=t;
    closeTimePicker();
  }

  function refreshTimePicker(mode) {
    
    if (mode==0)
      { 
        suffix="AM";
        sHTML = "<table><tr><td><table cellpadding=3 cellspacing=0 bgcolor='#f0f0f0'>";
        for (i=8;i<12;i++) {

          sHTML+="<tr align=right style='font-family:verdana;font-size:11px;color:#000000;'>";

          if (i==0) {
            hr = 12;
          }
          else {
            hr=i;
          }  

          for (j=0;j<4;j++) {
            sHTML+="<td width=57 style='cursor:hand;font-family:verdana;font-size:11px;' onmouseover='this.style.backgroundColor=\"#66CCFF\"' onmouseout='this.style.backgroundColor=\"\"' onclick='setTimePicker(\""+
            hr + ":" + padZero(j*15) + "&nbsp;" + suffix 
            + "\")'><a style='text-decoration:none;color:#000000' href='javascript:setTimePicker(\""+ hr + ":" + padZero(j*15) + "&nbsp;" + suffix + 
            "\")'>" + hr + ":"+padZero(j*15) +"&nbsp;"+ "<font color=\"#808080\">" + suffix + "&nbsp;</font></a></td>";
          }

          sHTML+="</tr>";
        }         
      }
    else
      { 
        suffix="PM"; 
        sHTML = "<table><tr><td><table cellpadding=3 cellspacing=0 bgcolor='#f0f0f0'>";
        for (i=0;i<5;i++) {

          sHTML+="<tr align=right style='font-family:verdana;font-size:11px;color:#000000;'>";

          if (i==0) {
            hr = 12;
          }
          else {
            hr=i;
          }  

          for (j=0;j<4;j++) {
            sHTML+="<td width=57 style='cursor:hand;font-family:verdana;font-size:11px;' onmouseover='this.style.backgroundColor=\"#66CCFF\"' onmouseout='this.style.backgroundColor=\"\"' onclick='setTimePicker(\""+
            hr + ":" + padZero(j*15) + "&nbsp;" + suffix 
            + "\")'><a style='text-decoration:none;color:#000000' href='javascript:setTimePicker(\""+ hr + ":" + padZero(j*15) + "&nbsp;" + suffix + 
            "\")'>" + hr + ":"+padZero(j*15) +"&nbsp;"+ "<font color=\"#808080\">" + suffix + "&nbsp;</font></a></td>";
          }

          sHTML+="</tr>";
        }        
      }


    sHTML += "</table></td></tr></table>";
    document.getElementById("timePickerContent").innerHTML = sHTML;
  }

  if (dom){
    document.write ("<div id='timepicker' style='z-index:9;position:absolute;visibility:hidden;'><table style='border-width:3px;border-style:solid;border-color:#0033AA' bgcolor='#ffffff' cellpadding=0><tr bgcolor='#0033AA'><td><table cellpadding=0 cellspacing=0 width='100%' background='" + imagePath + "titleback.gif'><tr valign=bottom height=21><td style='font-family:verdana;font-size:11px;color:#ffffff;padding:3px' valign=center><B>&nbsp;Select&nbsp;Time </B></td><td><img id='iconAM' src='" + imagePath + "am1.gif' onclick='document.getElementById(\"iconAM\").src=\"" + imagePath + "am1.gif\";document.getElementById(\"iconPM\").src=\"" + imagePath + "pm2.gif\";refreshTimePicker(0)' style='cursor:hand'></td><td><img id='iconPM' src='" + imagePath + "pm2.gif' onclick='document.getElementById(\"iconAM\").src=\"" + imagePath + "am2.gif\";document.getElementById(\"iconPM\").src=\"" + imagePath + "pm1.gif\";refreshTimePicker(1)' style='cursor:hand'></td><td align=right valign=center>&nbsp;<img onclick='closeTimePicker()' src='" + imagePath + "close.gif'  STYLE='cursor:hand'>&nbsp;</td></tr></table></td></tr><tr><td colspan=2><span id='timePickerContent'></span></td></tr></table></div>");
    refreshTimePicker(0);
  }

  var crossobj=(dom)?document.getElementById("timepicker").style : ie? document.all.timepicker : document.timepicker;
  var currentCtl

  function selectTime(ctl,ctl2) {
    var leftpos=0
    var toppos=0

    textCtl=ctl2;
    currentCtl = ctl
    currentCtl.src=imagePath + "timepicker2.gif";

    aTag = ctl
    do {
      aTag = aTag.offsetParent;
      leftpos  += aTag.offsetLeft;
      toppos += aTag.offsetTop;
    } while(aTag.tagName!="BODY");
    crossobj.left =  ctl.offsetLeft  + leftpos 
    crossobj.top = ctl.offsetTop +  toppos + ctl.offsetHeight +  2 
    crossobj.visibility=(dom||ie)? "visible" : "show"
    hideElement( 'SELECT', document.getElementById("timepicker") );
    hideElement( 'APPLET', document.getElementById("timepicker") );      
    bShow = true;
  }

  // hides <select> and <applet> objects (for IE only)
  function hideElement( elmID, overDiv ){
    if( ie ){
      for( i = 0; i < document.all.tags( elmID ).length; i++ ){
        obj = document.all.tags( elmID )[i];
        if( !obj || !obj.offsetParent ){
            continue;
        }
          // Find the element's offsetTop and offsetLeft relative to the BODY tag.
          objLeft   = obj.offsetLeft;
          objTop    = obj.offsetTop;
          objParent = obj.offsetParent;
          while( objParent.tagName.toUpperCase() != "BODY" )
          {
          objLeft  += objParent.offsetLeft;
          objTop   += objParent.offsetTop;
          objParent = objParent.offsetParent;
          }
          objHeight = obj.offsetHeight;
          objWidth = obj.offsetWidth;
          if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
          else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
          else if( overDiv.offsetTop >= ( objTop + objHeight + obj.height ));
          else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
          else
          {
          obj.style.visibility = "hidden";
          }
      }
    }
  }
     
  //unhides <select> and <applet> objects (for IE only)
  function showElement( elmID ){
    if( ie ){
      for( i = 0; i < document.all.tags( elmID ).length; i++ ){
        obj = document.all.tags( elmID )[i];
        if( !obj || !obj.offsetParent ){
            continue;
        }
        obj.style.visibility = "";
      }
    }
  }

  function closeTimePicker() {
    crossobj.visibility="hidden"
    showElement( 'SELECT' );
    showElement( 'APPLET' );
    currentCtl.src=imagePath + "timepicker.gif"
  }

  document.onkeypress = function hideTimePicker1 () { 
    if (event.keyCode==27){
      if (!bShow){
        closeTimePicker();
      }
    }
  }

  function isDigit(c) {
    
    return ((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9'))
  }

  function isNumeric(n) {
    
    num = parseInt(n,10);

    return !isNaN(num);
  }

  function padZero(n) {
    v="";
    if (n<10){ 
      return ('0'+n);
    }
    else
    {
      return n;
    }
  }

  function validateDatePicker(ctl) {

    t=ctl.value.toLowerCase();
    t=t.replace(" ","");
    t=t.replace(".",":");
    t=t.replace("-","");

    if ((isNumeric(t))&&(t.length==4))
    {
      t=t.charAt(0)+t.charAt(1)+":"+t.charAt(2)+t.charAt(3);
    }

    var t=new String(t);
    tl=t.length;

    if (tl==1 ) {
      if (isDigit(t)) {
        ctl.value=t+":00 am";
      }
      else {
        return false;
      }
    }
    else if (tl==2) {
      if (isNumeric(t)) {
        if (parseInt(t,10)<13){
          if (t.charAt(1)!=":") {
            ctl.value= t + ':00 am';
          } 
          else {
            ctl.value= t + '00 am';
          }
        }
        else if (parseInt(t,10)==24) {
          ctl.value= "0:00 am";
        }
        else if (parseInt(t,10)<24) {
          if (t.charAt(1)!=":") {
            ctl.value= (t-12) + ':00 pm';
          } 
          else {
            ctl.value= (t-12) + '00 pm';
          }
        }
        else if (parseInt(t,10)<=60) {
          ctl.value= '0:'+padZero(t)+' am';
        }
        else {
          ctl.value= '1:'+padZero(t%60)+' am';
        }
      }
      else
           {
        if ((t.charAt(0)==":")&&(isDigit(t.charAt(1)))) {
          ctl.value = "0:" + padZero(parseInt(t.charAt(1),10)) + " am";
        }
        else {
          return false;
        }
      }
    }
    else if (tl>=3) {

      var arr = t.split(":");
      if (t.indexOf(":") > 0)
      {
        hr=parseInt(arr[0],10);
        mn=parseInt(arr[1],10);

        if (t.indexOf("pm")>0) {
          mode="pm";
        }
        else {
          mode="am";
        }

        if (isNaN(hr)) {
          hr=0;
        } else {
          if (hr>24) {
            return false;
          }
          else if (hr==24) {
            mode="am";
            hr=0;
          }
          else if (hr>12) {
            mode="pm";
            hr-=12;
          }
        }
      
        if (isNaN(mn)) {
          mn=0;
        }
        else {
          if (mn>60) {
            mn=mn%60;
            hr+=1;
          }
        }
      } else {

        hr=parseInt(arr[0],10);

        if (isNaN(hr)) {
          hr=0;
        } else {
          if (hr>24) {
            return false;
          }
          else if (hr==24) {
            mode="am";
            hr=0;
          }
          else if (hr>12) {
            mode="pm";
            hr-=12;
          }
        }

        mn = 0;
      }
      
      if (hr==24) {
        hr=0;
        mode="am";
      }
      ctl.value=hr+":"+padZero(mn)+" "+mode;
    }
  }
