/////////////////////////////////////
////Inicio funcoes de Calendario/////
/////////////////////////////////////
init();

function openCalendarWindow(cwo) 
{
	var x = (screen.width - (wcal+9)) / 2
	var y = (screen.height - (hcal+40)) / 2
	calwin=this.open("","JanelaCalendar",cwo+"screenx="+x+",screeny="+y+",left="+x+",top="+y);
 	if (calwin!=null) {
 		calwin.focus();
 	}
	return calwin;
}
 
if (navigator.appName=='Microsoft Internet Explorer') 
{
	this.browser='MSIE';
} 
else 
{
	this.browser='Netscape';
}
this.versao=navigator.appVersion.substring(0,1);
this.servidor='';

function makeArray(n) 
{
	this.length = n;
	return this;
}

function init () 
{
	theDate       = new Date();
	today         = new Date();
	currMonth     = today.getMonth()+1;
	msPerDay      = 24*60*60*1000;
	monthName     = new makeArray(13);
	monthName[1]  = "Janeiro";
	monthName[2]  = "Fevereiro";
	monthName[3]  = "Mar&ccedil;o";
	monthName[4]  = "Abril";
	monthName[5]  = "Maio";
	monthName[6]  = "Junho";
	monthName[7]  = "Julho";
	monthName[8]  = "Agosto";
	monthName[9]  = "Setembro";
	monthName[10] = "Outubro";
	monthName[11] = "Novembro";
	monthName[12] = "Dezembro";
	
	wcal=162;
	hcal=190;
	fundoMes="#98A8E0"
	corfonteDias="Black"
	fundoDias="#1D368B"
	fundoNumeros="#FFFFFF"
	fundoAtual="#A4A49A"
}

function openCalendarWindow(cwo) 
{
	calwin=this.open("","JanelaCalendar",cwo);
	if (browser=="Netscape") 
	{
		if (calwin!=null) 
		{
			calwin.focus();
		}
	}
	return calwin;
}

function openCalendar (dataFldname,diaFldname,mesFldname) 
{
	datahoje= new Date()
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=yes,width="+wcal+",height="+hcal;
	calendarWindow = openCalendarWindow(windowOptions)
	if (dataFldname!=null) 
	{
		if ((dataFldname.value=="") || (dataFldname.value=="dd/mm/aaaa") || (dataFldname.value.indexOf("/")<1)) 
		{ 
			mes = datahoje.getMonth()+1;
			ano=datahoje.getFullYear();
			dia=0;
		} else 
		{
			posdia=dataFldname.value.indexOf("/")
			dia=eval(dataFldname.value.substring(0,posdia));
			posmes=dataFldname.value.indexOf("/",posdia+1)
			mes=eval(dataFldname.value.substring(posdia+1,posmes))
			ano=eval(dataFldname.value.substring(posmes+1,10))
			if (ano<1000) 
				ano += 2000
//				posano = ano.indexOF("2");
//				ano = ano.substring(posano+2, ano.length);
		}
	} 
	else 
	{
		dia=diaFldname.selectedIndex+1;
		mes=mesFldname.selectedIndex+1;
		ano=datahoje.getFullYear();
	}
	redraw(dia,mes,ano,dataFldname,diaFldname,mesFldname,window);
}

function redraw(dia,mes,ano,dataFld,diaFld,mesFld,pai) 
{
	calendarWindow.document.open();
	calTitle = "<TITLE>Calend&aacute;rio</TITLE>";
	calendarWindow.document.write(calTitle);
	calInit = "<sc";
	calInit+="ript language=\"javascript\"> ";
	calInit+="callingForm=null;dataFld=null;diaFld=null;mesFld=null;</scr";
	calInit+="ipt>";
	calendarWindow.document.write(calInit);
	htmlBuf=drawCalendar(dia,mes,ano);
	calendarWindow.document.write(htmlBuf);
	calendarWindow.document.close();
	if (dataFld!=null) 
	{
		calendarWindow.dataFld=dataFld;
	} else 
	{
		calendarWindow.diaFld=diaFld;
		calendarWindow.mesFld=mesFld;
	}
	calendarWindow.callingForm=pai;
	if (browser!='MSIE' && versao!='2') 
	{
		calendarWindow.focus();
	}
}

function fillDate(dia,mes,ano,dataFld,diaFld,mesFld) 
{
	var AnoYY
	if (dataFld!=null) 
	{
		AnoYY = ano%1000
		if (AnoYY<10)
			AnoYY = "0" + AnoYY
		if (dia<10)
			dia = "0" + dia
		if (mes<10)
			mes = "0" + mes			
		//data no formato dd/mm/aa	
		//dataFld.value=dia+"/"+mes+"/"+AnoYY
		//data no formato dd/mm/aaaa
		dataFld.value=dia+"/"+mes+"/"+ano
	} 
	else 
	{
		diaFld.selectedIndex=dia-1
		mesFld.selectedIndex=mes-1
	}
	calendarWindow.close();
}

function changeMonth (increment,mes,ano,dataFld,diaFld,mesFld,pai) 
{
	nextMonth = mes;
	if (increment == 1) 
	{
		nextMonth++ ;
	} else 
	{
		nextMonth-- ;
	}
	if (nextMonth > 12)   
	{
		nextMonth = 1;
		ano++;
	} 
	else
		if (nextMonth<1) 
		{
			nextMonth=12;
			ano--;
		}
	redraw(0,nextMonth,ano,dataFld,diaFld,mesFld,pai);
}


function firstSunday (fromDate) 
{
	while (fromDate.getDay() != 0) 
	{
		newTime = fromDate.getTime() - msPerDay;
		fromDate.setTime(newTime);
	}
}

/////////////////////////////////////
//////Fim funcoes de Calendario//////
/////////////////////////////////////

