// Select the object with the id [objID]
function $(objID)
{
	if (document.getElementById) {return document.getElementById(objID);}
    	else if (document.all) {return document.all[objID];}
    		else if (document.layers) {return document.layers[objID];}
}

// Check if the click is outside the calendar
function checkClick(e) {
	e ? (evt=e) : (evt=event);
	CSE = (evt.target) ? (evt.target) : (evt.srcElement);
	if ($('fc')) {
		if (!isChild(CSE, $('fc')))
			$('fc').style.display = 'none';
	}
}

// check if [s] is a chid of [d]
function isChild(s,d) {
	while(s) {
		if (s==d) 
			return true;
		s=s.parentNode;
	}
	return false;
}

// get the left side of [obj]
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;
}

// get the top side of [obj]
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;
}

/*
 * 
 * PREPARE CALENDAR LAYOUT
 *  
 */
document.write('<table id="fc">');
document.write('<tr>'+
					'<td id="fc_prev_month" onclick="csubm()">&nbsp;</td>'+
					'<td colspan="5" id="fc_current_month"></td>'+
					'<td id="fc_next_month" onclick="caddm()">&nbsp;</td>'+
				'</tr>');
document.write('<tr id="fc_daysofweek">'+
					'<td>Du</td>'+
					'<td>Lu</td>'+
					'<td>Ma</td>'+
					'<td>Mi</td>'+
					'<td>Jo</td>'+
					'<td>Vi</td>'+
					'<td>Sa</td>'+
				'</tr>');
for(var kk=1;kk<=6;kk++) {
	document.write('<tr class="fc_days">');
	for(var tt=1;tt<=7;tt++) {
		num=7 * (kk-1) - (-tt);
		document.write('<td id="v' + num + '">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table>');

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

// Calendar script
var now = new Date;
var sccm = now.getMonth();
var sccy  = now.getFullYear();
var ccm = now.getMonth();
var ccy  = now.getFullYear();
var g_time = '00:00';

var updobj;
var calendar_clicked = false;

var months = new Array('Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie');
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');
var calvalarr = new Array(42);

// Pass to the next month when the right arrow is pressed 
function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	cdayf();
	prepcalendar('',ccm,ccy);
}

// Pass to the previous month when the left arrow is pressed
function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm-=1;
	if (ccm<0) {
		ccm=11;
		ccy--;
	}
	prepcalendar('',ccm,ccy);
}

function cdayf() {
	if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
		return;
}

/*
 * 
 * INITIATE CALENDAR DISPLAY
 * 
 */
function lcs(ielem) {
	// Display the calendar
	updobj=ielem;
	$('fc').style.left = Left(ielem) + 'px';
	$('fc').style.top  = Top(ielem)+ielem.offsetHeight + 'px';
	$('fc').style.display='block';
	
	// First check if date is valid
	temp_split = ielem.value.split('-');
	is_date = true;
	
	if (temp_split.length != 3) { is_date = false;} 
	else {
		date = [temp_split[0], temp_split[1], temp_split[2]];

		if (isNaN(date[0]) || isNaN(date[1]) || isNaN(date[2])) { is_date = false; } 
		else if ((date[2] < 0) || (date[2] > 31)) { is_date = false; }
		else if ((date[1] < 0) || (date[1] > 12)) { is_date = false; }
		else if ((date[0] < 1900) || (date[0] > 2100)) { is_date = false; }
	}
	
	// Date is valid, now prepare the calendar
	if (is_date) {
		calendar_month = date[1]-1;
		calendar_year  = date[0];
		prepcalendar(date[2], date[1]-1, date/* based on --- Swazz Javascript Calendar v 1.0 3rd November 2006 ---
 * By Oliver Bryant (http://calendar.swazz.org)
 *
 * Refactored by Daia Lucian (http://www.invite.ro) && (http://www.wizartstudios.com)
 */
		[0]);
	}
	
}

// when the calendar input changes, update the time in the calendar
function parseCalendarDate(object)
{
	temp_split = object.value.split('/');
	
	if (temp_split.length != 3) { return false; } 
	else {
		time_split = temp_split[2].split(' ');
		
		if (time_split.length != 2) { return false; } 
		else {
			date = [temp_split[0], temp_split[1], time_split[0]];
			g_time = time_split[1];
		}
	}
}

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) {
	evtTgt(EvtObj(e)).style.background='#FFCC66';
}
function cs_out(e) {
	evtTgt(EvtObj(e)).style.background='#C4D3EA';
}
function cs_click(e) {
	updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	$('fc').style.display='none';
}

function f_cps(obj) {
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

function f_cpps(obj) {
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#ABABAB';
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='default';
}

function f_hds(obj) {
	obj.style.background='#FFF799';
	obj.style.font='bold 10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

// 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();
	$('fc_current_month').innerHTML= months[cm]+ ' ' + cy;
	marr=((cy%4)==0)?mnl:mnn;
	for(var d=1;d<=42;d++) {
		f_cps($('v'+parseInt(d)));
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			dip=((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
			htd=((hd!='')&&(d-cd==hd));
			if (dip)
				f_cps($('v'+parseInt(d)));
			else if (htd)
				f_hds($('v'+parseInt(d)));
			else
				f_cps($('v'+parseInt(d)));

			$('v'+parseInt(d)).onmouseover=cs_over;  //(dip)?null:cs_over;
			$('v'+parseInt(d)).onmouseout=cs_out; //(dip)?null:cs_out;
			$('v'+parseInt(d)).onclick=cs_click; //(dip)?null:cs_click;
			
			$('v'+parseInt(d)).innerHTML=d-cd;
			
			c_day = d-cd;
			c_mnth = cm+1;
			if (c_day < 10) { c_day = '0' + c_day; }
			if (c_mnth < 10) { c_mnth = '0' + c_mnth; }
			calvalarr[d]='' + cy + '-' + c_mnth + '-' + c_day;
		}
		else {
			$('v'+d).innerHTML='&nbsp;';
			$('v'+parseInt(d)).onmouseover=null;
			$('v'+parseInt(d)).onmouseout=null;
			$('v'+parseInt(d)).style.cursor='default';
		}
	}
}

prepcalendar('',ccm,ccy);
//$('fc'+cc).style.visibility='hidden';