function maxlength(elemento, qtdMaxima) {
    if (elemento.value.length > qtdMaxima) {
        elemento.value = elemento.value.substring(0, qtdMaxima);
    }
}

function somenteNumero(elemento) {
    elemento.value = elemento.value.replace(/\D/g, '');
}

function posicionaFoco(idElemento) {
    var elemento = document.getElementById(idElemento);
    if (elemento) {
    	setTimeout(function(){elemento.focus();}, 500);
    }
}

function validarValor(elemento, regExp) {
    if (!regExp.test(elemento.value)) {
        elemento.value = "";
    }
}

function posicionaFocoCampoInvalido(idFormulario, idSubview, temBotao) {
    var formulario = document.getElementById(idFormulario);
    var cssErro = /ui-state-error\b/;
    
    if (formulario) {
        var elemento;
        for (var i = 0; i < formulario.elements.length; i++) {
            elemento = formulario.elements[i];
            
            if (idSubview && elemento.id.indexOf(idSubview) != -1) { 
            	if (focaElemento(elemento, cssErro, idSubview, temBotao)) {
            		break;
            	}
            } else if (!idSubview) {
            	if (focaElemento(elemento, cssErro)) {
            		break;
            	}
            }
        }
    }
}

function focaElementoRadioButtonChecked(name){
	var elemento = window.document.getElementsByName(name);
	for (var i = 0; i < elemento.length; i++){
	  if (elemento[i].checked){
		  var paiElemento = elemento[i].parentNode.parentNode;
		  setTimeout(function(){jQuery(paiElemento).css('outline',0).attr('tabindex',-1).focus();}, 500);
	  }
	}
}

function focaElemento(elemento, cssErro, idSubview, temBotao) {
	if (elemento.type == 'radio' || elemento.type == 'checkbox') {
    	var paiElemento = elemento.parentNode.parentNode;
	    	if (cssErro.test(jQuery(paiElemento).children().get( 1 ).className)) {
	    		setTimeout(function(){jQuery(paiElemento).css('outline',0).attr('tabindex',-1).focus();}, 500);
	    		return true;
	    	}
    } else if (elemento.type == 'select-one') {
    	var paiElemento = elemento.parentNode.parentNode;
    	
    	if (cssErro.test(paiElemento.className)) {
    		setTimeout(function(){jQuery(paiElemento).css('outline',0).attr('tabindex',-1).focus();}, 500);
    		return true;
    	}
    } else if (cssErro.test(elemento.className)) {
    	if (idSubview || temBotao) {
    		if (temBotao != undefined) {
        		var elementos = document.getElementsByTagName('input');
        		
        		for (var i = 0; i < elementos.length; i++) {
        			if (elementos[i].id.indexOf(idSubview) != -1) {
        				if (elementos[i].id.indexOf('sourceValidation') != -1) {
        					elemento = elementos[i + 1];
        					break;
        				}
        			}
        		}
        	}
    		
    		if (isCalendarInput(elemento.id)) {
    			elemento = document.getElementById(formatId(elemento.id));
        	}
    	}
    	
    	setTimeout(function(){jQuery(elemento).focus();}, 500);
        return true;
    }
	
	return false;
}

function isCalendarInput(idElement) {
	if (idElement.indexOf('CalendarInput') != -1) {
		return true;
	}
	
	return false;
}

function formatId(idElement) {
	var newId = idElement.substring(idElement.lastIndexOf(':') + 1, idElement.length);
	
	newId = newId.substring(0, newId.lastIndexOf('CalendarInput'));
	
	return newId + 'DayInput';
}


function isIE() {
    if (jQuery.browser.msie) {
        return true;
    }
    
    return false;
}

function isFirefox() {
	if (jQuery.browser.mozilla) {
		return true;
	}
	
	return false;
}

function getVersionIE() {
	return parseInt(jQuery.browser.version, 10);
}

function equals(elementOne, elementoTwo) {
    return elementOne.value == elementoTwo.value;
}

function getModoDocumentoIE() {
	return document.documentMode;
}

function bloqueiaCtrlV(e) {
	if(window.event)
		ctrl = window.event.ctrlKey;
	else
		ctrl = e.ctrlKey;
	
	var tecla;
	if (window.event) //IE 
	{
 		tecla = window.event.keyCode;
    }
  	else if (e.which) //FF
    {
	    tecla = e.which;
   	} 
	if (ctrl && tecla==86 || tecla == 45){
		return false;
	}
	
	return true; 
}

function exibirMensagemCtrlV(e) {
	if(window.event)
		ctrl = window.event.ctrlKey;
	else
		ctrl = e.ctrlKey;
	
	var tecla;
	if (window.event) //IE 
	{
 		tecla = window.event.keyCode;
    }
  	else if (e.which) //FF
    {
	    tecla = e.which;
   	} 
	if (ctrl && tecla==86 || tecla == 45){
		mensagemCtrlV.show();
	}
}


function desabilitarBotaoMouse(elemento) {
	elemento.oncontextmenu = new Function("return false;");
}

function openElement(id) {
      jQuery("#" + id).toggle();
}

function toggleElement(id) {
    jQuery("#" + id).toggle();
}

function toggleMenu(id, img) {
    jQuery("span").removeClass("menu_marcado");
    jQuery("#" + id).addClass("menu_marcado");
}

jQuery(document).ready(function(){
	jQuery(".diffClass span").prettyTextDiff({cleanup:true});
});

function atualizar(){
	 if( window.localStorage){
	    if( !localStorage.getItem( 'firstLoad' ) ){
	      localStorage[ 'firstLoad' ] = true;
	      window.location.reload();
	    }else{
	      localStorage.removeItem( 'firstLoad' );
	    }
	 }
}
