// menu
function menu_over(id, linha) {
	// Busca parâmetros do menu selecionado
	var cel_cor_ie = document.getElementById(id + 'CEL_COR_IE').value;
	var cel_cor_ff = document.getElementById(id + 'CEL_COR_FF').value;
	var cel_cor_over = document.getElementById(id + 'CEL_COR_OVER').value;
	var fonte_cor_over = document.getElementById(id + 'FONTE_COR_OVER').value;
	
	// Célula do menu no evento onMouseOver
	var cel = document.getElementById(id + linha);
				
	// Cor de fundo da célula
	var cel_cor = cel.style.backgroundColor.toString().replace(' ','').replace(' ','');

	// Se cor da célula = cor normal da célula 
	if ((cel_cor == cel_cor_ie) || (cel_cor == cel_cor_ff)) {
		// Aplica cor over na célula
		cel.style.backgroundColor = cel_cor_over;
		cel.style.color = fonte_cor_over;
	}
}
	
function menu_out(id, linha) {
	// Busca parâmetros do menu selecionado
	var cel_cor_over_ie = document.getElementById(id + 'CEL_COR_OVER_IE').value;
	var cel_cor_over_ff = document.getElementById(id + 'CEL_COR_OVER_FF').value;
	var cel_cor = document.getElementById(id + 'CEL_COR').value;
	var fonte_cor = document.getElementById(id + 'FONTE_COR').value;

	// Célula do menu no evento onMouseOut
	var cel = document.getElementById(id + linha);
	
	// Cor de fundo da célula
	var cel_cor_over = cel.style.backgroundColor.toString().replace(' ','').replace(' ','');

	// Se cor da célula = cor over da célula
	if ((cel_cor_over == cel_cor_over_ie) || (cel_cor_over == cel_cor_over_ff)) {
		// Aplica cor normal na célula
		cel.style.backgroundColor = cel_cor;
		cel.style.color = fonte_cor;
	}
}	

function menu_click(url, id, linha) {
	// Se redireciona
	if (url) {
		var frame = document.getElementById('IF1');
		frame.src = url;
	} else { 
		var cel_cor = document.getElementById(id + 'CEL_COR').value;
		var fonte_cor = document.getElementById(id + 'FONTE_COR').value;
		var cel_cor_selecionada = document.getElementById(id + 'CEL_COR_SELECIONADA').value;
		var fonte_cor_selecionada = document.getElementById(id + 'FONTE_COR_SELECIONADA').value;
		
		// Restaura a cor de todas as linhas	
		var i = 0;
		while(document.getElementById(id + i)) {
			document.getElementById(id + i).style.backgroundColor = cel_cor;
			document.getElementById(id + i).style.color = fonte_cor;
			i++;
		}
	
		// Aplica a cor na linha selecionada
		document.getElementById(id + linha).style.backgroundColor = cel_cor_selecionada;
		document.getElementById(id + linha).style.color = fonte_cor_selecionada;
	}
}

// Busca pks selecionadas em um select
function select_dependente(id) {
	var url = 'select.dependente.php?ID=' + id;
	var indice = 0;
	
	var i = 0;
	while (document.getElementById(id).options[i]) {
		if ((document.getElementById(id).options[i].selected == true) && document.getElementById(id).options[i].value) {
			url = url + '&PK[' + indice + ']=' + document.getElementById(id).options[i].value;
			indice++;
		}
		
		i++;
	}
	
	document.getElementById('IF6').src = url; 
}

// Organiza a seleção em um combo múltiplo com classificação
function combo_classificacao(indice, classificacao) {
	var i = indice;
	while (classificacao[i]) {
		var j = 0;
		while (classificacao[j]) {
			if (classificacao[j].substr(0, classificacao[i].length) == classificacao[i]) {
				if (document.getElementById(classificacao[i]).selected == true) {
					document.getElementById(classificacao[j]).selected = true;
				} else {
					document.getElementById(classificacao[j]).selected = false;
				}
			}

			j++;
		}	
				
		i++;
	}
}

// Search text
function campo_text(id, campo, value, dependencia) {
	var out = "<nobr><input type='text' name='" + campo + "_TEXT' id='" + campo + "_TEXT' value='" + value + "' size='50' style='width:300px;border:1px solid #000000;' onChange=javascript:document.getElementById('IF6').src='search.php?'+campo_text_dependencia(Array(";

	var i = 0;
	while (dependencia[i]) {
		if (i > 0) {
			out = out + ",";
		}
		
		out = out + "Array('" + dependencia[i][0] + "','" + dependencia[i][1] + "')";
		i++;
	}	

	out = out + "))+'&WHERE='+this.value+'&ID=" + campo + "';>&nbsp;<img src='imagens/lupa.gif' border='0'></nobr>";
		
	if (id) {
		document.getElementById(id).innerHTML = out;
	} else {	
		return out;
	}	
}

function campo_text_dependencia(dependencia) {
	var dep = '';
	if (dependencia) {
		var i = 0;
		while (dependencia[i]) {	
			if (dependencia[i][1] == "SELECT") {
				var j = 0;
				var indice = 0;
				while (document.getElementById(dependencia[i][0]).options[j]) {
					if ((document.getElementById(dependencia[i][0]).options[j].selected == true) && document.getElementById(dependencia[i][0]).options[j].value) {
						dep = dep + "&DEPENDENCIA[" + i + "][" + indice + "]=" + document.getElementById(dependencia[i][0]).options[j].value;
						indice++;
					}
					
					j++;
				}
			}	
			
			i++;
		}
	}
	
	return dep;
}

// Search select
function campo_select(id, campo, value, descr, dependencia, onchange) {
	var out = "<div style='width:300px;border:1px solid #000000;'>";
	out = out + "<select name='" + campo + "_SELECT' id='" + campo + "_SELECT' style='width:300px;' onBlur=javascript:if((this.value)&&(this.value!='$')){";
	
	if (onchange) {
		out = out + onchange;
	}
	
	out = out + "document.getElementById('" + campo + "_HIDDEN').value=this.value;document.getElementById('" + campo + "_DIV').innerHTML=campo_text('','" + campo + "',this.options[this.selectedIndex].text,Array(";
	
	var i = 0;
	while (dependencia[i]) {
		if (i > 0) {
			out = out + ",";
		}
		
		out = out + "Array('" + dependencia[i][0] + "','" + dependencia[i][1] + "')";
		i++;
	}
	
	out = out + "));}else{document.getElementById('" + campo + "_HIDDEN').value='';document.getElementById('" + campo + "_DIV').innerHTML=campo_text('','" + campo + "','',Array(";
	
	var i = 0;
	while (dependencia[i]) {
		if (i > 0) {
			out = out + ",";
		}
		
		out = out + "Array('" + dependencia[i][0] + "','" + dependencia[i][1] + "')";
		i++;
	}
	
	out = out + "));}>";
	
	var i = 0;
	while (value[i]) {
		out = out + "<option value='" + value[i] + "'>" + descr[i] + "</option>";
		i++;
	}
	
	out = out + "</div>";

	if (id) {
		document.getElementById(id).innerHTML = out;
	} else {	
		return out;
	}
}

// Formata número
function formata_numero(num, casas) {
	var ret = '';
	
	num = num.toString().replace('.','').replace('.','').replace('.','').replace(',','.');
	var aux = num.split('.');
	
	if (isNaN(aux[1]) || isNaN(casas) || (aux[1] == 0)) {
		aux[1]  = '';
		while (casas > 0) {
			aux[1] = aux[1] + '0';
			casas--;
		}	
	} else {
		aux[1] = aux[1].substring(0,casas);
		
		var d = '1';
		var i = casas;
		while (i > 0) {
			d = d + '0';
			i--;
		}	
		d = new Number(d); 
	
		var tmp = Math.round(num * d) / d;
		tmp = tmp.toString();
		aux = tmp.split('.');
				
		if (aux[1]) {
			while (aux[1].length < casas) {
				aux[1] = aux[1] + '0';
			}
		} else {
			aux[1] = '';
		}
	}	
	
	if (isNaN(aux[0])) {
		aux[0] = '0';
	} else {	
		if(aux[0] < 0) {
			aux[0] = Math.abs(aux[0]);
			ret = ret + '-';
		}
				
		if (aux[0].length > 1) {
			var tmp = aux[0];
			aux[0] = '';
			for (var i = 0; i < tmp.length; i++) {
				var c = tmp.toString().charAt(i);
				if ((c != '0') || (aux[0] != '')) {
					aux[0] = aux[0] + c;
				}
			}
		}	
		
		for (var i = 0; i < Math.floor((aux[0].length-(1+i))/3); i++) {
			aux[0] = aux[0].substring(0,aux[0].length-(4*i+3)) + '.' + aux[0].substring(aux[0].length-(4*i+3));
		}
	}	

	ret = ret + aux[0];
	if (aux[1]) {
		ret = ret + ',' + aux[1];
	}	
      
	return ret;
}

// botão mais
function botao_mais(campos, url) {
	var i = 0;
	while(document.getElementById('ID_'+i)) {
		url = url + '&ID[' + i + ']=' + document.getElementById('ID_' + i).value;
		
		var j = 0;
		while(campos[j]) {
			url = url + '&' + campos[j] + '[' + i + ']=' + document.getElementById(campos[j] + '_' + i).value;
			j++;
		}	

		i++;
	}

	document.getElementById('IF4').src = url;
}

// Excluir
function excluir(acao, id_erro, id_linha, id_linha_filha, classificacao, nr_linha, mensagem, cor1, cor2, cor_destaque) {
	// Restaura linhas
	restaura_lista(id_linha, id_linha_filha, classificacao, id_erro, cor1, cor2);
	
	if (id_linha) {
		// Destaca a linha a ser deletada
		document.getElementById(id_linha + nr_linha).style.textDecoration = 'line-through';
		document.getElementById(id_linha + nr_linha).style.backgroundColor = cor_destaque;
		
		if (classificacao) {
			var i = 0;
			while (classificacao[i]) {
				if (classificacao[i].substr(0, nr_linha.length) == nr_linha) {
					document.getElementById(id_linha + classificacao[i]).style.textDecoration = 'line-through';
					document.getElementById(id_linha + classificacao[i]).style.backgroundColor = cor_destaque;
				}
				
				i++;
			}
		} else if (id_linha_filha) {
			var i = 0;
			while (document.getElementById(id_linha_filha + nr_linha + i)) {
				document.getElementById(id_linha_filha + nr_linha + i).style.textDecoration = 'line-through';
				document.getElementById(id_linha_filha + nr_linha + i).style.backgroundColor = cor_destaque;
				i++;
			}
		}
	} else if (id_linha_filha) {
		// Destaca a linha a ser deletada
		document.getElementById(id_linha_filha + nr_linha).style.textDecoration = 'line-through';
		document.getElementById(id_linha_filha + nr_linha).style.backgroundColor = cor_destaque;
	}
	
	// Substitui os separadores por espaço na mensagem
	var i = 0;
	var string = mensagem;
	mensagem = '';
	while (string.toString().charAt(i)) {
		mensagem = mensagem + string.toString().charAt(i).replace('+',' ');
		i++;
	}	
			
	//Mostra mensagem de confirmação
	out = "<table align='center' style='border:1px solid #000000;' bgcolor='#FFFFFF'>";
	out = out + "<tr><td align='center'><img src='imagens/alert.jpg' border='0'></td></tr>";
	out = out + "<tr><td align='center' style='color:#DD0000;'><b>" + mensagem + "</b></td></tr>";
	out = out + "<tr><td align='center'><input type='button' name='SIM' id='SIM' style='background-color:#666666; color:#FFFFFF; font-weight:bold; cursor:pointer; cursor:hand;' value='Sim' onClick=javascript:document.getElementById('IF1').src='" + acao + "';>";
	out = out + "&nbsp;&nbsp;<input type='button' name='NAO' id='NAO' value='N&atilde;o' style='background-color:#666666; color:#FFFFFF; font-weight:bold; cursor:pointer; cursor:hand;' onClick=javascript:restaura_lista('" + id_linha + "','" + id_linha_filha + "',Array(";
	var i = 0;
	while (classificacao[i]) {
		if (i > 0) {
			out = out + ',';
		}	
		
		out = out + "'" + classificacao[i] + "'";
		i++;
	}
	out = out + "),'" + id_erro + "','" + cor1 + "','" + cor2 + "');>";
	out = out + "</td></tr></table>";
	
	document.getElementById(id_erro + nr_linha).innerHTML = out;
}
								
function restaura_lista(id_linha, id_linha_filha, classificacao, id_erro, cor1, cor2) { 
	// Se classificacao 
	if (classificacao && (classificacao.length > 0)) {
		// Apaga mensagem de erro de todas as linhas e retira underline de todas as linhas
		var i = 0;
		while (classificacao[i]) {
			document.getElementById(id_erro + classificacao[i]).innerHTML = '';
			document.getElementById(id_linha + classificacao[i]).style.textDecoration = 'none';
			
			if ((i % 2) == 0) {
				var cor	= cor2;
			} else {
				var cor = cor1;
			}
			document.getElementById(id_linha + classificacao[i]).style.backgroundColor = cor;
			i++;
		}
	} else {
		// Apaga mensagem de erro de todas as linhas
		var i = 0;
		while (document.getElementById(id_erro + i)) {
			document.getElementById(id_erro + i).innerHTML = '';
			i++;
		}

		// Retira underline de todas as linhas
		if (id_linha) {
			var i = 0;
			while (document.getElementById(id_linha + i)) { 
				document.getElementById(id_linha + i).style.textDecoration = 'none';
				
				if ((i % 2) == 0) {
					var cor	= cor2;
				} else {
					var cor = cor1;
				}
				document.getElementById(id_linha + i).style.backgroundColor = cor;
				
				if (id_linha_filha) {
					var j = 0;
					while (document.getElementById(id_linha_filha + i + j)) {
						document.getElementById(id_linha_filha + i + j).style.textDecoration = 'none';
						
						if ((j % 2) == 0) {
							var cor	= cor2;
						} else {
							var cor = cor1;
						}
						document.getElementById(id_linha_filha + i + j).style.backgroundColor = cor;
						j++;
					}
				}	
							
				i++;
			}
		} else if (id_linha_filha) {
			var i = 0;
			while (document.getElementById(id_linha_filha + i)) {
				document.getElementById(id_linha_filha + i).style.textDecoration = 'none';
				
				if ((i % 2) == 0) {
					var cor	= cor2;
				} else {
					var cor = cor1;
				}
				document.getElementById(id_linha_filha + i).style.backgroundColor = cor;
				i++;
			}
		}
	}	
}

// FUNÇÕES DO CALENDÁRIO
var weekend = [0,6];
var fd = 0;

var gNow = new Date();
var ggWinCal;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

Calendar.Months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_item, p_WinCal, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))    return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;

	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
		
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.print = Calendar_print;

function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) {
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];

		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	var ret_arr = new Array();

	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}

	return ret_arr;
}

function Calendar_print() {
	ggWinCal.print();
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.show = function(id_calendario, id_msg, id_campo) {
	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	
	var out = "<table align='center'><tr>";
	out = out + "<td><img src='imagens/ano_anterior.gif' border='0' onClick=javascript:parent.Build('" + this.gReturnItem + "','" + id_calendario + "','" + id_msg + "','" + this.gMonth + "','" + (parseInt(this.gYear)-1) + "','" + this.gFormat + "'); alt='Ano Anterior' style='cursor:pointer;cursor:hand;'></td>";
	out = out + "<td><img src='imagens/mes_anterior.gif' border='0' onClick=javascript:parent.Build('" + this.gReturnItem + "','" + id_calendario + "','" + id_msg + "','" + prevMM + "','" + prevYYYY + "','" + this.gFormat + "'); alt='M&ecirc;s Anterior' style='cursor:pointer;cursor:hand;'></td>";
	out = out + "<td><b>" + this.gMonthName + " " + this.gYear+ "</b></td>";
	out = out + "<td><img src='imagens/mes_proximo.gif' border='0' onClick=javascript:parent.Build('" + this.gReturnItem + "','" + id_calendario + "','" + id_msg + "','" + nextMM + "','" + nextYYYY + "','" + this.gFormat + "'); alt='Pr&oacute;ximo M&ecirc;s' style='cursor:pointer;cursor:hand;'></td>";
	out = out + "<td><img src='imagens/ano_proximo.gif' border='0' onClick=javascript:parent.Build('" + this.gReturnItem + "','" + id_calendario + "','" + id_msg + "','" + this.gMonth + "','" + (parseInt(this.gYear)+1) + "','" + this.gFormat + "'); alt='Pr&oacute;ximo Ano' style='cursor:pointer;cursor:hand;'></td>";
	out = out + "</tr></table><br>";
	out = out + "<table border='0' width='235' style='border:2px solid #000000;'><tr>";
	out = out + "<td width='15%' style='background-color:#777777;color:#FFFFFF;'><b>Dom</b></td>";
	out = out + "<td width='14%' style='background-color:#777777;color:#FFFFFF;'><b>Seg</b></td>";
	out = out + "<td width='14%' style='background-color:#777777;color:#FFFFFF;'><b>Ter</b></td>";
	out = out + "<td width='14%' style='background-color:#777777;color:#FFFFFF;'><b>Qua</b></td>";
	out = out + "<td width='14%' style='background-color:#777777;color:#FFFFFF;'><b>Qui</b></td>";
	out = out + "<td width='14%' style='background-color:#777777;color:#FFFFFF;'><b>Sex</b></td>";
	out = out + "<td width='15%' style='background-color:#777777;color:#FFFFFF;'><b>S&aacute;b</b></td>";
	out = out + "</tr>";
				
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	   
	out = out + "<tr>";
	for (i=0; i<vFirstDay; i++) {
		out = out + "<td width='14%'></td>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		out = out + "<td width='14%' style='cursor:pointer;cursor:hand;background-color:#E0E0E0;' onClick=javascript:document.getElementById('" + id_campo + "').value='" + this.format_data(vDay) + "';document.getElementById('" + id_calendario + "').innerHTML='';document.getElementById('" + id_msg + "').innerHTML='';document.getElementById('FLAG_" + id_calendario + "').value='N'; onMouseOver=javascript:this.style.backgroundColor='#BBBBBB'; onMouseOut=javascript:this.style.backgroundColor='#E0E0E0';><b>" + vDay + "</b></td>";
		vDay=vDay + 1;
	}
	out = out + "</tr>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		out = out + "<tr>";

		for (j=0; j<7; j++) {
			out = out + "<td width='14%' style='cursor:pointer;cursor:hand;background-color:#E0E0E0;' onClick=javascript:document.getElementById('" + id_campo + "').value='" + this.format_data(vDay) + "';document.getElementById('" + id_calendario + "').innerHTML='';document.getElementById('" + id_msg + "').innerHTML='';document.getElementById('FLAG_" + id_calendario + "').value='N'; onMouseOver=javascript:this.style.backgroundColor='#BBBBBB'; onMouseOut=javascript:this.style.backgroundColor='#E0E0E0';><b>" + vDay + "</b></td>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			out = out + "</tr>";
		if (vOnLastDay == 1)
			break;
	}
	
	out = out + "</table>";
	
	document.getElementById(id_calendario).innerHTML = out;
}

Calendar.prototype.format_data = function(p_day) {
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	var vData = vDD + "\/" + vMonth + "\/" + vY4;
	return vData;
}

function Build(id_campo, id_calendario, id_msg, p_month, p_year, p_format) {
	var p_WinCal = ggWinCal; 
	var gCal = new Calendar(id_campo, p_WinCal, p_month, p_year, p_format);
	   
	gCal.show(id_calendario, id_msg, id_campo);
}

function show_calendar(id_campo, id_calendario, id_msg) {
	p_month =  new String(gNow.getMonth());
	p_year = new String(gNow.getFullYear().toString());
	
	Build(id_campo, id_calendario, id_msg, p_month, p_year, 'DD/MM/YYYY');
}

// CPF
function valida_cpf(c) {
	c = c.replace('-', '');
	c = c.replace('.', '');
	c = c.replace('.', '');
	var i;
	s = c;
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	var v = false;
	if(!c) return true;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(10-i);
	}
	if (d1 == 0) {
		v = true;
		return false;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1) {
		v = true;
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) {
		v = true;
		return false;
	}
	if (!v) {
		return true;
	}
}

function formata_cpf(cpf, id_erro) {
	cpf_original = cpf;
	
	if (valida_cpf(cpf)) {
		var tam = cpf.length;
		var ret = '';
		var count = 0;

		for (var i = tam; i > 0; i--) {
			var letra = cpf.substring(i-1, i);
			if ((letra == '0') || (letra == '1') || (letra == '2') || (letra == '3') || (letra == '4') ||
				(letra == '5') || (letra == '6') || (letra == '7') || (letra == '8') || (letra == '9')) {
				if (count == 2) {
					ret = ret + '-';
				}
				if ((count == 5) || (count == 8)) {
					ret = ret + '.';
				}
				ret = ret + letra;
				count++;
			}
		}

		var tam = ret.length;
		var cpf = ret;
		var ret = '';

		if (tam == 14) {
			for (var i = tam; i > 0; i--) {
				var letra = cpf.substring(i-1, i);
				ret = ret + letra;
			}
								
			document.getElementById(id_erro).innerHTML = '';	
			return ret;
		} else {
			alerta_em_linha(id_erro, 'CPF Inv&aacute;lido!');
			return cpf_original;
		}
	} else {
		alerta_em_linha(id_erro, 'CPF Inv&aacute;lido!');
		return cpf_original;
	}
}

// CNPJ
function formata_cnpj(cnpj, id_erro) {
	cnpj_original = cnpj;
	
	//if (valida_cnpj(cnpj)) {
		var tam = cnpj.length;
		var ret = '';
		var count = 0;

		for (var i = tam; i > 0; i--) {
			var letra = cnpj.substring(i-1, i);
			if ((letra == '0') || (letra == '1') || (letra == '2') || (letra == '3') || (letra == '4') ||
				(letra == '5') || (letra == '6') || (letra == '7') || (letra == '8') || (letra == '9')) {
				if (count == 2) {
					ret = ret + '-';
				} else if (count == 6) {
					ret = ret + '/';
				} else if ((count == 9) || (count == 12)) {
					ret = ret + '.';
				}
				ret = ret + letra;
				count++;
			}
		}

		var tam = ret.length;
		var cnpj = ret;
		var ret = '';

		if (tam == 18) {
			for (var i = tam; i > 0; i--) {
				var letra = cnpj.substring(i-1, i);
				ret = ret + letra;
			}
								
			document.getElementById(id_erro).innerHTML = '';	
			return ret;
		} else {
			alerta_em_linha(id_erro, 'CNPJ Inv&aacute;lido!');
			return cnpj_original;
		}
	//} else {
	//	alerta_em_linha(id_erro, 'CNPJ Inv&aacute;lido!');
	//	return cnpj_original;
	//}
}

// ALERTA DE ERRO EM LINHA
function alerta_em_linha(id, msg) {
	var id = document.getElementById(id);
	id.innerHTML = "&nbsp;<img src='imagens/alerta.gif' border='0'>&nbsp;<b>" + msg + "</b>";
}

// LOGIN
function valida_login() {
	var email = document.getElementById('LOGIN_FORM_EMAIL');
	var senha = document.getElementById('LOGIN_FORM_SENHA');
	var msg = document.getElementById('LOGIN_MSG');

	if (!email.value) {
		email.style.backgroundColor = '#EEF398';
		email.focus();
		
		var out = "<p align='center' style='background-color:#FFFFFF; color:#DD0000; border:1px solid #000000; padding:5px;'>";
		out += "<img src='imagens/alert.gif' border='0'><br>Digite o seu E-mail!</p>";	
		msg.innerHTML = out;
						
		return false;
	} else { 
		email.style.backgroundColor = '#FFFFFF';
	}
	
	if (!senha.value) {
		senha.style.backgroundColor = '#EEF398';
		senha.focus();
						
		var out = "<p align='center' style='background-color:#FFFFFF; color:#DD0000; border:1px solid #000000; padding:5px;'>";
		out += "<img src='imagens/alert.gif' border='0'><br>Digite a sua Senha!</p>";
		msg.innerHTML = out;
		
		return false;				
	} else {
		senha.style.backgroundColor = '#FFFFFF';
	}	
	
	document.LOGIN_FORM.action = 'login.php?MODULO=' + document.getElementById('MENU_MODULO').value + '&COLOR=' + document.getElementById('MENU_FONTE_COR').value.toString().replace('#','') + '&BGCOLOR=' + document.getElementById('MENU_CEL_COR').value.toString().replace('#','') + '&COLOR_SEL=' + document.getElementById('MENU_FONTE_COR_SELECIONADA').value.toString().replace('#','') + '&BGCOLOR_SEL=' + document.getElementById('MENU_CEL_COR_SELECIONADA').value.toString().replace('#','');
	document.LOGIN_FORM.submit();
}

// LOGOFF
function cancela_login() {
	var cancelar = document.getElementById('LOGIN_FORM_CANCELAR');
	
	if (cancelar.value = 'Logoff') {
		var frame = document.getElementById('IF1');
		frame.src = 'logoff.php?MODULO=' + document.getElementById('MENU_MODULO').value + '&COLOR=' + document.getElementById('MENU_FONTE_COR').value.toString().replace('#','') + '&BGCOLOR=' + document.getElementById('MENU_CEL_COR').value.toString().replace('#','') + '&COLOR_SEL=' + document.getElementById('MENU_FONTE_COR_SELECIONADA').value.toString().replace('#','') + '&BGCOLOR_SEL=' + document.getElementById('MENU_CEL_COR_SELECIONADA').value.toString().replace('#','');
	} else {	
		var email = document.getElementById('LOGIN_FORM_EMAIL');
		var senha = document.getElementById('LOGIN_FORM_SENHA');
		var msg = document.getElementById('LOGIN_MSG');
						
		email.disabled = false;
		email.style.backgroundColor = '#FFFFFF';
		email.style.border = '1px solid #000000';
		email.value = '';
		
		senha.disabled = false;
		senha.style.backgroundColor = '#FFFFFF';
		senha.style.border = '1px solid #000000';
		senha.value = '';
		
		msg.innerHTML = '';
		
		cancelar.value = 'Cancelar';
	}	
}	