
/*
 * Objeto para janela de calendario
 */
var _cl=new Calendar(),isIE4=/MSIE 4/.test(navigator.userAgent);
function Calendar(date){
	this.left=500;
	this.top=150;
	this.width=150;	
	this.height=130;
	this.imgNext=_pl('next.gif');
	this.imgPrev=_pl('prev.gif');
	this.imgTodayIn=_pl('hoje_in.gif');
	this.imgTodayOut=_pl('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=this.d+"/"+this.m+"/"+this.y;
	if(dt==this.c.value)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){
		this.d=dt.slice(0,2);
		this.m=dt.slice(3,5);
		this.y=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="calendar.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+'" border="0"></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+'" border="0"></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 {
			   if (sem==1) str+='<td align="center"><a class="sunday" '+((dia==this.d && testDt)?('id="marked" '):(''))+'href="javascript:opener.setValue(\''+_fi(dia,2)+'/'+this.m+'/'+this.y+'\');" onmouseover="oldColor=this.style.color;this.style.color=\'#DA0921\'" onmouseout="this.style.color=\'#000000\';this.style.color=oldColor">'+_fi(dia,2)+'</a></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+'\');" onmouseover="oldColor=this.style.color;this.style.color=\'#DA0921\'" onmouseout="this.style.color=\'#000000\';this.style.color=oldColor">'+_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;">Hoje</a><font id="padding"><br><br></font></td></tr></table></body></html>';

       // removido pq tirei as imagens para 'hoje'
		//'<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+'"></a><font id="padding"><br><br></font></td></tr></table></body></html>';
	return(String(str));
}
function _OC(){
//	this.calWindow=open('branco.html','_cl','width='+this.width+',height='+this.height+',left='+this.left+',top='+this.top);
//	this.calWindow.location.replace('janela.html?script=opener._cl.write()');	
	this.calWindow=open(_cl.path+'/janela.html?script=opener._cl.write()','_cl','width='+this.width+',height='+this.height+',left='+this.left+',top='+this.top);
}
function _ClC(){this.calWindow.close();}
function _pl(i){var t=new Image();t.src=i;return i;}
function openCalendar(field, psPath){

   // armazena o path dos fontes
   if (String(psPath) != "undefined")  {
   	_cl.path = psPath;
   }

   // identifica path(retorna até a área de sistemas)
   else  {
      _cl.path = "";
      asPath   = String(window.location).split("/");
      iPath    = -1;
      while (++iPath < asPath.length-2 && asPath[iPath].toLowerCase() != "sistemas") {
         if (_cl.path != "") _cl.path += "/";
         _cl.path += asPath[iPath];
      }
      _cl.path += "/comum/javascript/data";
   }
   
   // realiza os procedimentos normais   
	_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);
}

// fim do objeto calendar
