//--- conta caracteres ---//

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {

var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "mensagem";

var countBody = opt_countBody ? opt_countBody : "countBody";

var maxSize = opt_maxSize ? opt_maxSize : 1024;



var field = document.getElementById(countedTextBox);



if (field && field.value.length >= maxSize) {

field.value = field.value.substring(0, maxSize);

}

var txtField = document.getElementById(countBody);

if (txtField) {

txtField.innerHTML = field.value.length;

}

}

//--- fecha conta caracteres ---//



//--- mascaras gerais ---//

function txtBoxFormat(objForm, strField, 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 = objForm[strField].value;

// Limpa todos os caracteres de formatação que

// já estiverem no campo.

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++;

}



objForm[strField].value = sCod;

if (nTecla != 8) { // backspace

if (sMask.charAt(i-1) == "9") { // apenas números...

return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9

else { // qualquer caracter...

return true;

}

} else {

return true;

}

}

//--- fecha mascaras gerais ---//



//--- auto tab ---//

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {

var keyCode = (isNN) ? e.which : e.keyCode;

var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

if(input.value.length >= len && !containsElement(filter,keyCode)) {

	input.value = input.value.slice(0, len);

	input.form[(getIndex(input)+1) % input.form.length].focus();

}



function containsElement(arr, ele) {

var found = false, index = 0;

while(!found && index < arr.length)

if(arr[index] == ele)

	found = true;

else

	index++;

	return found;

}



function getIndex(input) {

var index = -1, i = 0, found = false;

while (i < input.form.length && index == -1)

	if (input.form[i] == input)index = i;

	else i++;

	return index;

}

return true;

}

//--- fim auto tab ---//



//--- 1 - Abrir janela poup-up simples -----------------------------//



// Argumento 1 - Ex: teste.html

// Argumento 2 - Ex: janela1

// Argumento 3 - EX: toolbar=no,location=no,

//					 status=no,menubar=no,scrollbars=no,

//					 resizable=no,width=350,height=250,left=80,top=80

// Argumento 4 - Ex: 1,0



function abre(site,nome,desc,esconde){

	window.open(site,nome,desc);

	if(esconde==1){

		window.blur();

		window.focus();	

	}

}

//--- Fim 1 --------------------------------------------------------//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//--- 2 - Abrir janela poup-up centralizada ------------------------//



// Argumento 1 - Ex: teste.html

// Argumento 2 - Ex: janela1

// Argumento 3 - Ex: 300

// Argumento 4 - Ex: 500

// Argumento 5 - Ex: yes ,no

// Argumento 6 - Ex: yes ,no



function abre_centro(site, nome, w, h, scroll, tam){



	var winl = (screen.width - w) / 2;

	var wint = (screen.height - h) / 2;

	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+tam+'';

	win = window.open(site, nome, winprops)

		if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }

		return win;



}

//--- Fim 2 --------------------------------------------------------//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//--- 3 - Criar Título Personalizado do site -----------------------//



function titulo_dinamico(a,b,c){

	

	

	var a='Texto1';

	var b='Texto2';

	var c='Texto3';

	

	var t = new Date();

 	s = t.getSeconds();

 	

 	if (s == 10) {

  		document.title = a;}

 	else if (s == 20) {

 		document.title = b;}

 	else if (s == 30) {

 		document.title = c;}

 	else if (s == 40) {

 		document.title = a;}

 	else if (s == 50) {

		document.title = b;}

 	else if (s == 00) {

 		document.title = c;}

 											

 	setTimeout("titulo_dinamico()", 10);

}



//--- Fim 3 --------------------------------------------------------//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//--- 4 - Mascarar campos ------------------------------------------//

/*

Itens de Mascara:

	Numérico: "9"

	AlfaNumérico: "#"



EX: onkeydown="mascara(this,event,'99/99/9999 ## 99:99');"> 



*/



function mascara(Campo,teclapres,mascara){ 

    

    var idnumer = "9";

    var idalpha = "#";

	var strtext = Campo.value;

    var tamtext = strtext.length;

    var tammask = mascara.length;

    var arrmask = new Array(tammask);

    

    for (var i = 0 ; i < tammask; i++){ 

        arrmask[i] = mascara.slice(i,i+1) 

    }

    	

    if(tammask < tamtext+1 && (teclapres.keyCode != 8 && teclapres.keyCode != 9 && teclapres.keyCode != 13 && teclapres.keyCode != 37 && teclapres.keyCode != 38 && teclapres.keyCode != 39 && teclapres.keyCode != 40 && teclapres.keyCode != 46)){

    	window.event.returnValue = false;

    }

  

    if(arrmask[tamtext] == idnumer){ 

        if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){

            verificarMascara(Campo,arrmask[tamtext],teclapres.keyCode,strtext);

        }else{

            window.event.returnValue = false;

        } 

    } 

    

    if(arrmask[tamtext] == idalpha){

    	verificarMascara(Campo,arrmask[tamtext],teclapres.keyCode,strtext);

    }

    

    if(arrmask[tamtext+1]!=idnumer || arrmask[tamtext+1]!=idalpha){

    	verificarMascara(Campo,arrmask[tamtext],teclapres.keyCode,strtext);

    }

}



// Função Auxiliaar de mascara



function verificarMascara(Campo,arrpos,teclapres,strtext){ 

    if (((arrpos == "(") || (arrpos == ")") || (arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres == 8)){

        separador = arrpos;

        masktext = strtext + separador;

        Campo.value = masktext;

    }

}

//--- Fim 4 --------------------------------------------------------//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//



/*#####################################################################################

#######################################################################################

############################### FUNÇÕES DE VALIDAÇÃO ##################################

#######################################################################################

#####################################################################################*/



function FormataValor(campo,tammax,teclapres) {



	var tecla = teclapres.keyCode;



    caracteres = '01234567890';



	if ((caracteres.search(String.fromCharCode(tecla))!=-1) || tecla == 8 || tecla == 9 ||(tecla >= 96 && tecla <= 105)){

        var 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 ; }

    

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

    		

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

    		if ( tam <= 2 ){ 

    	 		campo.value = vr ; }

    	 	if ( (tam > 2) && (tam <= 5) ){

    	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }

    	 	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{

     	

       //event.returnValue = false;   

           

     }   

}



var reDigits = /^[+]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;



function verifica_valor(pStr){



	if(pStr != 'form'){



		if (reDigits.test(pStr)) {

			document.getElementById("valor_invalido").style.visibility	= 'hidden'		;

			document.getElementById("valor_invalido").style.position	= 'absolute'	;

			document.getElementById("valor_invalido").style.top			= '0'			;

			document.getElementById("verificaa").style.color = "green";

		} else {

			document.getElementById("verificaa").style.color = "red";

		}



	}else{



		pStr = document.getElementById("input_consumo").value;



		if (pStr != "" && reDigits.test(pStr)){

			document.form_valor.submit();

		}else{

			document.getElementById('input_consumo').focus();

			document.getElementById("valor_invalido").style.visibility		= 'visible'		;

			document.getElementById("valor_invalido").style.position		= 'static'		;

			document.getElementById("valor_invalido").innerHTML = "<p style='font-weight:bold;color:red'>Por favor digite um valor válido para o consumo</p>";

			return false;

		}



	}



}



function escondediv(tipo_pessoa){

	

		if	(tipo_pessoa == "pj") {

			document.getElementById('id_cpf').style.display = 'none';

			document.getElementById('input_cpf').value = '';

			document.getElementById('id_cnpj').style.display = '';

			document.getElementById('id_rg').style.display = 'none';

			document.getElementById('input_rg').value = '';

			document.getElementById('id_ie').style.display = '';

			document.getElementById('id_nome').style.display = 'none';

		}

		

		if	(tipo_pessoa == "pf") {

			document.getElementById('id_cnpj').style.display = 'none';

			document.getElementById('input_cnpj').value = '';

			document.getElementById('id_cpf').style.display = '';

			document.getElementById('id_ie').style.display = 'none';

			document.getElementById('input_ie').value = '';

			document.getElementById('id_rg').style.display = '';

			document.getElementById('id_nome').style.display = '';

		}

		

}



function escondediv_cdbackup(cd) {

	if (cd == "cdbackup") {

			document.getElementById('id_cdbackup').style.display = '';

	}else{

			document.getElementById('id_cdbackup').style.display = 'none';

			document.getElementById('cliente_data_cdbackup').value = '';

	}

}



function escondediv_contrato(contrat){

	if (contrat == "contrato") {

			document.getElementById('id_contrato').style.display = '';

	}else{

			document.getElementById('id_contrato').style.display = 'none';

			document.getElementById('input_cliente_contrato').value = '';

	}

}

	

function escondediv_boleto(bolet){

	if (bolet == "boleto") {

			document.getElementById('id_boleto').style.display = '';

	}else{

			document.getElementById('id_boleto').style.display = 'none';

			document.getElementById('input_numero_boleto_eletronico').value = '';

	}

}


