
	function GlobalVars() {
		if (navigator.appName=='Microsoft Internet Explorer') {
			this.browser='MSIE';
		} else {
			this.browser='Netscape';
		}
		this.selecaoatual=null;
		this.botaoantigo=null;
		this.versao=navigator.appVersion.substring(0,1);
	}
	
	function t(cor,tag) {
		if(navigator.appName.indexOf("Netsc") != -1) { return; }
		tag.style.backgroundColor = cor;
	}

	global=new GlobalVars()


function makeArray(n) {
	this.length = n;
	return this;
}

function init () {
	theDate         = new Date();
	today           = new Date();
	currMonth       = today.getMonth();
	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=220;
	hcal=250;
	fundoMes="#84BBFF"
	corfonteDias="White"
	fundoDias="Black"
	fundoNumeros="#F0ECE0"
}

function openCalendarWindow(cwo) {
	calwin=this.open("","JanelaCalendar",cwo);
	if (global.browser=="Netscape") {
		if (calwin!=null) {
			calwin.focus();
		}
	}
	return calwin;
}
function openCalendar(dataFldname) {
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=220,height=250";
	calendarWindow = openCalendarWindow(windowOptions)
	if ((dataFldname.value=="") || (dataFldname.value.indexOf("/")<1)) { 
            hoje = new Date()
		dia = hoje.getDate();
		mes = hoje.getMonth()+1;
		ano = hoje.getYear();
	} 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))
	}
	redraw(dia,mes,ano,dataFldname,window);
}

function redraw(dia,mes,ano,dataFld,pai) {
	//window.alert("redraw calendario");
	//window.alert(dia+mes+ano+dataFld+pai);
	calendarWindow.document.open();
//	window.alert("enchendo buffer")
	calTitle = "<TITLE>Calend&aacute;rio</TITLE>";
	calendarWindow.document.write(calTitle);
	//window.alert("ok");
	calInit = "<sc";
	calInit+="ript language=\"javascript\"> ";
	calInit+="callingForm=null;dataFld=null;</scr";
	calInit+="ipt>";
	//window.alert("calInit"+calInit);
	calendarWindow.document.write(calInit);
	htmlBuf=drawCalendar(dia,mes,ano);
//	window.alert("iniciando para escreve")
	calendarWindow.document.write(htmlBuf);
	//window.alert("calendario escrito")
	calendarWindow.document.close();
	calendarWindow.dataFld=dataFld;
	calendarWindow.callingForm=pai;
	if (global.browser!='MSIE' && global.versao!='2') {
		calendarWindow.focus();
	}
}

function fillDate(dia,mes,ano,dataFld) {

      if (dia < 10){
              dia = "0" + dia
      }
      if (mes < 10){
              mes = "0" + mes
      }
	dataFld.value=dia+"/"+mes+"/"+ano
	calendarWindow.close();

}

function changeMonth (increment,mes,ano,dataFld,pai) {
//	window.alert("mudança de mes")
	nextMonth = mes;
	if (increment == 1) {
		nextMonth++ ;
	} else {
		nextMonth-- ;
	}
	if (nextMonth > 12)   {
		nextMonth = 1;
		ano++;
	} else
		if (nextMonth<1) {
			nextMonth=12;
			ano--;
		}
	
//	window.alert("iniciando redesenho")
	redraw(0,nextMonth,ano,dataFld,pai);
}

function drawCalendar (diaNum,mesNum,anoNum) {
	theDate = new Date(anoNum,mesNum-1,1);
	htmlBuffer  = "<HTML>";
	htmlBuffer += "<BODY BGCOLOR=#FFFFFF>";
	htmlBuffer += "<FORM id=form1 name=form1>";
	htmlBuffer += "<CENTER><table id=Table4 BORDER=0 CELLSPACING=2 CELLPADDING=2 WIDTH=170>";
	htmlBuffer += "<Td colspan=7 bgcolor="+fundoMes+"><TABLE width=100%><TR width=100%><TD width=10% align=left><a href=\"javascript:callingForm.changeMonth(-1,"+mesNum+","+anoNum+",dataFld,callingForm)\"><img src=\"imagens/botao_seta_esq.gif\" border=0></a>";
	htmlBuffer += "<td align=center><font size=-1><B> ";
	htmlBuffer += monthName[mesNum];
	htmlBuffer += " " + anoNum;
	htmlBuffer += "<Td width=10% align=right><a href=\"javascript:callingForm.changeMonth(1,"+mesNum+","+anoNum+",dataFld,callingForm)\"><img src=\"imagens/botao_seta_dir.gif\" border=0></a>";
	htmlBuffer += "</table><TR>";
	htmlBuffer = htmlBuffer+"<Td bgcolor="+fundoDias+"><font COLOR=red size=-1>Dom</font><Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Seg<Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Ter<Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Qua<Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Qui<Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Sex<Td bgcolor="+fundoDias+"><font COLOR="+corfonteDias+" size=-1>Sab<TR>";
	drawBody(diaNum,theDate,anoNum);
	htmlBuffer += "</TABLE>";
	htmlBuffer += "</BODY></HTML>";
	return htmlBuffer;
}

function drawBody (dia,theDate,ano) {
	thisMonth = theDate.getMonth();
	thisDate  = theDate.getDate();
	firstSunday(theDate);
	for (w=0; w<6; w++) {
		for (d=0; d<7; d++) {
			date = theDate.getDate();
			htmlBuffer   += "<TD ALIGN=CENTER ";
			if ((date==dia)&&(theDate.getMonth()== thisMonth)) {
				htmlBuffer+=" bgcolor="+fundoMes+" >";
			} else {
				htmlBuffer=htmlBuffer+" bgcolor="+fundoNumeros+">";
			}
			// skip previous month
			if (theDate.getMonth() != thisMonth) {
				htmlBuffer += "<BR>";
			} else {
				htmlBuffer += "<a href=\"javascript:";
				htmlBuffer += "callingForm.fillDate(" + theDate.getDate() + "," + (theDate.getMonth()+1) + "," + ano  + ",dataFld)\">";
				if ( date < 10 ) {
					htmlBuffer += "0";
				}
				htmlBuffer += date;
			}
			htmlBuffer += "</a>";
			// increment the date
			newTime = theDate.getTime() + msPerDay;
			theDate.setTime(newTime);
			// check for DST 
			if (theDate.getHours() != 23) {
				theDate.setTime(newTime + 3600000);
			} 
		}
		htmlBuffer += "<TR>";
	}
	htmlBuffer += "</FORM>";
}
function firstSunday (fromDate) {
	while (fromDate.getDay() != 0) {
		newTime = fromDate.getTime() - msPerDay;
		fromDate.setTime(newTime);
	}
}


	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;
}


