function Calendar(obj){
	try{
		this.saida = 'cal'; // Elemento de Retorno do Calendário
		
		this.data = new Date();
		this.dia = this.data.getDate();
		this.mes = this.data.getMonth();
		this.ano = this.data.getFullYear();
		
		this.valor 	  = null; // Variavel, que guarda a última seleção de data (dd/mm/yyyy)
		this.url 	  = null; // Caso setada, joga a data (dd-mm-yyyy) como argumento final ex: index.jsp?data=
		this.elemento = null; // Id, do elemento de retorno ex: div,p,input,textarea
		this.funcao   = null; // Função à ser chamada com o argumento requerido de retornod e valor da data (dd/mm/yyyy), this.selecao
		this.colorir = 0;
		this.permissao = null;
		
		this.cabecalho = null; //Cabeçalho do Calendário
		this.navegador = null;
		this.estrutura = ''; //Calendário
		
		this.meses = new Array('Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');
		
		this.obj = obj;
		this.selecao = null;
		
		this.eventos = null;
	}catch(e){}
}


Calendar.prototype.verificaEvento = function(dia,mes,ano){
	try{
		var r = 0;
		for (i = 0; i < this.eventos.length; i++){
			var diaE = this.eventos[i].substring(8,10);
			var mesE = this.eventos[i].substring(5,7);
			var anoE = this.eventos[i].substring(0,4);
			if (diaE == dia && mesE == mes && ano == anoE){
				r = 1;
				break;
			}
		}
		return r;
	}catch(e){}	
}


Calendar.prototype.montaCalendario = function(){
	try{
		var mes_atual = new Date(this.ano, this.mes, 1);
		var mes_proximo = new Date(this.ano, this.mes + 1, 1);
			
		var primeiro_dia = mes_atual.getDay();
		var dias_no_mes = Math.round((mes_proximo.getTime() - mes_atual.getTime()) / (1000 * 60 * 60 * 24));
		if(this.cabecalho != null)
			this.estrutura = this.cabecalho;
		else
			this.estrutura = '';
		if(this.navegador == "links")
			this.estrutura += '<p style=\"padding:0 0 5px 0;\"><a class="pag" href="javascript:void(0);" onClick="' + this.obj  + '.anteriorMes();">&lsaquo;</a><label>' + this.meses[this.mes] + '/' + this.ano + '</label><a class="pag" href="javascript:void(0);" onClick="' + this.obj  + '.proximoMes();">&rsaquo;</a></p>\n';		
		if(this.navegador == "mes")
			this.estrutura += '<p style=\"padding:0 0 5px 0;>' + this.meses[this.mes] + '</p>\n';		
		this.estrutura += '<ul>\n\t<li class="dias"><label>D</label><label>S</label><label>T</label><label>Q</label><label>Q</label><label>S</label><label>S</label></li>\n\t<li>';
	
		for(i=0; i < primeiro_dia; i++)
			this.estrutura += '<label class="vazio">&nbsp;</label>';   
		i = primeiro_dia;
		var page = null;
		var pageA = null; 
		for(j = 1; j <= dias_no_mes; j++){
			pageA = "'http://intranet.claretianas.br/eventos/addEvento.jsp?dt="+this.ano+"-"+this.completaZero((parseInt(this.mes)+1),2)+"-"+this.completaZero(parseInt(j),2)+"&'";
			page = "'/servicobiblico/"+this.completaZero(parseInt(j),2)+"/"+this.completaZero((parseInt(this.mes)+1),2)+"/"+this.ano+"'";
			k = i%7;
			if(k == 0 && k && i != 0){
				this.estrutura += '</li>\n\t<li>';
			}
				
			if(this.dia == j && this.selecao != j && this.data.getMonth() == this.mes && this.data.getYear() == this.ano && this.verificaEvento(this.completaZero(parseInt(j),2),this.completaZero((parseInt(this.mes)+1),2),this.ano) != 1){
				if (this.permissao)
					this.estrutura += '<label class="hoje" onclick="clearTimeout(intervalo);clearTimeout(intervalo2);clearTimeout(intervalo3);criaIframe('+pageA+',420,480)"><a href="javascript:void(0);">' + j + '</a></label>';
				else
					this.estrutura += '<label class="hoje"><a href="javascript:void(0);">' + j + '</a></label>';
			}else{
				if (this.verificaEvento(this.completaZero(parseInt(j),2),this.completaZero((parseInt(this.mes)+1),2),this.ano) == 1){
					if (this.permissao)
						this.estrutura += '<label class="agenda"><a href="javascript:void(0);"  onclick="window.location.href='+page+'">'+j+'</a></label>';
					else
						this.estrutura += '<label class="agenda"><a href="javascript:void(0);" onclick="clearTimeout(intervalo);clearTimeout(intervalo2);clearTimeout(intervalo3);criaIframe('+page+',350,250)">'+j+'</a></label>';
				}else{
					if (this.permissao)
						this.estrutura += '<label><a href="javascript:void(0);" ondblclick="clearTimeout(intervalo);clearTimeout(intervalo2);clearTimeout(intervalo3);criaIframe('+pageA+',420,450)">' + j + '</a></label>';
					else
						this.estrutura += '<label><a href="javascript:void(0);">' + j + '</a></label>';
				}
			}
			i++;
		}
		if(k==0)
			this.estrutura += '</ul>';
		else
			this.estrutura += '</li>\n</ul>';	
	}catch(e){}
}
Calendar.prototype.mostraCalendario = function (){
	try{
		document.getElementById(this.saida).innerHTML = this.estrutura;	
	}catch(e){}	
}
Calendar.prototype.selecionaData = function (diaS,el){
	try{
		this.selecao = diaS;
		this.valor = this.completaZero(diaS,2) + '-' + this.completaZero((this.mes+1),2) + '-' + this.ano;
		if(this.url != null){
			window.location.href = this.url + 'dt=' + this.completaZero(diaS,2) + '-' + this.completaZero((this.mes+1),2) + '-' + this.ano;
		}
		if(this.elemento != null){
			var d = document.getElementById(this.elemento);
			if(d.value === undefined)
				d.innerHTML = this.valor;	
			else
				d.value = this.valor;
		}
		if(this.funcao != null){
			eval(this.funcao);
		}			
		if(this.colorir != 0){
			this.montaCalendario();
			this.mostraCalendario();
			el.className = 'atual';
		}
	}catch(e){}
}
Calendar.prototype.completaZero = function(valor,formatacao){
	try{
		var retorno = "";
		var nt = valor.toString().length;
		var nn = formatacao-nt;
		
		for(x=1; x<=nn; x++)
			retorno += "0";
			
		retorno += valor;
		return retorno;
	}catch(e){}
}
Calendar.prototype.proximoMes = function (){
	try{
		this.mes = this.mes+1;
		if(this.mes > 11){
			this.mes = 0;
			this.ano = this.ano+1;
		}
		this.selecao = null;
		this.montaCalendario();
		this.mostraCalendario();
	}catch(e){}
}
Calendar.prototype.anteriorMes = function (){
	try{
		this.mes = this.mes-1;	
		if(this.mes < 0){
			this.mes = 11;
			this.ano = this.ano-1;
		}
		this.selecao = null;
		this.montaCalendario();
		this.mostraCalendario();
	}catch(e){}
}
Calendar.prototype.verificaUrl = function(){
	try{
		var url = window.location.href.toString();
		var a = (url.lastIndexOf("d="));
		if(url.substring(a,url.length).length == 9 && a > 2){
			this.mes = parseInt(url.substring(a+2,a+4)-1);
			this.ano = parseInt(url.substring(a+5,a+9));
		}
	}catch(e){}
}
Calendar.prototype.carregarCalendario = function (){
	try{
		this.montaCalendario();
		this.mostraCalendario();
	}catch(e){}
}
Calendar.prototype.contaDias = function(obj){
	try{
		return Math.round((new Date(this.ano,this.mes, this.selecao).getTime() - new Date(obj.ano,obj.mes,obj.selecao).getTime()) / (1000 * 60 * 60 * 24));
	}catch(e){}
}

var agendaA = new Array();
var intervalo = null;
var intervalo2 = null;
var intervalo3 = null;

function criaIframe(page, width, height){
	try{
		var ie = /msie/i.test(navigator.userAgent);
		var p = document.getElementById("eventos");
		if (p == null){
			p = parent.window.document.getElementById('eventos');
			p.style.display = 'block';
			p.style.width = (width)+'px';
			p.style.height = (height+50)+'px';
			var ifrm = p.getElementsByTagName("iframe")[0];
			ifrm.src = page+"dummy="+new Date().getTime();
			ifrm.width = (ie ? (width+15) : width)+'px';
			ifrm.height = (ie ? (height+80) : height)+"px";
		}else{
			while (p.childNodes.length > 1) p.removeChild(p.lastChild);

			p.style.display = 'block';
			p.style.width = (width)+'px';
			p.style.height = (height+30)+'px';
	
			var carregando = document.createElement("img");
			setTimeout(function(){ carregando.src = "http://intranet.claretianas.br/imagens/carregando3.gif";},50);
			carregando.style.marginTop = (parseInt(height)/2)+"px";
			carregando.style.marginLeft = ((width/2)-110)+"px";
			
			var fechar = document.createElement("img");
			setTimeout(function(){ fechar.src = "http://intranet.claretianas.br/imagens/fechar.gif"; },50);
			fechar.alt = "fechar";
			fechar.style.cursor = "pointer";
			fechar.style.position = "absolute";
			fechar.style.right = "10px";
			fechar.style.top = "10px";
			fechar.onclick = function(){
				p.style.display = 'none';
				while (p.childNodes.length > 1)p.removeChild(p.lastChild);
			}

			var ifrm = document.createElement("iframe");
			ifrm.id = 'ifrm';
			ifrm.name = 'ifrm';
			ifrm.width = (ie ? (width+15) : width)+'px';
			ifrm.height = (ie ? (height+20) : height +65)+"px";			
			ifrm.frameBorder = "no";
			ifrm.style.marginTop = '-10px';
			ifrm.style.display = 'none';
			
			p.style.height = (ie ? (height+40) : height + 80)+"px";
			
			p.appendChild(fechar);
			p.appendChild(carregando);
			intervalo = setTimeout(function(){
				ifrm.src = page+"dummy="+new Date().getTime();
				p.appendChild(ifrm);
			}, 200);
			ifrm.onload = carrega(p,carregando,ifrm);
		}
	}catch(e){}
}

function carrega(p,carregando,ifrm){
	try{
		if (p.childNodes.length > 2)intervalo2 = setTimeout(function(){p.removeChild(carregando);ifrm.style.display = 'block';p.style.display = 'block';},500);
	}catch(e){}
}

function Reload(){
	p = parent.window.document.getElementById('eventos');
	p.style.width = '300px';
	p.style.height = '200px';
	var ifrm = p.getElementsByTagName("iframe")[0];
	ifrm.style.width = '290px';
	ifrm.style.height = '190px';
	setTimeout(function(){parent.location.reload();},1400);
}

function feriado(d){
	var local = document.getElementById('local');
	var horaIni = document.getElementById('horaIni');
	var horaFim = document.getElementById('horaFim');
	
	if (d.value == '3'){
		local.title = '';
		horaIni.title = '';
		horaFim.title = '';
		local.className = 'not_erro';
		horaIni.className = 'not_erro';
		horaFim.className = 'not_erro';
	}else{
		local.title = 'Local';
		horaIni.title = 'Hora de Início';
		horaFim.title = 'Hora Final';
		/*local.className = 'erro_input';
		horaIni.className = 'erro_input';
		horaFim.className = 'erro_input';*/
	}
}



function validarAgenda(d){

	try{
		var elementos = d.elements;
		var erro = false;
		var pos = 0;
		for(i = 0; i < elementos.length; i++){
			if (elementos[i].title != ""){
				if(elementos[i].id != null){
					if(elementos[i].type == 'text'){
						if(elementos[i].value.length == ""){
							elementos[i].className = 'erro_input';
							erro = true;
						}else
							elementos[i].className = '';
						if (elementos[i].name.substring(0,5) == "email" && elementos[i].value != ""){
							var reEmail2 =/^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
							if(!reEmail2.test(elementos[i].value)){
								elementos[i].className = 'erro_input';
								erro = true;
							}else
								elementos[i].className = '';
						}
					}
					if(elementos[i].type == 'select-one'){
						if(elementos[i].value == '0'){
						   elementos[i].className = 'erro_input';
						   erro = true;
						}else
							elementos[i].className = 'borda_input';
					}
					if(elementos[i].type == 'textarea'){
						if(elementos[i].value.length < 3){	
						   elementos[i].className = 'erro_input';
						   erro = true;
						}else
							elementos[i].className = 'borda_input';
					}
				}
			}
		}
		
		var dataInicio = document.getElementById('dataIni');
		var dataFim = document.getElementById('dataFim');
		var anoI = parseInt(dataInicio.value.substring(6,10));
		var anoF = parseInt(dataFim.value.substring(6,10));		
		var mesI = dataInicio.value.substring(3,5) * 1;
		var mesF = dataFim.value.substring(3,5) * 1;
		var	diaI = parseInt(dataInicio.value.substring(0,2)) == 0 ? parseInt(dataInicio.value.substring(1,2)) : parseInt(dataInicio.value.substring(0,2));
		var	diaF = parseInt(dataFim.value.substring(0,2)) == 0 ? parseInt(dataFim.value.substring(1,2)) : parseInt(dataFim.value.substring(0,2));
		if ((dataInicio.value != "" && dataFim.value != "")){
			if (anoF < anoI) erro = true;
			else if(anoF == anoI && mesF < mesI) erro = true;
			else if (anoF == anoI && mesF == mesI && diaF < diaI) erro = true;
			else if (anoF == anoI && mesF == mesI && diaF >= diaI) erro = false;
			else if (anoF == anoI && mesF > mesI) erro = false;
			else if (anoF > anoI) erro = false;
			if (erro) dataFim.className = 'erro_input';
		}
		if(erro) return false;
	}catch(e){
		alert('problema no formulario');
		alert(e);
		return false;
	}
}
