//Estilo do Botão do Menu
function fnStyleMnu(obj, num) {
	for (i=1; i<=num; i++) {
		document.getElementById("mnu" + String(i)).className = "";
	}
	
	if (document.getElementById("mnu" + obj)) //Se existir
		document.getElementById("mnu" + obj).className = "active";
}

//Limita texto no textarea
function calculaTexto(campo, numtotal) {
	var valor = document.getElementById(campo.id).value.length
	var contar = (valor)
	if (contar > numtotal)	{ 
		document.getElementById(campo.id).value = document.getElementById(campo.id).value.substring(0, numtotal);
	}
}

//Formata Campo Data
function fmtDate(campo, e) {
	myVal = campo.value;
	
	if (myVal.length > 2 && !myVal.match(/\//)) {
		myVal = '';
	} else {
		if (window.event) {
			keycode = window.event.keyCode;
		} else 
		if (e) {
			keycode = e.which;
		}
		//Soh Numeros
		if (keycode < 48 || keycode > 57) {
			if ((keycode < 96 || keycode > 105) && (keycode != 0)) {
				myVal = myVal.substr(0, (myVal.length - 1));
			}
		}
	
		if (myVal.length == 2 || myVal.length == 5) {
			myVal += '/';
		}
	}
	
	campo.value = myVal;
}

//Formata CEP
function fmtCEP(campo, e) {
	myVal = campo.value;
	
	if (myVal.length == 5) {
		myVal += '-';
	}
	
	campo.value = myVal;
}

//Formata Hora
function fmtHora(evento, Campo){
	var charCode = (navigator.appName == "Netscape") ? evento.which : evento.keyCode;

	if (!(charCode == 13 || charCode == 8 || charCode == 0 || charCode == 58 || (charCode>=48 && charCode<=57)))
		return false;

	if(charCode != 0 && charCode != 58) {
		if(Campo.value.length == 2){
			Campo.value += ":";
		}
	}

	return true;
}
 
//Exibe ou esconde um objeto
function fnShow(o) {
	var obj = document.getElementById(o);
	if (obj.style.display == "")
		obj.style.display = "none";
	else
		obj.style.display = "";
}

//Verifica se eh Email Valido
function isEmail(v) {
	var a=0
	var p=0
	
	if (v.length==0)
		return(true);
	
	for(var i=1;i<v.length;i++) {
		if(!v.charAt(i))
			return false
		else 
			if(v.charAt(i)=='@'){
				a++;
				if(v.charAt(i+1)=='')
					return false
			}
		else
			if(v.charAt(i)=='.'){
				p++;
				if(v.charAt(i+1)==''||v.charAt(i+1)=='@'||v.charAt(i-1)=='@')
					return false
			}
	}
	
	if(a==1&&p)
		return true
}


//Adiciona item de um combo pra outro (remove o item da fonte)
function addMoveItemSelect(d, f, msg, eof) {
	var dest = document.getElementById(d);
	var from = document.getElementById(f);
	
	if (from.value.length > 0) {
		var newoption = new Option(from.options[from.selectedIndex].text, from.options[from.selectedIndex].value, false, false);
     	dest.options[dest.length] = newoption;
		from.options[from.selectedIndex] = null;
	} else {
		alert(msg);
	}
}

//Ordena um combobox
function sortSelect(ob){
	var obj = document.getElementById(ob);
    var o = new Array();
	
    for (var i=0; i<obj.options.length; i++){
        o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
    }
    o = o.sort(
        function(a,b){ 
            if ((a.text+"") < (b.text+"")) { return -1; }
            if ((a.text+"") > (b.text+"")) { return 1; }
            return 0;
        } 
    );

    for (var i=0; i<o.length; i++){
        obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    }
}

//Valida se jah existe valor igual no combo
function fnIgualCombo(o, v) {
	var obj = document.getElementById(o);
	var ret = false;
	
	for (i=0; i<obj.length; i++) {
		if (v == obj.options[i].value) {
			ret = true;
			break;
		}
	}
	
	return ret;
}

//Remove item num combo
function delItemSelect(o, i) {
	var obj = document.getElementById(o);
	
	obj.options[i] = null;
}

//Adiciona item num combo
function addItemSelect(o, val, txt) {
	var obj = document.getElementById(o);
	
	var newoption = new Option(txt, val, false, false);
	obj.options[obj.length] = newoption;
}

//Valida campo
function fnValField(id, texto) {
	var obj = document.getElementById(id); 
	if (trim(obj.value) == "") {
		alert(texto);
		obj.focus();
		return false;		
	} else
		return true
}

function trim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

//Formata valor
function fmtMoney(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 || whichCode == 8) return true;

key = String.fromCharCode(whichCode);// Valor para o código da Chave

if (strCheck.indexOf(key) == -1) return false; // Chave inválida

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 CPF
function  fmtCPF(src, e) {
	var mask = "###.###.###-##";
	var i = src.value.length;
		
	var keycode = (navigator.appName == "Netscape") ? e.which : e.keyCode;

	if (!(keycode==8 || keycode==0)) {
		var saida = mask.substring(0,1);
		var texto = mask.substring(i)
		if (texto.substring(0,1) != saida) {
			src.value += texto.substring(0,1);
		}
	}
}


//Valida CPF
function validacpf(id_obj) {
	var i;
	 
	s = document.getElementById(id_obj).value;
	s = s.replace(".", "");
	s = s.replace(".", "");
	s = s.replace("-", "");
	
	var c = s.substr(0,9);
	 
	var dv = s.substr(9,2);
	 
	var d1 = 0;
	 
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(10-i);
	}
	 
	if (d1 == 0)
		return false;
	 
	d1 = 11 - (d1 % 11);
	 
	if (d1 > 9) d1 = 0;
	 
	if (dv.charAt(0) != d1)
		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)
		return false;
	 
	return true;
} 


//Permitir digitar somente números
function tratarNumero(e,pCaracteresAdicionais) { 
		var key = '';
		var len = len2 = 0;
		var strCheck = '0123456789' + pCaracteresAdicionais;
		var codigo = (window.Event) ? e.which : e.keyCode;
		if (codigo == 13){
			return true;  // Tecla Enter
		}
		if (codigo == 8){
			return true; // Tecla Backspace
		}
		key = String.fromCharCode(codigo);  // Pega o valor de "key"
		if (strCheck.indexOf(key) == -1){
			return false; // Nao é uma tecla valida	
		}
		else {  
			return true; // É uma tecla valida	
		}
	}
