var _cl=new Calendar(),isIE4=/MSIE 4/.test(navigator.userAgent);
function Calendar(date){
	this.left=500;
	this.top=150;
	this.width=200;	
	this.height=130;
	this.imgNext=_pl('calendario/next.gif');
	this.imgPrev=_pl('calendario/prev.gif');
	this.imgTodayIn=_pl('calendario/bt_hoje_in.gif');
	this.imgTodayOut=_pl('calendario/bt_hoje_out.gif');
	this.calWindow=null;
	this.setDate=_SD;
	this.previous=_PM;
	this.next=_NM;
	this.open=_OC;
	this.close=_ClC;
	this.toString=_TS;
	this.write=_WC;
	this.isDefDate=_IDD;
	this.setDate(date);
}
function _IDD(){
	var dt=String(this.d)+String(this.m)+String(this.y);
	if(dt==String(this.c.value).replace(/\//g,""))return true;
	else return false;
}
function _WC(){
	var w=this.calWindow;
	var d=w.document;
	var s=this.toString();
	if(isIE4){d.write(s);w.location.reload();}
	else{d.open();d.write(s);d.close();}
	w.focus();
}
function _SD(dt){
	var dmes,mtxt;
	if(dt){
		var s=(dt.length?dt.length:0);
		this.d=dt.slice(0,2);
		this.m=(s==8?dt.slice(2,4):dt.slice(3,5));
		this.y=(s==8?dt.slice(4,8):dt.slice(6,10));
		this.dt=new Date(this.m+"/"+this.d+"/"+this.y);
	}
	else{
		this.dt=new Date();
		this.d=_fi(this.dt.getDate(),2);
		this.m=_fi(this.dt.getMonth()+1,2);
		this.y=_fi(this.dt.getFullYear(),2);
	}
	dmes=[31,((((this.y%4==0)&&this.y%100!=0)||this.y%400==0)?29:28),31,30,31,30,31,31,30,31,30,31];
	mtxt=["Janeiro","Fevereiro","Mar&ccedil;o","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"];
	this.wd=this.dt.getDay();	
	this.md=dmes[Number(this.m)-1];
	this.ml=mtxt[Number(this.m)-1];
}
function _PM(){
	var pm,py;
	if(this.m=='01'){pm='12';py=Number(this.y)-1;}
	else{pm=_fi(Number(this.m)-1,2);py=this.y;}
	this.setDate(this.d+"/"+pm+"/"+py);
	this.write();
}
function _NM(){
	var nm,ny;
	if(this.m=='12'){nm='01';ny=Number(this.y)+1;}
	else{nm=_fi(Number(this.m)+1,2);ny=this.y;}
	this.setDate(this.d+"/"+nm+"/"+ny);
	this.write();
}
function _TS(){
	var first=new Date(this.m+"/01/"+this.y),
	fSem=first.getDay(),
	dia=(-first.getDay()+1),
	testDt=this.isDefDate(),
	l=function (d){return '<td class="week" align="center">'+d+'</td>'},
	str='<html><head><title>Calendário</title><link type="text/css" rel="stylesheet" href="calendario/calendario.css"></head>'+
		'<body onfocus="if(typeof(opener._cl)!=\'object\') window.close();" bgcolor="#FFFFFF" marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">'+
		'<table align="center" cellspacing="0" cellpadding="0" border="0" width="100%" height="100%"><tr>'+
		'<td valign="top" align="center" colspan="3">'+
		'<table width="100%" align="center" cellspacing="0" cellpadding="2" border="0"><tr>'+
		'<td align="left" class="image"><a href="javascript:void(0)" onclick="opener.prev();return false;"><img src="'+this.imgPrev+'" width="7" height="11" border="0" alt="Ant" title="Mês Anterior"></a></td>'+
		'<td align="center" class="header">'+this.ml+'/'+this.y+'</td>'+ 
		'<td align="right" class="image"><a href="javascript:void(0)" onclick="opener.next();return false;"><img src="'+this.imgNext+'" width="7" height="11" border="0"  alt="Prox" title="Próximo Mês"></a></td></tr></table>'+		
		'<table width="100%" align="center" cellspacing="0" cellpadding="0" border="0"><tr>'+
		l('D')+l('S')+l('T')+l('Q')+l('Q')+l('S')+l('S')+'</tr><tr>'
		'<td colspan="7" id="lineSpace">x</td></tr><tr>'+
		'<td colspan="7" id="line">x</td></tr>';
	while(dia<=this.md){
		str+='<tr>';
		for(var sem=1;sem<=7;sem++){
			if(dia<=0 || dia>this.md)str+='<td class="day">&nbsp;</td>';
			else str+='<td align="center"><a class="day" '+((dia==this.d && testDt)?('id="marked" '):(''))+'href="javascript:opener.setValue(\''+_fi(dia,2)+'/'+this.m+'/'+this.y+'\');">'+_fi(dia,2)+'</a></td>';
			dia++;
		}
		str+='</tr>';
	}
	str+='</table></td></tr><tr>'+
		'<td align="center" valign="bottom" colspan="3"><a href="#" id="today" onclick="opener.setToday();return false;" onmouseover="document.images.today.src=\''+this.imgTodayIn+'\'" onmouseout="document.images.today.src=\''+this.imgTodayOut+'\'"><img name="today" border="0" src="'+this.imgTodayOut+'" alt="Hoje" title="Hoje"></a><font id="padding"><br><br></font></td></tr></table></body></html>';
	return(String(str));
}
function _OC(){
	this.calWindow=open('calendario/nada.htm','_cl','width='+this.width+',height='+this.height+',left='+this.left+',top='+this.top);
	this.calWindow.location.replace('calendario/wopen.htm?script=opener._cl.write()');	
}
function _ClC(){this.calWindow.close();}
function _pl(i){var t=new Image();t.src=i;return i;}
function openCalendar(field){
	_cl.setDate(field.value);
	_cl.c=field;
	_cl.open();
}
function next(){_cl.next();}
function prev(){_cl.previous();}
function setValue(date){
	if(!_cl.c || typeof _cl.c.value=="undefined"){_cl.close();return;}
	_cl.c.value=date;
	_cl.c.focus();
	if(document.layers)_cl.c.blur();
	autoSkip(_cl.c);	
	_cl.close();
}
function setToday(){
	_cl.setDate();
	if(_cl.c) setValue(_cl.d+"/"+_cl.m+"/"+_cl.y);
	_cl.close();
}
function _fi(num,size){
	var str=String(num),i,dif=size-str.length,aux='';
	for(i=0;i<dif;i++)aux+='0';
	return(aux+str);
}

function autoSkip(field,orient){
	var ind=-1,f=field.form;
	for(i=0;i<f.elements.length;i++)
		if(field==f.elements[i]){ind=i;break;}
	focusCampByPos(f,ind,orient);
}
function autoFocus(f,justInputSelect){focusCampByPos((arguments.length==0?document.forms[0]:f),-1,'down',justInputSelect);}
function focusCampByPos(fr,ind,orient,justInpSelect){
	reValidTypes=(justInpSelect?/^(text|password|select.*)$/:/^(text|password|select.*|radio|checkbox.*)$/);
	orient=orient?orient:"down";
	var iNext=(orient=="down"?1:-1),el;
	if((typeof fr.elements[ind+iNext])=="undefined"){
      if(ind!=-1)if(fr.elements[ind]&&fr.elements[ind].blur)fr.elements[ind].blur();
		return;
   }
	for(var i=ind+iNext;i<fr.elements.length;i+=iNext){
		el=fr.elements[i];
		if(reValidTypes.test(el.type) && !el.disabled){el.focus();return;}
   }
	if(ind>=0 && fr.elements[ind]&&fr.elements[ind].type && fr.elements[ind].type!='hidden' && fr.elements[ind].blur)fr.elements[ind].blur();
}
