function bkSetCalendar(type)
{
	try
	{
		var ob = document.getElementById(bkidGetCalendarDisplayID()).rows[0];
		if(ob != null && ob.cells.length > 0)
		{
			var cell1 = ob.cells[0];
			if(bkIsMonth(cell1.childNodes[0].id,ob.cells.length,type))
			{
				if(ob.cells.length > 1)
				{
					if(type == 1)
					{
						cell1 = ob.cells[ob.cells.length-1].cloneNode(1);
						ob.replaceChild(ob.cells[1],ob.cells[0]);
						ob.appendChild(cell1);
					}
					else
					{
						cell1 = ob.cells[0].cloneNode(1);
						ob.replaceChild(ob.cells[ob.cells.length-2],ob.cells[ob.cells.length-1]);
						var cell0 = ob.insertCell(0);
						ob.replaceChild(cell1,ob.cells[0]);
					}
				}
				bkSetCalendarTABLE(cell1.childNodes[0],type);
			}
		}
	}
	catch(er)
	{
		alert(er);
	}
}
//Provjera da li postoji datum
function bkIsMonth(strID,leng,type)
{
	var sTid = strID.split(bkGetRChar());
	var YearIndex = parseInt(sTid[1]);
	var MonthIndex = parseInt(sTid[2]);
	var YearCount = parseInt(sTid[3]);
	if(type == 1)
	{
		MonthIndex += (leng-1);
		if(MonthIndex >= 12 && ((YearIndex+1)>=YearCount))
		{
			return false;
		}
	}
	else
	{
		if(MonthIndex <= 1 && ((YearIndex+1)<=3))
		{
			return false;
		}
	}
	return true;
}
//Ispisuje novu tablicu
function bkSetCalendarTABLE(t,type)
{
	var sTid = t.id.split(bkGetRChar());
	var YearIndex = parseInt(sTid[1]);
	var MonthIndex = parseInt(sTid[2]);
	var YearCount = parseInt(sTid[3]);
	if(type == 1)
	{
		if(MonthIndex == 12)
		{
			MonthIndex = 1;
			YearIndex++;
		}
		else
		{
			MonthIndex++;
		}
	}
	else
	{
		if(MonthIndex == 1)
		{
			MonthIndex = 12;
			YearIndex--;
		}
		else
		{
			MonthIndex--;
		}
	}
	var ob = document.getElementById(bkidGetCalendarDataID());
	var tr = ob.rows[YearIndex];
	//var TableID = "&&CalendarTable_"+YearIndex+"_"+MonthIndex+"_"+YearCount;
	var TableID = bkidGetMonthTABLEid(YearIndex,MonthIndex,YearCount);
	var Year =  ob.rows[YearIndex].cells[0].innerHTML;
	var MonthName = ob.rows[0].cells[MonthIndex].innerHTML+" "+Year;
	var FirstDay = tr.cells[(MonthIndex*5)-4].innerHTML;
	var DaysInMonth = tr.cells[(MonthIndex*5)-3].innerHTML;
	var BusyDays = tr.cells[(MonthIndex*5)-2].innerHTML;
	var HideDays = tr.cells[(MonthIndex*5)-1].innerHTML;
	var PreferredDays = tr.cells[(MonthIndex*5)-0].innerHTML;
	bkGetCalendarTABLE(t,TableID,MonthName,MonthIndex,Year,YearIndex,FirstDay,DaysInMonth,BusyDays,HideDays,PreferredDays);
}
//ispisuje detalje tablice
function bkGetCalendarTABLE(t,TableID,MonthName,MonthNu,Year,YearIndex,FirstDay,DaysInMonth,BusyDays,HideDays,PreferredDays)
{
	t.id = TableID;
	//var tID = "&&CalendarTD_"+Year+"_"+MonthNu+"_";
	var tID = bkidGetCalendarTDid(Year,MonthNu,"");
	var tc;
	var Day = 1;
	var i,j;
	for(i=0;i<t.rows.length;i++)
	{
		for(j=0;j<t.rows[i].cells.length;j++)
		{
			tc = t.rows[i].cells[j];
			if(i==0)
			{
				tc.innerHTML = MonthName;
				
			}
			else if(i==2)
			{
				if(j>=FirstDay)
				{
					tc.innerHTML = Day;
					tc.id = tID+Day;
					bkSetTD(tc,Day,BusyDays,HideDays,PreferredDays);
					Day++;
				}
				else
				{
					bkSetEmptyDay(tc);
				}
				
			}
			else if(i>2)
			{
				if(Day <= DaysInMonth)
				{
					tc.innerHTML = Day;
					tc.id = tID+Day;
					bkSetTD(tc,Day,BusyDays,HideDays,PreferredDays);
					Day++;
				}
				else
				{
					bkSetEmptyDay(tc);
				}
			}
		}
	}
}
//Ispisuje TD
function bkSetTD(td,innerHTML1,BusyDays,HideDays,PreferredDays)
{
	var i;
	if(HideDays == "*")
	{
		bkSetHideDay(td);
	}
	else
	{
		var Type = 1;
		var pBusyDays = BusyDays.split(",");
		var pHideDays = HideDays.split(",");
		var pPreferredDays = PreferredDays.split(",");
		for(i=0;i<pHideDays.length;i++)
		{
			if(pHideDays[i]==innerHTML1)
			{
				bkSetHideDay(td);
				Type = 2;
				break;
			}
		}
		if(Type == 1)
		{
			//alert(pBusyDays.length);
			for(i=0;i<pBusyDays.length;i++)
			{
				if(pBusyDays[i]==innerHTML1)
				{
					bkSetBusyDay(td);
					Type = 3;
					break;
				}
			}
			if(Type == 1)
			{
				for(i=0;i<pPreferredDays.length;i++)
				{
					if(pPreferredDays[i]==innerHTML1)
					{
						bkSetPreferredDay(td);
						Type = 5;
						break;
					}
				}
				if(Type == 1)
				{
			
					if(bkIsSelect(td))
					{
						bkSetSelectDay(td);
					}
					else
					{
						bkSetDay(td);
					}
				}
			}
		}
	}	
}
//Funkcije za detalje TD-a
function bkSetEmptyDay(td)
{
	td.innerHTML = "";
	td.style.backgroundColor = "";
	td.style.color = "";
	td.style.cursor = "";
	td.onclick = bkEmpty;
	td.onmouseover = bkEmpty;
	td.onmouseout = bkEmpty;
	td.setAttribute("onclick","");
	td.setAttribute("onmouseover","");
	td.setAttribute("onmouseout","");
	td.setAttribute("bkism","0");
	td.id = "";
}

function bkSetDay(td)
{
	td.style.backgroundColor = "#ffffff";
	td.style.color = "Black";
	td.style.cursor = "default";
	//td.setAttribute("onclick","bkSetSelectDay(this);bkSetHidden(this);");
	//td.setAttribute("onmouseover","this.style.backgroundColor='#99ccff';this.style.color='Black';");
	//td.setAttribute("onmouseout","this.style.backgroundColor='White';this.style.color='Black';");
	td.onclick = bkOnMouseClickTD;
	td.onmouseover = bkOnMouseOverTD;
	td.onmouseout = bkOnMouseOutTD;
	td.attributes["bkism"].value="1";
	td.style.zIndex = "1";
}
function bkSetHideDay(td)
{
	td.style.backgroundColor = "#aaaaaa";
	td.style.color = "Black";
	td.style.cursor = "default";
	td.onclick = bkEmpty;
	td.onmouseover = bkEmpty;
	td.onmouseout = bkEmpty;
	td.setAttribute("onclick","");
	td.setAttribute("onmouseover","");
	td.setAttribute("onmouseout","");
	td.setAttribute("bkism","2");
	td.id += "hide";
	td.style.zIndex = "2";
}
function bkSetBusyDay(td)
{
	td.style.backgroundColor = "Red";
	td.style.color = "White";
	td.style.cursor = "";
	td.onclick = bkEmpty;
	td.onmouseover = bkEmpty;
	td.onmouseout = bkEmpty;
	td.setAttribute("onclick","");
	td.setAttribute("onmouseover","");
	td.setAttribute("onmouseout","");
	td.setAttribute("bkism","3");
	td.id += "busy";
	td.style.zIndex = "3";
	
}
function bkSetPreferredDay(td)
{
	td.style.backgroundColor = "Orange";
	td.style.color = "White";
	td.style.zIndex = "5";
	td.style.cursor = "default";
	//td.setAttribute("onclick","bkSetSelectDay(this);bkSetHidden(this);");
	//td.setAttribute("onmouseover","this.style.backgroundColor='#99ccff';this.style.color='Black';");
	//td.setAttribute("onmouseout","this.style.backgroundColor='White';this.style.color='Black';");
	td.onclick = bkOnMouseClickTD;
	td.onmouseover = bkOnMouseOverTD;
	td.onmouseout = bkOnMouseOutTD1;
	td.attributes["bkism"].value="5";
}
function bkSetSelectDay(td)
{
	td.style.backgroundColor = "Blue";
	td.style.color = "White";
	td.style.cursor = "default";
	td.onclick = bkEmpty;
	td.onmouseover = bkEmpty;
	td.onmouseout = bkEmpty;
	td.setAttribute("onclick","");
	td.setAttribute("onmouseover","");
	td.setAttribute("onmouseout","");
	//td.setAttribute("bkism","4");
}
function bkOnMouseClickTD()
{
	try
	{
		bkSetSelectDay(this);
		bkSetHidden(this);
	}
	catch(er)
	{
		alert(er);
	}
}
function bkOnMouseOverTD()
{
	this.style.backgroundColor='#99ccff';
	this.style.color='Black';
}
function bkOnMouseOutTD()
{
	this.style.backgroundColor='White';
	this.style.color='Black';
}
function bkOnMouseOutTD1()
{
	this.style.backgroundColor='Orange';
	this.style.color='White';
}
function bkEmpty()
{
}
function bkSetHidden(td)
{
	var sID = td.id.split(bkGetRChar());
	var Y = sID[1];
	var M = sID[2];
	var D = sID[3];
	
	var S_Y = document.getElementById(bkidGetCalendarHiddenID(true,0));
	var S_M = document.getElementById(bkidGetCalendarHiddenID(true,1));
	var S_D = document.getElementById(bkidGetCalendarHiddenID(true,2));
	var E_Y = document.getElementById(bkidGetCalendarHiddenID(false,0));
	var E_M = document.getElementById(bkidGetCalendarHiddenID(false,1));
	var E_D = document.getElementById(bkidGetCalendarHiddenID(false,2));
	//alert(E_D);
	
	if(S_D.value != "-1")
	{
		if(E_D.value != "-1")
		{
			bkSelect(parseInt(S_Y.value),parseInt(S_M.value),parseInt(S_D.value),parseInt(E_Y.value),parseInt(E_M.value),parseInt(E_D.value),1);
			bkClearHiddenDate(1);
			S_Y.value = Y;
			S_M.value = M;
			S_D.value = D;
			bkSetDDL(parseInt(S_Y.value),parseInt(S_M.value)-1,parseInt(S_D.value)-1,parseInt(E_Y.value),parseInt(E_M.value)-1,parseInt(E_D.value)-1,0);
		}
		else
		{
			E_Y.value = Y;
			E_M.value = M;
			E_D.value = D;
			var dt1 = new Date();
			var dt2 = new Date();
			dt1.setFullYear(parseInt(S_Y.value),parseInt(S_M.value)-1,parseInt(S_D.value));
			dt2.setFullYear(parseInt(E_Y.value),parseInt(E_M.value)-1,parseInt(E_D.value));
			if(dt1 > dt2)
			{
				E_Y.value = S_Y.value;
				E_M.value = S_M.value;
				E_D.value = S_D.value;
				S_Y.value = Y;
				S_M.value = M;
				S_D.value = D;
			}
			var se = bkSelect(parseInt(S_Y.value),parseInt(S_M.value),parseInt(S_D.value),parseInt(E_Y.value),parseInt(E_M.value),parseInt(E_D.value),0);
			if(se == -1)
			{
				bkSelect(parseInt(S_Y.value),parseInt(S_M.value),parseInt(S_D.value),parseInt(E_Y.value),parseInt(E_M.value),parseInt(E_D.value),1);
				bkClearHiddenDate(2);
				//alert("Action Failed!");
			}
			else
			{
				bkSetDDL(parseInt(S_Y.value),parseInt(S_M.value)-1,parseInt(S_D.value)-1,parseInt(E_Y.value),parseInt(E_M.value)-1,parseInt(E_D.value)-1,2);
			}
		}
	}
	else
	{
		S_Y.value = Y;
		S_M.value = M;
		S_D.value = D;
		bkSetDDL(parseInt(S_Y.value),parseInt(S_M.value)-1,parseInt(S_D.value)-1,parseInt(E_Y.value),parseInt(E_M.value)-1,parseInt(E_D.value)-1,0);
	}
}
function bkClearHiddenDate(nu)
{	
	if(nu == 2 || nu == 1)
	{	var E_Y = document.getElementById(bkidGetCalendarHiddenID(false,0));
		var E_M = document.getElementById(bkidGetCalendarHiddenID(false,1));
		var E_D = document.getElementById(bkidGetCalendarHiddenID(false,2));
		E_Y.value = "-1";
		E_M.value = "-1";
		E_D.value = "-1";
	}
	if(nu == 2 || nu == 0)
	{
		var S_Y = document.getElementById(bkidGetCalendarHiddenID(true,0));
		var S_M = document.getElementById(bkidGetCalendarHiddenID(true,1));
		var S_D = document.getElementById(bkidGetCalendarHiddenID(true,2));
		S_Y.value = "-1";
		S_M.value = "-1";
		S_D.value = "-1";
	}
}

function bkGetYearIndex(innerH)
{
	var ob = document.getElementById(bkidGetCalendarDataID());
	for(z=2;z<ob.rows.length;z++)
	{
		if(ob.rows[z].cells[0].innerHTML == innerH)
		{
			return z;
		}
	}
	return -1;
}

function bkSelect(s_y,s_m,s_d,e_y,e_m,e_d,type)
{
	var sd = new Date(s_y,s_m-1,s_d);
	var ed = new Date(e_y,e_m-1,e_d);
	var td,tdID;
	while(sd <= ed)
	{
		tdID = bkidGetCalendarTDid(sd.getFullYear(),sd.getMonth()+1,sd.getDate());
		td = document.getElementById(tdID);
		if(type == 0)
		{
			if(td != null)
			{
				bkSetSelectDay(td);
			}
			else
			{
				return -1;
			}
		}
		else
		{
			if(td != null)
			{
				//var ism = td.attributes["bkism"].value;
				//alert(td.style.zIndex);
				if(td.style.zIndex == 5)
				{
					bkSetPreferredDay(td);
				}
				else
				{
					bkSetDay(td);
				}
			}
		}
		sd.setDate(sd.getDate()+1);
	}
	return 1;
}
function bkIsSelect(td)
{
	var sID = td.id.split(bkGetRChar());
	var Y = parseInt(sID[1]);
	var M = parseInt(sID[2]);
	var D = parseInt(sID[3]);
	var S_Y = parseInt(document.getElementById(bkidGetCalendarHiddenID(true,0)).value);
	var S_M = parseInt(document.getElementById(bkidGetCalendarHiddenID(true,1)).value);
	var S_D = parseInt(document.getElementById(bkidGetCalendarHiddenID(true,2)).value);
	var E_Y = parseInt(document.getElementById(bkidGetCalendarHiddenID(false,0)).value);
	var E_M = parseInt(document.getElementById(bkidGetCalendarHiddenID(false,1)).value);
	var E_D = parseInt(document.getElementById(bkidGetCalendarHiddenID(false,2)).value);
	
	var Sdt = new Date();
	var Edt = new Date();
	var dt = new Date();
	Sdt.setFullYear(S_Y,S_M,S_D);
	Edt.setFullYear(E_Y,E_M,E_D);
	dt.setFullYear(Y,M,D);
	if(dt >= Sdt && dt <= Edt)
	{
		return true;
	}
	return false;
}

function bkSetDDL(sy,smIndex,sdIndex,ey,emIndex,edIndex,type)
{
	var i;
	if(type == 2 || type == 0)
	{
		var ddl_sy = document.getElementById(bkidGetddlid(true,0));
		var ddl_sm = document.getElementById(bkidGetddlid(true,1));
		var ddl_sd = document.getElementById(bkidGetddlid(true,2));
		ddl_sm.selectedIndex = smIndex;
		ddl_sd.selectedIndex = sdIndex;
		for(i=0;i<ddl_sy.options.length;i++)
		{
			if(ddl_sy.options[i].value == sy)
			{
				ddl_sy.selectedIndex = i;
				break;
			}
			
		}
	}
	if(type == 2 || type == 1)
	{
		var ddl_ey = document.getElementById(bkidGetddlid(false,0));
		var ddl_em = document.getElementById(bkidGetddlid(false,1));
		var ddl_ed = document.getElementById(bkidGetddlid(false,2));
		ddl_em.selectedIndex = emIndex;
		ddl_ed.selectedIndex = edIndex;
		for(i=0;i<ddl_ey.options.length;i++)
		{
			if(ddl_ey.options[i].value == ey)
			{
				ddl_ey.selectedIndex = i;
				break;
			}
		}
	}
}
