//==================================================================================================================//
// VALIDAR E ENVIAR FORMULÁRIOS

function validaForm(formID) {

	var formulario = document.forms[formID];
	var labels	= formulario.getElementsByTagName("label");
	var campos	= formulario.getElementsByTagName("input");

/*
	window.alert('formID [' + formID + ']');
	window.alert('formulario [' + formulario + ']');
	window.alert('TOTAL CAMPOS [' + campos.length + ']');
	window.alert('TOTAL LABELS [' + labels.length + ']');
*/

	var erros=0;

	// Conferindo Labels
	var i=0;
	for (i=0 ; i<labels.length ; i++ ) {

		var checkFor = labels[i].getAttributeNode("for");
		var campo = document.getElementById(checkFor.value);
//		var campo = document.getElementById(checkFor);
//		window.alert('LABEL ATUAL ID[' + i + '] CLASS[ ' + labels[i].className + ' ]');
//		if (campo && campo.value!="") {
		if (checkFor) {
			if (checkFor.value!="" && (labels[i].className=='label_check' || labels[i].className=='label_erro' || labels[i].className=='label_ok')) {
				if (campo.value!="") {
					checkFor.className = "valida_ok";
					labels[i].className = "label_ok";
					window.alert('CAMPO OK ID[' + checkFor.value + '] VALOR[ ' + campo.value + ' ]');
				} else {
					checkFor.className = "valida_erro";
					labels[i].className = "label_erro";
					erros++;
				}
			}
			window.alert('NÚMERO DE ERROS[' + erros + ']');
		}

	}
	
	window.alert('CHEKANDO ERROS');

	// Retorna SIM ou NÃO?
	if (erros) {
		window.alert('TEM ERRO!');
		return false;
	} else {
		window.alert('NÃO TEM ERRO!');
		return true;
	}

}





/*
	// Conferindo Inputs
	var i=0;
	for (i=0 ; i<campos.length ; i++ ) {

		var CampoAtual = campos[i];
		var ClassAtual = CampoAtual.className;
		var Title = CampoAtual.title;
//		var LabelAtual = campos[i].getAttributeNode("for");
		window.alert('CAMPO ATUAL ID[' + i + '] CLASS[ ' + CampoAtual + ' ]');
		if (CampoAtual && Title=='Campo Obrigatório') {
//		if (checkFor && checkFor.value!="" && (ClassAtual=='label_check' || ClassAtual=='label_erro' || ClassAtual=='label_ok')) {
			if (CampoAtual.value == null) {
				CampoAtual.className = "valida_erro";
//				labels[i].className = "label_erro";
				erros++;
			} else {
				CampoAtual.className = "valida_ok";
//				labels[i].className = "label_ok";
				window.alert('CAMPO OK VALOR[ ' + CampoAtual.value + ' ]');
			}
		}
		
	}
//		window.alert('ERROS['+erros+']');
*/

/*
	// Conferindo Labels
	var i=0;
	for (i=0 ; i<labels.length ; i++ ) {

		var checkFor = labels[i].getAttributeNode("for");
		var ClassAtual = labels[i].className;

//		window.alert('CAMPO ATUAL ID[' + i + '] CLASS[ ' + ClassAtual + ' ]');
		if (checkFor && checkFor.value!="" && (ClassAtual=='label_check' || ClassAtual=='label_erro' || ClassAtual=='label_ok')) {
			if (campo.value != null) {
				campo.className = "valida_ok";
				labels[i].className = "label_ok";
				window.alert('CAMPO OK ID[' + checkFor.value + '] VALOR[ ' + campo.value + ' ]');
			} else {
				campo.className = "valida_erro";
				labels[i].className = "label_erro";
				erros++;
			}
		}
//		window.alert('ERROS['+erros+']');

	}
*/




/*
	// Conferindo Inputs
	var i=0;
	for (i=0 ; i<campos.length ; i++ ) {

		var CampoAtual = campos[i];
		var ClassAtual = campos[i].className;
		var TitleAtual = campos[i].title;
		var ValorAtual = campos[i].value;
		
		if (TitleAtual=='obrigatório') {
			if (ValorAtual!='')	{
				ClassAtual = 'valida_ok';
			} else {
				ClassAtual = 'valida_erro';
				erros++;
			}
		}

//		window.alert('CAMPO ATUAL ID[' + i + '] CLASS[ ' + ClassAtual + ' ]');
		window.alert('ERROS['+erros+']');

	}

*/

/*
	var i=0;
	for (i=0 ; i<campo.elements.length ; i++ ) {

		window.alert('CHECKCAMPO.Class [' + campo.elements[i].className + ']');

		if (campo.elements[i].className=='campo_obrigatorio') {

			window.alert('CAMPO ATUAL [' + i + ']');

			var valida 		= campo.elements[i].getAttribute("valida");
			var valida_msg 	= campo.elements[i].getAttribute("valida_msg");
			valida 			= ((valida == null) ? "" : valida )
			valida_msg 		= ((valida_msg == null) ? "Obrigatório" : valida_msg )
			var value = campo.elements[i].getAttribute("value");
			var name = campo.elements[i].name;
			var s = new String()
			if (valida.toLowerCase() == "true" )
			if (value == "" ) {
				alert(valida_msg);
				campo.elements[i].focus();
				return false;
			}
		
		}

	}
*/
	
/*







	window.alert('VALIDANDO FORMULÁRIO [' + formID + ']');

	var check = true;
	var formulario = formID.parentNode;

	window.alert('PRÉ-CAMPOS [' + formulario +'] ');

	var labels	= formulario.getElementsByTagName("label");
	var campos	= formulario.getElementsByTagName("input");
//	var botao	= formulario.getElementsByTagName("a")[0];
//	var span	= formulario.getElementsByTagName("span")[0];
	
	window.alert('CAMPOS [' + campos + ']');
	
	//verificar campos
	for (var i = 0; i < campos.length; i++) {

		var CampoAtual = campos[i];

		window.alert('CAMPO ATUAL [' + CampoAtual + ']');

		if (CampoAtual.className=='obrigatorio') {
			if (CampoAtual.value.length > 0) { CampoAtual.className = "campo_ok"; }
			else { CampoAtual.className = "campo_erro"; check = false; }
		}

//			if (typeof(VariableName) != "undefined")
		if (checkFor && checkFor.value != "") {
			var campo = document.getElementById(checkFor.value);
			if (campo.value.length > 0) { campo.className = "ok"; labels[i].className = "ok"; }
			else { campo.className = "erro"; labels[i].className = "erro"; check = false; }
		}

	}

}
*/


function enviaForm(quem) {

//	window.alert('PRÉ-TESTE [' + quem + ']');

	var check = true;
	var formulario = quem.parentNode;
	var labels = formulario.getElementsByTagName("label");
	var botao = formulario.getElementsByTagName("a")[0];
	var span = formulario.getElementsByTagName("span")[0];

	var erros=0;
	
//	window.alert('TESTE!');

	//verificar campos
	for (var i = 0; i < labels.length; i++) {

//		window.alert('TESTE_DENTRO!');

		var checkFor = labels[i].getAttributeNode("for");

//			if (typeof(VariableName) != "undefined")

		if (checkFor && checkFor.value != "") {
			var campo = document.getElementById(checkFor.value);
			if (campo.value.length > 0) { campo.className = "valida_ok"; labels[i].className = "label_ok"; }
			else { campo.className = "valida_erro"; labels[i].className = "label_erro"; check = false; erros++; }
		}

	}

//	return check;

//	window.alert('TESTE_FIM');

/*
	//enviar caso esteja tudo ok
	if (check) {
		span.innerHTML = "Enviando...";
		formulario.submit();
	}
*/

	if (check==false) {
//		window.alert('TEM ERRO!');
		if (erros>1) jAlert('Há ' + erros + ' campos de preendimento obrigatório pendentes', 'Erro');
		else jAlert('Há 1 campo de preendimento obrigatório pendente', 'Erro');
		return false;
	} else {
//		window.alert('NÃO TEM ERRO!');
		return true;
	}

}

