/* --- Swazz Javascript Calendar ---
/* --- v 1.0 3rd November 2006
By Oliver Bryant
http://calendar.swazz.org

Update:
DeS
 */

function CalendarClass()
{
	this.target_box = null;
	this.marr		= null;
	this.now 		= new Date;
	this.sccd		= this.now.getDate();
	this.sccm		= this.now.getMonth();
	this.sccy		= this.now.getFullYear();
	this.ccm		= this.now.getMonth();
	this.ccy		= this.now.getFullYear();
	this.mn			= ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	this.mnn		= ['31','28','31','30','31','30','31','31','30','31','30','31'];
	this.mnl		= ['31','29','31','30','31','30','31','31','30','31','30','31'];
	this.calvalarr	= [42];


	this.ua = navigator.userAgent.toLowerCase();
}

CalendarClass.prototype.checkClick = function(){
	e?evt=e:evt=event;
	CSE=evt.target?evt.target:evt.srcElement;
	var el = ById('fc');
	if (el)
		if (!isChild(CSE,el))
			el.style.display='block';
}

CalendarClass.prototype.isChild = function(s,d) {
	while(s) {
		if (s==d)
			return true;
		s=s.parentNode;
	}
	return false;
}

CalendarClass.prototype.Left = function(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;
}

CalendarClass.prototype.Top = function(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;
}



CalendarClass.prototype.Calendar_show = function(box,event)
	{
	var calendar = ById('fc');
	var obj = this;
	if(calendar){
		calendar.parentNode.removeChild(calendar);
		//return;
		}
	var calendar = document.createElement("table");
	calendar.id = 'fc';
	var newRow  = calendar.insertRow(0);

	var newCell = newRow.insertCell(0);
    newCell.className = 'calc_nav';
    newCell.onclick =  function(e){obj.csuby(e);};
    newCell.innerHTML = '&laquo;&laquo;';

	var newCell = newRow.insertCell(1);
    newCell.className = 'calc_nav';
    newCell.onclick =  function(e){obj.csubm(e);};
    newCell.innerHTML = '&laquo;';

	var newCell = newRow.insertCell(2);
    newCell.id = 'mns';
    SetAttr(newCell, 'colSpan', 3);

    var newCell = newRow.insertCell(3);
    newCell.className = 'calc_nav';
    newCell.onclick = function(e){obj.caddm(e);};
    newCell.innerHTML = '&raquo;';

    var newCell = newRow.insertCell(4);
    newCell.className = 'calc_nav';
    newCell.onclick = function(e){obj.caddy(e);};
    newCell.innerHTML = '&raquo;&raquo;';

	var newRow  = calendar.insertRow(1);
	newRow.className = 'cal_wh';
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = 'Mon';
    var newCell = newRow.insertCell(1);
    newCell.innerHTML = 'Tue';
    var newCell = newRow.insertCell(2);
    newCell.innerHTML = 'Wed';
    var newCell = newRow.insertCell(3);
    newCell.innerHTML = 'Thu';
    var newCell = newRow.insertCell(4);
    newCell.innerHTML = 'Fri';
    var newCell = newRow.insertCell(5);
    newCell.innerHTML = 'Sat';
    var newCell = newRow.insertCell(6);
    newCell.innerHTML = 'Sun';
    for(var kk=1;kk<=6;kk++) {
	var newRow  = calendar.insertRow(kk+1);
		for(var tt=1;tt<=7;tt++) {
				num=7 * (kk-1) - (-tt);
				var newCell = newRow.insertCell(tt-1);
				newCell.id = 'v' + num;
                newCell.className = 'cal_cell';
				newCell.innerHTML = '&nbsp;';
			}
		}
   var newRow  = calendar.insertRow(8);
   var newCell = newRow.insertCell(0);
   SetAttr(newCell, 'colSpan', 7);
   newCell.className = 'calc_today';
   newCell.onclick = this.today;
   newCell.innerHTML = 'Today: '+this.addnull(this.sccd,this.sccm+1,this.sccy);   document.body.appendChild(calendar);
   var position = defMousePosition(event);
   calendar.style.top =  position.y  +"px";
   calendar.style.left =  position.x  + "px";
   //box.parentNode.insertBefore(calendar,box);
   this.prepcalendar('',this.ccm,this.ccy);
  }


CalendarClass.prototype.set_select = function(day,id){
	var selectBox = ById(id);
    if(selectBox){
        for(var i=0;i<selectBox.length;i++)
        	{        	if(selectBox[i].value == day){
        		selectBox[i].selected=true;
              return;
              }        	}
       }
	}

CalendarClass.prototype.cs_click = function(e){
    this.target_box.value = this.calvalarr[this.evtTgt(this.EvtObj(e)).id.substring(1,this.evtTgt(this.EvtObj(e)).id.length)]
	ById('fc').parentNode.removeChild(ById('fc'));
	ClearEvent(e);
}

CalendarClass.prototype.evtTgt = function(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;
}
CalendarClass.prototype.EvtObj = function(e){if(!e)e=window.event;return e;}

CalendarClass.prototype.f_cps = function(obj){
	obj.style.background='#fff';
	obj.style.color='#333';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #606060';
	obj.style.cursor='pointer';
}
CalendarClass.prototype.f_cpps = function(obj){
	obj.style.background='#c4d3ea';
	obj.style.color='#ff3300';
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	obj.style.cursor='default';
}
CalendarClass.prototype.f_hds = function(obj){
	obj.style.background='#fff799';
	obj.style.color='#333';
	obj.style.textAlign='center';
	obj.style.cursor='pointer';
}

// day selected
CalendarClass.prototype.prepcalendar = function(hd, cm, cy )
{
	now=new Date();
	sd=now.getDate();
	td=new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd=td.getDay();
	if (cd==0)cd=6; else cd--;
	ById('mns').innerHTML=this.mn[cm]+ ' ' + cy;

	this.marr=((cy%4)==0)?this.mnl:this.mnn;

	for(var d=1;d<=42;d++)
	{
		this.f_cps ( ById ( 'v' + parseInt ( d ) ) );
		if ( ( d >= (cd -(-1) ) ) && ( d<=cd-(-this.marr[cm]) ) )
		{
			var el  = ById('v'+parseInt(d));
			if(el){
			el.onmouseover= function(){this.style.background='#2a76f4';}
			el.onmouseout= function(){this.style.background='#fff';}
			//el.onclick=this.cs_click;
			var obj = this;
             el.onclick = function(e){obj.cs_click(e)};//CalendarClass.prototype.cs_click
			// if today
			if (this.sccm == cm && this.sccd == (d-cd) && this.sccy == cy)
				el.style.color='#ff3333';
			el.innerHTML=d-cd;
            }
			this.calvalarr[d]=this.addnull(d-cd,cm-(-1),cy);
		}
		else
		{
			ById('v'+d).innerHTML='&nbsp;';
			var el =  ById('v'+parseInt(d));
			if(el){
				el.onmouseover=null;
				el.onmouseout=null;
				el.onclick=null;
				el.style.cursor='default';
			}
		}
	}
}

CalendarClass.prototype.caddy = function(e){
	this.ccy++;
	this.marr=((this.ccy%4)==0)?this.mnl:this.mnn;
	this.prepcalendar('',this.ccm,this.ccy);
	ClearEvent(e);
}

CalendarClass.prototype.csuby = function(e){
	this.ccy--;
	this.marr=((this.ccy%4)==0)?this.mnl:this.mnn;
	this.prepcalendar('',this.ccm,this.ccy);
	ClearEvent(e);
}

//prepcalendar('',ccm,ccy);
CalendarClass.prototype.caddm = function(e){
	this.marr=((this.ccy%4)==0)?this.mnl:this.mnn;

	this.ccm+=1;
	if (this.ccm>=12) {
		this.ccm=0;
		this.ccy++;
	}
	this.prepcalendar('',this.ccm,this.ccy);
	ClearEvent(e);
}
CalendarClass.prototype.csubm = function(e){
	this.marr=((this.ccy%4)==0)?this.mnl:this.mnn;

	this.ccm-=1;
	if (this.ccm<0) {
		this.ccm=11;
		this.ccy--;
	}
	this.prepcalendar('',this.ccm,this.ccy);
	ClearEvent(e);
}
CalendarClass.prototype.today = function(){
	updobj.value=this.addnull(this.now.getDate(),this.now.getMonth()+1,this.now.getFullYear());
	ById('fc').style.display='none';
	this.prepcalendar('',this.sccm,this.sccy);
}

CalendarClass.prototype.addnull = function(d,m,y){
	var d0='',m0='';
	if (d<10)d0='0';
	if (m<10)m0='0';
	return ''+d0+d+'/'+m0+m+'/'+y;
}

// Calendar script
calendar = new CalendarClass();
function calendar_show(box,target_id,event)
	{
    var event = event || window.event;
    if(box)
    	{
        calendar.target_box = ById(target_id);
        calendar.Calendar_show(box,event);
    	}
	}
