// JavaScript Document
function validaDados(obj){
//alert(obj.value);

obj.value = obj.value.replace(/[^0123456789Xx]/g,'');

//alert(obj.value);
}

function validaForm(form) {
	f = form;

	if (f.cnpj != undefined){
		if (f.cnpj.value == ''){
			alert('CNPJ inválido!'
				 +'\nTente novamente.');
			f.cnpj.focus();
			return false;
		}
	}
	if (f.nf != undefined){
		if (f.nf.value == '') {
			alert('Nota fiscal inválida!'
				 +'\nTente novamente.');
			f.nf.focus();
			return false;
		}
	}
	if (f.cs != undefined){
		if (f.cs.value == '') {
			alert('Chave de segurança inválida!'
				 +'\nTente novamente.');
			f.cs.focus();
			return false;
		}
	}
	if (f.imgCode != undefined){
		if (f.imgCode.value == '') {
			alert('Código de validação inválido!'
				 +'\nTente novamente.');
			f.imgCode.focus();
			return false;
		}
	}
	f.action = 'inc/img_auth.php';
	query(arq,local,local);
	f.submit();
}

function emiteAlerta(msg) {
	if (msg != "") {
		alert(unescape(msg));
	}
}

function novoCodigo(){
	MM_goToURL('parent','index.php');return document.MM_returnValue	
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function acesso(tp){
	if (tp == 1)
		query('acesso.php','lc','lc');
	else if (tp == 2)
//alert('vamooo');
		query('nf.php','lc','lc');
}

function createRequest () {
	var xmlhttp;
	try{
		xmlhttp = new XMLHttpRequest();
	} catch(ee) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function query (page, destinationId, loadingId, synchronous) {
	var asynchronous = true;
	if (synchronous != undefined && synchronous == true) asynchronous = false;
	
	var loading = document.getElementById(loadingId);
	loading.innerHTML = '<div> <img src="img/loading.gif" alt="carregando..."/></div>';
//	loading.style.textAlign = 'center';

	var xmlhttp = createRequest();
	
	if (xmlhttp) {
		xmlhttp.open("GET", page, asynchronous);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var destination = document.getElementById(destinationId);
					getXMLHTTPResult(xmlhttp, destination, loading);
				} else { alert( "Error: " + xmlhttp.statusText ); }
			}
		}
		xmlhttp.send(null);
	}
}

function getXMLHTTPResult (xmlhttp, destination, loading) {
	result = parseXML(xmlhttp, destination, loading);

	if (result == null) {
		result = xmlhttp.responseText;
		result = result.replace(/\+/g,' ');
		result = unescape(result);
		loading.innerHTML = '';
		destination.innerHTML = result;
	}
}

function parseXML (xmlhttp, destination, loading) {
	var response = xmlhttp.responseXML.getElementsByTagName('response')[0];

	if (response == undefined) { return null; }
	
	var results = response.getElementsByTagName('result');
	
	if (results.length > 0) {
		for (var i=0; i < results.length; i++) {
			line = destination.insertRow();
			
			if ((i % 2) == 1) destination.rows[i].style.background = '#F5F5F5';
			
			var values = results[i].getElementsByTagName('field');
			
			if (values.length > 0) {
				for (var j=0; j < values.length; j++) {
					cell = line.insertCell();
					{
						try { cell.innerHTML = values[j].firstChild.nodeValue; }
						catch (er) {
	//						alert (er.description);
						}
					}
				}
			} else {
				cell = line.insertCell();
				cell.style.color = 'red';
				cell.innerHTML = 'CONSULTA VAZIA';
			}
		}
	} else {
		line = destination.insertRow();
		cell = line.insertCell();
		cell.style.color = 'red';
		cell.innerHTML = 'CONSULTA VAZIA';
	}

	loading.innerHTML = '';
	
	return true;
}
