// JavaScript Document
//Funcções colocadas pelo Christopher após as 16horas do dia 6/10/2006

// var global_critica="OK"


function checa_enter_data(event,formulario,label,cpodata) {
	if (event.keyCode==13 || event.which==13) {
//		window.alert('Teclou enter...')
		return crit_data(formulario,label,cpodata)
	}
}


function crit_data(formulario,label,cpodata) {
	var criticavalida=true;
	
	var w_cpodata = document.getElementById(cpodata).value
	var w_dia  = (w_cpodata.substring(0,2));
	var w_mes  = (w_cpodata.substring(3,5));
	var w_ano  = (w_cpodata.substring(6,10));
	
	if (isNaN(w_dia) || isNaN(w_mes) || isNaN(w_ano)) 	{
		criticavalida=false;
//		window.alert('nao numerico...'+w_dia+w_mes+w_ano)
	}
// 		alert('Preencha a data somente com números.')
// 		cpodata.value="";
// 		cpodata.focus();
// 		return false;

	if ((w_dia < 01)||(w_dia > 31)) {
		criticavalida=false;
//		window.alert('<1 ou  >31...'+w_dia+w_mes+w_ano)
	}
	if 	((w_dia > 30) && (w_mes == 04 || w_mes == 06 || w_mes == 09 || w_mes== 11 )) {
		criticavalida=false;
//		window.alert('mes com 30...'+w_dia+w_mes+w_ano)
	}
	
	if (w_mes < 01 || w_mes > 12 || w_ano < 1900) {
		criticavalida=false;
//		window.alert('mes <1 e > 12...'+w_dia+w_mes+w_ano)
	}
	
	if (w_mes == 02 && (w_dia > 29 || (w_dia > 28 && (parseInt(w_ano/4)!=w_ano/4)))) {
		criticavalida=false;
//		window.alert('fevereiro...'+w_dia+w_mes+w_ano)
	}

	if (criticavalida==true) {
		document.getElementById(formulario).submit()
		return true;

	}	
	
	if (criticavalida==false) {
		window.alert('Data '+label+' inválida ! Deve ser data válida (dd/mm/aaaa)...')
		document.getElementById(cpodata).focus()
 		return false;

	}

}

function maskData(data) { 
	var valor=document.getElementById(data).value
//	if (valor.length==2 || valor.length==5) {
//		document.getElementById(data).value=valor+'/' 
//		}

	var w_tamanho=valor.length
	var w_indori1=0
	var w_destino=''
	while (w_indori1<(w_tamanho)) {
		w_indori2=w_indori1+1
		w_byte=(valor.substring(w_indori1,w_indori2))
		if (isNaN(w_byte)) 
			{
			w_indori1=w_indori1+1
		}else
		{
			w_destino=w_destino+w_byte
			w_indori1=w_indori1+1
		}
	}
	var w_tamanho=w_destino.length
	document.getElementById(data).value=w_destino

	if (w_tamanho > 4) {
		document.getElementById(data).value=((w_destino.substring(0,2))+'/'+(w_destino.substring(2,4))+'/'+(w_destino.substring(4,w_tamanho)))
 	 }
	if ((w_tamanho > 2)&&(w_tamanho < 5)) {
		document.getElementById(data).value=((w_destino.substring(0,2))+'/'+(w_destino.substring(2,w_tamanho)))
	 }
}



 // Funções antigas


function periodo(campo,campo2) {
	if (campo==0) {
		document.getElementById(campo2).className="invisivel"
	}
	if (campo==1) {
		document.getElementById(campo2).className="visivel"
	}
}

// * Verificação, preenchimento obrigatório de 1 campo *

function checa_enter_branco1(event,formulario,label,campo1){
		
	if (event.keyCode==13 || event.which==13) {
		return critbranco1(formulario,label,campo1) 
	}
}

function critbranco1(formulario,label,campo1) {
	var criticavalida=true;
	var w_campo1 = document.getElementById(campo1).value
	if (w_campo1=="") {
		window.alert('O campo '+label+' deve ser preenchido')
		document.getElementById(campo1).focus()
		criticavalida=false
 		return false;
	} 
	if (criticavalida==true) {
		document.getElementById(formulario).submit()
		return true;
	}		
}


function checa_enter_branco2(event,formulario,label,campo1,campo2) {
	if (event.keyCode==13 || event.which==13) {
		return critbranco2(formulario,label,campo1,campo2) 
	}
}

function critbranco2(formulario,label,campo1,campo2) {
	var criticavalida=true;
	var w_campo1 = document.getElementById(campo1).value
	var w_campo2 = document.getElementById(campo2).value
	
	if (w_campo1=="")  { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo1)
		criticavalida=false
		return false;
	} 
	if (w_campo2=="") { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo2)
		criticavalida=false
		return false;
	} 
	if (criticavalida==true) {
		document.getElementById(formulario).submit()
		return true;
	}		
}

function checa_enter_branco4(event,formulario,label,campo1,campo2,campo3,campo4) {
	if (event.keyCode==13 || event.which==13) {
		return critbranco4(formulario,label,campo1,campo2,campo3,campo4) 
	}
}

function critbranco4(formulario,label,campo1,campo2,campo3,campo4) {
	
	var criticavalida=true;
	var w_campo1 = document.getElementById(campo1).value
	var w_campo2 = document.getElementById(campo2).value
	var w_campo3 = document.getElementById(campo3).value
	var w_campo4 = document.getElementById(campo4).value
	if (w_campo1=="") { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo1)
		criticavalida=false
		return false;
	} 
	if (w_campo2=="") { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo2)
		criticavalida=false
		return false;
	} 
	if (w_campo3=="") { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo3)
		criticavalida=false
		return false;
	} 
	if (w_campo4=="") { 
		window.alert('Os campos '+label+' devem ser preenchidos')	
		document.getElementById().focus(campo4)
		criticavalida=false
		return false;
	} 
	if (criticavalida==true) {
		document.getElementById(formulario).submit()
		return true;
	}		
}



// * Verificação, de campo numérico *
function camponumerico(cponum) {
	w_cponum = cponum.value;

	if (isNaN(w_cponum)) 
	{
		alert('Este campo aceita somente números')
		cponum.value="";
		cponum.focus();
		return false;
	}
	else	
	{
		return true;
	}
}




