 try{
        xmlhttp = new XMLHttpRequest();
    }catch(ee){
        try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(E){
                xmlhttp = false;
            }
        }
    }



function envia_para_change(valor)
{
	var envia_para = document.form.envia_para;
	if(envia_para)
		envia_para.value = valor;
}

function reload(formulario)
{
//	var mensagem = "Esta página será ecarregada. Pressiona ok e aguarde.";
//	alert(mensagem);
	formulario.submit();

//	var mensagem = "Atenção! Esta página será ecarregada. Continuar?";
//	if(confirm_box(mensagem))
//	{
//		formulario.submit();
//		return true;
//	}
//	return false;

}


function numbersonly(e)
{
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function floatsonly(e)
{
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	else if ((("0123456789.").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

// chame: onKeyPress="return(FormataReais(this,'.',',',event))
function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;
key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}

function url_decode(str) {
	var n, strCode, strDecode = "";

	for (n = 0; n < str.length; n++) {
		if (str.charAt(n) == "%") {
			strCode = str.charAt(n + 1) + str.charAt(n + 2);
			strDecode += String.fromCharCode(parseInt(strCode, 16));
			n += 2;
		} else {
			strDecode += str.charAt(n);
		}
	}

	return strDecode;
}

function listaArea_geral() {
	document.getElementById("area_esp").style.display = 'none';
	document.getElementById("cargo").style.display = 'none';
	document.getElementById("descricao").style.display = 'none';
	document.getElementById("area_geral").style.display = 'none';
    document.getElementById("botao").style.display = 'none';
   	document.getElementById("novo_ramo").style.display = 'none';

    if(document.getElementById("ramo_area_cargo").ramo.value == 0){
    	document.getElementById("novo_ramo").style.display = 'block';
    }

	if(document.getElementById("ramo_area_cargo").ramo.value != ""){
		if(xmlhttp) {
			var frm = document.getElementById("ramo_area_cargo");
			var area_geral = frm.elements["area_geral"];
			var u ="ajax_coleta/listaArea_geral.php?ramo="+frm.ramo.value
			xmlhttp.open("GET",u,true);
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					mm=url_decode(xmlhttp.responseText);
					mm=eval(mm);
					area_geral.options.length = 1;
					for(var x = 0 ; x < mm.length; x++){
						area_geral.options[x+1]= new Option(mm[x]["nome"], mm[x]["id"]);
					}
				}
			}
			xmlhttp.send(null)
		}else{
			alert("Seu navegador não suporta a busca de Ramo, Area e Cargo.")
	    }
		document.getElementById("area_geral").style.display = 'block';
	}
}


function listaArea_esp() {

	document.getElementById("cargo").style.display = 'none';
	document.getElementById("descricao").style.display = 'none';
	document.getElementById("area_esp").style.display = 'none';
    document.getElementById("botao").style.display = 'none';
	if(document.getElementById("ramo_area_cargo").area_geral.value != ""){
	        if(xmlhttp) {
				var frm = document.getElementById("ramo_area_cargo");
				var area_esp = frm.elements["area_esp"];
				var u ="ajax_coleta/listaArea_esp.php?area_geral="+frm.area_geral.value

				xmlhttp.open("GET",u,true);
				xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					mm=url_decode(xmlhttp.responseText);
					mm=eval(mm);
					area_esp.options.length = 1;
					for(var x = 0 ; x < mm.length; x++){
						area_esp.options[x+1]= new Option(mm[x]["nome"], mm[x]["id"]);
					}
				}
			}
			xmlhttp.send(null)
		}else{
	            alert("Seu navegador não suporta a busca de Ramo, Area e Cargo.")
	    }
		document.getElementById("area_esp").style.display = 'block';
	}
}


function listaCargo() {

	document.getElementById("descricao").style.display = 'none';
	document.getElementById("cargo").style.display = 'none';
    document.getElementById("botao").style.display = 'none';
	if(document.getElementById("ramo_area_cargo").area_esp.value != ""){
	        if(xmlhttp) {
				var frm = document.getElementById("ramo_area_cargo");
				var cargo = frm.elements["cargo"];
				var u ="ajax_coleta/listaCargo.php?area_esp="+frm.area_esp.value

				xmlhttp.open("GET",u,true);
				xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					mm=url_decode(xmlhttp.responseText);
					mm=eval(mm);
					cargo.options.length = 1;
					for(var x = 0 ; x < mm.length; x++){
						cargo.options[x+1]= new Option(mm[x]["nome"], mm[x]["id"]);
					}
				}
			}
			xmlhttp.send(null)
		}else{
	            alert("Seu navegador não suporta a busca de Ramo, Area e Cargo.")
	    }
			document.getElementById("cargo").style.display = 'block';
	}
}


function mostra_descricao() {
		document.getElementById("descricao").style.display = 'none';
	    document.getElementById("botao").style.display = 'none';
		if(document.getElementById("ramo_area_cargo").cargo.value != ""){
	        if(xmlhttp) {
				var frm = document.getElementById("ramo_area_cargo");
				var descricao = frm.elements["descricao"];
				var descricao_org = frm.elements["descricao_org"];
				var u ="ajax_coleta/mostra_descricao.php?cargo="+frm.cargo.value

				xmlhttp.open("GET",u,true);
				xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					mm=url_decode(xmlhttp.responseText);
					mm=eval(mm);
					descricao.value = mm;
					descricao_org.value = mm;
				}
			}
			xmlhttp.send(null)
		}else{
	            alert("Seu navegador não suporta a busca de Ramo, Area e Cargo.")
	    }
		    document.getElementById("botao").style.display = 'block';
			document.getElementById("descricao").style.display = 'block';
	}
}


function valida_outro_ramo(frm) {
    if(frm.ramo.value == 0){
		var outro_ramo = frm.elements["outro_ramo"];
		if(outro_ramo.value.length == 0){
			alert("Por favor, digite o nome do ramo.");
			outro_ramo.focus();
			return(false);
		}
    }

    if(frm.regiao.value.length == 0){
		alert("Por favor, Selecione uma região.");
		frm.regiao.focus();
		return(false);
    }

    if(frm.faturamento.value.length == 0){
		alert("Por favor, Selecione um faturamento anual.");
		frm.faturamento.focus();
		return(false);
    }

    return(true);
}

function valida_email(nObjEmail, nObjMsg, msg){

	objEmail 	= window.document.getElementById(nObjEmail);
	objMsg 		= window.document.getElementById(nObjMsg);

	if(objEmail.value.indexOf('@') <= -1 || objEmail.value.indexOf('.') <= -1 || objEmail.value.length < 5){
		objMsg.innerHTML = msg;
		return false;
	}else
		return true;
}


// Metodo para abrir popUP
var win = null;
function NewWindow(mypage,myname,w,h,scroll,toolbar,statubar){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;

	settings = 'height='+h+',width='+w+',scrollbars='+scroll+',resizable,toolbar,status'
	win = window.open(mypage,myname,settings)
}
