/*
	Copyright Interactive Co.,Ltd 2005;
	cs_caledar.js v 1.0 2005-03-19;
	last update : 2005-03-24;
	Calendar Javascript File;
*/
var coords = { left:0, top:0 };

function cs_calendar( self, name )
{
	this._set_format = '';
	this.i_year = 1;
	this.i_month = 1;
	this.i_day = 1;

	if( self.length > 0 )
		this._self = self;
	else
		alert( "INPUT BOX-ын нэрийг оруулна уу" );

	if( name.length > 0 )
		this._name = name;
	else
		alert( "Javascript Class обьектыг өөрийг нь орууна уу" );
	
	this.calendar_days = function( c_Year, c_Month )
	{
		var nextMonth = new Date( c_Year, c_Month, 1 );
			nextMonth.setHours( nextMonth.getHours() - 3 );

		return nextMonth.getDate();
	}

	this.calendar_showhide = function( evt )
	{
		var m_div = document.getElementById( 'calendartbl' );
		if( m_div.style.visibility == 'visible' )
			m_div.style.visibility = 'hidden';
		else
		{
			m_div.style.visibility = 'visible';
			var s_top = 0, s_left = 0;
			evt = (evt) ? evt : event;

		    if (evt.pageX) 
			{
		        s_left = evt.pageX;
		        s_top = evt.pageY;
		    } 
			else if (evt.clientX) 
			{
		        s_left = document.body.scrollLeft - document.body.clientLeft;
		        s_top =  document.body.scrollTop - document.body.clientTop;
		        if (document.body.parentElement && document.body.parentElement.clientLeft) 
				{
		            var bodParent = document.body.parentElement;
		            s_left += bodParent.scrollLeft - bodParent.clientLeft;
		            s_top += bodParent.scrollTop - bodParent.clientTop;
				}
			}
//			alert( "TOP : "+s_top+", LEFT : "+s_left );
			m_div.style.top = ( ( evt.clientY - coords.top ) + s_top ) + 'px';
			m_div.style.left = ( ( evt.clientX - coords.left ) + s_left ) + 'px';

			this.calendar_init( this.i_year, this.i_month );
		}
	}
	
	this.calendar_today = function()
	{
		var today = new Date();
		this.i_year = today.getFullYear();
		this.i_month = today.getMonth();
		this.i_day = today.getDate();
	}
	
	this.calendar_init = function( g_year, g_month )
	{
		this.calendar_today();

		var week = Array( 'Да', 'Мя', 'Лх', 'Пү', 'Ба', 'Бя', 'Ня' );
		var week_disc = Array( 'Даваа', 'Мягмар', 'Лхагва', 'Пүрэв', 'Баасан', 'Бямба', 'Ням' );
		var counter = 0, newR, newC, tbody = document.getElementById( 'cdr' );
    	var firstDate = new Date( g_year, g_month-1, 1 ), bday = firstDate.getDay();
		var daycounter = 1;
	
		var prev_month, next_month, prev_year, next_year;
			prev_month = ( parseInt( g_month ) > 1 )? parseInt( g_month )-1 : g_month;
			next_month = ( parseInt( g_month ) < 12 )?parseInt( g_month )+1 : g_month;
			prev_year = ( parseInt( g_year ) > ( this.i_year - 5 ) ) ? g_year-1: g_year;
			next_year = ( parseInt( g_year ) < ( this.i_year + 5 ) ) ? g_year+1: g_year;
		
		for( var del_i = 0; del_i < 12; del_i++ )
		{
			if( tbody.rows.length > 0 )
			{
				for( var d_i = 0; d_i < tbody.rows.length; d_i++ )
					tbody.deleteRow();
			}
		}

		if( tbody.rows.length == 0 )
		{
			bday = ( bday == 0 ) ? 7 : bday;
			i_days = this.calendar_days( g_year, g_month, 1 ) + bday - 1;
			newR = tbody.insertRow( tbody.rows.length );
			newC = newR.insertCell( newR.cells.length );
//			newC.bgColor = "#F2F5E9";
			newC.colSpan = 7;
//			newC.
			newC.align = 'center';
			var strhead = '';
				strhead = '<a style="text-decoration:none;" title="'+prev_year+' он" href="javascript:'+this._name+'.calendar_init( ' + prev_year + ', ' + g_month + ' )">';
				strhead += '<strong><font style="color: #252525; font-size: 7px;"><<</font></strong></a><font style="font-size: 9px;" color="#0095DD"><strong>' + g_year + ' он</strong></font>';
				strhead += '<a style="text-decoration:none;" title="'+next_year+' он" href="javascript:'+this._name+'.calendar_init( ' + next_year + ', ' + g_month + ' )">';
				strhead += '<strong><font style="color: #252525; font-size: 7px;">>></font></strong></a>&nbsp;&nbsp;&nbsp;&nbsp;';
				strhead += '<a style="text-decoration:none;" title="'+prev_month+'-p cap" href="javascript:'+this._name+'.calendar_init( ' + g_year + ', ' + prev_month + ' )">';
				strhead += '<strong><font style="color: #252525; font-size: 7px;"><<</font></strong></a><font style="font-size: 9px;" color="#0095DD"><strong>'+ g_month + '-p cap</strong></font>';
				strhead += '<a style="text-decoration:none;" title="'+next_month+'-p cap" href="javascript:'+this._name+'.calendar_init( ' + g_year + ', ' + next_month + ' )">';
				strhead += '<strong><font style="color: #252525; font-size: 7px;">>></font></strong></a>';
			newC.innerHTML = strhead;

/*			newR = tbody.insertRow( tbody.rows.length );
			newC = newR.insertCell( newR.cells.length );
			newC.bgColor = "#252525";
			newC.height = "2px";
			newC.colSpan = 7;
			newC.innerHTML = '&nbsp;';*/

			newR = tbody.insertRow( tbody.rows.length );
			for( var i_week = 0; i_week < week.length; i_week++ )
			{
				newC = newR.insertCell( newR.cells.length );
//				newC.bgColor = "#F5BD0E";
				newC.align = 'center';
				newC.title = week_disc[i_week];
				newC.innerHTML = '<font  style="font-size: 9px;" color="#707070">' + week[i_week] + '</font>';
			}

			for( var i = 1; i <= i_days; i++ )
			{
				if( counter == 0 )
					newR = tbody.insertRow( tbody.rows.length );

				newC = newR.insertCell( newR.cells.length );
				newC.align = 'center';
				newC.width = 24;
				if( i < bday )
					newC.innerHTML = "&nbsp;"
				else
				{
					if( g_year == this.i_year && 
						g_month == this.i_month && 
						daycounter == this.i_day )
						newC.bgColor = "#CCFFDD";
//					else if( counter == 5 || counter == 6 )
//						newC.bgColor = "#AF8FFF";
					else
						newC.bgColor = "#F0F0F0";

					newC.title = g_year+"-"+g_month+"-"+daycounter;
					if( counter != 6 )
						newC.innerHTML = '<a style="text-decoration:none;" href="javascript:'+this._name+'.calendar_select('+g_year+', '+g_month+', '+daycounter+'); '+this._name+'.calendar_showhide( event );"><font style="color: #707070">' + daycounter + '</font></a>';
					else
						newC.innerHTML = '<a style="text-decoration:none;" href="javascript:'+this._name+'.calendar_select('+g_year+', '+g_month+', '+daycounter+'); '+this._name+'.calendar_showhide( event );"><font style="color: #F5878A">' + daycounter + '</font></a>';
					daycounter++;
				}

				if( counter == 6 )
					counter = 0;
				else
					counter ++;
			}
		}
	}
	this.calendar_format = function( usrformat )
	{
		this._set_format = usrformat;
	}

	this.calendar_select = function( s_year, s_month, s_day )
	{
		var text = document.getElementById( this._self ), date;

		if( parseInt( s_month ) < 10 )
			s_month = "0" + s_month;

		if( parseInt( s_day ) < 10 )
			s_day = "0" + s_day;

		switch( this._set_format )
		{
			case 'DD/MM/YYYY':
				date = s_day +"/"+s_month+"/"+s_year;
				break;
			case 'YYYY/MM/DD':
				date = s_year +"/"+s_month+"/"+s_day;
				break;
			default:
				date = s_year +"-"+s_month+"-"+s_day;
		}
		text.value = date;
	}
	
	this.calendar_echo = function()
	{
		var tags = '';
			tags = '<div id="calendartbl" style="position:absolute; top: 0; left: 0; visibility:hidden; z-index:9999999">';
			tags += '<table bgcolor="#FFFFFF" id="cdr" border="0" cellpadding="0" cellspacing="1" width="154" style="BORDER: 1px solid #A2A2A2;">';
			tags += '<tbody></tbody></table></div>';

//		document.write( tags );
		return tags;
	}
	
	this.calendar_text = function()
	{
//		alert( this._self );
		this.calendar_today();
		var textinput = '';
		this.i_month = parseInt( this.i_month ) + 1;
		if( parseInt( this.i_month ) < 10 )
			this.i_month = "0" + this.i_month;

		if( this.i_day < 10 )
			this.i_day = "0" + this.i_day;

			textinput = '<input type="text" id="' + this._self + '" class="ccalendar" name="'+this._self+'" size="8" maxlength="10" value="' + this.i_year + '-' + this.i_month + '-' + this.i_day + '" >';
			textinput += '<img onClick="' + this._name + '.calendar_showhide( event );" src="/image/b_calendar.png" border="0" align="absmiddle" alt="Дар..">';

//		document.write( textinput );
		return textinput;
	}
	
	this.calendar_has = function( vvalue )
	{
		var txt_obj = document.getElementById( this._self );
//		alert( this._self + ", " + vvalue + "OBJ" + txt_obj );
		txt_obj.value = vvalue;
	}
}

function beginCoord( vtop, vleft )
{
	coords.top = vtop; 
	coords.left = vleft;
}
