
var usesaved = 1; // valid variables = 1 or 0 
var dn = new Array('Н','П','В','С','Ч','П','С'); 
var ma = new Array('Януари','Февруари','Март','Април','Май','Юни','Юли','Август','Септември','Октомври','Ноември','Декември');
var daylink = 1; // valid variables = 1 or 0 
var linkstoptoday = 1;  // valid variables = 0, 1 and 2
var leftarr = '&#171;';
var rightarr = '&#187;';

var newDate = new Date();
var currmonth = new Date();
var calcook;
var globyear, globmonth, globday;

function makeCalendar(year,month,day){
	var il = makeCalendar.arguments.length;
	var spart;
	if(usesaved==1){
		spart=GetCookie('Calendar');
		if(spart!=null&&spart!='0'){
			var rekke = spart.split('&');
			newDate.setYear(rekke[0])
			newDate.setMonth(rekke[1]);
			newDate.setDate(rekke[2]);
		}else{
			if(il==3){
				newDate.setYear(year);
				newDate.setMonth(month);
				newDate.setDate(day);
			}
		}
	}

year = newDate.getYear();
month = newDate.getMonth();
day = newDate.getDate();
var dayweek = newDate.getDay();
	
globyear = year;
globmonth = month;
globday = day;
var Calendar, cal;
var aset1, aset2, asetend;
if(daylink==1){
  var aset1 = '<A HREF="';
  var aset2 = '">';
  var asetend = '</A>';
}

if(year<2000)year+=1900;
cal = dn[dayweek+1] + ' ' + year + ' ' +month+' '+day;
var tabset = '<TABLE align="center"  WIDTH="140" BORDER="0" CELLSPACING="0" CELLPADDING="1">\n';
var tabend = '</TABLE>';
var rowset = '<TR>';
var rowend = '</TR>\n';
var cellset = '<TD ALIGN="RIGHT" WIDTH="20"><SPAN CLASS="bodytext">';
var cellend = '</SPAN></TD>\n';
var cellsetW = '<TD ALIGN="RIGHT" WIDTH="20"><SPAN CLASS="wtext">';
var cellendW = '</SPAN></TD>\n';
//--------------------------------------------------------------------------------------------следва: месец, година и стрелки
cal = tabset + rowset;
if(usesaved==1)cal += '<TD ALIGN="CENTER" BGCOLOR="#FFFFCC"><SPAN CLASS="bodytext"';
if(usesaved==1)cal += '"><A HREF="javascript:clearCookie();go(1);">'+leftarr+'</A></SPAN></TD>';
if(usesaved==1)cal += '<TD COLSPAN="5"';
else cal+='<TD COLSPAN="7"';
cal += ' ALIGN="CENTER" BGCOLOR="#FFFFCC"><SPAN CLASS="bodytext">';
cal += ma[month] +', ' + year + '</SPAN>' + cellend;
if(usesaved==1)cal += '<TD ALIGN="CENTER" BGCOLOR="#FFFFCC"><SPAN CLASS="bodytext"><A HREF="javascript:clearCookie();go(2);">'+rightarr+'</A></SPAN></TD>';
//--------------------------------------------------------------------------------------------следва: дните от седмицата
cal += rowend + rowset + cellset;
for(i=0;i<6;i++)cal += dn[i] + cellend + cellset;
cal += dn[6] + cellend + rowend;
//------------------------------------------------------------------------------------------------------
var tmp;
newDate.setDate(1);
newDate.setMonth(month);
var midl;
var diu = newDate.getDay();
cal+=rowset;
t = newDate.getMonth();

for(i=0;i<diu;i++){cal += cellset +'&nbsp;' + cellend;}
var fresh = new Date();
var nu = new Date();
var bset = '<b class=btag>';
var bend = '</b>';
fresh = fresh.getDate();
for(i=1;i<32;i++){
  gd=newDate.getDate();
  gm=newDate.getMonth();
  gy=newDate.getYear();
  if(gy<2000)gy+=1900;
  if((gd>1||(gd==1&&i==1))&&gm==month){
	dayweek = newDate.getDay();
	if(dayweek==0)cal+=rowset;
	if(dayweek!=7){ 
		tmp1 = gm+1;
		tmp2 = gd;
		if(tmp1<10)tmp1 = '0' + tmp1;
		if(tmp2<10)tmp2 = '0' + tmp2;
	   	if(daylink==1){
		 	if(linkstoptoday==1&&nu.valueOf()>newDate.valueOf() || linkstoptoday==2){
				if(fresh==gd&&globmonth==currmonth.getMonth()){
					cal += cellset + bset + aset1 + "javascript:putToForm(" + gy + "," + tmp1 + "," + tmp2 + ");" + aset2 + gd + asetend + bend + cellend;
					SaveCookie('tabledata',gy, tmp1,tmp2);
				}
			else cal += cellset + aset1 + "javascript:putToForm(" + gy + "," + tmp1 + "," + tmp2 + ");" + aset2 + gd + asetend + cellend;
			}
		else{
			if(fresh==gd&&globmonth==currmonth.getMonth())cal += cellset + bset + tmp2 + bend + cellend;
			else cal += cellset + tmp2 + cellend;
			}
		}
	else{
		  if(fresh==gd&&globmonth==currmonth.getMonth())cal += cellset + bset + tmp2 + bend + cellend;
		  else cal += cellset + tmp2 + cellend;
		}
	}
	if(dayweek==6)cal+=rowend; 
  }
  newDate.setDate(gd+1);
}

if(dayweek<6){
  for(i=dayweek;i<6;i++){
    if(i<7)cal += cellset +'&nbsp;'+cellend;
	if(i==6)cal += rowend;
  }
}

cal += tabend;
Calendar = cal;
return Calendar;
}

function go(hva){
  if(hva==1)SaveCookie('Calendar',globyear,globmonth-1,globday);
  else SaveCookie('Calendar',globyear,globmonth+1,globday);
  document.location=document.location;
}


// *************************************** Start cookie  ******************************
// *************************************** Start cookie  ******************************

function getCookieVal(offset) 
   {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
   }

function GetCookie(name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) 
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) 
         break; 
      }
  return null;
}

function SetCookie(name, value){
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
   }


function SaveCookie(cokname,yearvar,monthvar,dayvar){
   var expdate = new Date();
   expdate.setTime(expdate.getTime() +  (60 * 60*1000*3)); 
   SetCookie(cokname, yearvar+'&'+monthvar+'&'+dayvar, expdate, "/", null, false);
}


function clearCookie(){
  var tid = new Date();
  tid.setTime(tid.getTime()+(60*60))
   SetCookie("Calendar", 0, tid, "/", null, false);
    SetCookie("tabdata", 0, tid, "/", null, false);
   history.go(0);
}

function udateDateForm(action){
	var updateDate= new Date();
	var fullstring=document.forms[0].Date.value;
	var partOfDate = fullstring.split('/');
	var updateDay=parseInt(partOfDate[0]);
	updateDay=updateDay+action;
	updateDate.setYear(partOfDate[2]);
	updateDate.setMonth(partOfDate[1]);
	updateDate.setDate(updateDay);
	var theNewDay=updateDate.getDate();
	var theNewMonth=updateDate.getMonth();
	var theNewYear=updateDate.getYear();
	document.forms[0].Date.value=theNewDay+"/"+theNewMonth+"/"+theNewYear;
	document.forms[0].showDate.value=document.forms[0].Date.value;
	return;
}

function putToForm(yearvar,monthvar,dayvar){
	document.forms[0].showDate.value=dayvar+"/"+monthvar+"/"+yearvar;
	document.forms[0].Date.value=document.forms[0].showDate.value;
	document.forms[0].submit();
	return;
}

function goToValView(){
	document.forms[0].submit();
	return;
}