var x999IE4compatible = (document.all);
var x999dayabbs = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
var x999daynames = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
var x999monthnames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
var x999monthabbs = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
var x999weekdays = [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ];
var x999months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
var x999years  = new Array(10);
for (i = 0; i < x999years.length; i++) x999years[i] = 1999 + i;
var x999today = new Date();
var x999currentday = x999today.getDate();
var x999currentmonth = x999today.getMonth();
var x999currentyear = x999today.getFullYear();
var x999left = 95;
var x999top = 80;
var x999width = 210;
var x999height = 235;
var x999cell_width = 20;
var x999cell_height = 20;
var x999bg_color = '#314167';
var x999offsety = 32;
var x999fontface = "verdana";
var x999weekcolor = "#E9ECDE";
var x999weeksize= "2";
var x999lowyear = 1900;
var x999highyear = 2999;
var x999weekfont = '<font face="' + x999fontface + '" size="' + x999weeksize + '" color="' + x999weekcolor + '">';
var x999font = '<font face="' + x999fontface + '" size="1">';
var x999style = "style = 'font-family:" + x999fontface + ";color:#000000;font-size:7pt;font-weight:bold;'";
var x999headerstyle = "style = 'font-family:" + x999fontface + ";color:#000000;font-size:1;font-weight:bold;'";
var x999daystyle = "style = 'font-family:" + x999fontface + ";color:#000000;font-size:7pt;font-weight:notmal;'";
var x999tableborder = '1';
var x999showday = 7;
var frm_name = '';
var frm_fld = ''

//Bharat
//Variable used for hiding the calendar control
var CalCreated = false;

function x999cal(is_popup, win, handler,form_name,field_name, left, top, height, bg_color,width, 
  cell_width, cell_height, sel_month, sel_year)
{
  this.is_popup = is_popup;
  this.parent = null;
  this.window = null;
  if (this.is_popup)
    this.parent = win;
  else
    this.window = win;
  frm_name = form_name;
  frm_fld = field_name;
  this.handler = handler;
  this.left = left;
  this.top = top;
  this.width = width || x999width;
  this.height = height || x999height;
  this.cell_width = cell_width || x999cell_width;
  this.cell_height = cell_height || x999cell_height;
  this.bg_color = bg_color || x999bg_color;
  this.sel_month = sel_month;
  this.sel_year = sel_year;
  if (this.sel_month == null) this.sel_month = x999currentmonth;
  if (this.sel_year == null) this.sel_year = x999currentyear;
  this.days = new Array(42);
  this.cal = null;
  this.day_table = null;

  x999cal.prototype.create_cal = x999cal_create_cal_IE4;
  x999cal.prototype.show_days = x999cal_show_days_IE4;
  x999cal.prototype.parseday = parsedays_IE4;
  
  x999cal.prototype.popup_cal = x999cal_popup_cal;
  x999cal.prototype.prev_year = x999cal_prev_year;
  x999cal.prototype.next_year = x999cal_next_year;
  x999cal.prototype.new_year = x999cal_new_year;
  x999cal.prototype.prev_month = x999cal_prev_month;
  x999cal.prototype.next_month = x999cal_next_month;
  x999cal.prototype.clicked_day = x999cal_clicked_day;
  if (x999cal.prototype.id == null) x999cal.prototype.id = 0;
  x999cal.prototype.id++;
  this.id = x999cal.prototype.id;
  this.cal_id = 'cal' + this.id;
  this.day_table_id = 'day_table' + this.id;
  this.select_form_id = 'select_form' + this.id;
}

function x999cal_create_cal_IE4()
{
  if (this.is_popup)
  {
  //Original Values- Changed by M.Maloney, Meta5 Inc on 2-19-2003 to position calendar near control
    //var x = this.parent.screenLeft + (this.left || x999left);
    //var y = this.parent.screenTop + (this.top || x999top);
//Removed 2-23-03 By M. Maloney Meta5 Inc.  caused random placement of window
//	var x = document.body.scrollLeft + event.clientX;
//	var y =  document.body.scrollTop + event.clientY;
//Position the Calendar next to the clicked control
	var x =  event.clientX;
	var y =  event.clientY;
	//new x and y
	//this.cal.style.pixelLeft = document.body.scrollLeft + event.clientX;
	//this.cal.style.pixelTop =  document.body.scrollTop + event.clientY;
	
   this.window = this.parent.open("", this.cal_id, 'left=' + x + ',top=' + y + ',width=' + this.width + ',height=' + this.height + ',scrollbars=no,resizeable=no');
	self.onblur=mycalblur;
   this.window.document.open();
   this.window.document.writeln('<title>EMS Report Calender</title>');
    this.window.document.close();
  }
//  this.window.document.body.insertAdjacentHTML("beforeEnd", "<div id='" + this.cal_id + "' style='z-index:3; position:relative'></div>");
//Fix:	M.Maloney 2-25-2003 Needed to Change Div Position to absolute since it has been moved to a PopUp Window
  this.window.document.body.insertAdjacentHTML("beforeEnd", "<div id='" + this.cal_id + "' style='z-index:3; position:absolute'></div>");
  this.cal = this.window.document.all[this.cal_id];
  if (this.is_popup)
  {
    this.cal.style.pixelLeft = 0;
    this.cal.style.pixelTop = 10;
//	this.cal.style.offsetLeft = event.clientX;
//    this.cal.style.pixelTop =  event.clientY;
  }
  else
  {
    this.cal.style.pixelLeft = document.body.scrollLeft + event.clientX;
    this.cal.style.pixelTop =  document.body.scrollTop + event.clientY;
  }

  this.cal.style.zIndex = 1;
  this.cal.style.width = this.width;
  this.cal.style.height = this.height;
  this.cal.style.backgroundColor = this.bg_color;
  this.cal.style.visibility = 'visible';
  this.cal.picker = this;
  var x999today = new Date();
  var vYear = x999today.getFullYear();
  var str =
  "<center>\n" +
  "<table onclick='javascript:event.cancelBubble=true;' align='center'><tr><td><table align='center' border='0'>\n" +
  "      <form  name='" + this.select_form_id + "'>\n" +
  "  <tr>\n" +
  "    <td align='center'>\n" +
  "        <input type='button' " + x999style + "  value='&lt;&lt;' onClick='document.all." + this.cal_id + ".picker.prev_year()'>\n" +
  "        <input type='button' " + x999style + "  value='&lt;&nbsp;' onClick='document.all." + this.cal_id + ".picker.prev_month()'>\n" +
  "        <select name='month_sel' " + x999style + "  onChange='document.all." + this.cal_id + ".picker.new_year()'>\n";
  for (i = 0; i < x999months.length; i++)
   str += "        <option value='" + i + "'>" + x999months[i] + "\n";
  str += "        </select>\n" +
  "        <input type = 'text' value = '" + vYear + "' size = '4' maxlength = '4' name = 'year_sel' " + x999style + " onBlur='document.all." + this.cal_id + ".picker.new_year()'>\n" +
  "        <input type='button' " + x999style + "  value='&gt;&nbsp;' onClick='document.all." + this.cal_id + ".picker.next_month()'>\n" +
  "        <input type='button' " + x999style + "  value='&gt;&gt;' onClick='document.all." + this.cal_id + ".picker.next_year()'>\n" +
  "     </td>\n" +
  "  </tr>\n" +  
  "      </form>\n" +
  "</table>\n" +
  "</center>\n" +  
  "<div id='" + this.day_table_id + "' style='position:absolute'></div>\n";
  
  this.cal.innerHTML = str;
  this.day_table = this.window.document.all[this.day_table_id];
  this.day_table.style.pixelLeft = 4;
  this.day_table.style.pixelTop = x999offsety;
  this.day_table.style.zIndex = 9;
  this.day_table.style.visibility = 'inherit';
  this.day_table.picker = this;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var offset = 7 + 1 - newYear.getDay();
    if (offset == 8) offset = 1;
    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
    var weeknum = Math.floor((daynum-offset+7)/7);
    if (month == 0 && weeknum > 7) weeknum = 0;
    if (offset != 1) weeknum = weeknum + 1;
    if (weeknum == 0) {
        year--;
        var prevNewYear = new Date(year,0,1);
        var prevOffset = 7 + 1 - prevNewYear.getDay();
        if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
    }
    return weeknum;
}

function x999cal_show_days_IE4()
{
  var month_select = this.window.document.forms[this.select_form_id]['month_sel'];
  var year_select = this.window.document.forms[this.select_form_id]['year_sel'];
  month_select.selectedIndex = this.sel_month;
 // year_select.selectedIndex = this.sel_year - x999years[0];
  year_select.value = this.sel_year;

  var days_in_month = (new Date(this.sel_year, this.sel_month+1, 0)).getDate();
  var day_of_week_first = (new Date(this.sel_year, this.sel_month, 1)).getDay() - 0;
  if (day_of_week_first< 0 ) day_of_week_first += 7;
  for (i = 0; i < this.days.length; i++) this.days[i] = 0;
  for (i = 0; i < days_in_month; i++)
    this.days[i + day_of_week_first] = i + 1;

  var str =
  "<center>\n" +
  "&nbsp;&nbsp;<table align='center' border='" + x999tableborder + "'>\n" +
  "<form>\n" +
  "  <tr>\n";
  for (j = 0; j < x999weekdays.length; j++)
  {
    str += "    <td align='center' valign='middle' width='" + this.cell_width + "'>" + x999weekfont + x999weekdays[j] + "</td>\n";
  }
  str += "  </tr>\n";
  for (i = 0; i < 6; i++)
  {
    str += "  <tr>\n";
    for(j = 0; j < 7; j++)
    {
      var val = this.days[i * 7 + j];
      if (val > 0 && val < 10) val = " " + val + " ";      
      if (this.days[i * 7 + j]){
		//Bharat
		//Modified to disable all other days except monday.
		//Chris- Changed to disable all days except Saturday.
//		if (j==1)
		if (j==x999showday || x999showday==7)
			str += "    <td align='center' valign='middle' height='" + this.cell_height + "'>" + x999font + "<input type='button' " + x999daystyle + " value='" + val + "' onClick='document.all." + this.day_table_id + ".picker.clicked_day(" + this.days[i * 7 + j] + ")'></td>\n";
		else
			str += "    <td align='center' valign='middle' height='" + this.cell_height + "'>" + x999font + "<input type='button' " + x999daystyle + " value='" + val + "' onClick='document.all." + this.day_table_id + ".picker.clicked_day(" + this.days[i * 7 + j] + ")'  DISABLED></td>\n";
		}
      else
        str += "    <td>&nbsp;</td>\n";
    }
    str += "  </tr>\n";
  }
  str +=
  "</form>\n" +
  "</table>\n" +
  "</center>\n";
  this.day_table.innerHTML = str;
}

function parsedays_IE4(daytext)
{
	if (daytext.indexOf("Saturday") >= 0)
	{
		return 6;
	}
	if (daytext.indexOf("Monday") >= 0)
	{
		return 1;
	}
	if (daytext.indexOf("Sunday") >= 0)
	{
		return 0;
	}
	if (daytext.indexOf("Tueday") >= 0)
	{
		return 2;
	}
	if (daytext.indexOf("Wednesday") >= 0)
	{
		return 3;
	}
	if (daytext.indexOf("Thursday") >= 0)
	{
		return 4;
	}
	if (daytext.indexOf("Friday") >= 0)
	{
		return 5;
	}
	return 7;
}

function x999cal_popup_cal(formFieldName, daytext)
{	
  showday = this.parseday(daytext);
  x999showday = showday;
  frm_fld=formFieldName.name;
  if (this.is_popup)
  {	
    if (this.cal && this.window && !this.window.closed)
      this.window.close();
    else
    {
      this.create_cal();
      this.show_days();
    }
  }
  else
  {
    if (!this.cal)
    {
	//Bharat
	// Variable added to hide the calendar if the user clicks on other links
      this.create_cal();
      this.show_days();
      CalCreated = true;
    }
    else
    {
		this.show_days();
		//Bharat
		//Modified to position the calender
        if (this.cal.style.visibility == 'visible')
          this.cal.style.visibility = 'hidden';
        else
			{
    			this.cal.style.pixelLeft = document.body.scrollLeft + event.clientX;
				this.cal.style.pixelTop = document.body.scrollTop + event.clientY;
				this.cal.style.visibility = 'visible';
			}
    }
  }
}

//function x999cal_new_month()
//{
//  var month_select = this.cal.document.forms[this.select_form_id]['month_sel'];
//  this.sel_month = new Number(month_select.options[month_select.selectedIndex].value);
//  var year_select = this.cal.document.forms[this.select_form_id]['year_sel'];
//  this.sel_year = new Number(year_select.options[year_select.selectedIndex].value);
//  this.show_days();
//}

function x999cal_new_year()
{
 var month_select = this.cal.document.forms[this.select_form_id]['month_sel'];
 this.sel_month = new Number(month_select.options[month_select.selectedIndex].value);
 var year_select = this.cal.document.forms[this.select_form_id]['year_sel'];
 this.sel_year = new Number(year_select.value);
 if ( (isNaN(parseInt(this.sel_year))) || (this.sel_year < x999lowyear) || (this.sel_year > x999highyear) )
 {

 window.alert('This is not a valid year.');
 var x999today = new Date;
 var yr = x999today.getFullYear();
 this.sel_year = yr;
 }
this.show_days();
}

function x999cal_prev_month()
{
  this.sel_month--;
  if (this.sel_month < 0)
  {
    this.sel_month = 11;
    this.sel_year--;
  }
  this.show_days();
}


function x999cal_prev_year()
{
 this.sel_year--;
 this.show_days();
}

function x999cal_next_year()
{
 this.sel_year++;
 this.show_days();
}


function x999cal_next_month()
{
  this.sel_month++;
  if (this.sel_month > 11)
  {
    this.sel_month = 0;
    this.sel_year++;
  }
  this.show_days();
}

function x999cal_clicked_day(day)
{
  if (this.is_popup)
    this.window.close();
  else
    this.cal.style.visibility = 'hidden';
  var sel_date = new Date(this.sel_year, this.sel_month, day);
  if (this.handler) this.handler(this, sel_date);
}
function twodigit(num)
{
  var numStr = num + '';
  if (numStr.length == 1) {
     numStr = "0" + numStr; }
  return numStr;
}
function righttwodigits(num)
{
  var numStr = num + '';
  if (numStr.length == 4) {
     numStr = numStr.substring(1,3);}
  return numStr;
}
function get_days_in_month(mon, yr)
{
  var month = new Number(mon);
  var year = new Number(yr);
  var mdays = [ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
                [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ];
  var isleap =
     (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1 : 0;
  return mdays[isleap][month];
}
function my_date_selected(cal, date)
{

 var dt = eval("document."+frm_name+"."+frm_fld);

 dt.value = twodigit(date.getMonth() + 1) + '-'  + twodigit(date.getDate()) + '-'  + date.getFullYear();

}

function mycalblur(){
this.window.close();
}
