
function Limpar(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 )
	{ tam = tam - 1 ; }

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{

		if ( tam <= dec )
		{ campo.value = vr ; }

		if ( (tam > dec) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + "." + vr.substr( tam - dec, tam ) ; }
			if ( (tam >= 6) && (tam <= 8) ){
				campo.value = vr.substr( 0, tam - 5 ) + "" + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ) ;
			}
			if ( (tam >= 9) && (tam <= 11) ){
				campo.value = vr.substr( 0, tam - 8 ) + "" + vr.substr( tam - 8, 3 ) + "" + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ) ; }
				if ( (tam >= 12) && (tam <= 14) ){
					campo.value = vr.substr( 0, tam - 11 ) + "" + vr.substr( tam - 11, 3 ) + "" + vr.substr( tam - 8, 3 ) + "" + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ) ; }
					if ( (tam >= 15) && (tam <= 17) ){
						campo.value = vr.substr( 0, tam - 14 ) + "" + vr.substr( tam - 14, 3 ) + "" + vr.substr( tam - 11, 3 ) + "" + vr.substr( tam - 8, 3 ) + "" + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - 2, tam ) ;}
	}

}


function str_ltrim(aString)
{
	return aString.replace(/^[\s]+/g,"");
}

function doPostBack(form, codigo, titulo, metodoget){
	var acao;
	acao = '?step=' + codigo;
	if (metodoget==1)
	{
		form.step.value=codigo;
	}
	if (titulo !='') acao+='&titulo='+titulo;
	form.action=acao;
	form.submit();
}

function remCaracteres(texto, padrao)
{
	var Char;
	for (i = 0; i < texto.length; i++)
	{
		Char = texto.charAt(i);
		if (padrao.indexOf(Char) != -1) texto = texto.replace(texto.charAt(i),"");
	}
	return texto;

}

function isCpf(s){
	var i;
	var cpf = s.value;
	cpf = remCaracteres(cpf, ".-")
	var c = cpf.substr(0,9);
	var dv = cpf.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
	if (d1 == 0 && s.value != ""){
		alert("CPF Invalido");
		s.value = "";
		s.focus();
		return false;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if ((dv.charAt(0) != d1) && s.value != "")
	{
		alert("CPF Invalido");
		s.value = "";
		s.focus();
		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 && s.value != "")
	{
		alert("CPF Invalido");
		s.value = "";
		s.focus();
	}
	return true;
}

function isDate(OBJ, APAGA)
{
	var dia = OBJ.value.substring(0,2);

	if (OBJ.value.substring(3,5) < 10)
	var mes = OBJ.value.substring(4,5);
	else
	var mes = OBJ.value.substring(3,5);

	var ano = OBJ.value.substring(6,10);

	if(( ano%4==0 && ano%100 !=0 ) || ( ano%400==0 ) )
	fev = 29;
	else
	fev = 28;

	var array_mes=new Array(31,fev,31,30,31,30,31,31,30,31,30,31);
	var indice = mes-1;
	if (((dia > array_mes[indice] || dia <= 0) || (mes > 12 || mes <= 0) || (ano < 1910 || ano > 2100)) && (OBJ.value != ""))
	{
		alert("Data Inválida.");
		if(APAGA==true){
			OBJ.value = "";
		}
		OBJ.focus();
	}

}

function isHora(OBJ, APAGA){
	var CAMPO = OBJ.value;
	if(CAMPO!=''){
		var CAMPO = CAMPO.substring(0,4);
		if(CAMPO.indexOf(":") == -1){
			HORA = CAMPO.substring(0,2)+":"+CAMPO.substring(2);
		} else {
			var HORA = CAMPO;
		}
		var hor = HORA.substring(0,2);
		var min = HORA.substring(3);
		if(hor > 24 || hor == '' || min > 60 || min =='' || min == ":" || min.length < 2){
			alert("Hora inválida.\r\nO formato correto deve ser \'hh:mm\'");
			if(APAGA==true){
				OBJ.value = "";
			}
			OBJ.focus();
		} else {
			OBJ.value = HORA;
		}
	}
	else {
		return false;
	}
}

function isEmail(email, apaga) {
	var objRegExp  = /^[A-Za-z]([\w\.]*)@([A-Za-z0-9\.]*)\.(([A-Za-z]{3}\.[A-Za-z]{2}$)|([A-Za-z]{2}\.[A-Za-z]{2}$)|([A-Za-z]{3}$)|([a-z]{2}$))/i ;
	if((objRegExp.test(email.value) == false) && (email.value != "")) {
		alert("E-mail inválido!");
		if(apaga==true){
			email.value = "";
		}
		email.focus();
		return false;
	} else {
		return true;
	}
}

function isNumeric(str)
{
	var len = str.value.length;

	for (i=0; i<len; i++)
	{
		c = str.value.charAt(i);
		if (c < '0' || c > '9')
		{
			alert ("Este campo não deve conter letras ou caracteres especiais");
			str.value = "";
			str.focus();
			return false;
		}
	}
	return true;
}

function validaFormulario(FORM,CAMPO) {
	alert(CAMPO.length);
	for (i=0;i<CAMPO.length;i++) {
		alert(document.frm_aluno.matricula.value);
	}
}

function desativaCampo(frm_campo1, frm_campo2)
{
	var chkbox = frm_campo1;
	if(chkbox.checked)
	{
		frm_campo2.value = "Desabilitado";
		frm_campo2.disabled = true;
	} else {
		frm_campo2.value = "";
		frm_campo2.disabled = false;
	}
}

function desativaSelect(campo1, campo2)
{
	if(campo1.value!=""){
		campo2.disabled = true;
		campo1.disabled = false;
	}else if (campo1.value==""){
		campo1.disabled = true;
		campo2.disabled = false;
	}
}

function IsNum(valor)

{
	var numeros = "0123456789.";
	var num =true;
	var Char;


	for (i = 0; i < valor.length && num == true; i++)
	{
		Char = valor.charAt(i);
		if (numeros.indexOf(Char) == -1)
		{
			num = false;
		}
	}
	return num;

}

function formataCampo(frm_campo, tipo)
{
	var valor = frm_campo.value;
	var len = valor.lenght;
	var novo_valor='';

	switch (tipo){

		case "cep":

		if (len < 8){
			for (i=1;i<(8-len);i++){
				novo_valor&='0';
			}
		}
		novo_valor &=
		valor.substr(0,3) & '.' &
		valor.substr(3,3) & '.' &
		valor.substr(6,3) & '-' &
		valor.substr(9,2);

		break;

		case "cpf":

		if (len < 11){
			for (i=1;i<(11-len);i++){
				novo_valor&='0';
			}
		}
		novo_valor &=
		valor.substr(0,3) & '.' &
		valor.substr(3,3) & '.' &
		valor.substr(6,3) & '-' &
		valor.substr(9,2);

		break;

		case "cnpj":

		if (len < 14){
			for (i=1;i<(14-len);i++){
				novo_valor&='0';
			}
		}
		novo_valor &=
		valor.substr( 0,2) & '.' &
		valor.substr( 2,3) & '.' &
		valor.substr( 5,3) & '/' &
		valor.substr( 8,4) & '-' &
		valor.substr(12,2);

		break;

	}
}

function submitenter(FORM, ACAO, e)
{
	var	keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
		FORM.acao.value = ACAO;
		FORM.submit();
		return false;
	}
	else
	return true;
}

function ativaCampo(CAMPO1, CAMPO2) {

	if(CAMPO1.checked)
	CAMPO2.disabled = false;
	else
	CAMPO2.disabled = true;

}

function alertaObrigatorio(){

	alert("Alguns dos campos obrigatórios não foram preenchidos corretamente.\nVerifique os campos marcados com *");
	return false;

}




function keyDown(e) {
	key_code = e.which;
}

function mascara(Campo, Mascara, evtKeyPress){
	var i, j, Valor, TamanhoCampo, TamanhoMascara, MascaraOk, Codigo, Tecla;

	Tecla = KeyCode(evtKeyPress);
	//	alert(Tecla);
	//            TAB          F5
	if (Tecla==8 || Tecla==9 || Tecla==116) return true;

	Valor = Campo.value;
	Valor = Valor.toString().replace( ':', '' );
	Valor = Valor.toString().replace( '-', '' );
	Valor = Valor.toString().replace( '-', '' );
	Valor = Valor.toString().replace( '.', '' );
	Valor = Valor.toString().replace( '.', '' );
	Valor = Valor.toString().replace( '/', '' );
	Valor = Valor.toString().replace( '/', '' );
	Valor = Valor.toString().replace( '(', '' );
	Valor = Valor.toString().replace( '(', '' );
	Valor = Valor.toString().replace( ')', '' );
	Valor = Valor.toString().replace( ')', '' );
	Valor = Valor.toString().replace( ' ', '' );
	Valor = Valor.toString().replace( ' ', '' );
	TamanhoCampo = Valor.length;
	TamanhoMascara = Mascara.length;

	i = 0; j = 0; Codigo = '';
	TamanhoMascara = TamanhoCampo;

	while ( i <= TamanhoMascara ){

		MascaraOk = ((Mascara.charAt(i) == '-') ||
		(Mascara.charAt(i) == '.') ||
		(Mascara.charAt(i) == '/') ||
		(Mascara.charAt(i) == '(') ||
		(Mascara.charAt(i) == ')') ||
		(Mascara.charAt(i) == ':') ||
		(Mascara.charAt(i) == ' ')
		);

		if (MascaraOk){
			Codigo += Mascara.charAt(i);
			TamanhoMascara++;
		}
		else {
			Codigo += Valor.charAt(j);
			j++;
		}
		i++;
	}

	Campo.value = Codigo;

	if (Tecla != 8 && Tecla != 9){
		if (Mascara.charAt(i-1) == "9" && Mascara.length > Valor.length){
			return (((Tecla > 47) && (Tecla < 58)) || ((Tecla > 95) && (Tecla < 106)));
		}else{
			return true;
		}
	}else{
		return true;
	}
}

function KeyCode(e)
{
	if(document.all){
		return e.keyCode;
	}
	else if(navigator.appName == 'Netscape'){
		return e.which;
	}
}

function numerico(event){
	Tecla = KeyCode(event);
	if ((Tecla > 47) && (Tecla < 58))
	return true;
	else
	return false;
}

function FormataValor(campo,teclapres) {
	var tecla = teclapres.keyCode;
	var tammax = campo.maxLength - 1;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
	
	if ( tecla == 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
		campo.value = vr.substr( 0, tam - 2 ) + '.' + vr.substr( tam - 2, tam ) ; 
		if ( tam <= 2 ){
			campo.value = vr ; }
			if ( (tam > 2) && (tam <= 5) ){
				if ( (tam >= 6) && (tam <= 8) ){
					campo.value = vr.substr( 0, tam - 5 ) + '' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ) ; }
					if ( (tam >= 9) && (tam <= 11) ){
						campo.value = vr.substr( 0, tam - 8 ) + '' + vr.substr( tam - 8, 3 ) + '' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ) ; }
						if ( (tam >= 12) && (tam <= 14) ){
							campo.value = vr.substr( 0, tam - 11 ) + '' + vr.substr( tam - 11, 3 ) + '' + vr.substr( tam - 8, 3 ) + '' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ) ; }
							if ( (tam >= 15) && (tam <= 17) ){
								campo.value = vr.substr( 0, tam - 14 ) + '' + vr.substr( tam - 14, 3 ) + '' + vr.substr( tam - 11, 3 ) + '' + vr.substr( tam - 8, 3 ) + '' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ) ;}
			}
	} else {
		
		return false;
		
	}

}

function validaSenha(SENHA, CONFIRMACAO) {

	if(SENHA==CONFIRMACAO){

		if(SENHA=='')
		return false;

		return true;

	} else {

		return false;

	}
}

function aumentaFckEditor(campo, act){
	
	if(act == '+')
		campo.height = parseInt(campo.height) + 100;
	else {
		if(campo.height >= 350)
			campo.height = parseInt(campo.height) - 100;
		else
			alert('Você não pode diminuir mais o tamanho do campo.');
			
	}
	
}

//////////////////////////////////////
//			TABELA DE CORES			//
//////////////////////////////////////
// create 6-element array
var hex = new Array(6)

// assign non-dithered descriptors
hex[0] = "FF"
hex[1] = "CC"
hex[2] = "99"
hex[3] = "66"
hex[4] = "33"
hex[5] = "00"



// accept triplet string and display as background color
function display(triplet, cp) {
	// set color as background color
	var cor = '#' + triplet;
	document.getElementById(cp).style.background = cor;
	document.getElementById(cp).style.color = cor;
	document.getElementById(cp).value = cor;
	// display the color hexadecimal triplet
	//alert('O JavaSite Responde: A cor deste background é ' + triplet)

}

// draw a single table cell based on all descriptors
function drawCell(red, green, blue, cp) {
	// open cell with specified hexadecimal triplet background color
	document.write('<TD BGCOLOR="#' + red + green + blue + '" width=\'10\' height=\'10\'>')
	// open a hypertext link with javascript: scheme to call display function
	document.write('<div style="width:10px; height:10px; cursor:pointer" onClick="javascript:display(\'' + (red + green + blue) + '\', \'' + cp + '\')">')
	// print transparent image (use any height and width)
	//document.write('<IMG SRC="place.gif" BORDER=0 HEIGHT=12 WIDTH=12>')
	// close link tag
	//document.write('</A>')
	document.write('</div>')
	// close table cell
	document.write('</TD>')
}

// draw table row based on red and blue descriptors
function drawRow(red, blue, cp) {
	// open table row
	document.write('<TR>')
	// loop through all non-dithered color descripters as green hex
	for (var i = 0; i < 6; ++i) {

		drawCell(red, hex[i], blue, cp)

	}
	// close current table row
	document.write('</TR>')

}

// draw table for one of six color cube panels
function drawTable(blue, cp) {
	// open table (one of six cube panels)
	document.write('<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>')
	// loop through all non-dithered color descripters as red hex
	for (var i = 0; i < 6; ++i) {

		drawRow(hex[i], blue, cp)

	}
	// close current table
	document.write('</TABLE>')	

}

// draw all cube panels inside table cells
function drawCube(cp) {
	// open table
	document.write('<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 style="border:1px solid #000000; padding:3px"><TR>')
	// loop through all non-dithered color descripters as blue hex
	for (var i = 0; i < 6; ++i) {

		// open table cell with white background color
		document.write('<TD BGCOLOR="#FFFFFF">')
		// call function to create cube panel with hex[i] blue hex
		drawTable(hex[i], cp)
		// close current table cell
		document.write('</TD>')
		if(i == 2){
			
			document.write('</TR><TR>');
			
		}

	}
	// close table row and table
	document.write('</TR></TABLE>')
}
