
///////////////////////////////////////////////////////////////
//
// Funcao isSet():
//
//
function isSet(varname) {
	return (typeof varname != 'undefined');
}


///////////////////////////////////////////////////////////////
//
// Funcao trim():
//
//
function trim(inputStringTrim) {
	fixedTrim = "";
	lastCh = " ";
	for (x=0; x < inputStringTrim.length; x++) {
		ch = inputStringTrim.charAt(x);
		if ((ch != " ") || (lastCh != " ")) { fixedTrim += ch; }
		lastCh = ch;
	}
	if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
		fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1); 
	}
	return fixedTrim;
}


///////////////////////////////////////////////////////////////
//
// Funcao isEmpty():
//
//
function isEmpty(str) {
  return str.match(/^\s*$/) != null;
}

///////////////////////////////////////////////////////////////
//
// Funcao isEmptyIter():
//
//
function isEmptyIter(val, index) {
	
	return $F(val).match(/^\s*$/);
}

///////////////////////////////////////////////////////////////
//
// Funcao isAlpha():
//
//
function isAlpha(str) {
  return str.match(/^[a-zA-Z]+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isAlphaNum():
//
//
function isAlphaNum(str) {
  return str.match(/^\w+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isEmail():
//
//
function isEmail(str) {
  // pattern from bugzilla editparams.cgi
  return str.match(/^[\w\.\+\-=]+@[\w\.\-]+\.[\w\-]+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isNum():
//
//
function isNum(str) {
  return str.match(/^\d+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isCurr():
//
// Verifica se é dinheiro no formato 1.234.567,00, 1234567,00 ou 123456
//
function isCurr(str) {
  return str.match(/^(\d{1,3}(\.\d{3})*|\d+)(\,\d{2})?$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isTimeStr():
//
//
function isTimeStr(str) {
	return str.match(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Funcao isDateStr():
//
//
function isDateStr(str) {
  if (str.match(/^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20){1}\d{2}$/) == null) return false;

  // A expressao regular acima casa 29/02 mesmo em anos não-bissextos.
  // Verificando abaixo.
  contents = str.split("/");
  dia = contents[0];
  mes = contents[1];
  ano = contents[2];
  fev29 = ((dia == 29) && (mes == 2));
  bissexto = ((ano % 4) == 0);
  return (!fev29 || bissexto);

}


///////////////////////////////////////////////////////////////
//
// Funcao validaData():
//
//
function validaData(dia, mes, ano) { 

	//dia = (str.value.substring(0,2)); 
    //mes = (str.value.substring(3,5)); 
	//ano = (str.value.substring(6,10)); 

	retorno = true; 

    if ((dia == "00") || (mes == "00") || (ano == "00")) {
		retorno = false;
	}
	
    // verifica o dia valido para cada mes 
    if ((dia < 1)||(dia < 1 || dia > 30) && 
		(mes == 4 || mes == 6 || 
		 mes == 9 || mes == 11 ) || 
		 dia > 31) { 
    	retorno = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 1 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
		retorno = false; 
	} 
    
return retorno;

}


///////////////////////////////////////////////////////////////
//
// Funcao comparaDatas():
//
//
function comparaDatas(diaDataMenor, mesDataMenor, anoDataMenor, diaDataMaior, mesDataMaior, anoDataMaior) { 

	retorno = true; 

	var dataMenor = new Date();
	dataMenor.setFullYear(anoDataMenor,mesDataMenor,diaDataMenor);

	var dataMaior = new Date();
	dataMaior.setFullYear(anoDataMaior,mesDataMaior,diaDataMaior);

	if (dataMenor > dataMaior) {
		retorno = false;
	}

return retorno;

}


///////////////////////////////////////////////////////////////
//
// Funcao zeroFill():
//
//
function zeroFill(size, num) {
	var texto = String(num);
	while (texto.length < size) {
		texto = '0' + texto;
	}
	return texto;
}

///////////////////////////////////////////////////////////////
//
// Funcao currencyFormat():
// ex: OnKeyPress="return(currencyFormat(this, ',', '.', 12, event))"
//
function currencyFormat(fld, milSep, decSep, TamMax, e)
{
   valor_campo = eval(fld);
   valor_campo = valor_campo.value.length;
   max_campo = eval(TamMax);
      /* BUG FIX */
      /* Interactive Way - Bug Fixed by Emanuel de Souza (e.souza@iway.com.br) */
      var navuAgent = navigator.userAgent;
      if(navuAgent.indexOf("MSIE") != -1)
      {
        whichCode = e.keyCode;
      } else {
        whichCode = e.which;
      }
      //var whichCode = (window.Event) ? e.which : e.keyCode;
      /* END OF BUG FIX */

   if (valor_campo < max_campo || whichCode == 13 || whichCode == 127 || whichCode == 8)
   {
      var sep = 0;
      var key = '';
      var i = j = 0;
      var len = len2 = 0;
      var strCheck = '0123456789';
      var aux = aux2 = '';
      if (whichCode == 13)
         return true;  // Enter
      if (whichCode == 8 /*BackSpace*/ || whichCode == 127 /*Del*/)
         return true;
      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;
   }
   else
   {
      return false;
   }
}
