var myCalendarDateLink;
var myCalendarHolidays = "01.01 01.05 04.05 23.06 24.06 18.11 24.12 25.12 26.12 31.12";
var myCalendarInvertDates = "";

function CreateCalendar(lang)
{
  var table, thead, tfoot, tr, th, td, span;
  table = document.createElement("table");
  table.id = "myCalendar";
  table.className = "calendar";
  table.style.width = 195 + "px";
  thead = document.createElement("thead");
  table.appendChild(thead);

  // << < Month, Year > >>
  tr = document.createElement("tr");
  thead.appendChild(tr);
  th = document.createElement("th");
  th.className = "title"
  th.appendChild(document.createTextNode("<<"));
  th.style.cursor = "pointer";
  th.onmouseover = function () {MouseOver(this);}
  th.onmouseout = function() {MouseOut(this);}
  th.onclick = function() {DecYear(lang);}
  tr.appendChild(th);
  th = document.createElement("th");
  th.className = "title"
  th.appendChild(document.createTextNode("<"));
  th.style.cursor = "pointer";
  th.onmouseover = function () {MouseOver(this);}
  th.onmouseout = function() {MouseOut(this);}
  th.onclick = function () {DecMonth(lang);};
  tr.appendChild(th);
  th = document.createElement("th");
  th.className = "title"
  th.colSpan = 3;
  th.id = "myCalMonthYear";
  th.name = "";
  th.appendChild(document.createTextNode(""));
  tr.appendChild(th);
  th = document.createElement("th");
  th.className = "title"
  th.appendChild(document.createTextNode(">"));
  th.style.cursor = "pointer";
  th.onmouseover = function () {MouseOver(this);}
  th.onmouseout = function() {MouseOut(this);}
  th.onclick = function () {IncMonth(lang);};
  tr.appendChild(th);
  th = document.createElement("th");
  th.className = "title"
  th.appendChild(document.createTextNode(">>"));
  th.style.cursor = "pointer";
  th.onmouseover = function () {MouseOver(this);}
  th.onmouseout = function() {MouseOut(this);}
  th.onclick = function() {IncYear(lang);}
  tr.appendChild(th);

  // Days of week
  tr = document.createElement("tr");
  thead.appendChild(tr);
  for (var i = 0; i < 7; i++) {
    th = document.createElement("th");
	th.className = "header"
	var dow = GetDaysOfWeek(lang);
    th.appendChild(document.createTextNode(dow[i]));
    tr.appendChild(th);
  }
  
  // Today
  tfoot = document.createElement("tfoot");
  table.appendChild(tfoot);
  tr = document.createElement("tr");
  tfoot.appendChild(tr);
  th = document.createElement("th");
  th.className = "footer";
  th.colSpan = 7;
  th.style.cursor = "pointer";
  th.onmouseover = function () {MouseOver(this);}
  th.onmouseout = function() {MouseOut(this);}
  th.onclick = function () {SetDate(this.name);};
  var today = new Date();
  th.name = String(100 + today.getDate()).substring(1, 3) + "." + String(100 + today.getMonth() + 1).substring(1, 3) + "." + today.getFullYear();
  th.appendChild(document.createTextNode(GetWordToday(lang) + ": " + th.name));
  tr.appendChild(th);

  document.body.appendChild(table);
}

function RefreshCalendar(lang)
{
  obj = document.getElementById("myCalBody");
  if (obj) document.getElementById("myCalendar").removeChild(obj);
  
  var arr_seldate = myCalendarDateLink.value.split(".");
  var seldd = parseInt(arr_seldate[0], 10);
  if (isNaN(seldd)) seldd = new Date().getDate();
  var selmm = parseInt(arr_seldate[1], 10);
  if (isNaN(selmm)) selmm = new Date().getMonth() + 1;
  var selyy = parseInt(arr_seldate[2], 10);
  if (isNaN(selyy)) selyy = new Date().getFullYear();
  if (document.getElementById("myCalMonthYear").name == "") document.getElementById("myCalMonthYear").name = selmm + "." + selyy;
  var arr_caldate = document.getElementById("myCalMonthYear").name.split(".");
  var dd = seldd;
  var mm = arr_caldate[0];
  var yy = arr_caldate[1];
  document.getElementById("myCalMonthYear").innerHTML = GetMonths(lang)[mm - 1] + " " + yy;

  var maxd = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if (mm == 2 && yy % 4 == 0) maxd[2 - 1] = 29;
  var firstd =  new Date(yy, mm - 1, 1);
  var dow = firstd.getDay(); if (dow == 0) dow = 7;
  dd = 2 - dow;
  if (dd < 1) {
	mm--; if (mm < 1) {mm = 12; yy--;}
	dd = maxd[mm - 1] + dd;
  }
  var tbody = document.createElement("tbody");
  tbody.id = "myCalBody";
  for (var j = 1; j <= 6; j++) {
	if (j > 1 && mm != arr_caldate[0]) break;
    var tr = document.createElement("tr");
    for (var i = 1; i <= 7; i++) {
      var td = document.createElement("td");
	  td.style.width = "30px";
  	  td.name = String(100 + parseInt(dd, 10)).substring(1, 3) + "." + String(100 + parseInt(mm, 10)).substring(1, 3) + "." + yy;
	  td.onclick = function () {SetDate(this.name);};
	  if ((i >= 6 && myCalendarInvertDates.indexOf(td.name) == -1) || myCalendarHolidays.indexOf(td.name.substring(0, 5)) != -1 || (i < 6 && myCalendarInvertDates.indexOf(td.name) != -1)) {
	    if (mm == arr_caldate[0]) td.style.color = "#ff0000"; else td.style.color = "#ffc0c0";
	  } else {
	    if (mm == arr_caldate[0]) td.style.color = "#000000"; else td.style.color = "#c0c0c0";
	  }
	  if (yy == selyy && mm == selmm && dd == seldd) {
	    td.style.fontWeight = "bold";
		td.style.backgroundColor = "#c0c0ff";
	  } else {
	    td.onmouseover = function () {this.style.backgroundColor = "#e0e0ff";};
	    td.onmouseout = function () {this.style.backgroundColor = "";};
	  }
	  td.appendChild(document.createTextNode(dd));
	  tr.appendChild(td);
	  dd++;
	  if (dd > maxd[mm - 1]) {
	    dd = 1;
		mm++; if (mm > 12) {mm = 1; yy++;}
	  }
    }
	tbody.appendChild(tr);
  }
  document.getElementById("myCalendar").appendChild(tbody);
  document.getElementById("myCalendar").style.height = ((j + 2) * 22) + "px";
}

function MouseOver(obj) 
{
  obj.style.color = "#c0c0ff";
}

function MouseOut(obj) 
{
  obj.style.color = "";
}

function DecMonth(lang) 
{
  var arr_mmyy = document.getElementById("myCalMonthYear").name.split(".");
  arr_mmyy[0]--;
  if (arr_mmyy[0] < 1) {arr_mmyy[0] = 12; arr_mmyy[1]--;}
  document.getElementById("myCalMonthYear").name = arr_mmyy[0] + "." + arr_mmyy[1];
  RefreshCalendar(lang);
}

function IncMonth(lang) 
{
  var arr_mmyy = document.getElementById("myCalMonthYear").name.split(".");
  arr_mmyy[0]++;
  if (arr_mmyy[0] > 12) {arr_mmyy[0] = 1; arr_mmyy[1]++;}
  document.getElementById("myCalMonthYear").name = arr_mmyy[0] + "." + arr_mmyy[1];
  RefreshCalendar(lang);
}

function DecYear(lang) 
{
  var arr_mmyy = document.getElementById("myCalMonthYear").name.split(".");
  arr_mmyy[1]--;
  document.getElementById("myCalMonthYear").name = arr_mmyy[0] + "." + arr_mmyy[1];
  RefreshCalendar(lang);
}

function IncYear(lang) 
{
  var arr_mmyy = document.getElementById("myCalMonthYear").name.split(".");
  arr_mmyy[1]++;
  document.getElementById("myCalMonthYear").name = arr_mmyy[0] + "." + arr_mmyy[1];
  RefreshCalendar(lang);
}

function SetDate(value) 
{
  myCalendarDateLink.value = value;
  if (myCalendarDateLink.onchange) myCalendarDateLink.onchange();
  document.getElementById("myCalendar").parentNode.removeChild(document.getElementById("myCalendar"));
  document.onmousedown = "";
}

function RemoveCalendar(evt) 
{
  var posX = 0, posY = 0;
  if (window.event) evt = event;
  if (evt.pageX || evt.pageY) {
    posX = evt.pageX;
    posY = evt.pageY;
  } else {
    var de = document.documentElement;
    var b = document.body;
    posX = evt.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
    posY = evt.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
  }
  var obj = document.getElementById("myCalendar");
  if (posX < parseInt(obj.style.left) || posX > (parseInt(obj.style.left) + parseInt(obj.style.width)) || posY < parseInt(obj.style.top) || posY > (parseInt(obj.style.top) + parseInt(obj.style.height))) {
    document.getElementById("myCalendar").parentNode.removeChild(document.getElementById("myCalendar"));
    document.onmousedown = "";
  }
}

function GetMonths(lang)
{
  if (lang == "lv") {
    return new Array("Janvāris", "Februāris", "Marts", "Aprīlis", "Maijs", "Jūnijs", "Jūlijs", "Augusts", "Septembris", "Oktobris", "Novembris", "Decembris")
  } else if (lang == "ru") {
    return new Array("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь")
  } else if (lang == "ee") {
    return new Array("Jaanuar", "Veebruar", "Märts", "Aprill", "Mai", "Juuni", "Juuli", "August", "September", "Oktoober", "November", "Detsember")
  } else if (lang == "fi") {
    return new Array("Tammikuu", "Helmikuu", "Maaliskuu", "Huhtikuu", "Toukokuu", "Kesäkuu", "Heinäkuu", "Elokuu", "Syyskuu", "Lokakuu", "Marraskuu", "Joulukuu")
  } else {
    return new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
  }
}

function GetDaysOfWeek(lang)
{
  if (lang == "lv") {
    return new Array("Pr", "Ot", "Tr", "Ct", "Pk", "Ss", "Sv")
  } else if (lang == "ru") {
    return new Array("Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс") 
  } else if (lang == "ee") {
    return new Array("Esm", "Tei", "Kol", "Nel", "Ree", "Lau", "Püh")   //Esmaspäev, Teisipäev, Kolmapäev, Neljapäev, Reede, Laupäev, Pühapäev
  } else if (lang == "fi") {
    return new Array("Ma", "Ti", "Ke", "To", "Pe", "La", "Su")
  } else {
    return new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
  }
}

function GetWordToday(lang)
{
  if (lang == "lv") {
    return "šodien"
  } else if (lang == "ru") {
    return "сегодня"
  } else if (lang == "ee") {
    return "täna"
  } else if (lang == "fi") {
    return "tänään"
  } else {
    return "today"
  }
}

function ShowCalendar(objid, lang, evt)
{
  myCalendarDateLink = document.getElementById(objid);
  var posX = 0, posY = 0;
  if (evt.pageX || evt.pageY) {
    posX = evt.pageX;
    posY = evt.pageY;
  } else {
    var de = document.documentElement;
    var b = document.body;
    posX = evt.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
    posY = evt.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
  }

  if (document.getElementById("myCalendar")) {} else CreateCalendar(lang);
  var obj = document.getElementById("myCalendar");
  RefreshCalendar(lang);
  obj.style.left = posX + "px";
  obj.style.top = posY + "px";

  document.onmousedown = RemoveCalendar;
}