function valida(enviar) {

	if (enviar.obra.value == "") {
		enviar.obra.focus();
		return false;
	}
	if (enviar.setor.value == "") {
		enviar.setor.focus();
		return false;
	}
	if (enviar.funcao.value == "") {
		enviar.funcao.focus();
		return false;
	}
	if (enviar.colaborador.value == "") {
		enviar.colaborador.focus();
		return false;
	}
	if (enviar.email.value == "") {
		enviar.email.focus();
		return false;
	}
    val = eval("enviar.email.value");
	longueur = val.length;
	pos = val.indexOf("@");
	pos2 = val.lastIndexOf(".");
	if (pos==-1 || pos2==-1 || pos2<pos || pos<1 || pos2-pos<2 || longueur-pos2<2) {
		alert('Email Inválido!');
        enviar.email.focus();
		return false;
    }
	return true;
}

function limpaSelect(select) {
	var i;
	for(i=select.options.length-1;i>=0;i--) {
		select.remove(i);
	}	
}

function buscaFuncao(codigo, id_selecionado){

	limpaSelect(document.getElementById('funcao'));
	if (codigo > 0)	{

		var indice = document.getElementById('funcao').selectedIndex;
		if (document.getElementById) {
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode("Aguarde...")); 
			document.getElementById('funcao').appendChild(new_opcao);

			ajax = ajaxInit();
			if(ajax) {         
				document.getElementById('dvfuncao').style.display = 'block';
				document.getElementById('dvfuncao').style.visibility = 'visible';
				
				ajax.open("GET", "../intranet/lib/?l=funcao&id=" + codigo, true);
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = function() {
					if(ajax.readyState == 4) {
						if(ajax.status == 200) {
							var result = ajax.responseXML;
							buscaFuncaoXML(result, id_selecionado);
						}
					}
				}
				ajax.send(null);
			}
		}
	}
	else {
		var indice = document.getElementById('funcao').selectedIndex;
		if (document.getElementById) { 
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode(" ")); 
			document.getElementById('funcao').appendChild(new_opcao);
		}
	}
}

function buscaFuncaoXML(result, id_selecionado) {

	var subs = result.getElementsByTagName("nome");
	document.getElementById('funcao').innerHTML = "";
	var new_opcao = document.createElement("option"); 
	new_opcao.setAttribute("value",0); 
	new_opcao.appendChild(document.createTextNode("Selecione")); 
	document.getElementById('funcao').appendChild(new_opcao);

	for (var i = 0; i < subs.length; i++) {
		new_opcao = create_opcao(subs[i], id_selecionado); 
		document.getElementById('funcao').appendChild(new_opcao);
	}
}

function buscaSetor(codigo, id_selecionado){

	limpaSelect(document.getElementById('setor'));
	limpaSelect(document.getElementById('funcao'));
	if (codigo > 0)	{

		if (document.getElementById) {
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode("Aguarde...")); 
			document.getElementById('setor').appendChild(new_opcao);

			ajax = ajaxInit();
			if(ajax) {           
				document.getElementById('dvsetor').style.display = 'block';
				document.getElementById('dvsetor').style.visibility = 'visible';
				
				ajax.open("GET", "../intranet/lib/?l=setor&id=" + codigo, true);
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 4 && ajax.status == 200){
						var result = ajax.responseXML;
						buscaSetorXML(result, id_selecionado);
					}
				}
				ajax.send(null);
			}
		}
	}
	else {
		var indice = document.getElementById('setor').selectedIndex;
		if (document.getElementById) { 
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode(" ")); 
			document.getElementById('setor').appendChild(new_opcao);
		}
	}
}

function buscaSetorXML(result, id_selecionado) {

	var subs = result.getElementsByTagName("nome");
	document.getElementById('setor').innerHTML = "";
	var new_opcao = document.createElement("option"); 
	new_opcao.setAttribute("value",0); 
	new_opcao.appendChild(document.createTextNode("Selecione")); 
	document.getElementById('setor').appendChild(new_opcao);

	for (var i = 0; i < subs.length; i++) {
		new_opcao = create_opcao(subs[i], id_selecionado); 
		document.getElementById('setor').appendChild(new_opcao);
	}
}

function buscaColaborador(codigo, id_selecionado){

	limpaSelect(document.getElementById('colaborador'));
	if (codigo > 0)	{

		if (document.getElementById) {
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode("Aguarde...")); 
			document.getElementById('colaborador').appendChild(new_opcao);

			ajax = ajaxInit();
			if(ajax) {   
				
				document.getElementById('dvcolaborador').style.display = 'block';
				document.getElementById('dvcolaborador').style.visibility = 'visible';
				
				ajax.open("GET", "../intranet/lib/?l=colaborador&id=" + codigo, true);
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 4 && ajax.status == 200){
						var result = ajax.responseXML;
						buscaColaboradorXML(result, id_selecionado);
					}
				}
				ajax.send(null);
			}
		}
	}
	else {
		var indice = document.getElementById('colaborador').selectedIndex;
		if (document.getElementById) { 
			var new_opcao = document.createElement("option"); 
			new_opcao.setAttribute("value",0); 
			new_opcao.appendChild(document.createTextNode(" ")); 
			document.getElementById('colaborador').appendChild(new_opcao);
		}
	}
}

function buscaColaboradorXML(result, id_selecionado) {

	var subs = result.getElementsByTagName("nome");
	document.getElementById('colaborador').innerHTML = "";
	var new_opcao = document.createElement("option"); 
	new_opcao.setAttribute("value",0); 
	new_opcao.appendChild(document.createTextNode("Selecione")); 
	document.getElementById('colaborador').appendChild(new_opcao);

	for (var i = 0; i < subs.length; i++) {
		new_opcao = create_opcao(subs[i], id_selecionado); 
		document.getElementById('colaborador').appendChild(new_opcao);
	}
}

function buscaDados(codigo) {

	if (codigo > 0)	{
		ajax = ajaxInit();
		if(ajax) {           
			
			ajax.open("GET", "../intranet/lib/?l=dados&id=" + codigo, true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4 && ajax.status == 200){
					var result = ajax.responseText;
					if (result != '') {					
						//document.getElementById('dadoscolaborador').innerHTML = result;
						document.getElementById('camposform').style.visibility="visible";
						document.getElementById('camposform').style.display="block";
					}
				}
			}
			ajax.send(null);
		}
	}
}

function create_opcao(sub_combo, id_selecionado) { 
    var new_opcao = document.createElement("option"); 
    var texto = document.createTextNode(sub_combo.childNodes[0].data); 

    new_opcao.setAttribute("value",sub_combo.getAttribute("id")); 
    new_opcao.appendChild(texto);

	if (sub_combo.getAttribute("id") == id_selecionado) {
		new_opcao.setAttribute("selected","selected");
	}	

    return new_opcao; 
}

