/*****************************************************************
	FUNÇÕES EM JAVASCRIPT PARA FORMATAÇÃO
 	DEVELOPER: Jaime Eugênio						      	11/01/2007
*****************************************************************/
function fJS_TipoNavegador()
{
	 var userAgent = navigator.userAgent.toLowerCase();
	 var sNameNav  = "";
	 //--> Identificação
	 var is_op     = false; //is opera
	 var is_saf    = false; //is safari
   var is_ie     = false; //is internet explorer
	 var is_nn     = false;//is netscape
	 var is_moz    = false; //is mozilla
	 //--> Validação!
	 if(userAgent.indexOf('opera') != -1)
	 {
			is_op    = true;
			sNameNav = "OPERA";
	 }
   if(userAgent.indexOf('safari') != -1)
	 {
			is_saf   = true;
			sNameNav = "SAFIRA";
	 }
   if(userAgent.indexOf('msie') != -1 && (!is_op) && (!is_saf))
	 {
			is_ie    = true;
			sNameNav = "IE";
	 }
   if(userAgent.indexOf('netscape') != -1)
	 {
			is_nn    = true;
			sNameNav = "NETSCAPE";
	 }
	 if((navigator.product == 'Gecko') && (!is_saf))
	 {
			is_moz   = true;
			sNameNav = "FIREFOX";
	 }
	 if(!sNameNav)
	 {
	 		alert("ATENÇÃO: Navegador não identificado!   ");
			return false;
	 }
	 return sNameNav;
	 //-->

}  // fJS_TipoNavegador

//............................................................................
function fJS_VersaoNavegador()
{
		var sVersao = navigator.appVersion.substr(0,1);
		//-->
		return sVersao;
		//-->
}  // fJS_VersaoNavegador
//............................................................................
function fJS_Formatar(mascara, documento)
{
		var i      = documento.value.length;
  	var sSaida = mascara.substring(0,1);
  	var sTexto = mascara.substring(i)
    //-->
  	if (sTexto.substring(0,1) != sSaida)
		{
			 documento.value += sTexto.substring(0,1);
  	}
		//-->
}  // fJS_Formatar
//............................................................................
//onKeyPress="return(fJS_FormataMoeda(this,'.',',',event));
function fJS_FormataMoeda(fld, milSep, decSep, e)
{ 
   var sep = 0;
   var key = '';
   var i = j = 0;
   var len = len2 = 0;
   var strCheck = '0123456789';
   var aux = aux2 = '';
   var whichCode = (window.Event) ? e.which : e.keyCode;
   
   if(fJS_TipoNavegador()== "IE")whichCode = e.keyCode; //--> Força IE7 
      
   if (whichCode == 13) return true;  // Enter
   else if(whichCode == 0)return true; //Delete/Tab
   else if(whichCode == 8)return true; //Backspace
   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 fJS_SomenteNumeros(e)
{
   var key = '';
   var strCheck = '0123456789';
   var whichCode = (window.Event) ? e.which : e.keyCode;

   if (whichCode == 13) return true;  // Enter
	 if (whichCode == 0) return true;  // Delete
	 if (whichCode == 8) return true;  // Backspace
	 //-->
   key = String.fromCharCode(whichCode);  // Get key value from key code
   if (strCheck.indexOf(key) == -1) return false;  // Not a valid key

}  // fJS_SoNumeros
//............................................................................
function fJS_FormataFone(obj)
{
	 var sNav   = fJS_TipoNavegador();
	 var iValor = obj.value;
	 var sNome  = obj.name;
	 var oCampo = document.getElementById(sNome);
	 //-->
	 if(sNav == "OPERA")
	 {
			 if(iValor.length == 1)oCampo.value = "("+iValor;
			 else fJS_Formatar("###)####-####", obj);
	 }
	 else
	 {
	 		if(iValor.length == 0)oCampo.value = "("+iValor;
	 		else fJS_Formatar("###)####-####", obj);
	 }
}  // fJS_FormataFone
//............................................................................
function fJS_ValidaHora(sCampo) {
	sValorTxt = sCampo.value;
	if ((sValorTxt.substr(0,2) > 23) || (sValorTxt.substr(3,2) > 59)) {
		alert("ATENÇÃO: Hora inválida! Digite novamente!  ");
		sCampo.focus();
		sCampo.value = "";
	}
}
//............................................................................

