/////////////////////////////////////////////////////////////////////////////CALENDAR CONTROL CREATION
function positionInfo(object) {

  var p_elm = object;

  this.getElementLeft = getElementLeft;
  function getElementLeft() {
    var x = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      x+= elm.offsetLeft;
      elm = elm.offsetParent;
    }
    return parseInt(x);
  }

  this.getElementWidth = getElementWidth;
  function getElementWidth(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetWidth);
  }

  this.getElementRight = getElementRight;
  function getElementRight(){
    return getElementLeft(p_elm) + getElementWidth(p_elm);
  }

  this.getElementTop = getElementTop;
  function getElementTop() {
    var y = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      y+= elm.offsetTop;
      elm = elm.offsetParent;
    }

    return parseInt(y);
  }

  this.getElementHeight = getElementHeight;
  function getElementHeight(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetHeight);
  }

  this.getElementBottom = getElementBottom;
  function getElementBottom(){
    return getElementTop(p_elm) + getElementHeight(p_elm);
  }
}

function CalendarControl(calendarId,calendarValue,inputClass) {
	if (calendarValue==undefined) calendarValue='';
	document.write("<input id=\""+calendarId+"\" name=\""+calendarId+"\" type=\"text\" value=\""+calendarValue+"\" class=\""+inputClass+"\" />\r");
	document.write("<a href=\"javascript:toggleCalendar('"+calendarId+"');\" style=\"border:0px;\"><img src=\"includes/calendar.js/calendar.png\" alt=\"\" title=\"\" style=\"border:0px;\" /></a>\r");
	document.write("<div id='calendarDiv_"+calendarId+"' class='CalendarControl'></div>\r");
	var currentYear = 0;
	var currentMonth = 0;
	var currentDay = 0;
	
	var selectedYear = 0;
	var selectedMonth = 0;
	var selectedDay = 0;
	
	var dateField = null;
	calendar = document.getElementById("calendarDiv_"+calendarId);
	calendar.innerHTML = calendarDrawTable(calendarId);
      var fieldPos = new positionInfo(calendarId);
      var calendarPos = new positionInfo('calendarDiv_'+calendarId);
      var x = fieldPos.getElementLeft();
      var y = fieldPos.getElementTop();
      setProperty('left', x + "px");
      setProperty('top', y + "px");
      setElementProperty('left', x + "px", 'calendarDiv_'+calendarId);
      setElementProperty('top', y + "px", 'calendarDiv_'+calendarId);	 

  function getProperty(p_property){
    var p_elm = calendarId;
    var elm = null;

    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    if (elm != null){
      if(elm.style){
        elm = elm.style;
        if(elm[p_property]){
          return elm[p_property];
        } else {
          return null;
        }
      } else {
        return null;
      }
    }
  }

  function setElementProperty(p_property, p_value, p_elmId){
    var p_elm = p_elmId;
    var elm = null;

    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    if((elm != null) && (elm.style != null)){
      elm = elm.style;
      elm[ p_property ] = p_value;
    }
  }

  function setProperty(p_property, p_value) {
    setElementProperty(p_property, p_value, calendarId);
  }
	  
}


function toggleCalendar(calendarId) {
	calendar = document.getElementById("calendarDiv_"+calendarId);
        if (calendar.style.display=='block') {
           calendar.style.display='none';
        } else {
           calendar.style.display='block';
        }
}

function showCalendar(calendarId) {
	calendar = document.getElementById("calendarDiv_"+calendarId);
        calendar.style.display='block';
}

function hideCalendar(calendarId) {
	calendar = document.getElementById("calendarDiv_"+calendarId);
        calendar.style.display='none';
}

function getDaysInMonth(year, month) {
	return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
}

function getDayOfWeek(year, month, day) {
	var date = new Date(year,month-1,day)
	return date.getDay()-1;
}

function setDate(year, month, day, calendarId) {
		if (month < 10) {month = "0" + month;}
		if (day < 10) {day = "0" + day;}
		
		var dateString = day+"/"+month+"/"+year;
		dateField = document.getElementById(calendarId);
                dateField.value = dateString;
		hideCalendar(calendarId);
	return;
}

function refreshCalendar(calendarId,selectedDay,selectedMonth,selectedYear) {
	if(selectedMonth > 12) {
		selectedMonth = 1;
		selectedYear++;
	} else if(selectedMonth == 0) {
		selectedMonth = 12;
		selectedYear--;
	}

	calendar = document.getElementById("calendarDiv_"+calendarId);
	calendar.innerHTML = calendarDrawTable(calendarId,selectedDay,selectedMonth,selectedYear);
}


function getCurrentYear() {
	var year = new Date().getYear();
	if(year < 1900) year += 1900;
	return year;
}

function getCurrentMonth() {
	return new Date().getMonth() + 1;
}
function getCurrentDay() {
	return new Date().getDate();
}


function calendarDrawTable(calID,selectedDay,selectedMonth,selectedYear) {
	if (selectedDay==undefined) {
	   selectedDay= getCurrentDay();
        }
	if (selectedMonth==undefined) {
	   selectedMonth= getCurrentMonth();
	   currentMonth= getCurrentMonth();
        } else {
           currentMonth=selectedMonth;
        }
	if (selectedYear==undefined) {
	   selectedYear= getCurrentYear();
	   currentYear= getCurrentYear();
        } else {
           currentYear=selectedYear;
        }
	var dayOfMonth = 1;
	var validDay = 0;
    var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
    if (startDayOfWeek==-1) startDayOfWeek = 6;
    var daysInMonth = getDaysInMonth(currentYear, currentMonth);
    var css_class = null; //CSS class for each day

	var table = "<table cellspacing='0' cellpadding='0' border='0'>";
	table = table + "<tr class='header'>";
	table = table + "  <td colspan='2' class='previous'><a href=\"javascript:refreshCalendar('"+calID+"',"+selectedDay+","+(currentMonth-1)+","+currentYear+");\">&lt;</a> <a href=\"javascript:refreshCalendar('"+calID+"',"+selectedDay+","+currentMonth+","+(currentYear-1)+");\">&laquo;</a></td>";
	table = table + "  <td colspan='3' class='title'>" + months[currentMonth-1] + "<br>" + currentYear + "</td>";
	table = table + "  <td colspan='2' class='next'><a href=\"javascript:refreshCalendar('"+calID+"',"+selectedDay+","+currentMonth+","+(currentYear+1)+");\">&raquo;</a> <a href=\"javascript:refreshCalendar('"+calID+"',"+selectedDay+","+(currentMonth+1)+","+currentYear+");\">&gt;</a></td>";
	table = table + "</tr>";
    table = table + "<tr><th>"+ daysArray[0] +"</th><th>"+ daysArray[1] +"</th><th>"+ daysArray[2] +"</th><th>"+daysArray[3]+"</th><th>"+daysArray[4]+"</th><th>"+daysArray[5]+"</th><th>"+daysArray[6]+"</th></tr>";
	
	for(var week=0; week < 6; week++) {
		table = table + "<tr>";
		for(var dayOfWeek=0; dayOfWeek < 7; dayOfWeek++) {
			if(week == 0 && startDayOfWeek == dayOfWeek) {
				validDay = 1;
			} else if (validDay == 1 && dayOfMonth > daysInMonth) {
				validDay = 0;
			}
			
			if(validDay) {
				if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
					css_class = 'current';
				} else if (dayOfWeek == 5 || dayOfWeek == 6) {
					css_class = 'weekend';
				} else {
					css_class = 'weekday';
				}
				
				table = table + "<td><a class='"+css_class+"' href=\"javascript:setDate("+currentYear+","+currentMonth+","+dayOfMonth+",'"+calID+"')\">"+dayOfMonth+"</a></td>";
				dayOfMonth++;
			} else {
				table = table + "<td class='empty'>&nbsp;</td>";
			}
		}
		table = table + "</tr>";
	}
	
	table = table + "<tr class='header'><th colspan='7' style='padding: 3px;'><a href=\"javascript:hideCalendar('"+calID+"');\">"+CloseTXT+"</a></td></tr>";
	table = table + "</table>";
	return table;
}