var formX = null; // cria variável, atribui valor na pagina "form.xsl"
function swapObject(objHide, objShow){
	hideObject(objHide);
	showObject(objShow);
}

function hideObject(obj){
	document.getElementById(obj).style.display = "none";
}

function showObject(obj){
	document.getElementById(obj).style.display = "block";
}

function showTR(obj){
	document.getElementById(obj).style.display = "table-row";
}

function hideObjectByClassName(container, obj){
	var tags = container.childNodes;
	var regExp = new RegExp(obj);
	for (i = 0; i < tags.length; i++) {
		if(regExp.test(tags[i].className)){
			tags[i].style.display = "none";
		}
	}
}

function showObjectByClassName(container, obj){
	var tags = container.childNodes;
	var regExp = new RegExp(obj);
	for (i = 0; i < tags.length; i++) {
		if(regExp.test(tags[i].className)){
			tags[i].style.display = "block";
		}
	}
}

function getElementByClassName(container, obj){
	var tags = container.childNodes;
	var regExp = new RegExp(obj);
	for (i = 0; i < tags.length; i++) {
		if(regExp.test(tags[i].className)){
			return tags[i];
		}
	}
	return null;
}

function getElementsByClassName(container, obj){
	var tags = container.childNodes;
	var elementsArray = new Array();
	var regExp = new RegExp(obj);
	for (i = 0; i < tags.length; i++) {
		if(regExp.test(tags[i].className)){
			elementsArray.push(tags[i]);
		}
	}
	return elementsArray;
}

function showGrayBox(){
	showObject("grayBox");
	showObject("aboveGrayBox");
}

function hideGrayBox(){
	hideObject("grayBox");
	hideObject("aboveGrayBox");
	document.getElementById("aboveGrayBox").innerHTML = "";
}

function getMarcadas(){
	var marcada = "";
	for(var i = 0; i < formX.length; i++){
		if(formX[i].type == "checkbox"){
			if(formX[i].checked){
				if(formX[i].name.indexOf("marcada")==0){
					marcada += formX[i].value + ";";
				}
			}
		}
	}
   
	formX.checkMarcadas.value=marcada;
	return marcada;   
}

function formata(campo,estilo,sonum,e){
	if(estilo == 'cnpj')
		estilo = '##.###.###/####-##';
		
	if(estilo == 'cep')
		estilo = '#####-###';
		
	if(estilo == 'tel')
		estilo = '(##)####-####';
		
	if(estilo == 'rg')
		estilo = '##.###.###-#';
		
	if(estilo == 'cpf')
		estilo = '###.###.###-##';
		
	if(estilo == 'data')
		estilo = '##/##/####';
		
	if(estilo == 'hora')
		estilo = '##:##:##';
		
	if(estilo == 'horaMin')
		estilo = '##:##';
/*		
	if(estilo == 'ie' && document.cadastro.estado.value == ""){
		document.cadastro.estado.focus();
		estilo = '#';
	}
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'AC')
		estilo = '##.###.###/###-##';

	if(estilo == 'ie' && (document.cadastro.estado.value == 'AL' || document.cadastro.estado.value == 'AP'
						|| document.cadastro.estado.value == 'CE' || document.cadastro.estado.value == 'ES'
						|| document.cadastro.estado.value == 'MA' || document.cadastro.estado.value == 'MS'
						|| document.cadastro.estado.value == 'PA' || document.cadastro.estado.value == 'PB'
						|| document.cadastro.estado.value == 'PI'))
		estilo = '##.######-#';
			
	if(estilo == 'ie' && document.cadastro.estado.value == 'AM')
		estilo = '##.###.###-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'BA')
		estilo = '###.###-##';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'DF')
		estilo = '##.######.###-##';

	if(estilo == 'ie' && (document.cadastro.estado.value == 'GO' || document.cadastro.estado.value == 'RN'))
		estilo = '##.###.###-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'MT')
		estilo = '####.######-#';
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'MG')
		estilo = '###.###.###/####';
		
	if(estilo == 'ie' && (document.cadastro.estado.value == 'SP' || document.cadastro.estado.value == '0'))
		estilo = '###.###.###.###';
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'PR')
		estilo = '###.#####-##';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'PE')
		estilo = '##.#.###.#######-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RJ')
		estilo = '##.###.##-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RS')
		estilo = '###/######-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RO')
		estilo = '#############-#';
	
	if(estilo == 'ie' && (document.cadastro.estado.value == 'RR' || document.cadastro.estado.value == 'SE'))
		estilo = '########-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'SC')
		estilo = '###.###.###';

	if(estilo == 'ie' && document.cadastro.estado.value == 'TO')
		estilo = '##########-#';			
*/
	var keycode;
	if(window.event)
		keycode = e.keyCode;
	else if(e.which)
		keycode = e.which;
	
	if(keycode == 8 || keycode == 46 || keycode == null){
		return true;
	}else{
		cnum = false;
		
		for(i=48;i<=57;i++){
			if(keycode == i)
				cnum = true;
		}
		
		for(i=96;i<=105;i++){
			if(keycode == i)
				cnum = false;
		}
	
		if(sonum && !cnum)
			return false;
	
		retorno = "";
	
		for(i=0;i<=campo.value.length;i++){
			if(i < estilo.length){
				if(estilo.charAt(i) == '#')
					retorno += campo.value.charAt(i);
				else
					retorno += estilo.charAt(i);
			}
			else{
				return false;
			}
		}
	
		campo.value = retorno;
	}
}

function replaceAll(valor, antes, depois){
   while( valor.indexOf(antes) >= 0 ) {
      valor = valor.replace(antes, depois);
   }
   return valor;
}

function verifica_email(txtEmail){
	var email = txtEmail.value;
	var regex = new RegExp("^[a-zA-Z\\d\\._-]*@[a-zA-Z0-9-]*(\\.[a-zA-Z]*)(\\.[a-zA-Z]*)?(\\.[a-zA-Z]{2})?");

	if(!regex.test(email))
		return false;
	
	email = email.replace(regex, "");
	if(email.length>=1){
		return false;
	}

	return true;
}

function verifica_data(txtData){
	var data = txtData.value;
	var date = new Date();
	var ano_atual = date.getYear();
	
	if (data.length == 0){
		return true;
	}				
	if (data.length != 10 && data.length != 0){
		alert("Preencha a DATA corretamente");
		return false;
	}
	dia = new Number(data.substring(0,2));
	mes = new Number(data.substring(3,5));
	ano = new Number(data.substring(6,10));
	situacao = "";
	
	if (data.substring(2,3) != "/" || data.substring(5,6) != "/"){
		situacao = "falsa";
	}
	// verifica o dia valido para cada mes
	if ((dia < 1)||(dia < 01 || dia > 30) && (  mes == 4 || mes == 6 || mes == 9 || mes == 11 ) || dia > 31) {
		situacao = "falsa";
	}
	// verifica se o mes e valido
	if (mes < 1 || mes > 12 ) {
		situacao = "falsa";
	}
	// verifica se e ano bissexto
	if (mes == 2 && ( dia < 1 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) {
		situacao = "falsa";
	}
	// verifica ano
	if(ano > ano_atual){
		situacao = "falsa";
	}
		
	if (situacao == "falsa") {
		alert("Preencha a DATA corretamente");
		return false;
	}
	
	// verifica idade
	if(ano < (ano_atual - 100)){
		alert("Data muito antiga, favor corrigir.");
		return false;
	}
	return true;
}

function verifica_cpf(cpf){
	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;
	}
	add = 0;
	for (var i=0; i < 9; i ++)
		add += parseInt(cpf.charAt(i)) * (10 - i);
	
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
		rev = 0;
	
	if (rev != parseInt(cpf.charAt(9)))
		return false;
	
	add = 0;
	for (var i=0; i < 10; i ++)
		add += parseInt(cpf.charAt(i)) * (11 - i);
	
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
		rev = 0;
	
	if (rev != parseInt(cpf.charAt(10)))
		return false;
	
//	alert('O CPF INFORMADO É VÁLIDO.');
	return true;
}


/************/
/** navbar **/
/************/
function moveFirst() {
	document.formX.action.value="firstPage";
	formX.submit();
}

function moveNext() {
	document.formX.action.value="nextPage";
	formX.submit();
}

function moveLast() {
	document.formX.action.value="lastPage";
	formX.submit();
}

function movePrev() {
	document.formX.action.value="prevPage";
	formX.submit();
}

/****************/
/** navbarajax **/
/****************/
function moveFirstAjax() {
	document.formX.action.value="firstPage";
	navAjax();
}
function moveNextAjax() {
	document.formX.action.value="nextPage";
	navAjax();
}
function moveLastAjax() {
	document.formX.action.value="lastPage";
	navAjax();
}
function movePrevAjax() {
	document.formX.action.value="prevPage";
	navAjax();
}
function navAjax() {
	formX.currentRow.value=formX.currentRowAjax.value;
	formX.totalRows.value=formX.totalRowsAjax.value;

	ChamaPaginaArray( createQueryString(formX), "listagem");
	location.href = "#topo";
}
