var _Now = new Date();
var ORG_DATE_FORMAT = 13;
var ORG_DATE_SEPARATOR = ' de ';
var ORG_DATE_START_DAY = 0;
//var ORG_LANGUAGE_CODE = 1033;
var ORG_NUMBER_FORMAT = 'us';
var ORG_TIME_FORMAT = 0;
//var ORG_CURRENCY_SYMBOL = '$';
var ORG_SHOW_WEEK_NUMBER = '0';

var _calendario;
var _oCalInput;

var _sCalMonths		=	new	Array
						(
							"Jan",
							"Fev",
							"Mar",
							"Abr",
							"Maio",
							"Jun",
							"Jul",
							"Ago",
							"Set",
							"Out",
							"Nov",
							"Dez"
						);
						
var _sCalLongMonths	=	new	Array
						(
							"Janeiro",
							"Fevereiro",
							"Março",
							"Abril",
							"Maio",
							"Junho",
							"Julho",
							"Agosto",
							"Setembro",
							"Outubro",
							"Novembro",
							"Dezembro"
						);
							
var _sCalDays		=	new Array
						(
							"D", 
							"S", 
							"T", 
							"Q", 
							"Q", 
							"S", 
							"S"
						);
						
var _sCalLongDays	= new Array
						( 
							"Domingo",  
							"Segunda-feira",  
							"Terça-feira",  
							"Quarta-feira",  
							"Quinta-feira",  
							"Sexta-feira",  
							"Sábado" 
						);

/*
	_iCalFormat Formats
	0  - M/d/yy
	1  - M/d/yyyy
	2  - MM/dd/yy
	3  - MM/dd/yyyy
	4  - d/M/yy
	5  - d/M/yyyy
	6  - dd/MM/yy
	7  - dd/MM/yyyy
	8  - yy/M/d
	9  - yyyy/M/d
	10 - yy/MM/dd
	11 - yyyy/MM/dd
*/

//the following two arrays date formats and timeformats are not localized by design
var _sDateFormats	= new Array
						(
							"M/d/yy",
							"M/d/yyyy",
							"MM/dd/yy",
							"MM/dd/yyyy",
							"d/M/yy",
							"d/M/yyyy",
							"dd/MM/yy",
							"dd/MM/yyyy",
							"yy/M/d",
							"yyyy/M/d",
							"yy/MM/dd",
							"yyyy/MM/dd",
							"yyyy/MM/dd"
						);
						
var _sTimeFormats	= new Array
						(
							"h:mm tt",
							"hh:mm tt",
							"H:mm",
							"HH:mm",
							"h:mm:ss tt",
							"hh:mm:ss tt",
							"H:mm:ss",
							"HH:mm:ss"
						);
						
var _iCalStartDay	= ORG_DATE_START_DAY;
var _dCalMinDate	= new Date(2001, 2, 5);
var _dCalMaxDate	= new Date(2100, 11, 31);
var _sCalSeperator	= ORG_DATE_SEPARATOR;
var _iCalFormat		= ORG_DATE_FORMAT;

var _sShowWeekNumbers = ORG_SHOW_WEEK_NUMBER;
var iShowWeekCalWidth = 170;
var iNoWeekCalWidth   = 155;
var iCalHeight        = 162;


function InitCalendar(iDateFormat, sSeperator, iStartDay, dMinDate, dMaxDate)
{
	_iCalFormat = iDateFormat;
	
	if (sSeperator)
	{
		_sCalSeperator = sSeperator;
	
		if (iStartDay)
		{
			_iCalStartDay = iStartDay;
			
			if (dMinDate)
			{
				_dCalMinDate = dMinDate;
				
				if (dMaxDate)
				{
					_dCalMaxDate = dMaxDate;
				}
			}
		}
	}
}
function EstilizarCalendario() {
	with(_calendario.style) {
		position = 'absolute';
		left = '530px';
		top = '45px';
		zIndex = '123564';
	}
}
function someCalendario() {
	document.getElementById('calendario').style.display = 'none';
	document.getElementsByTagName('p')[0].getElementsByTagName('img')[0].style.display = 'inline';
}
function mostraCalendario() {
	document.getElementById('calendario').style.display = 'block';
	document.getElementsByTagName('p')[0].getElementsByTagName('img')[0].style.display = 'none';
}

function LaunchCalendar(oInputElem, dInitDate) {
	if(isNaN(dInitDate))
		dInitDate = new Date();
	_oCalInput = oInputElem;
	var iCalWidth = _sShowWeekNumbers == "1" ? iShowWeekCalWidth : iNoWeekCalWidth;
	try{document.getElementById('calendario').parentNode.removeChild(document.getElementById('calendario'));}catch(e){}
	_calendario = document.createElement('div');
	_calendario.setAttribute('id','calendario');
	if(navigator.appName == 'Microsoft Internet Explorer')
		_calendario.innerHTML = '<table style="border-collapse:collapse;width:100%;">' + DrawMonth(dInitDate, "ReturnDate(this);", HILITE_NONE, null).innerHTML + '</table>';
	else
		_calendario.appendChild(DrawMonth(dInitDate, "ReturnDate(this);", HILITE_NONE, null));
	EstilizarCalendario();
	_calendario.style.width = iCalWidth+'px';
	_calendario.style.height = iCalHeight+'px';
	if(navigator.appName != 'Microsoft Internet Explorer')
	    _calendario.onmouseout = someCalendario;
	else
	    _calendario.onmouseleave = someCalendario;
	_calendario.onmouseover = mostraCalendario;
	_calendario.onmousemove = mostraCalendario;
	for(i=0;i<document.getElementsByTagName('p').length;i++) {
		if(document.getElementsByTagName('p')[i].getAttribute('class') == 'calendario' || document.getElementsByTagName('p')[i].attributes['class'] == 'calendario' || document.getElementsByTagName('p')[i].className == 'calendario') {
			document.getElementsByTagName('p')[i].appendChild(_calendario);
			break;
		}
	}
	mostraCalendario();
	//_calendario.show(0, 18, iCalWidth, iCalHeight, event.srcElement);
}// Returns true if the return value is set to a valid date, false otherwise.

function ChangeDate(){
    //var DataTxt = document.getElementById("CalOnline").value;
	var DataTxt = document.getElementById("CalOnline").innerHTML;
    //DataTxt = DataTxt.replace('de','');
    var regEx = new RegExp (' de ', 'gi') ;
    DataTxt = DataTxt.replace(regEx,'/');
    var DateArr = DataTxt.split('/');
    var Day = DateArr[0];
    var Month = DateArr[1];
        
    for(i=0;i<12;i++)
        {
        if(_sCalLongMonths[i]==DateArr[1])
            {
            Month=i+1;
            break;
            }
        }
    var Year = DateArr[2];
   // var _Date = new Date(Day,Month-1,Year);
   
        var Data = Day + "/" + Month + "/" + Year;
        
        /*var _Campo = document.createElement('input');
        _Campo.setAttribute('name', 'ValorReferenceDate');
        _Campo.setAttribute('id', 'ValorReferenceDate');
        _Campo.setAttribute('value', Data);
        document.forms[0].submit();*/
        eraseCookie('ValorReferenceDate');
        createCookie('ValorReferenceDate',Data);
        window.location.reload();
        //document.forms[0].submit();//location.href=location.href;
}

function criaElemento(el) {
	if(el == undefined)
		return;
	try {
		var elemento = document.createElement(el);
		return elemento;
	}
	catch(e) {return;}
}
function DrawMonth(D, sOnClick, hiliteCode, hiliteDay) { 
	// Determine hiliting range
	var dHiliteStart = null;
	var dHiliteEnd   = null;
	
	switch(hiliteCode) {
		case HILITE_DAY:
			dHiliteStart = new Date( hiliteDay );
			dHiliteEnd   = new Date( hiliteDay );
			break;
		case HILITE_WEEK:
			dHiliteStart = new GetFirstDayOfWeek( hiliteDay );   
			dHiliteEnd   = new GetLastDayOfWeek( hiliteDay ); 
			break;
		default:
			// Do nothing-- rather than let the user get an error, emulate HILITE_NONE.
			break;
	}
	
	// We are stuck with a full DateTime even though we only want to
	// check at the date level.  So, adjust the hilite start/end times
	// to be at the start/end of the days, respectively.  Otherwise
	// our date comparisons won't work.
	if(dHiliteStart != null) {
		dHiliteStart.setMilliseconds( 0 );
		dHiliteStart.setSeconds( 0 );
		dHiliteStart.setMinutes( 0 );
		dHiliteStart.setHours( 0 );
	}
	if(dHiliteEnd != null) {
		dHiliteEnd.setMilliseconds( 999 );
		dHiliteEnd.setSeconds( 59 );
		dHiliteEnd.setMinutes( 59 );
		dHiliteEnd.setHours( 23 );
	}
	
	var tmpDate = new Date( D );
	tmpDate.setHours(0,0,0,0);
	
	var dToday		= new Date();
	
	dToday.setHours(0,0,0,0);
	
	var iToday		= dToday.valueOf();
	
	var dInitDate	= new Date(tmpDate.valueOf());
	var iInitMonth	= dInitDate.getMonth();

	tmpDate = GetFirstDayInCalendar(tmpDate);

	// write out the top navigation
	//
	var _table = criaElemento('table');
	var _tableMes = criaElemento('table');
	var _tr = criaElemento('tr');
	var _td = criaElemento('td');
	var _a = criaElemento('a');
	
	var iValue = dInitDate.valueOf();
	_a.innerHTML = tmpDate<_dCalMinDate?'':'&lt;';
	_a.href = '#';
	_a.setAttribute('onclick', sOnClick);
	_a.setAttribute('rel', iValue);
	_a.className = 'nav';
	_td.style.textAlign = 'center';
	_td.appendChild(_a);
	_tr.appendChild(_td);
	
	_a = criaElemento('a');
	_a.href = '#';
	_a.setAttribute('rel', iValue);
	_td = criaElemento('td');
	_a.innerHTML = _sCalMonths[iInitMonth] + ' ' + dInitDate.getFullYear();
	_a.setAttribute('onclick', sOnClick);
	_td.style.textAlign = 'center';
	_td.style.width = '109px';
	_td.appendChild(_a);
	_tr.appendChild(_td);
	
	var dLastDay = new Date(tmpDate.valueOf());
	dLastDay.setDate(dLastDay.getDate() + 42);
	_a = criaElemento('a');
	_a.setAttribute('rel', iValue);
	_a.href = '#';
	_td = criaElemento('td');
	_a.innerHTML = dLastDay>_dCalMaxDate?'':'&gt;';
	_a.setAttribute('onclick', sOnClick);
	_a.className = 'nav';
	_td.style.textAlign = 'center';
	_tr.style.height = '16px';
	_td.appendChild(_a);
	_tr.appendChild(_td);
	_tableMes.appendChild(_tr);
	_tableMes.style.width = '100%';
	
	_td = criaElemento('td');
	_tr = criaElemento('tr');
	_td.setAttribute('colspan', _sShowWeekNumbers=='1'?'8':'7');
	_td.appendChild(_tableMes);
	_td.style.borderBottom = '1px solid red';
	_tr.appendChild(_td);
	_table.appendChild(_tr);
	
	var i=0, ii=_iCalStartDay;
	_tr = criaElemento('tr');
	if(_sShowWeekNumbers == "1") {
		_td = criaElemento('td');
		_tr.appendChild(_td);
	}
	while(i<7) {
		_td = criaElemento('td');
		_td.innerHTML = _sCalDays[ii];
		_sCalDays[ii];
		_tr.appendChild(_td);
		i++;
		ii++;
		if(ii>6)
			ii = 0;
	}
	_table.appendChild(_tr);
	var iDate	= 0;
	var sStyle	= "";
	
	for(i=0;i<6;i++) {
		_tr = criaElemento('tr');
		if(_sShowWeekNumbers == "1") {
			var iWeekNumber = getWeekNumber(GetLastDayOfWeek(tmpDate));
			_td = criaElemento('td');
			_td.innerHTML = iWeekNumber;
			_tr.appendChild(_td);
		}
		for(ii=0;ii<7;ii++) {
			iDate = tmpDate.getDate();
			iValue = tmpDate.valueOf();
			var dayIsInThisMonth = (tmpDate.getMonth() == iInitMonth);
			_td = criaElemento('td');
			if(tmpDate<_dCalMinDate || tmpDate>_dCalMaxDate)
				_td.innerHTML = '';
			else if(tmpDate>_Now) {
				_td.innerHTML = iDate;
			}
			else {
				_a = criaElemento('a');
				_a.href = '#';
				_a.setAttribute('rel', iValue);
				_a.innerHTML = iDate;
				_a.setAttribute('onclick', sOnClick);
				_td.appendChild(_a);
			}
			tmpDate.setDate(iDate + 1);
			_tr.appendChild(_td);
		}
		_table.appendChild(_tr);
	}
	
	var dToday = new Date();
	// 7 days + 1 week number = 8 columns
	_a = criaElemento('a');
	_a.href = '#';
	_a.setAttribute('rel', dToday.valueOf());
	_td = criaElemento('td');
	_tr = criaElemento('tr');
	_a.innerHTML = FormatDate(dToday);
	_a.setAttribute('onmousedown', sOnClick);
	_td.appendChild(_a);
	_td.setAttribute('colspan', _sShowWeekNumbers=='1'?'8':'7');
	_td.style.borderTop = '1px solid red';
	_tr.appendChild(_td);
	_table.style.width = '100%';
	_table.style.border = '1px solid black';
	_table.style.backgroundColor = '#FFF';
	_table.style.borderCollpase = 'collapse';
	_table.appendChild(_tr);
	_table.onmouseout = someCalendario;
	return _table; 
}



function SetDateValues(oInput)
{
	oInput.value = Trim(oInput.value);

	var s = oInput.value;
	
	if (s.length > 0)
	{
		var D = ParseDate(s, _iCalFormat);
		
		if (D)
		{
			oInput.value		= FormatDate(D);
			oInput.returnValue	= FormatUtcDate(D);
			return true;
		}
		else
		{
			oInput.select();
			return false;
		}
	}
	else
	{
		oInput.returnValue = "";
		return true;
	}
}


function GetFirstDayInCalendar(D) {
	D.setDate(1);

	var i = D.getDay() - _iCalStartDay;

	if (i < 0)
	{
		i += 7;
	}

	if (i == 0)
	{
		return D;
	}

	D.setDate((i * -1) + 1);
	
	D.setHours(0);
	D.setMinutes(0);
	D.setSeconds(0);

	return D;
}

var HILITE_NONE = 0;
var HILITE_DAY = 1;
var HILITE_WEEK = 2;

///
/// Renders the control to display a month using the given day
///
/// D        - day around which the month should be rendered
/// sOnClick - function to fire when a date is clicked
/// hiliteCode - rules for hiliting the dates around the given day
///            - HILITE_NONE - no 'range' is hilited
///            - The day represented by 'hiliteDay' is hilited
///            - The week that contains 'hiliteDay' is hilited
/// hiliteDay  - The day to hilite


// Build the bottom "Today" Bar
function getTodayBarHtml(iColSpan, sOnClick)
{
	var dToday = new Date();
	
	var s = "<tr height='18'><td style='padding-top:1px;cursor:hand;border:1px solid #000000;font-family:verdana;font-size:8pt;text-align:Center;' colspan='";
	
	s += iColSpan; 
	s += "' onclick='" + sOnClick + "' d='" + dToday.valueOf() + "' onmouseover='runtimeStyle.color = \"#e89f2e\";' onmouseout='runtimeStyle.color = \"\";'><div style='height:16px;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff,EndColorStr=#cecfde);'>";
	s += "Hoje: " + FormatDate(dToday);
	return s + "</div></td></tr>";
}


// Returns the week number the date falls on
// oDate - A Date Object with any given date to calculate a week number for.
function getWeekNumber( oDate ) {
	// Get the beginning of the year.
	var oYearStart = new Date( oDate.valueOf( ) );
	oYearStart.setMonth( 0, 1 );  // January is month 0
	oYearStart.setHours( 0, 0, 0, 0 );
	
	var iMSSinceYearStart = oDate.valueOf( ) - oYearStart.valueOf( );
	
	var fWeeksSinceYearStart = iMSSinceYearStart / 604800000;  // 1000 / 60 / 60 / 24 / 7;
	
	// Drop the extra decimal digits to make a whole number.
	var iWeeksSinceYearStart = Math.ceil( fWeeksSinceYearStart );
	
	if( iWeeksSinceYearStart == 0 )
	{
		// Deal with case where Jan 1 is on a Monday; this formula will produce 0 because jan 1 - jan 1 = 0.
		iWeeksSinceYearStart = 53;
	}
	
	return iWeeksSinceYearStart;
}


function DrawYear(D, sOnClick) {
	var tmpDate = new Date( D );
	tmpDate.setMonth(0);
	tmpDate.setDate(1);
	
	// write out the top navigation
	//
	var s = "<table cellpadding='0' cellspacing='0' width='";
	s += _sShowWeekNumbers == "1" ? iShowWeekCalWidth : iNoWeekCalWidth;
	s += "' style='border-collapse:collapse;table-layout:fixed'><tr><td style='font-family:verdana;font-size:8pt;text-align:center;border:1px solid #7b9ebd;height:18px;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff,EndColorStr=#cecfde);' colspan='3'><table cellpadding='0' cellspacing='0' style='table-layout:fixed'><tr height='18' style='font-family:verdana;font-size:8pt;cursor:hand;'>";

	var iValue	= tmpDate.valueOf();
	var iYear	= tmpDate.getFullYear();

	// previous year navigation
	//
	if (iYear <= _dCalMinDate.getFullYear())
		s += "<td align='center' width='22' style='cursor:default;' nav='0'>&nbsp;</td>";
	else
		s += "<td onclick='" + sOnClick + "' align='center' width='22' d='" + iValue + "' nav='2' onmouseover='this.runtimeStyle.color = \"#e89f2e\";' onmouseout='this.runtimeStyle.color = \"\";'>&lt;</td>";

	// write out the year 
	s += "<td align='center' style='cursor:default;' nav='0'>" + iYear + "</td>";

	// next year navigation
	//
	if (iYear >= _dCalMaxDate.getFullYear())
		s += "<td align='center' width='22' style='cursor:default;' nav='0'>&nbsp;</td>";
	else
		s += "<td onclick='" + sOnClick + "' align='center' width='22' d='" + iValue + "' nav='2' onmouseover='this.runtimeStyle.color = \"#e89f2e\";' onmouseout='this.runtimeStyle.color = \"\";'>&gt;</td>";

	s += "</tr></table></td></tr>";

	// write out the months
	//
	var iMonth = 0;

	for (i = 0; i < 4; i++)
	{
		s += "<tr style='height:" + (31 + (i % 2)) + "px;' onmouseover='event.srcElement.runtimeStyle.color = \"#e89f2e\";' onmouseout='event.srcElement.runtimeStyle.color = \"\";'>";

		for(ii = 0; ii < 3; ii++)
		{
			iMonth	= tmpDate.getMonth();
			iValue	= tmpDate.valueOf();

			s += "<td onclick='" + sOnClick + "' style='font-family:verdana;font-size:8pt;text-align:center;border:1px solid #7288ac;cursor:hand;' d='" + iValue + "' nav='1'>" + _sCalMonths[iMonth] + "</td>";

			tmpDate.setMonth(iMonth + 1);
		}

		s += "</tr>";
	}

	s += getTodayBarHtml(3, sOnClick);
	s += "</table>";

	return s;
}


function ReturnDate(o) {
	var D = new Date(parseInt(o.rel, 10));
	if (o.className == 'nav') {
		var m = D.getMonth();
		var d = D.getDate();
		var a = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if (D.getYear() % 4 == 0)
			a[1] = 29;

		if (o.innerHTML == "&lt;") {
			m--;
			if ((m > 0) && (d > a[m]))
				D.setDate(a[m]);

			D.setMonth(m);
		}
		else if (o.innerHTML == "&gt;") {
			m++;
			
			if ((m < 12) && (d > a[m]))
				D.setDate(a[m]);
			
			D.setMonth(m);
		}
		//document.getElementById('calendario').innerHTML = 'teste';
		document.getElementById('calendario').style.display = 'none';
		setTimeout("LaunchCalendar(document.getElementById('CalOnline'), "+D.valueOf()+")", 200);
		//var _tabela = DrawMonth(D, "ReturnDate(this);", HILITE_NONE, null);
		/*if(navigator.appName != 'Microsoft Internet Explorer')
			_calendario.replaceChild(_tabela, document.getElementById('calendario').getElementsByTagName('table')[0]);
		else*/
			//document.getElementById('calendario').removeChild(document.getElementById('calendario').getElementsByTagName('table')[0]);
			//document.getElementById('calendario').appendChild(_tabela);
			//document.getElementById('calendario').getElementsByTagName('table')[0].innerHTML = DrawMonth(D, "ReturnDate(this);", HILITE_NONE, null).innerHTML;
		//_calendario.appendChild(_tabela);

	}
	else {
	    _oCalInput.innerHTML = FormatDate(D);
		//_oCalInput.innerHTML = FormatUtcDate(D);
		someCalendario();
		ChangeDate();
		//_oCalInput.fireEvent("onchange");
		//document.getElementById('calendario').style.display = 'none';
	}
}


function ParseDate(s)
{
	try
	{
		var a = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		var S = "";
		
		var i;
		for (i = 0; i < s.length; i++)
		{
			switch (s.charAt(i))
			{
				case "/": S = "/"; break;
				case ".": S = "."; break;
				case "-": S = "-"; break;
			}

			if (S.length > 0)
			{
				break;
			}
		}

		
		var c;
		var iS = 0;
		for (i = 0; i < s.length; i++)
		{
			c = s.charAt(i);
			if (c != S && !IsValidNumber(c) )
			{
				throw  "Você deve inserir uma data válida." ;
			}
			if (c == S)
			{
				iS++;
			}
		}
		
		if (iS != 2)
		{
			throw  "Você deve inserir uma data válida." ;
		}
		
		var m, d, y;
		if (_iCalFormat < 4)
		{
			m = s.substring(0, s.indexOf(S));
			d = s.substring(m.length + 1, s.indexOf(S, m.length + 1));
			y = s.substr(m.length + d.length + 2);
		}
		else if (_iCalFormat > 3 && _iCalFormat < 8)
		{
			d = s.substring(0, s.indexOf(S));
			m = s.substring(d.length + 1, s.indexOf(S, d.length + 1));
			y = s.substr(m.length + d.length + 2);
		}
		else
		{
			y = s.substring(0, s.indexOf(S));
			m = s.substring(y.length + 1, s.indexOf(S, y.length + 1));
			d = s.substr(m.length + y.length + 2);
		}
		
		m = parseInt(m, 10) - 1;

		if (m < 0 || m > 11)
		{
			throw  "Você deve inserir uma data válida." ;
		}

		y = parseInt(PadYear(y), 10);
		
		if (y < _dCalMinDate.getFullYear() || y > _dCalMaxDate.getFullYear())
		{
			throw  "Você deve inserir uma data válida." ;
		}

		if (y % 4 == 0)
		{
			a[1] = 29;
		}

		d = parseInt(d, 10);

		if (d == 0 || d > a[m])
		{
			throw  "Você deve inserir uma data válida." ;
		}

		var D = new Date(y, m, d);

		if ((D < _dCalMinDate) || (D > _dCalMaxDate))
		{
			throw  "Você deve inserir uma data válida." ;
		}

		return D;
	}
	catch(e)
	{
		alert(e);
		
		return false;
	}
}


function ParseUtcDate(s)
{
	if (s.length > 10)
	{
		// Date + Time
		return new Date(parseInt(s.substr(0, 4), 10), (parseInt(s.substr(5, 2), 10) - 1), parseInt(s.substr(8, 2), 10), parseInt(s.substr(11, 2), 10), parseInt(s.substr(14, 2), 10), parseInt( s.substr( 17, 2), 10 ));
	}
	else
	{
		// date only, no time info
		//
		return new Date(parseInt(s.substr(0, 4), 10), (parseInt(s.substr(5, 2), 10) - 1), parseInt(s.substr(8, 2), 10));
	}
}


function FormatDate(D)
{
	var m = D.getMonth() + 1;
	var d = D.getDate();
	var y = D.getFullYear();
	var mes = D.getMonth();

	if (_iCalFormat % 2 == 0 )
	{
		y = String(y).substr(2, 2);
	}

	if (_iCalFormat == 2 || _iCalFormat == 3 || _iCalFormat == 6 || _iCalFormat == 7 || _iCalFormat == 10 || _iCalFormat == 11 || _iCalFormat == 13)
	{
		m = PadNumber(m);
		d = PadNumber(d);
	}

	if (_iCalFormat < 4)
	{
		return m + _sCalSeperator + d + _sCalSeperator + y;
	}

	if (_iCalFormat > 3 && _iCalFormat < 8)
	{
		return d + _sCalSeperator + m + _sCalSeperator + y;
	}
	if (_iCalFormat == 13)
	{        
        return d + _sCalSeperator + _sCalLongMonths[mes] + _sCalSeperator + y;
	}
	return y + _sCalSeperator + m + _sCalSeperator + d;
}


function FormatUtcDate(D)
{
	return String(D.getFullYear()) + "-" + PadNumber( String(D.getMonth() + 1)) + "-" + PadNumber(String(D.getDate())) + "T" + PadNumber(String(D.getHours())) + ":" + PadNumber(String(D.getMinutes())) + ":00";
}


function PadNumber(s)
{
	if (String(s).length == 1)
	{
		return "0" + s;
	}

	return s;
}


function PadYear(s)
{
	s = new String(s);

	if (s.length == 4)
	{
		return s;
	}
	
	if (s.length == 1)
	{
		s = 0 + s;
	}

	if (parseInt(s, 10) < 30)
	{
		return "20" + s;
	}

	return "19" + s;
}


function FormatUtcDateTime(s)
{
	var f = 0;
	var hh = parseInt(s.substr(11, 2), 10);
	var mm = s.substr(14, 2);
	var ss = s.substr(17, 2);
	
	var tt = "";

	switch (f)
	{
		case 0: case 1: case 4: case 5:
		
			if (hh == 12)
			{
				tt	= " PM";
			}
			else if (hh > 12)
			{
				hh	= hh - 12;
				tt	= " PM";
			}
			else
			{
				tt	= " AM";
			}
			
			break;
	}
	
	switch (f)
	{
		case 1: case 3: case 5: case 7:
			
			hh = PadNumber(hh);
			
			break;
	}
	
	if (f > 3)
	{
		return FormatDate(ParseUtcDate(s)) + " " + hh + ":" + mm + ":" + ss + tt;
	}
	else
	{
		return FormatDate(ParseUtcDate(s)) + " " + hh + ":" + mm + tt;
	}
}

// Returns the first day of the week that contains the given date
function GetFirstDayOfWeek( D )
{
	var daysToSubtract = (D.getDay() + 7 - _iCalStartDay) % 7;
	return new Date(D.getTime() - (daysToSubtract * 24 * 60 * 60 * 1000));
}

// Returns the last day of the week that contains the given date
function GetLastDayOfWeek( D )
{
	var firstDay = GetFirstDayOfWeek( D );
	return new Date( firstDay.getTime( ) + ( 6 * 24 * 60 * 60 * 1000 ) );  // Add 6 days to the first day of the week
}


function IsValidNumber(n)
{
	if (n == null)
		return false;

	var l = n.length;

	if (l == 0)
		return false;

	var s = 0;

	if (n.charAt(0) == "-")
		s = 1;

	var i,c;
	
	for (i = s; i < l; i++)
	{
		c = n.charCodeAt(i);

		if (c < 46 || c > 57)
			return false;
	}

	return true;
}
