//  Attention : formvalidate.js is required!
var weekend = [0,6];
var fontface = "Verdana";
var fontsize = 2;
var lowerYear=1900;
var maxYear=9999;
var gNow = new Date();
var vWinCal=0;
var gDialog;
var gForm;
var ggWinCal;
var pattern;
var csspath = siteRoot + "css/calendar.css";
isNav = (document.layers) ?1:0;
Opera = (navigator.userAgent.indexOf('Opera') >= 0)? 1:0;
isIE = (document.all && !Opera)?1:0;
isNav6= (document.getElementById && !isIE && !Opera)?1:0;

// Added field
var gDateField;

 
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))
    {
     //alert("Error Creating Calendar");
     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;
//****************************************************************************************
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) 
	    {
		 if((parseInt(p_Year, 10) - 1)>lowerYear){
		   ret_arr[0] = 11;
		   ret_arr[1] = parseInt(p_Year, 10) - 1;
		 }else{
		   ret_arr[0] = 0;
		   ret_arr[1] = parseInt(p_Year, 10);
		 }
		}
	 else 
	    {
		 ret_arr[0] = parseInt(p_Month, 10) - 1;
		 ret_arr[1] = parseInt(p_Year, 10);
		}
    } 
 else 
    if (incr == 1) 
       {
		// F O R W A R D
		if (p_Month == 11) 
		   {
		    if((parseInt(p_Year, 10)+1)<maxYear){
			  ret_arr[0] = 0;
			  ret_arr[1] = parseInt(p_Year, 10) + 1;
			}else{
		      ret_arr[0] = 11;
		      ret_arr[1] = parseInt(p_Year, 10);
		    }
		   }
		else 
		   {
			ret_arr[0] = parseInt(p_Month, 10) + 1;
			ret_arr[1] = parseInt(p_Year, 10);
		   }
	  }
 return ret_arr;
}
// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
//new Calendar();
//****************************************************************************************
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  width='100%'><tr><td class=darkblue>";
 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="'+csspath+'" rel="stylesheet" type="text/css">');
 this.wwrite("</head>");

 this.wwrite("<body   BGCOLOR='#FFFFFF' topmargin='5' leftmargin='5' marginheight='0' marginwidth='5'>");
 this.wwriteA("<div class=PageSubName>"+this.gMonthName + " " + this.gYear+"</div>");

 // 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 ><TR ><TD class=darkblue>");	
 this.wwrite("<TABLE WIDTH='100%' BORDER=0 CELLSPACING=1 CELLPADDING=0 ><TR >");
 newYear=((parseInt(this.gYear, 10)-1)>=lowerYear)?(parseInt(this.gYear, 10)-1):parseInt(this.gYear, 10);	
 strLink='javascript:opener.Build('+this.gMonth+','+newYear+');';
this.wwrite("<TD ALIGN=center class=darkblue><A class=tablemenu HREF='"+strLink+"'>[&nbsp;&lt;&lt;&nbsp;]</a></TD>");
	
 strLink='javascript:opener.Build('+prevMM+','+prevYYYY+');';
 this.wwrite("<TD ALIGN=center class=darkblue><A class=tablemenu HREF='"+strLink+"'>[&nbsp;&lt;&nbsp;]</a></TD>");
	
 strLink='javascript:opener.Build('+nextMM+','+nextYYYY+');';
 this.wwrite("<TD ALIGN=center class=darkblue><A class=tablemenu HREF='"+strLink+"'>[&nbsp;&gt;&nbsp;]</a></TD>");
	newYear=((parseInt(this.gYear, 10)+1)<=maxYear)?(parseInt(this.gYear, 10)+1):parseInt(this.gYear, 10);
 strLink='javascript:opener.Build('+this.gMonth+','+newYear+');';
 this.wwrite("<TD ALIGN=center class=darkblue><A class=tablemenu HREF='"+strLink+"'>[&nbsp;&gt;&gt;&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:opener.SetData("+vDay+","+this.gMonth+","+this.gYear+",opener.pattern);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:opener.SetData("+vDay+","+this.gMonth+","+this.gYear+",opener.pattern);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) 
{
 //alert("Build \nWindow:"+ggWinCal+"\nMonth:"+p_month+"\nYear="+p_year);
 gCal = new Calendar( ggWinCal, p_month, p_year);
 gCal.show();
}
//******************************************************************************************
// Changed function
function SetData(dd,mm,yyyy,pat)
{
    dd=(parseInt(dd, 10)<10)?"0"+dd:dd;
    mm=(parseInt(mm, 10)<9)?"0"+(mm+1):mm+1;
    if (gForm && gDialog) {
        if (isIE||isNav6||Opera) {
            document.forms[gForm].elements[gDialog].value = dateConvBack(new Array(mm,dd,yyyy),pat);
          
        } else if (isNav) {
            document.forms[gForm].elements[gDialog].value = dateConvBack(new Array(mm,dd,yyyy),pat);
        } 
    }
    else if(gDateField) {
        gDateField.value = dateConvBack(new Array(mm,dd,yyyy),pat);
    }
    return;
}
// Changed function
//****************************************************************************************
function ShowCalendar(FieldName,FormName,fieldpattern) 
{
 gNow = new Date();
 gDialog =FieldName;
 gForm   =FormName;
 
 if(!fieldpattern||fieldpattern=="")pattern=defaultDatePattern; 
 else pattern=fieldpattern;
 
 p_date=parseInt(dateConv(document.forms[gForm].elements[gDialog].value,pattern)[1], 10);
 p_month=parseInt(dateConv(document.forms[gForm].elements[gDialog].value,pattern)[0]-1, 10);
 p_year=parseInt(dateConv(document.forms[gForm].elements[gDialog].value,pattern)[2], 10);

 
 
 if (!(p_date,p_month,p_year)||!checkDate(document.forms[gForm].elements[gDialog],"","",pattern))
    {
     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.closed)
    {
     vWinCal.close();
    }
 if(isNav6){
   vWinCal = window.open("", "Calendar", "width=350,height=200,status=0,resizable=0,top=200,left=200,toolbar=0,scrollbars=0,menubar=0,location=0,directories=0");
 }else{
  vWinCal = window.open("", "Calendar", "width=250,height=200,status=0,resizable=0,top=200,left=200,toolbar=0,scrollbars=0,menubar=0,location=0,directories=0");
 }
 vWinCal.focus();
 if( vWinCal.opener==null)
       vWinCal.opener = self;
 ggWinCal=vWinCal;
 Build(p_month, p_year);
}

///// Added function
// This function is written for a case when form is placed in layer 
// (document.forms[formName] isn't an object)
// So an input field itself (but really only a reference) is sent as a parametr
function ShowCalend(dField, fieldpattern) 
{
 gNow = new Date();
 gDateField = dField;
 
 if(!fieldpattern||fieldpattern=="")pattern=defaultDatePattern; 
 else pattern=fieldpattern;
 
 p_date=parseInt(dateConv(gDateField.value,pattern)[1], 10);
 p_month=parseInt(dateConv(gDateField.value,pattern)[0]-1, 10);
 p_year=parseInt(dateConv(gDateField.value,pattern)[2], 10);
 
 if (!(p_date,p_month,p_year)||!checkDate(gDateField,"","",pattern))
    {
     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.closed)
    {
     vWinCal.close();
    }
 if(isNav6){
   vWinCal = window.open("", "Calendar", "width=350,height=200,status=0,resizable=0,top=200,left=200,toolbar=0,scrollbars=0,menubar=0,location=0,directories=0");
 }else{
  vWinCal = window.open("", "Calendar", "width=250,height=200,status=0,resizable=0,top=200,left=200,toolbar=0,scrollbars=0,menubar=0,location=0,directories=0");
 }
 vWinCal.focus();
 if( vWinCal.opener==null)
       vWinCal.opener = self;
 ggWinCal=vWinCal;
 Build(p_month, p_year);
}
////// Added function

//****************************************************************************************
function closeCalendar()
{
if(vWinCal&&!vWinCal.closed)vWinCal.close();
}
//****************************************************************************************

