//  function DateToInt(p_date)
//     { return p_date.replace(/(\d+)\/(\d+)\/(\d+)/, "$3$1$2") }

  // See if the current year is a leap year
  function IsLeapYear(intYear)
  {
    // If the current year is wholly divisible by 4
    if (intYear % 4 == 0)
    {
      // This is a leap year
      return true;
    }

    // This is not a leap year
    return false;
  }

  // Calculate the days in the month selected
  function DaysInMonth(intMonth, intYear)
  {
    // Create a new array of days
    var arrDays=new Array(12);

    // Populate the array with the number of days in each month
    arrDays[0] = 31;
    arrDays[1] = (IsLeapYear(intYear)) ? 29 : 28;
    arrDays[2] = 31;
    arrDays[3] = 30;
    arrDays[4] = 31;
    arrDays[5] = 30;
    arrDays[6] = 31;
    arrDays[7] = 31;
    arrDays[8] = 30;
    arrDays[9] = 31;
    arrDays[10] = 30;
    arrDays[11] = 31;

    // Return the number of days in the selected month
    return arrDays[intMonth];
  }

  // Return the month name
  function MonthName(intMonth)
  {
    // Create a new array of months
    var arrMonths = new Array(12);
    
    // Populate the array with the month names
    arrMonths[0] = "January";
    arrMonths[1] = "February";
    arrMonths[2] = "March";
    arrMonths[3] = "April";
    arrMonths[4] = "May";
    arrMonths[5] = "June";
    arrMonths[6] = "July";
    arrMonths[7] = "August";
    arrMonths[8] = "September";
    arrMonths[9] = "October";
    arrMonths[10] = "November";
    arrMonths[11] = "December"; 

    // Return the month name for the selected month
    return arrMonths[intMonth];
  }

  // Calculate the dates to draw
  function DrawCalendar(dteSelected, intCalType)
  {
    // Variables for the date
    var dteNow = new Date(dteSelected);
    var intYear = dteNow.getFullYear();
    var intMonth = dteNow.getMonth();
	
    if (intCalType == 1) 
    {
		  
		  //check for january.  if jan. roll back to Dec.
		  if (intMonth == 0)
			{
			  intMonth = 11;
				intYear = intYear - 1;
			}
			else
			{
			  intMonth = intMonth - 1;
			}
    }
    else if (intCalType == 2)
    { 
      //do nothing
    }
    else
    { 
		  if (intMonth == 11)
			{
			  intMonth = 0;
				intYear = intYear + 1;
			}
			else
			{
			  intMonth = intMonth + 1;
			}
    }
		
				
		
    var dteDate = dteNow.getDate();
    var strMonthName = MonthName(intMonth);
    var dteFirstDayInstance = new Date(intYear, intMonth, 1);
    var intFirstDay = dteFirstDayInstance.getDay();
    var intNumDays = DaysInMonth(intMonth, intYear);
    
    // Initialize the current date to null
    dteNow = null;

    // If the year is less than 1900
    if (intYear < 1900)
    {
      // Add 1900 to get the correct year
      intYear += 1900;
    }
    
    // Initialize the first day
    dteFirstDayInstance = null;
		
    // Draw the calendar
    DrawCal(intFirstDay + 1, intNumDays, dteDate, strMonthName, intMonth, intYear, dteSelected, intCalType);

    // Increase the month by 1
    intMonth = intMonth + 1;

    // If the month is past December
    if (intMonth > 11) 
    {
      // Reset the month to January
      intMonth = 0;
      
      // Increase the year by 1
      intYear = intYear + 1;
    }
  
    // If the month is before January
    if (intMonth < 1)
    {
      // Reset the month to december
      intMonth = 12;
      
      // Decrease the year by 1
      intYear = intYear - 1;
    }
  }

  // Draw the actual calendar box
  function DrawCal(intFirstDay, dteLastDate, dteDate, strMonthName, intMonth, intYear, dteSelected, intCalType)
  {
    // Calendar variables
    var dteNow = new Date();
	var dteTodaysDate = new Date();
	var Sysdate = (dteTodaysDate.getMonth()+1) + "/" + dteTodaysDate.getDate() + "/" + dteTodaysDate.getFullYear();
    var intCurrentMonth = (dteNow.getMonth() + 1);
    var intCurrentDay = dteNow.getDate();
    var intCurrentYear = dteNow.getFullYear();
    var strCurrentMonthName = MonthName(intCurrentMonth);
    var strText = "";
    var dteCalDate = "";
    var strCellBack = "";
    var strCellText = "";
    var dteToday = "";
    var strDateType = "";
    var intPrevMonth = "";
    var intPrevYear = "";

    // Initialize the current date to null
    dteNow = null;
    //set to draw correct calendar
    if (intCalType == 1) 
    {
      // Set the date type
      strDateType = "firstDate";
    }
    else if (intCalType == 2)
    { 
      // Set the date type
      strDateType = "secondDate";
    }
    else
    { 
		  // Set the date type
      strDateType = "thirdDate";
    }
	
  
    // Set today
    dteToday += (intCurrentMonth + 1);
    dteToday += intCurrentDay;
    dteToday += intCurrentYear;

		
		var intRowCount = 0;
    // Set the table text
    strText += '<table border="1" cellspacing="1">';
    strText += '  <td colspan="7" align="middle" valign="top" bgcolor="lightgrey">';
  

      // month name
      strText += '    <font face="arial" size="1">&nbsp; &nbsp; ' + strMonthName + '&nbsp; &nbsp</font>';

  
    // Close the column
    strText += '  </td>';

    // Set the variables for open columns
    var strOpenCol = '<td><font face="arial" size="1">';
    var strCloseCol = '</font></td>';

    // Set the heading array for days of the week
    var arrWeekDay = new Array(7);
    
    // Populate the array
    arrWeekDay[0] = "Sun";
    arrWeekDay[1] = "Mon";
    arrWeekDay[2] = "Tue";
    arrWeekDay[3] = "Wed";
    arrWeekDay[4] = "Thu";
    arrWeekDay[5] = "Fri";
    arrWeekDay[6] = "Sat";

    // Set the header text
    strText += '<tr align="middle" valign="top" bgcolor="#FFFFFF">';

    // Go through the days of the week
    for (var intDayNum = 0; intDayNum < 7; ++intDayNum)
    {
      // Create the headings
      strText += strOpenCol + arrWeekDay[intDayNum] + strCloseCol ;
    }

    // Close the row
    strText += '</tr>';
  	
  	
  	// Current columns in the calendar
    var intDigit = 1;
    var intCurrCell = 1;
  	
		
		
		
  	// Go through the month
    for (var intRow = 1; intRow <= Math.ceil((dteLastDate + intFirstDay - 1) / 7); ++intRow)
    {
		  //increment row counter
			intRowCount = intRow;
			
      // Start the row
      strText += '<tr align="right" valign="top" bgcolor="#FFFFFF">';

      // Go through the week
      for (var intCol = 1; intCol <= 7; ++intCol)
      {
        // If it's past the end of the month
        if (intDigit > dteLastDate)
        {
          // Pad the rest of the days
          do
          { 
            // Add a blank cell
            strText += '<td width="22" bgcolor="FFFFFF"><font face="arial" size="1">&nbsp;</font></td>';
            intCol++;
          }
          while (intCol <= 7)
        
          // Exit the month loop
          break        
        }
      
        // If the month doesn't start on the first day of the week
        if (intCurrCell < intFirstDay)
        {
          // Add a blank cell
          strText += '<td width="22"><font face="arial" size="1">&nbsp;</font></td>';
          intCurrCell++;
        }
        else
        {
          // Initialize the variables
          dteCalDate = "";
          intLinkMonth = "";
          intLinkDay = "";
          intLinkYear = "";
          dteCalDate += (intMonth + 1) + "/";
          intLinkMonth = (intMonth + 1);
          dteCalDate += intDigit + "/";
          intLinkDay = intDigit;
          dteCalDate += intYear;
          intLinkYear = intYear;
          strCellBack = "";
          strCellText = "";

		  // If this is the selected date
		  v_show_dark = false;
		  
		  if (p_view=='DAY') if (dteCalDate == dteSelected) v_show_dark=true;
		  // Show the work week as dark grey
		  if (p_view=='5WEEK' || p_view=='7WEEK') {
		  	dteTmp = new Date(dteSelected);
			if (p_view=='5WEEK') {
				dteStartDate = new Date(dteTmp.getFullYear(), dteTmp.getMonth(), dteTmp.getDate() - (dteTmp.getDay() - 1));
				dteEndDate = new Date(dteTmp.getFullYear(), dteTmp.getMonth(), dteTmp.getDate() + (5 - dteTmp.getDay() ));
			}
			if (p_view=='7WEEK') {
				dteStartDate = new Date(dteTmp.getFullYear(), dteTmp.getMonth(), dteTmp.getDate() - dteTmp.getDay());
				dteEndDate = new Date(dteTmp.getFullYear(), dteTmp.getMonth(), dteTmp.getDate() + (6 - dteTmp.getDay()));
			}
			dteCalDay = new Date(dteCalDate);
			if (dteCalDay>=dteStartDate && dteCalDay <= dteEndDate) v_show_dark = true;
		  } 
          if (v_show_dark)
          {
            // Set the background color
            strCellBack = "#c0c0c0";
            strCellText += intDigit;
          }
  	      else
   	      {
	          // Set today's date box to yellow
       	    strCellText += intDigit;
			if (Sysdate == dteCalDate) {
				strCellBack = "GOLD";  /* "#ffffcc";*/ }
			else {
 	        	// Set the background color
		        strCellBack = "#ffffff";
 	      	}
			}

          // If this is today
          if ((intDigit == dteDate) && (strCurrentMonthName == strMonthName))
          {
            // Draw the cell
            strText += '<td width="22" bgcolor="' + strCellBack + '"><font face="arial" size="1">';
            strText += strCellText;
            strText += '</font></td>';
          }
          else
          {
            // Draw the cell
            strText += '<td width="22" bgcolor="' + strCellBack + '"><font face="arial" size="1">';
            strText += strCellText;
            strText += strCloseCol;
          } 

          // Increase the date
          intDigit++;
        }
      }
      //increment colcounter
			intRowCount = intRowCount + 1;
      // Close the row
      strText += '</tr>';
    }
    // check column counter - must be six.
	  if (intRowCount <= 6)
		{
		  strText += '<tr align="right" valign="top" bgcolor="#FFFFFF"> ';
		  
		  for (var intCol = 1; intCol <= 7; ++intCol)
		  {
		    strText += '<td width="22"><font face="arial" size="1"> &nbsp; </font></td>';
		  }
		  
		  strText += '</tr>';
		}
		
    // Close the table
    strText += '</table>';
  
    // Draw the table
    document.write(strText) ;
  }

  function showquery()
  {
    alert("StrDate=" + strDate)
  }  

