/**
 * Essa função existe para retornar um campo a partir de uma string com
 * o ID do campo. Como podemos ter problemas de compatibilidade com getElementById,
 * separei ele em uma função. Assim, se precisar retornar o campo pelo ID usando
 * outras funções, basta modificar esta aqui e o resto vai funcionar.
 */
function Campo(nomeDoCampo) {
	return document.getElementById(nomeDoCampo);
}

function TipoBrowser(){
//    Usado para identificar o tipo do browser
    if (navigator.userAgent.indexOf("MSIE") != -1) {
            return "IE";
    } else {            
            return "NS";
    }
}		



function CampoAtual() {
	return window.event.srcElement;
}

function TeclaAtual() {
//	if(TipoBrowser()=="IE")
		return window.event.keyCode;
//	else
//		return window.event.charCode;
}

// Funções úteis //

function LimpaCampo(campo) {
	if (campo.value.length > 0)
		campo.value = '';
}

function Check(campoChecked, campoUnchecked) {
	campoChecked.checked = true;
	campoUnchecked.checked = false;
}

function SomenteNumeros() {
	var tecla = TeclaAtual();
	return TeclaNumerica(tecla) || TeclaDeControle(tecla)
}




function SomenteNumerosJaDigitados(objTextControl){

	var strValidNumber="1234567890";
	
	for (nCount=0; nCount < objTextControl.value.length + 1; nCount++) {
	
		strTempChar=objTextControl.value.substring(nCount - 1,nCount);
	
		if (strValidNumber.indexOf(strTempChar,0)==-1) {
			objTextControl.value = objTextControl.value.substring(0,nCount - 1);
		}

	}
	
}


function TabParaProximoCampo(quantidadeDeCaracteres, proximoCampo) {
	if (proximoCampo == null || proximoCampo == 'undefined')  return true;

	if (CampoAtual().value.length >= quantidadeDeCaracteres 
		&& !TeclaDeControle(TeclaAtual())) {

			proximoCampo.focus();
	}
}

function SomenteNumerosETabParaProximoCampo(quantidadeDeCaracteres, proximoCampo) {
	if (SomenteNumeros()) {
		TabParaProximoCampo(quantidadeDeCaracteres, proximoCampo)
		return true;
	}
	return false;
}

function TeclaNumerica(tecla) {
	return (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105);
}

function ZerosAEsquerda(valor, tamanhoTotalComZeros) {
}

/**
 * Teclas de controle são: Tab, Backspace, Del, Seta Esquerda, Seta Direita, Home, End, Ctrl+X, Ctrl+C, Ctrl+V, Ctrl+Ins, Shift+Ins e Shift+Del
 */
function TeclaDeControle(tecla) {
	return (EhTab(tecla)
			|| EhBackspace(tecla) 
			|| EhDelete(tecla)
			|| tecla == 37 
			|| tecla == 39 
			|| tecla == 36 
			|| tecla == 35);
}

function EhBackspace(tecla) {
	return tecla == 8;
}

function EhDelete(tecla) {
	return tecla == 46 ;
}

function EhTab(tecla) {
	return tecla == 9;
}

String.prototype.trim = function(){
	s = this.replace(/^\s*/,'').replace(/\s*$/, '');
	return s;
} 

String.prototype.isEmpty = function() {
	return this.trim().length == 0;
}

function FormataValor(campo,tammax,teclapres) {
	var tecla;
    if(TipoBrowser()=="IE")
        tecla = teclapres.keyCode;
    else
   	    tecla = teclapres.charCode;

	if (EhTab(tecla)) return true;
	if ((tecla != 8) && !TeclaNumerica(tecla)) return false;
	
	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){ 
		vr = vr.substr(0,vr.length - 1);		
	}
	
	
	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 ) ;
		}
	}		
		
}



//Temporario**************************
//************************************

function FormataValorTemp(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;

	if (EhTab(tecla)) return true;
	if ((tecla != 8) && !TeclaNumerica(tecla)) return false;
	
	vr = CampoAtual().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 ){ 
	 		CampoAtual().value = vr ; 
		}
	 	if ( (tam > 2) && (tam <= 5) ){
	 		CampoAtual().value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		CampoAtual().value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		CampoAtual().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) ){
	 		CampoAtual().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) ){
	 		CampoAtual().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 ) ;
		}
	}		
}

//************************************
//************************************


function NewWindow(myPage, myName, Width, Height, Scroll, Resizable) {
	/**
 	 * Função original de sOul
 	 *
 	 */
	var winTop = ((screen.height - Height) / 2);
	var winLeft= ((screen.width - Width) / 2);
	winProps = 'top=' +winTop+ ',left=' +winLeft+ ',height=' +Height+ ',width=' +Width+ ',Scrollbars=' +Scroll+ ',Resizable=' +Resizable+ ';'
	Win = window.open(myPage, myName, winProps);

		if (parseInt(navigator.appVersion) >= 4) {
			Win.window.focus(); //set focus to the window
		}
}

