/*-------------------------------------------------------------------------------------------------------------
SHOW CALENDAR
-------------------------------------------------------------------------------------------------------------*/


var oCalendarPopup = new Object();

	oCalendarPopup.body_content = false;
	oCalendarPopup.date = false;
	oCalendarPopup.divTemplate = false;
	oCalendarPopup.controller = false;

	oCalendarPopup.popup = false;
	oCalendarPopup.popup_id = 'calendar';
	oCalendarPopup.popup_content = false;
	oCalendarPopup.aPosition = new Array(0,0);
	oCalendarPopup.visibility = false;

	oCalendarPopup.event_obj = false;
	oCalendarPopup.onclick = false;	
	oCalendarPopup.hide_after_click = true;
	
	oCalendarPopup.request = null;
	oCalendarPopup.url = new Object();
	oCalendarPopup.url.base = 'ajax_calendar.php?';
	oCalendarPopup.url.extra  = '';
	oCalendarPopup.onload = false;
	

	oCalendarPopup.construct = function ( sType, sDate, oPopup, sContentId, divTemplate )
	{
		this.url.type = (sType||false) ? '&type='+sType : '';
		this.date = sDate||false ? sDate : false;
		this.visbility = false;
		this.body_content = gid('body_content');
		this.divTemplate = (divTemplate||false) ? '&divTemplate='+divTemplate : '';
		
		if ( oPopup||false )
		{
			this.popup = oPopup;
		}
		else
		{
			this.popup = document.createElement('div');
			this.popup.setAttribute("id",this.popup_id);
			this.popup.style.display ='none';
			this.popup.style.position ='absolute';
			this.popup.style.left = 0;
			this.popup.style.top  = 0;
			//this.popup.style.width = 205;
			//this.popup.style.height = 181;
			this.popup.style.backgroundColor = "#7f9db9";
			
			var eBody = document.getElementsByTagName("body").item(0);
			window.onload = function () {
				
				document.getElementsByTagName("body").item(0).appendChild(oCalendarPopup.popup);
			};
		}
		
		if ( sContentId||false )
		{
			this.popup_content = gid(sContentId);
		}
		else
		{
			this.popup_content = this.popup;
		}
	};
	
	
	oCalendarPopup.load = function ( sDate, obj)
	{
		
		this.date = sDate||false ? sDate : false;
		// for home page search
		/*if(obj.id == 'es_when') {
			this.url.extra += '&homepage=yes';

		} */
		
			var url = this.url.base + 'date='+this.date + this.url.type + getURLRandom() + this.url.extra + this.divTemplate;
		
		
		var bVisibility = this.visbility;
		var bOnload = this.onload ? true : false;
		
		var func = function ()
		{
			
			oCalendarPopup.popup_content.innerHTML = oCalendarPopup.request.responseText;
			if ( !bVisibility ) {oCalendarPopup.display('',obj);}
			if ( bOnload ) {oCalendarPopup.onload();}
		};
		
		this.request = GetXmlHttpObject();
		this.request.onreadystatechange = function () { onRSC(oCalendarPopup.request,func); };
		this.request.open( "GET", url, true );
		this.request.send( null );
	};


	oCalendarPopup.place = function ( aPosition )
	{
		this.aPosition = aPosition;
	};
	
	
	oCalendarPopup.display = function ( aPosition, obj, func)
	{
		//alert(this.aPosition);
		this.aPosition = aPosition||false ? aPosition : this.aPosition;
		this.visbility = true;
		
		this.popup.style.left = this.aPosition[0]+ 'px';
		this.popup.style.top  = this.aPosition[1]+ 'px';
		oCalendarPopup.controller = obj;
		
		if( oCalendarPopup.popup.style.display == 'none'  )
		{
			oCalendarPopup.popup.style.display ='block';

			oCalendarPopup.body_content.onmouseup = function () { 
				oCalendarPopup.hide();
				if(func) {func(false, obj);}
			};
			
			oCalendarPopup.controller.onmouseup = function(e) {
				hideOthers(1);
				if( !e )
				{
					e = window.event;
					e.cancelBubble = true;
					return false;
				}
				e.stopPropagation();
				
				return false;
			};

			if(func) {func( true, obj);}
		}
		else
		{
			oCalendarPopup.popup.style.display ='none';
			//oCalendarPopup.popup_content.innerHTML==''
			oCalendarPopup.body_content.onmouseup = null;
			oCalendarPopup.controller.onmouseup = null;
			if(func) {func(false, obj);}
		}
		
	};

	
	oCalendarPopup.hide = function ()
	{
		this.visbility = false;
		
		if ( this.popup.style.display!='none' )
		{
			this.popup.style.display = 'none';
			this.body_content.onmouseup = null;
			//this.popup_content.innerHTML==''
		}
	};
	
	
	oCalendarPopup.get = function ()
	{
		if ( oCalendarPopup.event_obj )
		{
			var aEls  = this.popup_content.getElementsByTagName('input');
			var aDate = Array( this.event_obj.getAttribute('date'), 
								aEls[0].value,
								aEls[1].value,
								this.event_obj.innerHTML
							  );
			//alert(this.event_obj.getAttribute('date'));
			//alert(aDate);
			return aDate;
		}
	};
	

	oCalendarPopup.click = function (obj)
	{
		oCalendarPopup.event_obj = obj;		
		if ( this.onclick ) {oCalendarPopup.onclick();}
		if ( this.hide_after_click ) {this.hide();}
		oCalendarPopup.event_obj = false;
	};


function get_calendar_date( obj )
{
	oCalendarPopup.click(obj);
}

function load_new_calendar( sDate )
{
	oCalendarPopup.load( sDate );
}


/*-------------------------------------------------------------------------------------------------------------
PARSE DATE FROM INPUT
-------------------------------------------------------------------------------------------------------------*/


function parse_date_input(sInput)
{
	var iY = false;	
	var iYear  = 0;
	var iMonth = 0;
	var iDay   = 0;
	
	// YEAR
	if ( (iY=sInput.search(/\b(\d{4})\b/))>-1 )
	{
		iYear = sInput.match(/\b(\d{4})\b/)[1];
	}
	else
	{
		return false;
	}
	
	// MONTH & DAY
	if ( /[a-z]/i.test(sInput) )
	{
		for (var i=1;i<=12;i++)
		{
			var reM = new RegExp( aMonthes[i], "i" );
			if ( reM.test(sInput) )
			{
				iMonth = i;
				break;
			}
		}
		
		var aM = sInput.match(/\b(\d{1,2})\b/);
		iDay = (aM||false) ? aM[1] : 0;
		
		if ( iMonth==0 || iDay==0 ) {return false;}
	}
	else
	{
		var iTmp = sInput.search(/\b(\d{1,2})\b/);
		if (iTmp==-1) {return false;}
		
		var aTmp = sInput.match(/\b(\d{1,2})\b/g);
		if ( aTmp.length!=2 ) {return false;}
		
		
		iMonth = iTmp>iY ? aTmp[0] : aTmp[1];
		iDay = iTmp>iY ? aTmp[1] : aTmp[0];
	}
	
	iYear  = parseInt(iYear,10);
	iMonth = parseInt(iMonth,10);
	iDay   = parseInt(iDay,10);
	
	if (iMonth>12 || iDay>31) {return false;}
	
	var sDate = iYear+'-'+(iMonth<10?'0':'')+iMonth+'-'+(iDay<10?'0':'')+iDay;
	
	return new Array( sDate,iYear,iMonth,iDay );
}