
var weekend = [0,6];
var fontface = "Verdana";
var fontsize = 2;

var gNow = new Date();
var gDialog;
var gForm;
var ggWinCal;
var gStyles="";

var yMin = 1901;  // minimum year can to be set
var yMax = 2100;  // maximum year can to be set

isNav = document.layers ? 1 : 0;
isIE =  document.all ? 1 : 0;

Calendar.Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//****************************************************************************************
function Calendar(p_WinCal, p_month, p_year) 
{
 if ((p_WinCal == null) ||(p_month == null) || (p_year == null))
    {
     return;
    } 

 this.gWinCal = p_WinCal;
 this.gMonthName = Calendar.get_month(p_month);
 this.gMonth = new Number(p_month);
 this.gYear = p_year;

}
//****************************************************************************************
Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.getMonthes = Calendar_getMonthes;
//****************************************************************************************
function Calendar_getMonthes() 
{
 return Calendar.Months;
}
//****************************************************************************************
function Calendar_get_month(monthNo) 
{
 return Calendar.Months[monthNo];
}
//****************************************************************************************
function Calendar_get_daysofmonth(monthNo, p_year) 
{
 // Check for leap year ..
 //	1.Years evenly divisible by four are normally leap years, except for... 
 //	2.Years also evenly divisible by 100 are not leap years, except for... 
 //	3.Years also evenly divisible by 400 are leap years. 
 if ((p_year % 4) == 0) 
    {
	 if ((p_year % 100) == 0 && (p_year % 400) != 0)
		return Calendar.DOMonth[monthNo];
	 return Calendar.lDOMonth[monthNo];
	} 
 else
	return Calendar.DOMonth[monthNo];
}
//****************************************************************************************
function Calendar_calc_month_year(p_Month, p_Year, incr) 
{
 // Will return an 1-D array with 1st element being the calculated month 
 //	and second being the calculated year 
 //	after applying the month increment/decrement as specified by 'incr' parameter.
 //	'incr' will normally have 1/-1 to navigate thru the months.
 
 var ret_arr = new Array();
	
 if (incr == -1) 
    {
  	 // B A C K W A R D
	 if (p_Month == 0) 
	    { ret_arr[0] = 11;			            ret_arr[1] = parseInt(p_Year) - 1;}
	 else { ret_arr[0] = parseInt(p_Month) - 1;	ret_arr[1] = parseInt(p_Year); }
    } 
 else 
   if (incr == 1) 
      {
	   // F O R W A R D
	   if (p_Month == 11) 
          { ret_arr[0] = 0; ret_arr[1] = parseInt(p_Year) + 1; }
	   else { ret_arr[0] = parseInt(p_Month) + 1; ret_arr[1] = parseInt(p_Year); }
      }
 return ret_arr;
}
//****************************************************************************************
function Calendar_calc_month_year(p_Month, p_Year, incr) 
{
 // Will return an 1-D array with 1st element being the calculated month 
 //	and second being the calculated year 
 //	after applying the month increment/decrement as specified by 'incr' parameter.
 //	'incr' will normally have 1/-1 to navigate thru the months.
 
 var ret_arr = new Array();
	
 if (incr == -1) 
    {
	 // B A C K W A R D
	 if (p_Month == 0) 
	    {
		 ret_arr[0] = 11;
		 ret_arr[1] = parseInt(p_Year) - 1;
		}
	 else 
	    {
		 ret_arr[0] = parseInt(p_Month) - 1;
		 ret_arr[1] = parseInt(p_Year);
		}
    } 
 else 
    if (incr == 1) 
       {
		// F O R W A R D
		if (p_Month == 11) 
		   {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		   }
		else 
		   {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		   }
	  }
 return ret_arr;
}

//****************************************************************************************
Calendar.prototype.GetMonthControl = function()
{
    var i, mControl, sel;
    var mnths = Calendar.getMonthes()
    mControl = "<select name='mControl' onChange=\"if (opener) opener.Build(this.options[this.selectedIndex].value, document.forms[0].yControl.options[document.forms[0].yControl.selectedIndex].value)\">\n";
    
    for (i=0; i<mnths.length; i++)
    {
        sel = (i == this.gMonth ? " selected" : "");
        mControl += "<option value='" + i + "'" + sel + ">" + mnths[i] + "\n";
    }
    mControl += "</select>";
    return mControl;
}

//****************************************************************************************
Calendar.prototype.GetYearControl = function()
{
    var i, yControl, sel;
//    var yMin = 1901;
//    var yMax = 2100;
    yControl = "<select name='yControl' onChange=\"if (opener) opener.Build(document.forms[0].mControl.options[document.forms[0].mControl.selectedIndex].value, this.options[this.selectedIndex].value)\">\n";
    for (i=yMin; i <= yMax; i++)
    {
        sel = (i == this.gYear ? " selected" : "");
        yControl += "<option value='" + i + "'" + sel + ">" + i + "\n";
    }
    yControl += "</select>";
    return yControl;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
//new Calendar();
//**************************************s**************************************************
Calendar.prototype.getMonthlyCalendarCode = function() 
{
 var vCode = "";
 var vHeader_Code = "";
 var vData_Code = "";
	
 // Begin Table Drawing code here..
 vCode += "<TABLE BORDER=0 cellpadding=0 cellspacing=0 class=darkblue width='100%'><tr><td>";
 vCode += "<TABLE BORDER=0 cellpadding=0 cellspacing=1 width='100%'>";
	
 vHeader_Code = this.cal_header();
 vData_Code = this.cal_data();
 vCode = vCode + vHeader_Code + vData_Code;
	
 vCode +="</TABLE>";
 vCode +="</td></tr></TABLE>";
	
 return vCode;
}

//****************************************************************************************
Calendar.prototype.show = function() 
{
 var vCode = "";
 this.gWinCal.document.open();
 
 // Setup the page...
 this.wwrite("<html>");
 this.wwrite("<head><title>Calendar</title>");
 this.wwrite('<link href="'+gStyles+'" rel="stylesheet" type="text/css">');
 this.wwrite("</head>");

 this.wwrite("<body BGCOLOR='#FFFFFF'>");
 this.wwriteA("<TABLE CELLPADDING=0 CELLspacing=0 border=0 width='100%'><tr><form><td>");
 this.wwriteA("<div class=PageSubName>" + this.GetMonthControl() + "</div>");
 this.wwriteA("</td><td align=right>");
 this.wwriteA("<div class=PageSubName>" + this.GetYearControl() +"</div>");
 this.wwriteA("</td></form></tr></table>");

 // Show navigation buttons
 var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
 var prevMM = prevMMYYYY[0];
 var prevYYYY = prevMMYYYY[1];

 var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
 var nextMM = nextMMYYYY[0];
 var nextYYYY = nextMMYYYY[1];
	
 this.wwrite("<TABLE WIDTH='100%' BORDER=0 CELLSPACING=0 CELLPADDING=0 class=darkblue><TR><TD>");	
 this.wwrite("<TABLE WIDTH='100%' BORDER=0 CELLSPACING=1 CELLPADDING=0 class=darkblue><TR>");
	
 strLink='javascript:if (opener) opener.Build('+prevMM+','+prevYYYY+');';
 this.wwrite("<TD ALIGN=center><A class=tablemenu HREF='"+strLink+"'>&nbsp;<&nbsp;</a></TD>");
	
 strLink='javascript:if (opener) opener.Build('+nextMM+','+nextYYYY+');';
 this.wwrite("<TD ALIGN=center><A class=tablemenu HREF='"+strLink+"'>&nbsp;>&nbsp;</a></TD>");
 
 this.wwrite("<TD width='100%' class=regular>&nbsp;</TD>");	
 
 strLink='javascript:if (opener) opener.Build('+this.gMonth+','+(parseInt(this.gYear)-1)+');';
 this.wwrite("<TD ALIGN=center><A class=tablemenu HREF='"+strLink+"'>&nbsp;<<&nbsp;</a></TD>");	
 
 strLink='javascript:if (opener) opener.Build('+this.gMonth+','+(parseInt(this.gYear)+1)+');';
 this.wwrite("<TD ALIGN=center><A class=tablemenu HREF='"+strLink+"'>&nbsp;>>&nbsp;</a></TD>");			
	
 this.wwrite("</TR></TABLE>");
 this.wwrite("</TD></TR></TABLE><BR>")

 // Get the complete calendar code for the month..
 vCode = this.getMonthlyCalendarCode();
 this.wwrite(vCode);

 this.wwrite("</body></html>");
 this.gWinCal.document.close();
}
//****************************************************************************************
Calendar.prototype.wwrite = function(wtext) 
{
 this.gWinCal.document.writeln(wtext);
}
//****************************************************************************************
Calendar.prototype.wwriteA = function(wtext) 
{
 this.gWinCal.document.write(wtext);
}
//****************************************************************************************
Calendar.prototype.cal_header = function() 
{
 var vCode = "";
	
 vCode += "<TR>";
 vCode += "<TD WIDTH='15%' class=darkblue align=center><div class=tablemenu>&nbsp;Sun&nbsp;</div></TD>";
 vCode += "<TD WIDTH='14%' class=darkblue align=center><div class=tablemenu>&nbsp;Mon&nbsp;</div></TD>";
 vCode += "<TD WIDTH='14%' class=darkblue align=center><div class=tablemenu>&nbsp;Tue&nbsp;</div></TD>";
 vCode += "<TD WIDTH='14%' class=darkblue align=center><div class=tablemenu>&nbsp;Wed&nbsp;</div></TD>";
 vCode += "<TD WIDTH='14%' class=darkblue align=center><div class=tablemenu>&nbsp;Thu&nbsp;</div></TD>";
 vCode += "<TD WIDTH='14%' class=darkblue align=center><div class=tablemenu>&nbsp;Fri&nbsp;</div></TD>";
 vCode += "<TD WIDTH='15%' class=darkblue align=center><div class=tablemenu>&nbsp;Sat&nbsp;</div></TD>";
 vCode += "</TR>";
	
 return vCode;
}
//****************************************************************************************
Calendar.prototype.cal_data = function() 
{
 var vDate = new Date();
 vDate.setDate(1);
 vDate.setMonth(this.gMonth);
 vDate.setFullYear(this.gYear);

 var vFirstDay=vDate.getDay();
 var vDay=1;
 var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
 var vOnLastDay=0;
 var vCode = "";

 //Get day for the 1st of the requested month/year..
 //Place as many blank cells before the 1st day of the month as necessary. 

 vCode = vCode + "<TR>";
 for (i=0; i<vFirstDay; i++) 
     {
	  vCode += "<TD" + this.write_weekend_string(i) + ">&nbsp;</TD>";
	 }

 // Write rest of the 1st week
 for (j=vFirstDay; j<7; j++) 
     {
	  vCode +="<TD "+this.write_weekend_string(j)+" align=right><A class=link HREF='javascript:if (opener) opener.SetData("+vDay+","+this.gMonth+","+this.gYear+");window.close();'>"+this.format_day(vDay)+"</A></TD>";
 	  vDay=vDay + 1;
	 }
 vCode = vCode + "</TR>";

 // Write the rest of the weeks
 for (k=2; k<7; k++) 
     {
	  vCode = vCode + "<TR>";
	  for (j=0; j<7; j++) 
	      {
		   vCode = vCode + "<TD " + this.write_weekend_string(j) + " align=right>" + 
			   	  "<A class=link HREF='javascript:if (opener) opener.SetData("+vDay+","+this.gMonth+","+this.gYear+");window.close();'>" +this.format_day(vDay)+"</A></TD>";
		   vDay=vDay + 1;

		   if (vDay > vLastDay) 
			  {
			   vOnLastDay = 1;
			   break;
			  }
		  }
 
	  if (vOnLastDay == 1) 
	     {
          for (m=1; m<(7-j); m++) 
              {
	           vCode += "<TD " + this.write_weekend_string(j+m) + ">&nbsp;</TD>";
	          }	     
	      vCode = vCode + "</TR>";    
	      break;
	     } 
	  vCode = vCode + "</TR>";   
	}
 // Fill up the rest of last week with proper blanks, so that we get proper square blocks

	
 return vCode;
}
//****************************************************************************************
Calendar.prototype.format_day = function(vday) 
{
 var vNowDay = gNow.getDate();
 var vNowMonth = gNow.getMonth();
 var vNowYear = gNow.getFullYear();

 if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
 	return ("<FONT COLOR=\"RED\"><B>" + vday + "</B></FONT>");
 else
	return (vday);
}
//****************************************************************************************
Calendar.prototype.write_weekend_string = function(vday) 
{
 var i;

 // Return special formatting for the weekend day.
 for (i=0; i<weekend.length; i++) 
     {
 	  if (vday == weekend[i]) return (" class=light");
	 }
 return (" class=white");	
}
//****************************************************************************************
function Build( p_month, p_year) 
{
 if (p_year > yMax) p_year = yMax;
 if (p_year < yMin) p_year = yMin;
 
 gCal = new Calendar( ggWinCal, p_month, p_year);
 gCal.show();
}
//******************************************************************************************
function SetData(dd,mm,yyyy)
{
 which=document.forms[gForm];
 which.elements["num_"+gDialog+"_date" ].value         =dd;
 which.elements[gDialog+"_month"].options.selectedIndex=mm;
 which.elements["num_"+gDialog+"_year" ].value         =yyyy;       
 
 return;
}
//******************************************************************************************
function GetData()
{
 which=document.forms[gForm];
 var dd   = parseInt(which.elements["num_"+gDialog+"_date" ].value);
 var mm   = parseInt(which.elements[gDialog+"_month"].options.selectedIndex);
 var yyyy = parseInt(which.elements["num_"+gDialog+"_year" ].value);       
 if (!isNaN(dd) && !isNaN(mm) && !isNaN(yyyy))  
    {
     gNow =  new Date(yyyy,mm,dd);
     if ( gNow.getMonth() == mm && gNow.getDate() == dd) // Incorrect day was input
     {
         return 1;
     }
     else
     {
         if (IE) gNow = new Date(yyyy, mm+1, 0);
         else if (NN) 
         {
            dd = 28;
            gNow = new Date(yyyy, mm, dd);
            while (gNow.getMonth() == mm)
            {
                dd = dd+1;
                gNow = new Date(yyyy, mm, dd);
            }
            gNow = new Date(yyyy, mm, dd-1);
         }
         return 0;
     }
    } 
 return 0;
}
//****************************************************************************************
function ShowCalendar(SelectName,FormName,p_date,p_month,p_year,strStyles) 
{
 gDialog =SelectName;
 gForm   =FormName;
 gNow    =new Date();
 if (strStyles) gStyles=strStyles;
 else gStyles="../styles.css"
 if (!(p_date && p_month && p_year)  ) 
    {
     GetData();
     p_month = new String(gNow.getMonth());
     p_year  = new String(gNow.getFullYear().toString());
    }
 else gNow=new Date(p_year,p_month,p_date);
 if (vWinCal && vWinCal.open        && !vWinCal.closed)        { vWinCal.close()       ;  vWinCal=0;    }
 vWinCal = window.open("", "Calendar", "width=250,height=250,status=0,resizable=0,top=200,left=200,toolbar=0,location=0,directories=0,scrollbars=0");
 vWinCal.opener = self;
 ggWinCal=vWinCal;
 Build(p_month, p_year);
 ggWinCal.focus()
}
//****************************************************************************************

/* validation functions */


//****************************************************************************************
/* function validDate(formObj, groupName, symbolVal)
{
    with (formObj)
    {
        yaerVal = eval("num_" + groupName + "_year.value");
        monthVal = eval("num_" + groupName + "_year");
        dateVal = eval("num_" + groupName + "_date")
    }
} */
