/* --- Swazz Javascript Calendar --- */
/* --- v 1.0 3rd November 2006 By Oliver Bryant http://calendar.swazz.org */
/* --- mod v 2.1 18 Aug 2008 By Gall Zvyagin http://gkirok.com */

/* How to use
  you need create new object for calendar (with name of object in param) and call to writeCalendar like this:

  <script>
    fcfrom=new calendar('fcfrom', 'fillfrom(e);');
    fcfrom.writeCalendar(1, 5, 2008, 'left:140px; top:80px;');
  </script>
*/

function getObj(objID) {
  if (document.getElementById) {return document.getElementById(objID);}
  else if (document.all) {return document.all[objID];}
  else if (document.layers) {return document.layers[objID];}
}

function checkClick(e) {
  e?evt=e:evt=event;
  CSE=evt.target?evt.target:evt.srcElement;
}

function isChild(s,d) {
  while(s) {
    if (s==d) {
      return true;
    }
    s=s.parentNode;
  }
  return false;
}

function Left(obj) {
  var curleft = 0;
  if (obj.offsetParent)  {
    while (obj.offsetParent)  {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  } else {
    if (obj.x) {
      curleft += obj.x;
    }
  }
  return curleft;
}

function Top(obj) {
  var curtop = 0;
  if (obj.offsetParent)  {
    while (obj.offsetParent)  {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  } else {
    if (obj.y) {
      curtop += obj.y;
    }
  }
  return curtop;
}

// Calendar script

function evtTgt(e) {
  var el;
  if(e.target) { el=e.target; }
  else if(e.srcElement) { el=e.srcElement; }
  if(el.nodeType==3) { el=el.parentNode; } // defeat Safari bug
  return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}

function cs_over(e) {
  this.out_classname = evtTgt(EvtObj(e)).className;
  evtTgt(EvtObj(e)).className='over_day';
}
function cs_out(e) {
  evtTgt(EvtObj(e)).className=this.out_classname;
}
function cs_click(e) {
  var ev=evtTgt(EvtObj(e)).getAttribute('actionCode')
  eval(ev);
}

var mn=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');

// day selected
function prepcalendar(hd,cm,cy) {
  now=new Date();
  sd=now.getDate();
  td=new Date();
  td.setDate(1);
  td.setFullYear(cy);
  td.setMonth(cm);
  cd=td.getDay();
  getObj(this.id+'mns').innerHTML=mn[cm]+ ' ' + cy;
  marr=((cy%4)==0)?mnl:mnn;
//alert(this.cur_day + ' ' + this.cur_month +' '+this.cur_year +' '+cm+' '+cy);  
  for(var d=1;d<=42;d++) {
    if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {

      getObj(this.id+'v'+parseInt(d)).onmouseover=this.cs_over;
      getObj(this.id+'v'+parseInt(d)).onmouseout=this.cs_out;
      getObj(this.id+'v'+parseInt(d)).onclick=this.cs_click;
      getObj(this.id+'v'+parseInt(d)).style.cursor='pointer';

      getObj(this.id+'v'+parseInt(d)).setAttribute('datein',cy+'/'+(cm-(-1) > 9 ? cm-(-1) : '0'+(cm-(-1)))+'/'+(d-cd > 9 ? d-cd : '0'+(d-cd)));
      getObj(this.id+'v'+parseInt(d)).innerHTML=d-cd;
      if(d-cd == this.cur_day && cm == this.cur_month && cy == this.cur_year) {
        var timeid = this.id;
        var timed = d;
        setTimeout(function () { getObj(timeid+'v'+parseInt(timed)).className = 'current_day'; },10);
      } else {
        getObj(this.id+'v'+parseInt(d)).className = 'day';
      }

    } else {
      getObj(this.id+'v'+d).innerHTML='&nbsp;';
      getObj(this.id+'v'+parseInt(d)).onmouseover=null;
      getObj(this.id+'v'+parseInt(d)).onmouseout=null;
      getObj(this.id+'v'+parseInt(d)).style.cursor='default';
      getObj(this.id+'v'+parseInt(d)).className = 'day';
    }
  }
}

function caddm() {
  marr=((this.ccy%4)==0)?mnl:mnn;

  this.ccm+=1;
  if (this.ccm>=12) {
    this.ccm=0;
    this.ccy++;
  }
  this.cdayf();
  this.prepcalendar('',this.ccm,this.ccy);
}

function csubm() {
  marr=((this.ccy%4)==0)?mnl:mnn;

  this.ccm-=1;
  if (this.ccm<0) {
    if(this.ccy>2003) {
      this.ccm=11;
      this.ccy--;
    } else {
      this.ccm=0;
    }
  }
  this.cdayf();
  this.prepcalendar('',this.ccm,this.ccy);
}

function cdayf() {
  if ((this.ccy<this.sccy)|((this.ccy!=this.sccy)||(this.ccm<this.sccm))) {
    return;
  } else {
    this.ccy=this.sccy;
    this.ccm=this.sccm;
  }
}

function closeCal(id) {
  getObj(id).style.display="none";
  return false;
}

function setCurDate(set_day, set_month, set_year) {
  this.ccy = this.cur_year = set_year >= 0 ? set_year : this.ccy;
  this.ccm = this.cur_month = set_month >= 0 ? set_month : this.ccm;
  this.cur_day = set_day >= 0 ? set_day : 1;
}
/**
  int set_day : set day
  int set_month : set month
  int set_year : set year
  string style : calendar div style (position)
**/
function writeCalendar(set_day, set_month, set_year, style) {
  document.write('<div id="'+this.id+'" class="calendar" cellspacing="0" style="'+style+'">');
  document.write('<div class="choose_a_month"><div onclick="'+this.id+'.csubm();" class="previous_month"></div><div colspan=4 id="'+this.id+'mns" class="current_month"></div><div class="next_month" onclick="'+this.id+'.caddm();"></div><div class="close_arrow" onclick="this.parentNode.parentNode.style.display = \'none\'"></div></div>');
  document.write('<div class="days_of_month"><div class="day_name">S</div><div class="day_name">M</div><div class="day_name">T</div><div class="day_name">W</div><div class="day_name">T</div><div class="day_name">F</div><div class="day_name last_day_name">S</div></div>');
  for(var kk=1;kk<=6;kk++) {
    document.write('<div class="days_of_week">');
    for(var tt=1;tt<=7;tt++) {
      num=7 * (kk-1) - (-tt);
      document.write('<div id="'+this.id+'v' + num + '" class="day" datein="" actionCode="'+this.actionCode+'">&nbsp;</div>');
    }
    document.write('</div>');
  }
  document.write('</div>');

  document.all?document.attachEvent('onclick',this.checkClick):document.addEventListener('click',this.checkClick,false);

  this.setCurDate(set_day, set_month, set_year);

  this.prepcalendar('',this.ccm,this.ccy);
}

/**
  string id : object name
  string action : eval this string when user click on "day"
**/
function calendar(id, actionCode) {
  now = new Date;
  this.sccm=now.getMonth();
  this.sccy=now.getFullYear();
  this.ccm=now.getMonth();
  this.ccy=now.getFullYear();
  this.cur_day;
  this.cur_month;
  this.cur_year;
  this.out_classname = 'day';

  this.id = id;
  this.actionCode = actionCode;

  this.writeCalendar = writeCalendar;
  this.closeCal = closeCal;
  this.cdayf = cdayf;
  this.csubm = csubm;
  this.caddm = caddm;
  this.prepcalendar = prepcalendar;
  this.cs_click = cs_click;
  this.cs_out = cs_out;
  this.cs_over = cs_over;

  this.checkClick = checkClick;
  
  this.setCurDate = setCurDate;
}
