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 == 'cnh')
		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 == 'preco')
		estilo = '##########';
	
	if(estilo == 'parcelamento')
		estilo = '##';
	if(estilo == 'num')
		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;
}

/************/
/** 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";
}
