// JavaScript Document
var GLOBALDATE = "";
var sent_msg
function sendMail(hallId, error_msg, sending_msg, _sent_msg)
{
	var organization = $id_mstv("mailOrganization").value;
	var name = $id_mstv("mailName").value;
	var email = $id_mstv("mailEmail").value;
	if(!isEmail(email))
		return alert(error_msg);
	sent_msg = _sent_msg
	var tel = $id_mstv("mailTel").value;
	var project = $id_mstv("mailProject").value;
	var comment = $id_mstv("mailComment").value;
	var mailDate = $id_mstv("mailDate").value;
	
	$id_mstv("mailStatus").innerHTML = sending_msg;

	
	xajax_send(hallId, GLOBALDATE, organization, name, email, tel, project, comment, mailDate);
}

function processHallsFormCallBack()
{
	$id_mstv("mailOrganization").value = "";	
	$id_mstv("mailName").value = "";	
	$id_mstv("mailEmail").value = "";	
	$id_mstv("mailTel").value = "";	
	$id_mstv("mailProject").value = "";	
	$id_mstv("mailComment").value = "";	
	$id_mstv("mailDate").value = "";	
	
	$id_mstv("mailStatus").innerHTML = sent_msg;
}

function getChilds(element, childName)
{
	var	a = new Array();
	for(var i = 0; i < element.childNodes.length; i++)
	{
		if(element.childNodes[i].nodeName.toLowerCase() == childName.toLowerCase())
			a.push(element.childNodes[i]);
	}
	return a;
}


function Cal2(id, date, callBackFunc, lng, months, overwriteCurrentDay)
{
//	this.months = new Array("Sausis", "Vasaris", "Kovas", "Balandis", "Gegužė", "Birželis", "Liepa", "Rugpjūtis", "Rugsėjis", "Spalis", "Lapkritis", "Gruodis");
	this.months = months;
	this.overwriteCurrentDay = overwriteCurrentDay;
	this.id = id;
	this.date = date;
	this.callBackFunc = callBackFunc;
	this.lng = lng;
	
	this.activeDiv = null;
	this.activeDay = null;
	
	this.body = null;
	this.daysDiv = null;
	
	this.year = date.getFullYear();
	this.month = date.getMonth();
	
	this.days = new Array();
	
	this.eventDays = new Array();
	
	this.init = function()
	{
		this.body = $id_mstv(id);
		this.draw();
	}
	
	this.draw = function()
	{
		var childs = getChilds(this.body, "div");
		
		var titleDiv = childs[1];
			titleDiv.innerHTML = this.year +" "+ this.months[this.month];
		
		var prevMonthDiv = childs[5];
		var nextMonthDiv = childs[6];
		
		if(this.month == 0)
		{
			prevMonthDiv.innerHTML = "&lt; " + this.months[11];	
			nextMonthDiv.innerHTML = this.months[1] + " &gt;";
		}
		else if(this.month == 11)
		{
			prevMonthDiv.innerHTML = "&lt; " + this.months[10];
			nextMonthDiv.innerHTML = this.months[0] + " &gt;";;
		}
		else
		{
			prevMonthDiv.innerHTML = "&lt; " + this.months[this.month - 1];	
			nextMonthDiv.innerHTML = this.months[this.month + 1] + " &gt;";;	
		}
		
		var _this = this; //kad butu matomas is mygtuku skoupo
		prevMonthDiv.onclick = function()
		{
			_this.prevMonth();
		}
		
		nextMonthDiv.onclick = function()
		{
			_this.nextMonth();
		}
		
		this.daysDiv = childs[3];
		
		var today = new Date();
		
		//alert(this.year +":"+ today.getFullYear() +":"+ this.month +":"+ today.getMonth() +":"+ today.getDate())
		
		var firstDay = new Date(this.year, this.month, 1).getDay();
		firstDay = firstDay == 0 ? 7 : firstDay;
		var div = null;
		for(var i = 0;  i < firstDay - 1; i++)
		{
			div = this.createDiv();
			div.innerHTML = "&nbsp;";
		}
			
		for(i = 0; i < this.getDaysInMonth(this.year, this.month); i++)	
		{
			div = this.createDiv();
			div.innerHTML = i+1;
			div.day = i + 1;
			
			if(this.year == today.getFullYear() && this.month == today.getMonth() && today.getDate() == i + 1)
			{
				div.className = "today";
				div.setAttribute("class", "today");
			}
			
		
			if(this.activeDate)
			{
				if(this.year == this.activeDate.getFullYear() && this.month == this.activeDate.getMonth() && this.activeDate.getDate() == i + 1)
				{
					//div.setAttribute("realClass", div.getAttribute("class"));
					div.realClass = div.className;
					div.className = "today";
					this.setActiveDiv(div);
				}
			}
			
			isweekend = new Date(this.year, this.month, i + 1).getDay();
			if(isweekend == 0 || isweekend == 6)
			{
					
				div.className = "weekend";
				div.setAttribute("class", "weekend");
				//div.style.display="none";
			}
			
			div.onmouseover = function()
			{
				if(this.getAttribute("active") == 1)
					return;
				
				//this.setAttribute("realClass", this.className);
				this.realClass = this.className;
				this.className = "hover";
				//this.setAttribute("class", "hover");
			}
			
			div.onclick = function()
			{
				_this.date = new Date(Date.parse(_this.year+"/"+(_this.month + 1)+"/"+this.day));
				_this.callBackFunc(_this, this);
				
			}
			
			div.onmouseout = function()
			{
				if(this.getAttribute("active") == 1)
					return;
				
				//this.setAttribute("class", this.getAttribute("realClass"));	
				this.className = this.realClass;
				//alert(this.getAttribute("normalClass"));
			}
		}
		
		var lastDay = new Date(this.year, this.month, i).getDay();
		
		if(lastDay != 0)
		{
			for(i = lastDay; i < 7; i++)
			{
				div = this.createDiv();
				div.innerHTML = "&nbsp;";
			}
		}
		
		
		try
		{
			for(i = 0; i < this.eventDays[this.year][this.month].length; i++)
			{
				if(this.eventDays[this.year][this.month][i].full == 1)
				{
					if(this.overwriteCurrentDay)
						this.days[firstDay - 2 + this.eventDays[this.year][this.month][i].date].className = "full";
					else
						this.days[firstDay - 2 + this.eventDays[this.year][this.month][i].date].className += "full";
				}
				else
				{
					if(this.overwriteCurrentDay)
						this.days[firstDay - 2 + this.eventDays[this.year][this.month][i].date].className = "part";					
					else
						this.days[firstDay - 2 + this.eventDays[this.year][this.month][i].date].className += "part";					
				}
			}
		}
		catch(e)
		{
			//nėra eventų	
		}
		
		
	}
	
	this.setActiveDay = function(date)
	{
		date = new Date(date);
		if(date.getFullYear()) // na jei geras date objektas
		{
			this.activeDate = date;
		}
	}
	
	this.setActiveDiv = function(div, date)
	{
		try
		{
		//	this.activeDiv.setAttribute("class", this.activeDiv.getAttribute("realClass"));
		//	this.activeDiv.setAttribute("active", 0);
			this.activeDiv.className = this.activeDiv.realClass;
			this.activeDiv.setAttribute("active", 0);
		}
		catch(e)
		{
			//nebuvo dar aktyvaus divo	
		}
		
		this.activeDiv = div;
		div.setAttribute("active", 1);
		//div.setAttribute("class", "active");
		div.className = "active";
		this.activeDate = new Date(date);
	}
	
	this.addEvent = function(date, isFull)
	{
		var _date = new Date(Date.parse(date));
		if(this.eventDays[_date.getFullYear()] == null)
			this.eventDays[_date.getFullYear()] = new Array();
		
		if(this.eventDays[_date.getFullYear()][_date.getMonth()] == null)
			this.eventDays[_date.getFullYear()][_date.getMonth()] = new Array();
		//this.eventDays[_date.getFullYear()][_date.getMonth()].push(_date.getDate());
		this.eventDays[_date.getFullYear()][_date.getMonth()].push({date:_date.getDate(),full:isFull});
	}
	
	
	this.createDiv = function()
	{
		var div = document.createElement("div");
		this.daysDiv.appendChild(div);
		this.days.push(div);
		return div;
	}
	
	this.getDaysInMonth = function(year, month)
	{
	    return 32 - new Date(year, month, 32).getDate();
	}
	
	this.clear = function()
	{
		try
		{
			for(var i = 0; i < this.days.length; i++)	
				this.days[i].parentNode.removeChild(this.days[i]);
			
		}
		catch(e){/*kalendorius pildomas pirma karta*/}
		this.days = new Array();
	}
	
	this.prevMonth = function()
	{
		this.clear();
		this.month -= 1;
		if(this.month < 0)
		{
			this.month = 11;
			this.year--;
		}
		if(this.date.getDate() > this.getDaysInMonth(this.year, this.month))
			this.date.setDate(this.getDaysInMonth(this.year, this.month));
		this.date.setMonth(this.month);
		this.date.setYear(this.year);

		//openMonth(this);
		this.draw();
	}
	
	this.nextMonth = function()
	{
		this.clear();
		this.month += 1;
		if(this.month > 11)
		{
			this.month = 0;
			this.year++;
		}
		if(this.date.getDate() > this.getDaysInMonth(this.year, this.month))
			this.date.setDate(this.getDaysInMonth(this.year, this.month));
			
		this.date.setMonth(this.month);
		this.date.setYear(this.year);
		//openMonth(this);
		this.draw();
	}
}