/*
	showDiv(idDiv, hide)
	hide == 0 --> Mostra a Div
	hide == 1 --> Mostra a Div e esconde a última aberta
	hide == 2 --> Mostra a Div e esconde todas as outras abertas
	hide == 3 --> Esconde todas as Div's
*/

var stack = [];

function showDiv(id, hide) {

	var hideDiv;
	var myDiv;

	if ((stack.length != 0) && (hide == 1)) {
		hideDiv = stack.pop();
		myDiv = document.getElementById(hideDiv);
		myDiv.style.display = "none";
	}

	if ((stack.length != 0) && ((hide == 2) || (hide == 3))) {
		for (var i = 0; i <= stack.length; i++) {
			hideDiv = stack.pop();
			myDiv = document.getElementById(hideDiv);
			myDiv.style.display = "none";
		}
	}

	if (hide <= 2) {
		myDiv = document.getElementById(id);
	
		if (myDiv.style.display == "none") {
			myDiv.style.display = "";
			stack.push(id);
		}
	}
}


function formatarText(objeto, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8) {
			return true;
		}
	}
	sValue = objeto.value;
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	i = 0; nCount = 0; sCod = "";
	mskLen = fldLen;
	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; }
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}
	objeto.value = sCod;
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); } 
		else { // qualquer caracter...
			return true;
		} 
	}
	else {
		return true;
	}
}

function trim(str){
	str = str.replace(/ /g,'');
	str = str.replace(/^ /,'');
	str = str.replace(/ $/,'');
	return str;
}

function retornarNumero(str){
	var len = str.length;
	var retorno = '';
	var char;
	for ( var i = 0; i <= len-1; i++ ){
		char = str.substring(i, i+1);
		if (!isNaN(char)){ retorno += char;	}
	}
	return trim(retorno);
}

function validarEmail(src) {
	emailReg = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[a-zA-Z]$'
	var regex = new RegExp(emailReg);
	return regex.test(trim(src));
}

function validarCurriculo(frm){
	if (frm.arquivo.value!='') {
		var arquivo = StrRev(frm.arquivo.value);
		arquivo = StrRev(arquivo.substring(0, arquivo.indexOf(".")));
		arquivo = arquivo.toLowerCase();
		if (arquivo!='doc' && arquivo!='docx' && arquivo!='txt' && arquivo!='pdf'){
			alert('Formato de arquivo inválido. Extensões válidas: doc, docx, txt ou pdf.');
			return false;
		} else {
				return true;
		}
	} else {
			alert('Selecione um arquivo que contenha seu currículo.');
			return false;
	}
}
	
function StrRev(str){
	var tmp = "";
	for (i=str.length-1; i >= 0; i--){
		tmp += str.charAt(i);
	}
	return tmp;
}

function validarCPF(StrCPF){
	var sStr = StrCPF.slice(0,1)
	var indStr = 1
	var trocou = false;
	
	while (indStr < StrCPF.length){
		if(sStr != StrCPF.substr(indStr,1)){
			trocou = true;
		}
		indStr++;
	}
	
	if (!trocou){
		return false
	}
	
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	StrCPF1 = "";
	len = StrCPF.length;
	x = len -1;
	
	for (var i=0; i <= len - 3; i++){
		y = StrCPF.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	StrCPF1 = StrCPF.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	
	for (var i=0; i <= len - 2; i++){
		soma = soma + (StrCPF1.substring(i,i+1) * x);
		x = x - 1;
	}
	
	dig2 = 11 - (soma % 11);
	
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	if ((dig1 + "" + dig2) == StrCPF.substring(len,len-2)){
		return true;
	} else {
		return false;
	}
}
