/* FUNCTION LIMPA CAMPO */

function barra(objeto){
	if (objeto.value.length == 2 || objeto.value.length == 5 ){
		objeto.value = objeto.value+"/";
	}
}


function janela(url,nome,atributos){

	window.open(url,nome,atributos);

}



function SublinhaLinha(ObjParam){



//	alert('ObjParam		'	+ ObjParam);

//	var obj = document.getElementById('TR'+ObjParam);

//	obj.className = "bg_amarelo";



}



function limpa(f){

	f.value = '';

}



/* FUNCTION VALIDA BRANCO */

function ValidaBranco(objParam){



	if(objParam.value == ''){

		return false;

	}

	else{

		return true;

	}



}



/* FUNCTION VALIDA NÚMERO */

function ValidaNum(objParam){



	if(!ValidaBranco(objParam)){

		return false;

	}

	else if(isNaN(objParam.value)){

		return false;

	}

	else{

		return true;

	}



}



/* FUNCTION VALIDA INPUT TYPE="RADIO" */

function ValidaRadio(objParam){



	total = objParam.length - 1;

//	alert('O número de radios é ' + total);



	for(x=0; x<=total; x++){



		if(objParam(x).checked){

			return true;

			break;

		}



	 }



	 if(x > total){

		return false;

	 }



}



/* FUNCTION VALIDA INPUT TYPE="CHECKBOX" */

function ValidaCheck(objParam){



	total = objParam.length - 1;

//	alert('O número de checkbox é '	+ total);

	

	var i = 0;

	var cont = 0;



	while (i <= total){

		

		if(objParam[i].checked==true){

			cont=1;

		}



	i++;

	}



	if (cont == 0){

		return false;

	}

	else{

		return true;

	}



}



/* FUNCTION VALIDA CPF */

function ValidaCPF(objParam){



	var i;

	

	var c 	= objParam.substr(0,9);

	var dv 	= objParam.substr(9,2);

	var d1 	= 0;

	

	for (i=0; i < 9; i++){

		d1 += c.charAt(i)*(10-i);

	}



	if (d1 == 0){

		alert("CPF Invalido")

		return false;

	}



	d1 = 11 - (d1 % 11);



	if (d1 > 9) d1 = 0;



	if (dv.charAt(0) != d1){

		alert("CPF Invalido")

		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){

		alert("CPF Invalido")

		return false;

	}



	return true;



}



/* FUNCTION VALIDA E-MAIL */

function ValidaEmail(objParam){



	var invalid, s;



	invalid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;



	var s;



	if(!ValidaBranco(objParam)){



		return false;



	}



	else if (invalid.test(objParam.value) == false){



		return false;



	}



	else{



		return true;



	}







}



/* FUNCTION VALIDA CNPJ */

function validaCNPJ(objParam){

	

	CNPJ = document.frmDDl.cnpj.value;

	erro = new String;



	if (CNPJ == "00.000.000/0000-00") erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";

	

	if (CNPJ.length < 18) erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";

	

		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){

			

			if (erro.length == 0) erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";

		}

		

		//substituir os caracteres que nao sao numeros

		if(document.layers && parseInt(navigator.appVersion) == 4){

			x = CNPJ.substring(0,2);

			x += CNPJ.substring(3,6);

			x += CNPJ.substring(7,10);

			x += CNPJ.substring(11,15);

			x += CNPJ.substring(16,18);

			CNPJ = x;	

		} else {

			CNPJ = CNPJ.replace(".","");

			CNPJ = CNPJ.replace(".","");

			CNPJ = CNPJ.replace("-","");

			CNPJ = CNPJ.replace("/","");

		}

		var nonNumbers = /\D/;

		

		if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";	

		

		var a = [];

		var b = new Number;

		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

		

		for (i=0; i<12; i++){

			a[i] = CNPJ.charAt(i);

			b += a[i] * c[i+1];

		}

		

		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }

		

		b = 0;

		

		for (y=0; y<13; y++) {

			b += (a[y] * c[y]); 

		}

		

		if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }

		

		if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){

			erro +="Digito verificador com problema! \n\n";

		}

		

		if (erro.length > 0){

			//alert(erro);

			return false;

		} else {

			//alert("CNPJ valido!");

		}

		return true;

	}



/* FUNCTION VALIDA CNPJ */

function verificacnpj(valor) {

	if (valor == "") {

		return false;

	}

	else {

		if (!verificaCGC(valor)) {

			alert("Preencha com um número de CNPJ válido");

			document.forms[0].cnpj.value = "";

			document.forms[0].cnpj.focus();

		}

	}

}



/* FUNCTION COMBODINAMICO */

function combodinamico(nm_form_01,drop_pai_01,drop_filho_01,string_js_01,ponteiro) {

 var nm_form  	= nm_form_01;

 var drop_pai  	= drop_pai_01;

 var drop_filho = drop_filho_01;

 var string_js 	= string_js_01;

//    var ponteiro = 0

        f = eval("document."+nm_form);

        setor_selecionado = eval("f."+drop_pai+".options[f."+drop_pai+".selectedIndex].text");

 

        var setores = new Array ();

        var comunidades = new Array ();

        var array_aux = new Array ();

        setores = string_js.split("*");

        flag_achou = 0;

        for( var i=0 ; i<setores.length ; i++ ) {

                comunidades = setores[i].split("#");

                if ( comunidades[0] == setor_selecionado ) {

                        flag_achou = 1;

                        eval("f."+drop_filho+".options.length = comunidades.length");

                        eval("f."+drop_filho+".options[0].text  = 'Selecione'");

                        eval("f."+drop_filho+".options[0].value = ''");

                        for( var j=1 ; j<comunidades.length ; j++ ) {

                                array_aux = comunidades[j].split("|");

                                eval("f."+drop_filho+".options[j].text  = array_aux[0]");

                                eval("f."+drop_filho+".options[j].value = array_aux[1]");

        

                                if(eval(ponteiro == array_aux[1])){

                                  eval("f."+drop_filho+".selectedIndex = j");

                                }

                        }

                }

        }

        if (flag_achou==0){

                eval("f."+drop_filho+".options.length = 1");

                eval("f."+drop_filho+".options[0].text  = 'Selecione'");

                eval("f."+drop_filho+".options[0].value = ''");

                eval("f."+drop_filho+".options[0].selectedIndex = ''");

        }

}

//-->



/* FUNCTION OCULTA LAYER */

function MM_showHideLayers() { //v6.0

  var i,p,v,obj,args=MM_showHideLayers.arguments;

  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];

    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }

    obj.visibility=v; }

}



/* FUNCTION ABRE JANELA */

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}



/* FUNCTION CONFIRMA EXCLUSÃO DE ÍTENS */

function excluir(){



	if (window.confirm('Confirma a exclusão do(s) íten(s) selecionado(s)')){

		return true;

	}

	else{

		return false;

	}



}



function checkaTodos() {



	f = document.form;



	for (i=0; i<f.length; i++)

	{

		if (f.elements[i].type == "checkbox")

		{

			f.elements[i].checked = true;

			//f.elements[i].checked = false;

		}

	}

}



function limpaTodos() {



	f = document.form;



	for (i=0; i<f.length; i++)

	{

		if (f.elements[i].type == "checkbox")

		{

			f.elements[i].checked = false;

			//f.elements[i].checked = false;

		}

	}

}



/* FUNCTION FORMATA MOEDA */

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, 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 = objTextBox.value.length;

    for(i = 0; i < len; i++)

        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;

    aux = '';

    for(; i < len; i++)

        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);

    aux += key;

    len = aux.length;

    if (len == 0) objTextBox.value = '';

    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;

    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;

    if (len > 2) {

        aux2 = '';

        for (j = 0, i = len - 3; i >= 0; i--) {

            if (j == 3) {

                aux2 += SeparadorMilesimo;

                j = 0;

            }

            aux2 += aux.charAt(i);

            j++;

        }

        objTextBox.value = '';

        len2 = aux2.length;

        for (i = len2 - 1; i >= 0; i--)

        objTextBox.value += aux2.charAt(i);

        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);

    }

    return false;

}
function NumberFormat(num, inputDecimal){
	this.VERSION = 'Number Format v1.5.4';
	this.COMMA = ',';
	this.PERIOD = '.';
	this.DASH = '-'; 
	this.LEFT_PAREN = '('; 
	this.RIGHT_PAREN = ')'; 
	this.LEFT_OUTSIDE = 0; 
	this.LEFT_INSIDE = 1;  
	this.RIGHT_INSIDE = 2;  
	this.RIGHT_OUTSIDE = 3;  
	this.LEFT_DASH = 0; 
	this.RIGHT_DASH = 1; 
	this.PARENTHESIS = 2; 
	this.NO_ROUNDING = -1; 
	this.num;
	this.numOriginal;
	this.hasSeparators = false;  
	this.separatorValue;  
	this.inputDecimalValue; 
	this.decimalValue;  
	this.negativeFormat; 
	this.negativeRed; 
	this.hasCurrency;  
	this.currencyPosition;  
	this.currencyValue;  
	this.places;
	this.roundToPlaces; 
	this.truncate; 
	this.setNumber = setNumberNF;
	this.toUnformatted = toUnformattedNF;
	this.setInputDecimal = setInputDecimalNF; 
	this.setSeparators = setSeparatorsNF; 
	this.setCommas = setCommasNF;
	this.setNegativeFormat = setNegativeFormatNF; 
	this.setNegativeRed = setNegativeRedNF; 
	this.setCurrency = setCurrencyNF;
	this.setCurrencyPrefix = setCurrencyPrefixNF;
	this.setCurrencyValue = setCurrencyValueNF; 
	this.setCurrencyPosition = setCurrencyPositionNF; 
	this.setPlaces = setPlacesNF;
	this.toFormatted = toFormattedNF;
	this.toPercentage = toPercentageNF;
	this.getOriginal = getOriginalNF;
	this.moveDecimalRight = moveDecimalRightNF;
	this.moveDecimalLeft = moveDecimalLeftNF;
	this.getRounded = getRoundedNF;
	this.preserveZeros = preserveZerosNF;
	this.justNumber = justNumberNF;
	this.expandExponential = expandExponentialNF;
	this.getZeros = getZerosNF;
	this.moveDecimalAsString = moveDecimalAsStringNF;
	this.moveDecimal = moveDecimalNF;
	this.addSeparators = addSeparatorsNF;
	if(inputDecimal == null){ this.setNumber(num, this.PERIOD); } 
	else{ this.setNumber(num, inputDecimal); }
	this.setCommas(true);
	this.setNegativeFormat(this.LEFT_DASH); 
	this.setNegativeRed(false); 
	this.setCurrency(false); 
	this.setCurrencyPrefix('R$');
	this.setPlaces(3);
}
function setInputDecimalNF(val){ this.inputDecimalValue = val; }
function setNumberNF(num, inputDecimal){
	if (inputDecimal != null){ this.setInputDecimal(inputDecimal); }
	this.numOriginal = num;	
	this.num = this.justNumber(num);
}
function toUnformattedNF(){	return (this.num); }
function getOriginalNF(){ return (this.numOriginal); }
function setNegativeFormatNF(format){ this.negativeFormat = format; }
function setNegativeRedNF(isRed){ this.negativeRed = isRed; }
function setSeparatorsNF(isC, separator, decimal){
	this.hasSeparators = isC;
	if (separator == null) separator = this.COMMA;
	if (decimal == null) decimal = this.PERIOD;
	if (separator == decimal){ this.decimalValue = (decimal == this.PERIOD) ? this.COMMA : this.PERIOD;	} 
	else { this.decimalValue = decimal;	}
	this.separatorValue = separator;
}
function setCommasNF(isC){ this.setSeparators(isC, this.COMMA, this.PERIOD); }
function setCurrencyNF(isC){ this.hasCurrency = isC; }
function setCurrencyValueNF(val){ this.currencyValue = val; }
function setCurrencyPrefixNF(cp){
	this.setCurrencyValue(cp);
	this.setCurrencyPosition(this.LEFT_OUTSIDE);
}
function setCurrencyPositionNF(cp){	this.currencyPosition = cp }
function setPlacesNF(p, tr){
	this.roundToPlaces = !(p == this.NO_ROUNDING); 
	this.truncate = (tr != null && tr); 
	this.places = (p < 0) ? 0 : p; 
}
function addSeparatorsNF(nStr, inD, outD, sep){
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) { nStr = nStr.replace(rgx, '$1' + sep + '$2'); }
	return nStr + nStrEnd;
}
function toFormattedNF(){	
	var pos;
	var nNum = this.num; 
	var nStr;            
	var splitString = new Array(2);   
	if (this.roundToPlaces) {
		nNum = this.getRounded(nNum);
		nStr = this.preserveZeros(Math.abs(nNum)); 
	}
	else{ nStr = this.expandExponential(Math.abs(nNum)); }
	if (this.hasSeparators) { nStr = this.addSeparators(nStr, this.PERIOD, this.decimalValue, this.separatorValue);	}
	else{ nStr = nStr.replace(new RegExp('\\' + this.PERIOD), this.decimalValue); }
	var c0 = '';
	var n0 = '';
	var c1 = '';
	var n1 = '';
	var n2 = '';
	var c2 = '';
	var n3 = '';
	var c3 = '';
	var negSignL = (this.negativeFormat == this.PARENTHESIS) ? this.LEFT_PAREN : this.DASH;
	var negSignR = (this.negativeFormat == this.PARENTHESIS) ? this.RIGHT_PAREN : this.DASH;
	if (this.currencyPosition == this.LEFT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c0 = this.currencyValue;
	} 
	else if (this.currencyPosition == this.LEFT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c1 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c2 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c3 = this.currencyValue;
	}
	nStr = c0 + n0 + c1 + n1 + nStr + n2 + c2 + n3 + c3;
	if (this.negativeRed && nNum < 0) {	nStr = '<font color="red">' + nStr + '</font>';	}
	return (nStr);
}
function toPercentageNF(){
	nNum = this.num * 100;
	nNum = this.getRounded(nNum);
	return nNum + '%';
}
function getZerosNF(places){
	var extraZ = '';
	var i;
	for (i=0; i<places; i++) { extraZ += '0'; }
	return extraZ;
}
function expandExponentialNF(origVal){
	if (isNaN(origVal)) return origVal;
	var newVal = parseFloat(origVal) + ''; 
	var eLoc = newVal.toLowerCase().indexOf('e');
	if (eLoc != -1) {
		var plusLoc = newVal.toLowerCase().indexOf('+');
		var negLoc = newVal.toLowerCase().indexOf('-', eLoc); 
		var justNumber = newVal.substring(0, eLoc);
		if (negLoc != -1) {
			var places = newVal.substring(negLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, true, parseInt(places));
		} else {
			if (plusLoc == -1) plusLoc = eLoc;
			var places = newVal.substring(plusLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, false, parseInt(places));
		}
		newVal = justNumber;
	}
	return newVal;
} 
function moveDecimalRightNF(val, places){
	var newVal = '';
	if (places == null) { newVal = this.moveDecimal(val, false);}
	else{ newVal = this.moveDecimal(val, false, places); }
	return newVal;
}
function moveDecimalLeftNF(val, places){
	var newVal = '';
	if (places == null) { newVal = this.moveDecimal(val, true); }
	else{ newVal = this.moveDecimal(val, true, places);	}
	return newVal;
}
function moveDecimalAsStringNF(val, left, places){
	var spaces = (arguments.length < 4) ? this.places : places;
	if (spaces <= 0) return val; 
		var newVal = val + '';
		var extraZ = this.getZeros(spaces);
		var re1 = new RegExp('([0-9.]+)');
		if (left) {
		newVal = newVal.replace(re1, extraZ + '$1');
		var re2 = new RegExp('(-?)([0-9]*)([0-9]{' + spaces + '})(\\.?)');		
		newVal = newVal.replace(re2, '$1$2.$3');
	}
	else{
		var reArray = re1.exec(newVal); 
		if (reArray != null) { newVal = newVal.substring(0,reArray.index) + reArray[1] + extraZ + newVal.substring(reArray.index + reArray[0].length); }
		var re2 = new RegExp('(-?)([0-9]*)(\\.?)([0-9]{' + spaces + '})');
		newVal = newVal.replace(re2, '$1$2$4.');
	}
	newVal = newVal.replace(/\.$/, ''); 
	return newVal;
}
function moveDecimalNF(val, left, places){
	var newVal = '';
	if (places == null) { newVal = this.moveDecimalAsString(val, left);	}
	else{ newVal = this.moveDecimalAsString(val, left, places);	}
	return parseFloat(newVal);
}
function getRoundedNF(val){
	val = this.moveDecimalRight(val);
	if (this.truncate) { val = val >= 0 ? Math.floor(val) : Math.ceil(val); }
	else{ val = Math.round(val); }
	val = this.moveDecimalLeft(val);
	return val;
}
function preserveZerosNF(val){
	var i;
	val = this.expandExponential(val);
	if (this.places <= 0) return val; 
		var decimalPos = val.indexOf('.');
	if (decimalPos == -1) {
		val += '.';
		for (i=0; i<this.places; i++) {	val += '0';	}
	}
	else{
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = this.places - actualDecimals;
		for (i=0; i<difference; i++) { val += '0'; }
	}
	return val;
}
function justNumberNF(val){
	newVal = val + '';
	var isPercentage = false;
	if (newVal.indexOf('%') != -1) {
		newVal = newVal.replace(/\%/g, '');
		isPercentage = true; 
	}
	var re = new RegExp('[^\\' + this.inputDecimalValue + '\\d\\-\\+\\(\\)eE]', 'g');	
	newVal = newVal.replace(re, '');
	var tempRe = new RegExp('[' + this.inputDecimalValue + ']', 'g');
	var treArray = tempRe.exec(newVal); 
	if (treArray != null) {
		var tempRight = newVal.substring(treArray.index + treArray[0].length); 
		newVal = newVal.substring(0,treArray.index) + this.PERIOD + tempRight.replace(tempRe, ''); 
	}
	if (newVal.charAt(newVal.length - 1) == this.DASH ) {
		newVal = newVal.substring(0, newVal.length - 1);
		newVal = '-' + newVal;
	}
	else if (newVal.charAt(0) == this.LEFT_PAREN && newVal.charAt(newVal.length - 1) == this.RIGHT_PAREN) {
		newVal = newVal.substring(1, newVal.length - 1);
		newVal = '-' + newVal;
	}
	newVal = parseFloat(newVal);
	if (!isFinite(newVal)) { newVal = 0; }
	if (isPercentage) {	newVal = this.moveDecimalLeft(newVal, 3);}
	return newVal;
}