// Validação
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
function validaCNPJ(CNPJ) {
	// Verifica tamanho
	if (CNPJ.length != 18 && CNPJ.length != "") return false;
	// Substituir os caracteres que não são números
	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 ("/","");
	}

	if (IsNumeric(CNPJ) == false) return false;

	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])) {
		return false;
	}
	if (erro.length > 0) {
		return false;
	} else {
		return true;		
	}
	return true;
}

// Auto-incrementação e Formatação
// Conta Caracteres
function contatxt(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        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 = opt_maxSize - field.value.length;
        }
}
// Só aceita números e tecla ENTER
function sonumeros(e) {
	if (document.all)		// Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers)	// Nestcape
		var tecla = e.which;
	else 
		var tecla2 = e.which;

	if (tecla > 47 && tecla < 58 || tecla == 13) // numeros de 0 a 9 e tecla ENTER
		return true;
	else {
		if (tecla != 8)		// backspace
			event.keyCode = 0;
		else
			return true;
	}
}
// Formata String
function JFormata(valor,mascara) {
	var i = valor.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(i)
	if (texto.substring(0,1) != saida) {
		valor.value += texto.substring(0,1);
	}
}
// Desativa Copiar/Colar
function semcola(e) {
	if (document.all)		// Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers)	// Nestcape
		var tecla = e.which;
	else 
		var tecla2 = e.which;

	if (tecla == 17 && tecla == 78)
		return false;
	else {
		return true;
	}
}
//
var ttip

function VTooltip(id) {
	ttip = document.getElementById(id);
	ttip.style.display = "block"
}

document.onmousemove = ATooltip;

function ATooltip(e) {
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (ttip != null) {
		ttip.style.left = (x - 320) + "px";
		ttip.style.top 	= (y + 20) + "px";
	}
}

function ETooltip() {
	ttip.style.display = "none";
}

// Moeda
function Moeda(fld, milSep, decSep, 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;  // Enter
  if (whichCode == 8) return true;  // Delete
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
  len = fld.value.length;
  for(i = 0; i < len; i++)
  if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  aux = '';
  for(; i < len; i++)
  if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
  aux += key;
  len = aux.length;
  if (len == 0) fld.value = '';
  if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  if (len == 2) fld.value = '0'+ decSep + aux;
  if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
      if (j == 3) {
        aux2 += milSep;
        j = 0;
      }
      aux2 += aux.charAt(i);
      j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
  }
  return false;
}
//Formata número tipo moeda usando o evento onKeyDown
function MoedaClean(valor, validos) {
	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;
}
function FormMoeda(campo,tammax,teclapres,decimal) {
	if (document.all)		// Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers)	// Nestcape
		var tecla = e.which;
	else 
		var tecla2 = e.which;

	// var tecla = teclapres.keyCode;
	vr = MoedaClean(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 ); }
	}
}

// Pega os objetos selecionados
function SelecionarCategoria(formulario,origem,destino) {
	Acrescenta_Selecionados(eval("document.forms['"+formulario+"']."+origem),eval("document.forms['"+formulario+"']."+destino),true,true);
}
// Adiciona cada objeto de origem a caixa de destino
function Acrescenta_Selecionados(objeto_read,objeto_write,Bol_Selecao,Bol_Escolha) {
	for (i=0;i<objeto_read.length;i++) {
	    	if (objeto_read.options[i].selected==Bol_Escolha) {
			Bol_Achou = 0;
			for (j=0;j<objeto_write.length;j++) {
				if (objeto_read.options[i].value==objeto_write.options[j].value)
					Bol_Achou =1;
			}
			if (Bol_Achou == 0) {        
				Insere_Um(objeto_write,objeto_read.options[i].text,objeto_read.options[i].value,Bol_Selecao);
	            	}
	        }            
	}
}
// Adiciona o elemento
function Insere_Um(objeto,Txt_Texto,Int_Id,Bol_Selecao) {
	Insere_Elemento(objeto,Txt_Texto,Int_Id);
	Seta_Selecao(objeto,Bol_Selecao);
}
function Insere_Elemento(objeto,Txt_Texto,Int_Id) {
	var newElem = parent.document.createElement("OPTION");
	/* var newElem = document.createElement("OPTION"); */
	newElem.text = Txt_Texto;
	newElem.value = Int_Id;
	objeto.options.add(newElem);
	/*var newElem = new Option(Txt_Texto,Int_Id);
	objeto.options[objeto.length]=newElem;*/
}
function Seta_Selecao(objeto,Bol_Selecao) {
	objeto.options[objeto.length-1].selected = Bol_Selecao;
}
// Seleciona os itens a serem removidos da caixa
function RemoverCategoria(formulario,origem,destino) {
	Remove_Selecionados(eval("document.forms['"+formulario+"']."+destino),formulario,destino,true);
}
function Remove_Selecionados(objeto,formulario,destino,Bol_Selecao) {
	Int_Total = objeto.length;
	for (j=0;j<Int_Total;j++) {
		for (i=0;i<objeto.length;i++) {
    			if (objeto.options[i].selected==Bol_Selecao) {
				Remove_Um(objeto,i);
        		}            
    		}
	}
	for (i=0;i<eval("document.forms['"+formulario+"']."+destino+".length");i++) {
		eval("document.forms['"+formulario+"']."+destino+".options["+i+"].selected=true");
	}
}
// Remove o objeto
function Remove_Um(objeto,index) {
	objeto.options[index] = null
}

