//conta caracteres
var ns6 = document.getElementById && !document.all

function restrictinput(maxlength, e, placeholder) {
    if (window.event && event.srcElement.value.length >= maxlength)
        return false
    else if (e.target && e.target == eval(placeholder) && e.target.value.length >= maxlength) {
        var pressedkey = /[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
        if (pressedkey.test(String.fromCharCode(e.which)))
            e.stopPropagation()
    }
}

function countlimit(maxlength, e, placeholder) {
    var theform = eval(placeholder)
    var lengthleft = maxlength - theform.value.length
    var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder)
    if (window.event || e.target && e.target == eval(placeholder)) {
        if (lengthleft < 0)
            theform.value = theform.value.substring(0, maxlength)
        placeholderobj.innerHTML = lengthleft
    }
}

function displaylimit(thename, theid, thelimit) {
    var theform = theid != "" ? document.getElementById(theid) : thename
    var limit_text = 'max.: <b><span id="' + theform.toString() + '" class="text_blue">' + thelimit + '</span></b> caracteres'
    if (document.all || ns6)
        document.write(limit_text)
    if (document.all) {
        eval(theform).onkeypress = function() { return restrictinput(thelimit, event, theform) }
        eval(theform).onkeyup = function() { countlimit(thelimit, event, theform) }
    }
    else if (ns6) {
        document.body.addEventListener('keypress', function(event) { restrictinput(thelimit, event, theform) }, true);
        document.body.addEventListener('keyup', function(event) { countlimit(thelimit, event, theform) }, true);
    }
}

function fcn_verificaCheckExcluir_checked(pObjCheckBox){
						
	//verificando seexiste algum check de excluir na tela
	if(document.getElementById("chk_excluir")){			
		
		var vobj_checkBoxExcluir = pObjCheckBox
		var vint_TT_checkBox = vobj_checkBoxExcluir.length;
		var vstr_FL_selecionouExcluir = false;
		
		//verificando o total de check box
		if (isNaN(vint_TT_checkBox) == true){
			vint_TT_checkBox = 1;
		}
		
		if (vint_TT_checkBox == 1){
				
			if(vobj_checkBoxExcluir.checked == true){
				vstr_FL_selecionouExcluir = true;
			}
		
		}else{
			
			for (x = 0; x < vint_TT_checkBox; x++){
			
				if(vobj_checkBoxExcluir[x].checked == true){
					vstr_FL_selecionouExcluir = true;
					break;	
				}
			}
		}	
	}else{
		var vstr_FL_selecionouExcluir = false;
	}
	
	return vstr_FL_selecionouExcluir;
}

//Monta lista
function lista(fbox, tbox){
	
    var arrFbox = new Array();
    var arrTbox = new Array();
    var arrLookup = new Array();
    var i;
	
    for (i = 0; i < tbox.options.length; i++) {
	    arrLookup[tbox.options[i].text] = tbox.options[i].value;
	    arrTbox[i] = tbox.options[i].text;
    }
	
    var fLength = 0;
    var tLength = arrTbox.length;
	
    for(i = 0; i < fbox.options.length; i++) {
	    arrLookup[fbox.options[i].text] = fbox.options[i].value;
	    if (fbox.options[i].selected && fbox.options[i].value != "") {
		    arrTbox[tLength] = fbox.options[i].text;
		    tLength++;
	    }
	    else {
		    arrFbox[fLength] = fbox.options[i].text;
		    fLength++;
	    }
    }
	
    arrFbox.sort();
    arrTbox.sort();
    fbox.length = 0;
    tbox.length = 0;
	
    var c;
	
    for(c = 0; c < arrFbox.length; c++) {
	    var no = new Option();
	    no.value = arrLookup[arrFbox[c]];
	    no.text = arrFbox[c];
	    fbox[c] = no;
    }
	
    for(c = 0; c < arrTbox.length; c++) {
	    var no = new Option();
	    no.value = arrLookup[arrTbox[c]];
	    no.text = arrTbox[c];
	    tbox[c] = no;
    }
	
    avancar();
}

function avancar(){
	
    var strValues = "";
    var boxLength = document.frm_inside.lst_usuarioEnviar.length;
    var count = 0;
	
    if (boxLength != 0) {
	    for (i = 0; i < boxLength; i++) {
		    if (count == 0) {
			    strValues = document.frm_inside.lst_usuarioEnviar.options[i].value;
		    }
		    else {
			    strValues = strValues + ", " + document.frm_inside.lst_usuarioEnviar.options[i].value;
		    }
		    count++;			
	    }
    }
	
    //Carrega hidden selecionados com os valores selecionados
    document.frm_inside.selecionados.value = strValues

    //Valida Campos obrigatorios e da submit		
}

function compareOptionValues(a, b){ 

    // Radix 10: for numeric values
    // Radix 36: for alphanumeric values

    var sA = parseInt( a.value, 36 ); 
    var sB = parseInt( b.value, 36 ); 
    return 0
}

// Dual list move function
function moveDualList( srcList, destList, moveAll ){
	
    var selecionados = "";
	
    // Do nothing if nothing is selected
    if(( srcList.selectedIndex == -1 ) && ( moveAll == false )){
	    return;
    }

    newDestList = new Array( destList.options.length );

    var len = 0;

    for( len = 0; len < destList.options.length; len++ ){
	    if ( destList.options[ len ] != null ){
		    newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
	    }
    }

    for(var i = 0; i < srcList.options.length; i++ ){ 
	    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )	{
		    // Statements to perform if option is selected
		    // Incorporate into new list
		    newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
		    len++;
	    }
    }

    // Sort out the new destination list
    newDestList.sort( compareOptionValues );   // BY VALUES
    //newDestList.sort( compareOptionText );   // BY TEXT

    // Populate the destination with the items from the new array
    for ( var j = 0; j < newDestList.length; j++ ) {
	    if ( newDestList[ j ] != null ){
	    destList.options[ j ] = newDestList[ j ];
	    }
    }

    // Erase source list selected elements
    for( var i = srcList.options.length - 1; i >= 0; i-- ){ 
	    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) ){
		    // Erase Source
		    //srcList.options[i].value = "";
		    //srcList.options[i].text  = "";
		    srcList.options[i]       = null;
	    }
    }
	
    for (var i=0;i<destList.options.length;i++){
	    selecionados = selecionados + '' + destList.options[i].value + ', ';
    }
	
    document.frm_inside.selecionados.value = selecionados.substring(0,selecionados.length-2);
}

function fcn_validaAtributoData(pstr_DS_frmObjField){
				
	//tipos de retorno
	// 1 - data válida
	// 0 - data inválida
	// 2 - data em branco	
													
	var vint_DT_dia = eval(pstr_DS_frmObjField + "[0].value")
	var vint_DT_mes = eval(pstr_DS_frmObjField + "[1].value")
	var vint_DT_ano = eval(pstr_DS_frmObjField + "[2].value")
	
	var vint_FL_valido = 1

	//verificando se a data foi preenchida
	if (vint_DT_dia == '00' || vint_DT_mes == '00' || vint_DT_ano == '0000'){
	
		vint_FL_valido = 2
	
	//data preenchida
	}else{	
	
		//verificando se é ano bissexto
		var vint_FL_anoBissesto = vint_DT_ano % 4
							
		//verificando os dias de cada mes
		if (vint_DT_mes == '01'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '02'){
			//verficando se é ano bissexto
			if (vint_FL_anoBissesto){
				if (vint_DT_dia <= 29){
					vint_FL_valido = 1
				}else{
					vint_FL_valido = 0
				}
			}else{
				if (vint_DT_dia <= 28){
					vint_FL_valido = 1
				}else{
					vint_FL_valido = 0
				}
			}			
		}else if (vint_DT_mes == '03'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '04'){
			if (vint_DT_dia <= 30){
				vint_FL_valido = 1
			}else{
				vint_FL_valido = 0
			}
			
		}else if (vint_DT_mes == '05'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '06'){
			if (vint_DT_dia <= 30){
				vint_FL_valido = 1
			}else{
				vint_FL_valido = 0
			}
		}else if (vint_DT_mes == '07'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '08'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '09'){
			if (vint_DT_dia <= 30){
				vint_FL_valido = 1
			}else{
				vint_FL_valido = 0
			}			
		}else if (vint_DT_mes == '10'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '11'){
			if (vint_DT_dia <= 30){
				vint_FL_valido = 1
			}else{
				vint_FL_valido = 0
			}			
		}else if (vint_DT_mes == '12'){
			vint_FL_valido = 1
			
		}else if (vint_DT_mes == '00'){
			vint_FL_valido = 0			
		}			
	}
	
	return vint_FL_valido
}

//função para verificar a consistência da data de expiracao
function fcn_verificaConsistenciaDatas(pstr_nameDTinicio, pstr_nameDTexpiracao){

	var vint_DT_inicio, vint_DT_expiracao
	
	vint_DT_inicio		= eval("document.frm_inside." + pstr_nameDTinicio + "[2].value") + eval("document.frm_inside." + pstr_nameDTinicio + "[1].value") + eval("document.frm_inside." + pstr_nameDTinicio + "[0].value")
	vint_DT_expiracao	= eval("document.frm_inside." + pstr_nameDTexpiracao + "[2].value") + eval("document.frm_inside." + pstr_nameDTexpiracao + "[1].value") + eval("document.frm_inside." + pstr_nameDTexpiracao + "[0].value")
			
	if (parseInt(vint_DT_inicio) <= parseInt(vint_DT_expiracao)){
		return true		
	}else{
		return false
	}	
}

function fcn_formatarData(fsValor, foForm, foNome, onkeyup) {
	var piLoop;
	var psPalavra='';
	var psResposta='';
	var piTamanho = 0;

	if (window.event.keyCode != 8 && window.event.keyCode != 37 && window.event.keyCode != 39 && window.event.keyCode != 46)
	{
		for(piLoop=0; piLoop <= (fsValor.length-1); piLoop++)
		{
			if (fsValor.charAt(piLoop) != '/' && fsValor.charAt(piLoop) >= '0' && fsValor.charAt(piLoop) <= '9') 
				psPalavra = psPalavra + fsValor.charAt(piLoop);
		}
		for(piLoop=0; piLoop <= (psPalavra.length-1); piLoop++)
		{
			if (piLoop == 2) psResposta = psResposta + '/' + psPalavra.charAt(piLoop);
			if (piLoop == 4) psResposta = psResposta + '/' + psPalavra.charAt(piLoop);
			if (piLoop != 2 && piLoop != 4) psResposta = psResposta + psPalavra.charAt(piLoop);
			piTamanho = 10;			
		}
	
		document.getElementById(foNome).value = psResposta.substring(0, piTamanho);
		
	}
}

//formata numero
function fcn_formataNumero(fsValor, foForm, foNome) {
	var piLoop;
	var psPalavra='';
	
	if (window.event.keyCode != 8 && window.event.keyCode != 37 && window.event.keyCode != 39 && window.event.keyCode != 46)
	{
		for(piLoop=0; piLoop <= (fsValor.length-1); piLoop++)
		{
			if (fsValor.charAt(piLoop) != '.' && fsValor.charAt(piLoop) >= '0' && fsValor.charAt(piLoop) <= '9') 
				psPalavra = psPalavra + fsValor.charAt(piLoop);
		}
		document.forms[foForm].elements[foNome].value = psPalavra;
	}
}

function fcn_validaNumero() {
	var vstr_caracter = document.layers ? evt.which
							: document.all ? event.keyCode
							: evt.keyCode;
	
	if (vstr_caracter != 46 && vstr_caracter != 8 && vstr_caracter != 9 && vstr_caracter != 35 && vstr_caracter != 36 && vstr_caracter != 37 && vstr_caracter != 38 && vstr_caracter != 39 && vstr_caracter != 40 && vstr_caracter != 190 && vstr_caracter != 194) {					
		if ((vstr_caracter < 48 || vstr_caracter > 58) && (vstr_caracter < 96 || vstr_caracter > 105)) {return false;}
	}
}