﻿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);
  }
}
//-----------------------------------------------Tạo Calender Control
function CalendarControl() 
{

  var calendarId = 'CalendarControl';
  var currentYear = 0;
  var currentMonth = 0;
  var currentDay = 0;

  var selectedYear = 0;
  var selectedMonth = 0;
  var selectedDay = 0;

  var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  var dateField = null;// giá trị thời gian trên textbox

  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;
    }
  }
//---------------------------------- Đặt thuộc tính và giá trị
  function setProperty(p_property, p_value) 
  {
    setElementProperty(p_property, p_value, calendarId);
  }
//-------------------------------- Lấy số ngày theo tháng, năm
  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];
  }
//---------------------------------Lấy ngày trong tuần theo năm, tháng
  function getDayOfWeek(year, month, day) 
  {
    var date = new Date(year,month-1,day)
    return date.getDay();
  }
//-------------------------------Đặt ngày đã đc user chọn lựa cho dateField, sau đó ẩn calenderframe
  this.setDate = setDate;
  function setDate(year, month, day)
   {
    if (dateField)
     {
       if (day < 10)
       {
         day = "0" + day;
       }    
       if (month < 10) 
       {
         month = "0" + month;
       }
      var dateString = day+"/"+month+"/"+year;
      dateField.value = dateString;
      hide();
    }
    return;
  }
  //----------------------------------------------------------
 
//------------------------------------------------- Lưu tháng trên calender sau khi thay đổi >
  this.changeMonth = changeMonth;
  function changeMonth(change) 
  {
    currentMonth += change;
    currentDay = 0;
    if(currentMonth > 12) 
    {
      currentMonth = 1;
      currentYear++;
    } 
    else if(currentMonth < 1)
     {
      currentMonth = 12;
      currentYear--;
    }

    calendar = document.getElementById(calendarId);
    calendar.innerHTML = calendarDrawTable();
  }
//-------------------------------------------------- Lưu năm trên calender sau khi thay đổi >>
  this.changeYear = changeYear;
  function changeYear(change) 
  {
    currentYear += change;
    currentDay = 0;
    calendar = document.getElementById(calendarId);
    calendar.innerHTML = calendarDrawTable();
  }
//-------------------------------------------------- Lấy Năm hiện tại
  function getCurrentYear() 
  {
    var year = new Date().getYear();
    if(year < 1900) year += 1900;
    return year;
  }
//-------------------------------------------------- Lấy tháng hiện tại
  function getCurrentMonth() 
  {
    return new Date().getMonth() + 1;
  } 
//--------------------------------------------------- Lấy ngày hiện tại
  function getCurrentDay()
   {
    return new Date().getDate();
  }
  //-----------------------------------------
 function trim(str) 
{ 
if (str != null) 
{ 
    var i;
    for (i=0; i=0; i–) 
    { 
         if (str.charAt(i)!=" ") 
         { 
            str=str.substring(0,i+1);
            break;
         }
    }
     
    if (str.charAt(0)==" ") 
    {
        return "";
    }
    else 
    {
        return str; 
    } 
 }
 
 
}

//--------------------------------------------------------------------------------------------------
// hàm vẽ bảng calender
  function calendarDrawTable() 
  {

    var dayOfMonth = 1;
    var validDay = 0;
    var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
    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:changeCalendarControlMonth(-1);'>&lt;</a> <a href='javascript:changeCalendarControlYear(-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:changeCalendarControlYear(1);'>&raquo;</a> <a href='javascript:changeCalendarControlMonth(1);'>&gt;</a></td>";
    table = table + "</tr>";
    table = table + "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</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 == 0 || dayOfWeek == 6) 
          {
            css_class = 'weekend';
          } 
          else 
          {
            css_class = 'weekday';
          }

          table = table + "<td><a class='"+css_class+"' href=\"javascript:setCalendarControlDate("+currentYear+","+currentMonth+","+dayOfMonth+")\">"+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:hideCalendarControl();'>Close</a></td></tr>";
    table = table + "</table>";

    return table;
  }
//-----------------------------------------------------------------------------------------------
// hàm show calender frame, trong đó field là giá trị time trên textbox đc truyền vào
//
  this.show = show;
  function show(field) 
  {    
    // If the calendar is visible and associated with
    // this field do not do anything.
    if (dateField == field) 
    {
      return;
    } 
    else 
    {
      dateField = field;
    }
    
    if(dateField)   
    {
      try 
      {   
          str=dateField.value; 
          str= trim(str);          
          if( str.length!=0)
          {             
      	          myDate = new Date(str.substring(6,10),str.substring(3,5),str.substring(0,2));
        		  
		          if ( !isNaN(myDate) )
		          {
			         selectedDay = myDate.getDate();
			         selectedMonth = myDate.getMonth();//+1;			 
			         selectedYear = myDate.getFullYear();
		          }
		      }
		      else
		      {
		          selectedDay = getCurrentDay();
                  selectedMonth = getCurrentMonth();
                  selectedYear = getCurrentYear();
		      }
		  
		 
      } 
      catch(e) {alert(e);}
    }

    if (!(selectedYear && selectedMonth && selectedDay)) 
    {      
      selectedDay = getCurrentDay();
      selectedMonth = getCurrentMonth();
      selectedYear = getCurrentYear();
    }
   
    
    currentDay = selectedDay;
    currentMonth = selectedMonth;
    currentYear = selectedYear;

    if(document.getElementById)
    {
		
      calendar = document.getElementById(calendarId);
      calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);

      setElementProperty('display', 'block', 'CalendarControlIFrame');
      setProperty('display', 'block');

      var fieldPos = new positionInfo(dateField);
      var calendarPos = new positionInfo(calendarId);

      var x = fieldPos.getElementLeft();
      var y = fieldPos.getElementBottom();

      setProperty('left', x + "px");
      setProperty('top', y + "px");
      setElementProperty('left', x + "px", 'CalendarControlIFrame');
      setElementProperty('top', y + "px", 'CalendarControlIFrame');
      setElementProperty('width', calendarPos.getElementWidth() + "px", 'CalendarControlIFrame');
      setElementProperty('height', calendarPos.getElementHeight() + "px", 'CalendarControlIFrame');
    }
  }
//-----------------------------------------------------------------------------
  this.hide = hide;
  // hàm ẩn control calenderframe
  function hide() 
  {
    if(dateField)
     {
      setProperty('display', 'none');
      setElementProperty('display', 'none', 'CalendarControlIFrame');
      dateField = null;
    }
  }
}

var calendarControl = new CalendarControl();

// show table calendar và lấy giá trị trong textfield
function showCalendarControl(textField) 
{
  calendarControl.show(textField);
}
// ẩn calender khi close đc click/ hoặc khi chọn đc time
function hideCalendarControl() 
{
  calendarControl.hide();
}
// lưu time
function setCalendarControlDate(year, month, day)
 {
  calendarControl.setDate(year, month, day);
}
// đổi  số năm 
function changeCalendarControlYear(change) 
{
  calendarControl.changeYear(change);
}
// đổi tên tháng
function changeCalendarControlMonth(change)
 {
  calendarControl.changeMonth(change);
}

document.write("<iframe id='CalendarControlIFrame' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>");
document.write("<div id='CalendarControl'></div>");
