/*                                                         */
/*                      SysValues                          */
/*                                                         */
/*              Manipulação de valores                     */
/*            (Strings, int, arrays ...)                   */
/*                                                         */
/*          Desenvolvido por Dayan C.Galiazzi              */
/*             <dayan@creditaipu.com.br>                   */
/*                                                         */

/***********************************************************************/
/*                    Contas                                           */
/***********************************************************************/      
function contas(obj){
	var k=obj.value.length;
	var t=false;
	for(var i=0;i<k;i++)
		if(obj.value.charAt(i)=='-')
			t=true;
	if(k>1&&!t){
		var aux=obj.value.substring(0,k-1);
		var ult=obj.value.charAt(k-1);
		obj.value=aux+'-'+ult;
	}
};

/***********************************************************************/
/*            Controla inserção de valores nos campos de data          */
/***********************************************************************/      
function datas(obj,e){
	if(e.keyCode!=8){
		if(obj.value.length==2) obj.value+='/';
		if(obj.value.length==5) obj.value+='/';
	}
};

/***********************************************************************/
/*            Controla inserção de valores nos campos cpf              */
/***********************************************************************/
function cpf(obj,e){	
	if(e.keyCode!=8){
		if(obj.value.length==3) obj.value+='.';
		if(obj.value.length==7) obj.value+='.';
		if(obj.value.length==11) obj.value+='-';
	}
};

/***********************************************************************/
/*            Controla inserção de valores nos campos cnpj             */
/***********************************************************************/      
function cnpj(obj,e){
	if(e.keyCode!=8){
		if(obj.value.length==2) obj.value+='.';
		if(obj.value.length==6) obj.value+='.';
		if(obj.value.length==10) obj.value+='/';
		if(obj.value.length==15) obj.value+='-';
	}
};
      
/***********************************************************************/
/*            Controla inserção de valores nos campos cep              */
/***********************************************************************/      
function cep(obj,e){
	if(e.keyCode!=8) if(obj.value.length==5) obj.value+='-';
};
 
/***********************************************************************/
/*            Formata campo de leitura de código			           */
/***********************************************************************/     
function ctr_codigo(obj,tam){
	var aux=obj.value;
	var temp='';
	if(aux.length!=0 && aux.length!=tam)
		for(i=0;i<(tam-aux.length);i++)
			temp+='0';
	temp+=aux;
	obj.value=temp;         
};

/***********************************************************************/
/*            Formata campo de leitura de código qdo entra em foco     */
/***********************************************************************/
function ctr_codigo_focus(obj){
	var aux=obj.value;
	var temp='';
	var i=0;
	while(aux.charAt(i)=='0') i++;
	for(;i<aux.length;i++) temp+=aux.charAt(i);
	obj.value=temp;
}

/***********************************************************************/
/*            Controla inserção de valores nos campos de codigo        */
/***********************************************************************/
function cnpj_blur(obj){
	var aux='';
	var txt=obj.value;
	if(!(txt.indexOf('.')>=0 && txt.indexOf('-')>=0)){
		for(i=0;i<txt.length;i++){
			if(i==2 || i==5) aux+='.';
			if(i==8)   aux+='/';
			if(i==12)  aux+='-';
			aux+=txt.charAt(i);
		}
		obj.value=aux;
	}
}

/***********************************************************************/
/*            Controla inserção de valores nos campos cpf              */
/***********************************************************************/
function cpf_blur(obj){
	var aux='';
	var txt=obj.value;
	if(!(txt.indexOf('.')>=0 && txt.indexOf('-')>=0)){
		for(i=0;i<txt.length;i++){
			if(i==3 || i==6) aux+='.';
			if(i==9)   aux+='-';
			aux+=txt.charAt(i);
		}
		obj.value=aux;
	}
}

/***********************************************************************/
/*           Seta o cursor em determinada posição		               */
/***********************************************************************/
function setCaretTo(o,p){ 
	p++;
    if(o.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = o.createTextRange(); 
        range.move("character", p); 
        range.select(); 
    } 
	else if(o.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        o.focus(); 
        o.setSelectionRange(p,p); 
    } 
}	 

/***********************************************************************/
/*            Só aceita valor numerico					               */
/***********************************************************************/            
function soValor(o,e){
	var k=(F_IE)?e.keyCode:event.keyCode;
	var p=o.value.indexOf('.');	
	if(k==188 || k==110){		
		if(p==-1) o.value+='.';		
		k=190;
		cancelEvent(e);
	}
	if((k==190||k==194)&&p>=0){
		setCaretTo(o,p);							
		cancelEvent(e);	
	}		
	if(!(k>=48&&k<=57) && !(k>=96&&k<=105) && k!=13 && k!=8 && k!=46 && k!=9 && k!=39 && k!=37 && k!=40
		&& k!=38 && k!=16 && k!=36 && k!=35 && k!=190 && k!=194 && k!=110 && !e.ctrlKey && !e.shiftKey)
		cancelEvent(e);	
}

/***********************************************************************/
/*            Só aceita numeros							               */
/***********************************************************************/            
 function soNum(e){
	var k=e.keyCode;
	if( !(k>=48&&k<=57) && !(k>=96&&k<=105) && k!=13 && k!=8 && k!=46 && k!=9 && k!=39 && k!=37 && k!=40
		&& k!=38 && k!=16 && k!=36 && k!=35 && !e.ctrlKey && !e.shiftKey)
	cancelEvent(e);
}

/***********************************************************************/
/*        Formata campos pra celular							       */
/***********************************************************************/
function celular(obj,e){	
	if(e.keyCode!=8) if(obj.value.length==4) obj.value+='-';
}

/***********************************************************************/
/*        Adiciona option a um script							       */
/***********************************************************************/
function addOption(s,t,v){
	var se=getElm(s);
   	var p=se.length>0?se.length:0;
   	se.options[p]=new Option(t,v);
}

/***********************************************************************/
/*        Repete car num vezes									       */
/***********************************************************************/
function repeat(car,num){
	var str='';	for(var i=0;i<num;i++) str+=car;
	return str;
}

/***********************************************************************/
/*        Tira os zeros do inicio de uma string     			       */
/***********************************************************************/
function tiraZeros(t){
	var i=0;
	while(t.charAt(i)=='0') i++;
	return t.substr(i);
};