// JavaScript Document
function calendar()
{
	this.hours=new Array(9,10,11,12,13,14,15,16,17); //available hours
	
	this.used_date=new Array(); //Already selected hours (assoc array)
	this.date //index in used_date
	
	this.calendars=new Array(); //all mounth tables held here
	this.current_calendar=0; //active mounth (one which u can see now :)
	

	
	this.in_array=function(the_needle, the_haystack)// php's in_array ()
	{
		var the_hay = the_haystack.toString();
		
		if(the_hay == '')
		{
			return false;
		}
		
		var the_pattern = new RegExp(the_needle, 'g');
		var matched = the_pattern.test(the_haystack);
		return matched;
	}

	this.add0=function(val)
	{
		if(val<10)
			val="0"+val;
		return val;
	}
	this.show_hours=function(td) //generates hours table, receives pressed <td>
	{
		var table=new time_table_class("time_table");
		
		var day=td.innerHTML; //day number
		this.date=td.parentNode.parentNode.parentNode.parentNode.parentNode.id + "-" + this.add0(day); //<tr Id> <-- Year-mounth
	
		table.delete_rows();
		
		if(this.date in this.used_date) //if any used hours in this day
		{
			for(var i=0; i<this.hours.length-1; i++)
			{
				if(this.in_array(this.hours[i], this.used_date[this.date])) //insert only free hours
				{
					var row=table.insert_row(this.hours[i]+"-"+this.hours[i+1]);
					var cell=table.insert_cell(row,this.hours[i]+"-"+this.hours[i+1]);
					cell.style.border="1px solid #FF0000";
					
				}
				else
				{
					var row=table.insert_row(this.hours[i]+"-"+this.hours[i+1]);
					var cell=table.insert_cell(row,this.hours[i]+"-"+this.hours[i+1])	
					this.fill_cell_with_properties(cell,this.hours[i]);
				}
			}
		}
		else
		{
			
			for(var i=0; i<this.hours.length-1; i++) //insert all hours
			{
				var row=table.insert_row(this.hours[i]+"-"+this.hours[i+1]);
				var cell=table.insert_cell(row,this.hours[i]+"-"+this.hours[i+1])	
				this.fill_cell_with_properties(cell,this.hours[i]);
				
			}
		}
		
		var div_calendar=document.getElementById("div_calendar");

		var left=(parseInt(div_calendar.style.left)+parseInt(div_calendar.style.width)+3)+"px";
		var top=parseInt(div_calendar.style.top)+"px";
		
		table.set_style("top",top);
		table.set_style("left",left);
	}
	
	this.fill_cell_with_properties=function(cell,hour)
	{
		cell.hour=hour; //hour ::)
		cell.date=this.date
		cell.style.cursor="pointer";
			
		cell.onclick=function(e)
		{
			var Year_mounth_day=document.getElementById("Year_mounth_day");
			var Hrs=document.getElementById("Hrs");
			var time=document.getElementById("time");
			Year_mounth_day.value=this.date;
			Hrs.value=this.hour;
			time.value=this.date+", "+this.hour+" val.";
			
			var table=new time_table_class("time_table");
			table.delete_rows();
		}
	}
	
	this.show_calendar=function(nr)
	{
		document.getElementById(this.calendars[nr]).style.display="block";
		if(nr==0) document.getElementById("btn_prev_"+this.calendars[nr]).style.display="none"; //hide button "back"
		if(nr==this.calendars.length-1) document.getElementById("btn_next_"+this.calendars[nr]).style.display="none"; //hide button "next"
	}

	this.hide_calendar=function(nr)
	{
		if(!nr|| nr==undefined)
			nr=this.current_calendar;
		document.getElementById(this.calendars[nr]).style.display="none";
		var table=new time_table_class("time_table");
			table.delete_rows();
	}

	this.next_calendar=function ()
	{
		if(this.current_calendar!=this.calendars.length-1)
		{
			this.hide_calendar(this.current_calendar);
			this.show_calendar(++this.current_calendar)
		}
		
	}
	
	this.prev_calendar=function(nr)
	{
		if(this.current_calendar!=0)
		{
			this.hide_calendar(this.current_calendar);
			this.show_calendar(--this.current_calendar);
		}
	}
	
	this.add_calendar=function(cal)
	{
		this.calendars.push(cal);
	}
}

function time_table_class(table_name)
{
	if(!document.getElementById(table_name))
		{
			var mybody=document.getElementsByTagName("body").item(0);
			var tbl=document.createElement("TABLE");
			var tbl_body=document.createElement("TBODY");
			tbl.appendChild(tbl_body);
			mybody.appendChild(tbl);
			this.table=tbl;
			this.table.id=table_name;
			this.table.style.position="absolute";
			this.table.bgColor="#044a56";
			//this.table.border=1;
			
		}
		else this.table=document.getElementById(table_name);
		
	
	this.insert_row=function(td_value)
	{
		 var mycurrent_row=document.createElement("TR");
         this.table.firstChild.appendChild(mycurrent_row);
		 return mycurrent_row;
	}
	
	this.insert_cell=function(tr,td_value)
	{
		 mycurrent_cell=document.createElement("TD");
		 mycurrent_cell.style.border="1px solid #ffffff";
		 mycurrent_cell.align="center";
		 mycurrent_cell.style.color="#ffffff";
		 
         currenttext=document.createTextNode(td_value);
         mycurrent_cell.appendChild(currenttext);
		 tr.appendChild(mycurrent_cell);
		 return mycurrent_cell;
	}
	
	this.rows_length=function()
	{
		return this.table.rows.length;
	}
	
	this.delete_rows=function()
	{
		var len=this.rows_length()-1
		for(var i=len; i>=0; i--) //clear hours table
		{
			this.delete_row(i);
		}
	}
	
		
	this.delete_row=function(index)
	{
		this.table.deleteRow(index);
	}
	
	this.set_style=function(stylius,value)
	{
		this.table.style[stylius]=value;
	}
}
