function CrossEvent(evt)
{
	evt = evt? evt: (window.event? window.event: null);
	if (evt)
	{
		this.originalEvent = evt;
		this.type = evt.type;
		this.screenX = evt.clientX;
		this.screenY = evt.clientY;
	
		// IE: srcElement
		this.target = evt.target? evt.target: evt.srcElement;
	
		// N4: modificadores
		if (evt.modifiers)
		{
			this.altKey   = evt.modifiers & Event.ALT_MASK;
			this.ctrlKey  = evt.modifiers & Event.CONTROL_MASK;
			this.shiftKey = evt.modifiers & Event.SHIFT_MASK;
			this.metaKey  = evt.modifiers & Event.META_MASK;
		}
		else
		{
			this.altKey   = evt.altKey;
			this.ctrlKey  = evt.ctrlKey;
			this.shiftKey = evt.shiftKey;
			this.metaKey  = evt.metaKey;
		}
	
		// N4: which // N6+: charCode
		this.charCode = !isNaN(evt.charCode)? evt.charCode: !isNaN(evt.keyCode)? evt.keyCode: evt.which;
		this.keyCode = !isNaN(evt.keyCode)? evt.keyCode: evt.which;
		this.button = !isNaN(evt.button)? evt.button: !isNaN(evt.which)? evt.which-1: null;
		this.debug = "c:" + evt.charCode + " k:" + evt.keyCode
			+ " b:" + evt.button + " w:" + evt.which;
	}
} // CrossEvent

function bissexto(ano)
{
    return (ano % 400 == 0) || (ano % 4 == 0 && ano % 100 != 0);
}

function valida_data(nome)
{
    var dias_mes = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
    
    eval('dia = document.getElementById("dia.' + nome + '")');
    eval('mes = document.getElementById("mes.' + nome + '")');
    eval('ano = document.getElementById("ano.' + nome + '")');

    if(dia.value == 0 || mes.value == 0 || ano.value == 0)
		return;
		
	if(bissexto(ano.value))
         dias_mes[2]++;

    valor_mes = Number(mes.value);
    data_ano  = dias_mes[valor_mes];
    
    if(data_ano < Number(dia.value))
        eval('document.getElementById("dia.' + nome + '").value = data_ano');
}

function formata_inteiro(campo, e)
{
	if(!e)
		car = 48;
	else{
		car = (window.Event)? e.which : e.keyCode;
		if (navigator.appName == 'Microsoft Internet Explorer') { 
			car = window.event.keyCode;
		}		
	}
	
	if(car == 8 || car == 9)
		return true;
		
	if(car >= 48 && car <= 57)
	{
		if(e)
			car -= 48;
		else
			car = '';
					
		num = String(campo.value);
		num = num.replace(/\./g,'');
		num = String(parseInt(num,10));
		
		if(num == "NaN")
			num = String("0");
		
		if(num.length < 9)
		{
			num = num + car;
			if(num.length == 0)
				num = String("0");
			else
			{
				tam = num.length;
                if(tam > 3)
                {
                    prefixo = num.substring(tam - 3, tam);
                    tam -= 3;
                    
                    while(tam > 0)
                    {
                        prefixo = num.substring(tam-3, tam) + "." + prefixo;
                        tam -= 3;
                    };
                    
                    num = prefixo;
                }
			}
			campo.value = num;
		}
	}
	return false;
}

/* Formata valores
 * Uso: onkeypress="JavaScript: return formata_real(this, event);" onkeyup="JavaScript: formata_real(this,false);" value="0,00"
 */
 
//////////////////////////


//////////////////////////

function formata_real (campo, e)
{
	if(!e)
		car = 48;
	else
	{
		car = (window.Event)? e.which : e.keyCode;
		if (navigator.appName == 'Microsoft Internet Explorer')
		{ 
			car = window.event.keyCode;
		}		
	}
	
	if(car == 8 || car == 9)
		return true;
		
	if(car >= 48 && car <= 57)
	{
		if(e)
			car -= 48;
		else
			car = '';
					
		num = String(campo.value);
		num = num.replace(/\./g,'');
		num = num.replace(/,/g,'');
		num = String(parseInt(num,10));
		if(num == "NaN")
			num = String("0,00");
		if(num.length < 17)
		{
			num = num + car;
			while(num.length <= 2)
				num = String("0") + num;
			aux = num.substring(0,num.length-2)
			tam = aux.length
			prefixo = aux.substring(tam-3, tam);
			tam -=3 ;
			while(tam > 0)
			{
				prefixo = aux.substring(tam-3, tam) + "." + prefixo;
				tam -= 3;
			}
	 		num = prefixo + "," + num.substring(num.length-2, num.length)
			if(num == "0,,00")
				num = "0,00";
			campo.value = num;
		}
	}
	return false;
}
function Inteiro2Real(mnt)
{
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function deci(N){
       texto = N.toString();
       texto2 = ""
       if (texto.indexOf(',') != -1)
       {
          for (var i = 0; i < texto.length; i++)
          {
              if(texto.charAt(i) == ",")
                   texto2 += ".";
              else
                   texto2 += texto.charAt(i);
          }
          texto = texto2;
       }  
       texto[texto.indexOf(',')] = ".";
       ponto = texto.indexOf('.');
	    if (ponto == -1)
    	{
			 texto += ".00";
			 Term = texto
		}
		else
		{
			 texto += "0";
			 decimal = ponto + 3;
			 Term = texto.substring(0,decimal);
		}
		if (Term == ".0") 
		{
			Term = "0.00";
		}
		if (Term == ".00") 
		{
			Term = "0.00";
		}
		//if (Term == "NaN.00"){ Term = "Erro";}
		return Term;
}

function formata_cpf_old (campo, e)
{
	if(e)
		car = e.keyCode;
	else
		car = 48;
	if(car >= 48 && car <= 57)
	{
		if(e)
			car -= 48;
		else
			car = '';
					
		num = String(campo.value);
		num = num.replace(/\./g,'');
		num = num.replace(/-/g,'');
		num = String(parseInt(num,10));
		if(num == "NaN")
			num = String("0");
		if(num.length < 11)
		{
			num = num + car;
			while(num.length < 11)
				num = String("0") + num;
			aux = num.substring(0,num.length-2)
			tam = aux.length
			prefixo = aux.substring(tam-3, tam);
			tam -=3 ;
			while(tam > 0)
			{
				prefixo = aux.substring(tam-3, tam) + "." + prefixo;
				tam -= 3;
			}
	 		num = prefixo + "-" + num.substring(num.length-2, num.length)
			campo.value = num;
		}
	}
	return false;
}

function formata_cpf (campo, e)
{
	if (!e)
	return false;
	
	car = new CrossEvent(e);	
	
	if (car.charCode == 8 || car.charCode == 0)
		return true;
	
	if (car.charCode >= 48 && car.charCode <= 57 && campo.value.length < 14)
	{
		if (campo.value.length == 3)
		campo.value = campo.value + '.';
		else
		if (campo.value.length == 7)
		campo.value = campo.value + '.';
		else
		if (campo.value.length == 11)
		campo.value = campo.value + '-';
		
		return true;
	}

	return false;
}

function Valida_CPF (valor) 
{
	var CPF = new String();
	
	for(i = 0; i < valor.length; i++)
		if(String(parseInt(valor.charAt(i))) != "NaN")
		CPF = CPF + valor.charAt(i);

	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
		
	soma = 0;	
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
		
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
		resto = 0;
		
	if (resto != parseInt(CPF.charAt(9)))
		return false;
		
	soma = 0;	
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
		
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
		
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}

function formata_cpf_old2 (campo, e)
{
	if (!e)
		return false;
	
	car = (window.Event) ? e.which : e.keyCode;
	
	if (car == 8)
		return true;

	if (car >= 48 && car <= 57 && campo.value.length < 14)
	{
		if (campo.value.length == 3)
			campo.value = campo.value + '.';
		else
			if (campo.value.length == 7)
				campo.value = campo.value + '.';
			else
				if (campo.value.length == 11)
					campo.value = campo.value + '-';
		
		return true;
	}
	
	return false;
}

/*function formata_cpf (objeto, keypress)
{
	campo = eval (objeto);
	
	if (!keypress || (keypress + '' == 'undefined'))
		keypress = 58;

	if (keypress >= 48 && keypress <= 57 && campo.value.length <= 14)
	{
		if (campo.value.length == 3)
			campo.value = campo.value + '.';
		else
			if (campo.value.length == 7)
				campo.value = campo.value + '.';
			else
				if (campo.value.length == 11)
					campo.value = campo.value + '-';
	}
	else
		event.returnValue = false;
}*/

function formata_importacao (campo, e)
{
	keypress = (window.Event) ? e.which : e.keyCode;
	if (keypress == 8)
		return true;

	if (keypress >= 48 && keypress <= 57 && campo.value.length <= 12)
	{
		if (campo.value.length == 2)
			campo.value = campo.value + '/';
		else
			if (campo.value.length == 10)
				campo.value = campo.value + '-';
		return true;
	}
	return false;
}

function formata_cnpj (campo, e)
{	
	if(!e)
		return false;
	
	var keypress = new CrossEvent(e);

	if(keypress.keyCode == 8)
		return true;
	
	if (keypress.charCode >= 48 && keypress.charCode <= 57 && campo.value.length <= 19)
	{
		if (campo.value.length == 3)
			campo.value = campo.value + '.';
		else
			if (campo.value.length == 7)
				campo.value = campo.value + '.';
			else
				if (campo.value.length == 11)
					campo.value = campo.value + '/';
				else
					if (campo.value.length == 16)
						campo.value = campo.value + '-';
		return true;
	}
	return false;
}

function formata_cep (campo, e)
{	
	if (!e)
		return false;
	
	keypress = (window.Event) ? e.which : e.keyCode;
	if(keypress == 8)
		return true;

	if (keypress >= 48 && keypress <= 57 && campo.value.length <= 9)
	{
		if (campo.value.length == 5)
			campo.value = campo.value + '-';
		return true;
	}
	return false;
}

function AbrePopUp (Url, Name, Wi, He, scrolling, status, resizable) 
{
	if (!scrolling)
		scrolling = 'auto';
		
	if (!status)
		status = 'no';
	
	if (!resizable)
		resizable = 'no';
	
	PopUp = window.open(Url,Name,'width='+Wi+',height='+He+',scrollbars=' + scrolling + ',toolbar=no,location=no,status=' + status + ',menubar=no,resizable=' + resizable + ',left=100,top=100');
}

var rodando = false;

function remove()
{
    if(!rodando)
    {
        obj = document.all.divmenu;
    
        obj.style.width  = 0;
        obj.style.height = 0;
        obj.style.visibility = 'hidden';
    }
}
    
function funcao(e)
{
    var x = event.x;
    var y = event.y;
    
    obj = document.all.divmenu.style;
    
    if(x > parseInt(obj.left)     && x < parseInt(obj.left) + parseInt(obj.width) &&
       y > parseInt(obj.top) - 10 && y < parseInt(obj.top)  + parseInt(obj.height))
       return;       
    
    remove();    
}

function recursive(count,height)
{
  if(count < height)
  {
      count++;
      document.all.divmenu.style.height = count;
      
      var time = setTimeout("recursive(" + count + "," + height + ")",2);
  }
  else 
    rodando = false;
}

function Imprimir (query)
{
	AbrePopUp ('imprimir.php?' + query,'imprimir', 630, 300, 'yes');
}

function EnviarPorEmail (query)
{
	AbrePopUp ('/enviar.email.php?' + query,'imprimir', 540, 480, 'yes');
}
 
function animar(left,top,width,height,endereco)
{
}

function Login (id)
{
	if (id)
		AbrePopUp ('login.popup.php?propedital=' + id, 'login', 280, 175, 'no', 'no');
	else
		AbrePopUp ('login.popup.php', 'login', 280, 175, 'no', 'no');
}

function Go2 (id)
{
	document.location = 'index.php?id=' + id;
}

function Esqueci ()
{
	AbrePopUp ('esqueci.popup.php?', 'esqueci', 280, 255, 'no', 'no');
}


function VerCadastro ()
{
	AbrePopUp ('verCadastro.php', 'esqueci', 358, 300, 'yes', 'yes');
}

function MM_findObj (n, d)
{
	var p,i,x;
	if(!d)
	d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n]))
		x=(d.getElementById)?d.getElementById(n):d.all[n]; 
	for (i=0; !x&&i<d.forms.length; i++)
		x=d.forms[i][n];
	for(i=0; !x&&d.layers&&i<d.layers.length; i++)
		x=MM_findObj(n,d.layers[i].document);
	return x;
}

function MM_showHideLayers ()
{
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) 
		if ((obj=MM_findObj(args[i]))!=null) 
		{ 
			v=args[i+2];
			
			obj.style.left = (screen.width - parseInt (obj.style.width))/2;
			obj.style.top = (screen.height - parseInt (obj.style.height))/3;
			
			if (obj.style) 
			{ 
				obj=obj.style;
				v=(v=='show')?'visible':(v='hide')?'hidden':v; 
			}
			obj.visibility=v;			
		}	
}

function browserVerify ()
{	
	if (!is_ie5up)
	{
		var obj = document.getElementById ('idBrowser');
		
		obj.style.left = (screen.width - parseInt (obj.style.width))/2;
		obj.style.top  = '120px';
		
		obj.style.display = '';
	}
}

function observation ()
{
	var obj = document.getElementById ('idObservation');
	
	obj.style.left = (screen.width - parseInt (obj.style.width))/2 + 'px';
	obj.style.top  = '120px';
	
	obj.style.display = '';
}

function abreDocumento(admin,documento_id){
	AbrePopUp ('documento.php?admin='+admin+'&documento_id='+id, 'documento', 820, 335, 'yes', 'no');
}