function DesfocaCampo(){
	Id = arguments[0];
	Valor = arguments[1];
	Valor = SubstituiCaracteres('\r\n','',Valor);
	Valor = SubstituiCaracteres('"',"'",Valor);
	Funcao = arguments[2];
	Funcao = Funcao.replace('id',Id);
	Funcao = Funcao.replace('valor',Valor);	
	eval(Funcao);
}
function ContadorTexto(field,maxlimit) {
    cntfield = PegaElemento(field.id + 'Indicador');
    if (field.value.length > maxlimit){
        field.value = field.value.substring(0, maxlimit);        
    }else{
        cntfield.innerHTML = maxlimit - field.value.length;
    }
}
function EditorHTMLExiste(){
	if (typeof(CKEDITOR) != 'undefined'){
		return true;
	}
}
function InsereItemCombo(Coluna,Rotulo,Valor){
	AdicionaElementoComboBox(Coluna,Rotulo,Valor);
	MudaValorCampo(Coluna,Valor);
}
function Validacao(id){
	if (typeof(JanelaPopUp) != 'undefined'){
		if(!JanelaPopUp.closed) { 
			JanelaPopUp.close();
		}
	}
    xajax_FormValidation(id,PegaValoresFormulario(id));
}
function AdicionaElementoComboBox(Id,Rotulo,Valor){
	var objOption = new Option(Rotulo,Valor);
    document.getElementById(Id).options.add(objOption);
}
function PegaCampoHTML(Id){
	if (EditorHTMLExiste()){
		var oEditor = CKEDITOR.instances[Id];
		//CKEDITOR.instances.editor1.focus(); 
		if (oEditor){
			return oEditor;
		}
	}
}
function PegaValoresFormulario(id){	
	dados = new Array();
	for(c=0;c<document.getElementById(id).length;c++){
		Elemento = document.getElementById(id).elements[c];
		Id = Elemento.id;
		Valor = Elemento.value;
		if (Elemento.id){
			switch(Elemento.type.toLowerCase()){
			case 'text':case 'password':case 'hidden':case 'textarea':case 'file':
				CampoHTML = PegaCampoHTML(Id);
				if (CampoHTML){
					Valor = (CampoHTML.getData() ) ; 
				}
				//if (!CampoVazio(Valor)){
					//dados[Id] = Valor;
				//}
				dados[Id] = Valor;
			break;
			case 'select-one':
				dados[Id] = PegaComboValor(Elemento);
			break;
			case 'radio':
				dados[Id] = PegaValorRadio(Id);
			break;
			case 'checkbox':
				dados[Id] = PegaValorCheckBox(Id);
			break;
			}
		}
	}
	return dados;
}
function RadioSelecionado(NomeObjeto){
    for(i=0;i<document.getElementsByName(NomeObjeto).length;i++){
        objeto = document.getElementsByName(NomeObjeto)[i];
        if (objeto.checked){
            return true;
        }
    }
}
function PegaValorCheckBox(NomeObjeto){
    var valor = '';
    for(i=0;i<document.getElementsByName(NomeObjeto).length;i++){
        objeto = document.getElementsByName(NomeObjeto)[i];
        if (objeto.checked){
            valor = valor + objeto.value + ', ';
        }
    }
    if (valor){
    	valor = valor.substring(0,valor.length-2);
    }
    return valor;
}
function PegaValorRadio(NomeObjeto){
    var valor = '';
    for(i=0;i<document.getElementsByName(NomeObjeto).length;i++){
        objeto = document.getElementsByName(NomeObjeto)[i];
        if (objeto.checked){
            valor = objeto.value;
        }
    }
    return valor;
}
function AtivaBotao(botao){
    bt = parent.document.getElementById(botao);
    if (bt){
        bt.disabled=false;
    }
}
function DesativaBotao(botao){
    bt = parent.document.getElementById(botao);
    if (bt){
        bt.disabled=true;
    }
}
function FocaCampo(id){
	FocoCampoHTML = false;
	Objeto = document.getElementById(id);
	if (EditorHTMLExiste()){
		CampoHTML = PegaCampoHTML(id);
		CampoHTML.focus();
		FocoCampoHTML = true;
	}
	if (!FocoCampoHTML){
		Objeto.focus();
	}
}
function MudaValorCampo(Id,Valor){
	document.getElementById(Id).value = Valor;
}
function alertafocacampo(campo,mensagem){
    alert(mensagem);
    FocaCampo(campo);
}
function FocaCampoRadio(Id,Valor){
	for(i=0;i<document.getElementsByName(Id).length;i++){
        objeto = document.getElementsByName(Id)[i];
        if (objeto.value == Valor){
            objeto.checked = true;
        }
    }
}
function campovazio(valor) {
    valconv = trim(valor);
    if (valconv == "" || valconv == null || valconv.length == 0 || valconv == '0') {
        return true;
    } else {
        return false;
    }
}
function formatamonetario(campo,tammax,evt) 
{
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    tipobrowser = navigator.appName.toLowerCase();
    var codigochave = evt.keyCode;
    if ((tipobrowser != 'microsoft internet explorer') && (codigochave != 8)){
        codigochave = evt.charCode;
    }
    if (codigochave == 0){
        codigochave = evt.charCode;
    }
    //48 - 57
    var tecla = codigochave;
    var keycodesnum = new Array(44,46,48,49,50,51,52,53,54,55,56,57);
    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 ){
        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 ) ;
        }
    }
    if (!in_array(keycodesnum,tecla)){
        return false;
    }
}
function trim(str)
{
    while(str.charAt(0) == (" ") )

    {
        str = str.substring(1);
    }
    while(str.charAt(str.length-1) == " " )
    {
        str = str.substring(0,str.length-1);
    }
    return str;
}
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}
function phone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que nï¿½o ï¿½ dï¿½gito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parï¿½nteses em volta dos dois primeiros dï¿½gitos
    v=v.replace(/(\d{3})(\d)/,"$1-$2")    //Coloca hï¿½fen entre o quarto e o quinto dï¿½gitos
    var tamanho = v.substr(v.indexOf('-') + 1,v.length).length
    if (tamanho == 5){
        v=v.replace('-','');
        v=v.replace(/(\d{4})(\d)/,"$1-$2")
    }
    return v
}

function maskcpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que nï¿½o ï¿½ dï¿½gito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dï¿½gitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dï¿½gitos
    //de novo (para o segundo bloco de nï¿½meros)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hï¿½fen entre o terceiro e o quarto dï¿½gitos
    return v
}

function maskdata(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/^(\d{2})(\d{2})(\d)/,"$1/$2/$3")
    return v
}
function maskhora(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/^(\d{2})(\d{2})(\d)/,"$1:$2:$3")
    return v
}
function maskcep(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que nï¿½o ï¿½ dï¿½gito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse ï¿½ tï¿½o fï¿½cil que nï¿½o merece explicaï¿½ï¿½es
    return v
}

function cnpj(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que nï¿½o ï¿½ dï¿½gito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dï¿½gitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dï¿½gitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono dï¿½gitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um hï¿½fen depois do bloco de quatro dï¿½gitos
    return v
}
function executaenterdinamico(evt,funcao,parametros){
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    tipobrowser = navigator.appName.toLowerCase();
    var codigochave = evt.keyCode;
    if ((tipobrowser != 'microsoft internet explorer')){
        codigochave = evt.charCode;
    }
    if (codigochave == 13){
        argumentos= new Array();
        var c = 0;
        for(var i = 2; i < arguments.length; i++ ) {
            valor = arguments[i];
            if (typeof(valor) == 'number'){
                argumentos[c] = valor;
            }else{
                argumentos[c] = "'" + valor + "'";
            }
            c = c + 1;
        }
        eval(funcao + '(' + argumentos.join(',') + ')');
        return false;
    }
}
function camponumerico(evt){
    var codigochave=evt.keyCode? evt.keyCode : evt.charCode;
    var keycodesnum = new Array(8,9,13,44,46,48,49,50,51,52,53,54,55,56,57);
    if (!in_array(keycodesnum,codigochave)){
        return false;
    }else{
        return codigochave;
    }
}
function PegaComboValor(Combo){
    if (Combo){
        return Combo.options[Combo.selectedIndex].value;
    }
}
function PegaComboRotulo(combo){
    if (combo){
        return combo.options[combo.selectedIndex].text;
    }
}