var MonthDays=new Array(31,29,31,30,31,30,31,31,30,31,30,31);

//var oneDay=86400000;

//var td=new Date();

var today=new Date(cYr,cMt,cDy,0,0,0);

var periodEnd=new Date(cYr+1,(cMt==0?11:cMt-1),cDy,0,0,0);



//current

//var cYr=td.getFullYear();

//var cMt=td.getMonth();

//var cDy=td.getDate();



//selected

var sYr=2006;

var sMt=1;

var sDy=17;

var sDate;

var tYr,tMt,tDy;



var d=document;

var cal,ical,sec_d,sec_m,oFrames,oObjs,oBody;

var defcol='';

var oStatus,timerID;



//document.onload=place_cal;

var old_onload=(window.onload) ? window.onload : function(){};

window.onload=function(){old_onload(); place_cal();};



function place_cal() {

  var sel_tst=d.getElementById('sector_1_m');

  if(!sel_tst || sel_tst == -1) return;

  oBody=d.getElementsByTagName('body')[0];

  var img;

  for(var i=1;i<=2;i++) {

    var formCal=d.createElement('td');

    //img='<'+'a href="#" onclick="open_cal(event,'+i+');return false;" title="Calendar"><im'+'g src="/php/calendar/cal.gif" border="0" alt="Calendar"><'+'/a>';

    img='';

	sec_m=eval('d.SBform.sector_'+i+'_m');

    sec_m.parentNode.parentNode.appendChild(formCal);

    //sec_d.parentNode.parentNode.insertBefore(formCal,sec_d.parentNode);

    formCal.innerHTML=img;

  }

}



MonthDays[1]=tst_bissextile(sYr);



//test of bissextile

function tst_bissextile(tYr){

  var tst=new Date(tYr,1,29);

  if(tst.getDate()!=29)return 28;

  return 29;

}



function get_start(_Yr,_Mt,firtsday){

  var st=new Date(_Yr,_Mt,1, 0,0,0);

  while(st.getDay()!=firtsday) st=new Date(st.getFullYear(),st.getMonth(),st.getDate()-1,0,0,0);

  return st;

}



function get_end(_Yr,_Mt,firtsday) {

  var en = new Date(_Yr,_Mt,MonthDays[_Mt],0,0,0);

  var temp='['+en.getDate()+']';

  var lastday=(firtsday==0)?6:firtsday-1;

  while(en.getDay()!=lastday) en=new Date(en.getFullYear(),en.getMonth(),en.getDate()+1,0,0,0);

  return en;

}



function getObj(nm){

//var x = new getObj('idname');

  if(d.getElementById){this.obj=d.getElementById(nm);this.style=d.getElementById(nm).style;}

  else if(d.all){this.obj=d.all[nm];this.style=d.all[nm].style;}

  else if(d.layers){this.obj=d.layers[nm];this.style=d.layers[nm];}

}

//***********************

function open_cal(e,sec){

  var posx=0;

  var posy=0;

  if(!e) var e=window.event;

  if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY;}

  else if(e.clientX||e.clientY){posx=e.clientX+d.body.scrollLeft;posy=e.clientY+d.body.scrollTop;}



  if(typeof cal=='object') close_cal();



  if(MSIE5){

    oFrames=d.getElementsByTagName('iframe');

    oObjs=d.getElementsByTagName('object');

    for(var i=0;i<oFrames.length;i++) oFrames[i].style.visibility='hidden';

    for(var i=0;i<oObjs.length;i++) oObjs[i].style.visibility='hidden';

  }



  with(cal=d.createElement('div')){

  style.position='absolute';

  style.zIndex=500;

  className='cal';

  id='cal';

  cal.onmouseover=function(){if(typeof timerID != 'undefined') clearTimeout(timerID); cal.onmouseout=function(){timerID=setTimeout('close_cal()',1000);};};

  style.left=posx+'px';

  style.top=posy+'px';

  }

  with(ical=d.createElement('iframe')){

  src='';

  scrolling='no';

  ical.frameborder='0';

  style.border='0';

  className='cal';

  }

  //values from selects of one sector

  sec_m=eval('d.SBform.sector_'+sec+'_m');

  sec_d=eval('d.SBform.sector_'+sec+'_d');

  var cYM=sec_m.options[sec_m.selectedIndex].value;

  sYr=tYr=parseInt(cYM.substr(2,4),10);

  sMt=tMt=parseInt(cYM.substr(0,2),10)-1;

  sDy=tDy=parseInt(sec_d.options[sec_d.selectedIndex].value,10);

  sDate=new Date(sYr,sMt,sDy,0,0,0);

  oBody.appendChild(cal);

  oBody.appendChild(ical);

  //ical.getElementsByTagName('body')[0].appendChild(cal);



  make_cal();



  with(ical){

  style.width=cal.offsetWidth;

  style.height=cal.offsetHeight;

  style.top=cal.style.top;

  style.left=cal.style.left;

  style.zIndex=cal.style.zIndex-1;

  }

}



function close_cal(){

  cal.parentNode.removeChild(cal);

  ical.parentNode.removeChild(ical);

  cal=ical='';

  if(MSIE5){

    for(var i=0;i<oFrames.length;i++) oFrames[i].style.visibility='visible';

    for(var i=0;i<oObjs.length;i++) oObjs[i].style.visibility='visible';

  }

}



function make_cal() {

  cal.innerHTML='';

  MonthDays[1]=tst_bissextile(tYr);

  yr_field(tYr,cYr,2);

  mt_field(tMt,cMt);

  dy_field();

  oStatus=d.createElement('div');

  cal.appendChild(oStatus);

  oStatus.id='stat';

  oStatus.innerHTML=sDy+delim+(sMt+1)+delim+sYr;

  ical.style.height = cal.offsetHeight;

}



function cr_tbl(idval) {

  var oTBL=d.createElement('table');

  oTBL.id=idval;

  cal.appendChild(oTBL);

  var oTB=d.createElement('tbody');

  oTBL.appendChild(oTB);

  return oTB;

}

function over_top(obj){

  obj.onmouseover=function(){defcol=this.style.backgroundColor; this.style.backgroundColor='#FFFF00';}

  obj.onmouseout=function(){this.style.backgroundColor=defcol;};

}



function yr_field(selY, curY, max) {

  var oTB=cr_tbl('year');

  var oTR=d.createElement('tr');

  oTB.appendChild(oTR);

  var oTD;

  for(var i=0;i<max;i++) {

    oTD=d.createElement('td');

    oTD.innerHTML=curY+i;

    oTD.id='yr_'+(curY+i);

    oTD.className=(selY==curY+i)?'act_mt':'lnk_t';

    if(selY!=(curY+i)) {

      oTD.onclick=function(){tYr=parseInt(this.id.substr(3));make_cal();};

      over_top(oTD);

    }

    oTR.appendChild(oTD);

  }

}



function mt_field(selM, curM) {

  var oTB=cr_tbl('month');

  var oTR=d.createElement('tr');

  oTB.appendChild(oTR);

  var oTD;

  var mths=new Array();

  //var a_prev=new Array('&laquo;','','');

  //var a_next=new Array('','','&raquo;');

  mths[0]=(selM==0)?11:(selM-1);

  mths[1]=selM;

  mths[2]=(selM==11)?0:(selM+1);

  var mtTab=new Array('&laquo;&laquo;&laquo;',months[selM],'&raquo;&raquo;&raquo;');

  for(var i=0;i<3;i++) {

    oTD=d.createElement('td');

    //oTD.innerHTML=a_prev[i]+months[mths[i]]+a_next[i];

    oTD.innerHTML=mtTab[i];

    oTD.id='mt_'+mths[i];

    oTD.className=(selM==mths[i])?'act_mt':'lnk_t';

    if(selM!=mths[i]) {

      oTD.onmousedown=oTD.ondblclick=function(){tMt=parseInt(this.id.substr(3));make_cal();}; 

      over_top(oTD);

    }

    oTR.appendChild(oTD);

  }

}



function dy_field() {

  var oTB=cr_tbl('body');

  var oTR;

  var oTH;

  var oTD;

  var st=get_start(tYr,tMt,firstD);

  var actD=st;

  var en=get_end(tYr,tMt,firstD);

  var numdays=en-st;

  var numW=((numdays/(1000*60*60*24))+1)/7;

  //alert(today);

  oTR=d.createElement('tr');

  oTB.appendChild(oTR);

  oTB.onmouseout=function(){oStatus.innerHTML=sDy+delim+(sMt+1)+delim+sYr;};

  var dd=firstD;

  for(var j=0;j<7;j++){

    oTH=d.createElement('th');

    oTH.innerHTML=days[dd];

    oTR.appendChild(oTH);

    dd=(dd==6)?0:dd+1;

  }



  for(var i=0;i<numW;i++){

    oTR=d.createElement('tr');

    oTB.appendChild(oTR);

    for(var j=0;j<7;j++){

      oTD=d.createElement('td');

      oTR.appendChild(oTD);

      var xd=actD.getDate();

      oTD.innerHTML=xd;

      oTD.id=actD.valueOf();

      if(today.valueOf()<=actD.valueOf() && periodEnd.valueOf()>=actD.valueOf()){

        oTD.onmouseover=function(){defcol=this.style.backgroundColor; this.style.backgroundColor='#FFFF00'; on_over(this);};

        oTD.onmouseout=function(){this.style.backgroundColor=defcol;}; //'transparent'

        oTD.onclick=function(){on_sel(this);};

      }

      else oTD.style.color='#8D8D8D';

      if(today.valueOf()==actD.valueOf()) {

        oTD.style.color='red';

        oTD.title=flytoday;

        //oTD.onclick=function(){alert('Contact reservation center!');};

      }

      if(tMt!=actD.getMonth()) oTD.style.backgroundColor='#D4D4D4';

      if(sDate.valueOf()==actD.valueOf()) oTD.style.backgroundColor='white';

      if(actD.getDay() == 0) oTD.style.fontWeight='bold';

      actD=new Date(actD.getFullYear(),actD.getMonth(),actD.getDate()+1,0,0,0);

    }

    if(actD>=en) break;

  }

}



function on_sel(obj){

  var cell=new Date(parseInt(obj.id));

  close_cal();

  var dField=cell.getDate().toString();

  if(dField.length==1) dField='0'+dField;

  var mField=(cell.getMonth()+1).toString();

  if(mField.length==1) mField='0'+mField;

  for(var i=0;i<sec_m.options.length;i++) if(sec_m.options[i].value==mField+cell.getFullYear()) {sec_m.options[i].selected=true; break;}

  for(var j=0;j<sec_d.options.length;j++) if(sec_d.options[j].value==dField) {sec_d.options[j].selected=true; break;}

}



function on_over(obj) {

  var cell=new Date(parseInt(obj.id));

  oStatus.innerHTML=days[cell.getDay()]+' '+cell.getDate()+delim+(cell.getMonth()+1)+delim+cell.getFullYear();

}

