
// Trocar o estilo dos inputs ao receber foco
function trocarCssInput(){

	elem = document.getElementsByTagName("input");
	elemTextArea = document.getElementsByTagName("textarea");
	
	
	// Objetos do tipo textArea
	for (var i=0;i<elemTextArea.length;i++) {
		
		elemTextArea[i].onfocus = function () {
			this.className = 'textFocus';
		}
	
		elemTextArea[i].onblur = function () {
			this.className = 'text';
		}
			
	}
	
	
	// Outros objetos do tipo input
	for (var i=0;i<elem.length;i++) {
	
		// Aplica css para os campos disabled e readOnly
		/*if(elem[i].disabled || elem[i].readOnly){
			elem[i].className = 'desabilitado';
		}*/
		
		if ((elem[i].type == 'text' || elem[i].type == 'password') && elem[i].readOnly == false){
			
			elem[i].onfocus = function () {
				this.className = 'textFocus';
			}

			elem[i].onblur = function () {
				this.className = 'text';
			}

		}
			
	}
	
}

//checkar todos os checks da pagina
function selecionarTodos(ckall){
		 
	var form = document.forms['frmPrincipal'].name + ':';
	var grid = 'dataList:';
	var chekId = ':chk';
	
	var actVar = ckall.checked;
	
	var i = 0;
	while(document.getElementById(form + grid + i + chekId) != null){
	
		var osChk  = document.getElementById(form + grid + i + chekId);
		
		if(!osChk.disabled){
			osChk.checked = actVar;
		}
	
		i += 1;
	
	}
	
}
//método String.lpad(int pSize, char pCharPad)

function LPad(s,len,c){
  c=c || '0';
  while(s.length< len) s= c+s;
  return s;
}


  
function replaceAll(str, de, para){
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}

String.prototype.replaceAll = function(de, para){
    var str = this;
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}

var dateDif = {
// Fonte: http://www.bigbold.com/snippets/posts/show/2501
	dateDiff: function(strDate1,strDate2){
		return (((Date.parse(strDate2))-(Date.parse(strDate1)))/(24*60*60*1000)).toFixed(0);
	}
}
	

function diasEntreDatas(datInicio,datFim) {
	// Declarações de variáveis
	var mes;
	// Lista dos meses em inglês
	mes = [];
	mes[0] = "January";
	mes[1] = "February";
	mes[2] = "March";
	mes[3] = "April";
	mes[4] = "May";
	mes[5] = "June";
	mes[6] = "July";
	mes[7] = "August";
	mes[8] = "September";
	mes[9] = "October";
	mes[10] = "November";
	mes[11] = "December";

	// Pega a data de hoje no seguinte formato: November 22 2006
	dataInicio = mes[datInicio.substring(3,5)-1] + ' ' + datInicio.substring(0,2) + ' ' + datInicio.substring(6,10);
	dataFim = mes[datFim.substring(3,5)-1] + ' ' + datFim.substring(0,2) + ' ' + datFim.substring(6,10);

	return dateDif.dateDiff(dataInicio, dataFim);
}

var flgOnFocus = false;

function currencyFormatN(fld, milSep, decSep, e) {

	if (flgOnFocus == true){
		fld.value = '';
		flgOnFocus = false;
	}

	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event==true) ? e.which : e.keyCode;
	
	if (whichCode == 13) return true;  // Enter
		key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
		len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}

	return false;

}

function valOnFocus(){
	flgOnFocus = true;
}
	
//busca um valor de um select a partir de um valor
function buscaCodigo(valor, objSel) {
	
    objSel = document.getElementById(objSel);
	
	for (i=0; i < objSel.length; i++){
    	
      	tam = (valor.length) ;
      	
      	if (objSel.options[i].text.substring(0, tam).toUpperCase() == valor.toUpperCase()) {
	        objSel.selectedIndex = i;
	        break;
        }
      	
    }
    
}

function iif(cond,verd,falso){
	 
	var valor;
	
	if(cond){
		valor = verd;
	}else{
		valor = falso;
	}

	return valor;
	
}

// Permite a digitacao somente de numeros, sem hifen ou pontuacao
function checkDigit(event) {
	if (window.event.keyCode == 13)
		return (0);
	else if ((window.event.keyCode < 48) || (window.event.keyCode > 57 ))
		window.event.keyCode = 0;
}

function mascaraHora(keypress, v){

	v.value=v.value.replace(/\D/g,"") //Remove tudo o que não é dígito
	v.value=v.value.replace(/^[^012]/,"") //valida o primeiro dígito #
	v.value=v.value.replace(/^([2])([^0-3])/,"$1") //valida o segundo dígito ##
	v.value=v.value.replace(/^([\d]{2})([^0-5])/,"$1")//valida o terceiro dígito ###
	v.value=v.value.replace(/(\d{2})(\d)/,"$1:$2") //Coloca dois ponto entre o segundo e o terceiro dígitos ##:##
	v.value=v.value.substr(0,5) //Remove digitos extras (aceita no max 5 caracteres(contando o ':' no meio) )

}

function formatar_mascara(src, mascara) {
	var campo = src.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(campo);
	if(texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
	}
}

function focusOn(obj){

	obj = document.getElementById(obj);
	if(obj != null){
		window.setTimeout(function(){obj.focus(); }, 10);
	}
	
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// Imita função left
function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

//Imita função right
function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}




