//**********************************************************************************************
// Arquivo: FORMAT.JS
// Data: Novembro de 2002
// Caiuby N. de Freitas
//
// Rotinas:
//
// function OnlyNumbers(fld, e)
//    Uso: onKeyPress="javascript:return OnlyNumbers(this, event);"
// function OnlyAlpha(fld, e)
//    Uso: onKeyPress="javascript:return OnlyAlpha(this, event);"
// function NumberFormat(fld, e, integers, decimals)
//    Uso: onKeyPress="javascript:return NumberFormat(this,event,10,2);"
// function ValidEmail(campo)
// function MemoCounter(fld, countfld, maxlimit)
//    Uso:  onFocus="textCounter(this,document.forms[0].limite,1000);" onKeyDown="textCounter(this,document.forms[0].limite,1000);" onKeyUp="textCounter(this,document.forms[0].limite,1000);"
// function CurrencyFormat(fld, milSep, decSep, e)
//    Uso:  onKeyPress="return(CurrencyFormat(this,'.',',',event))"
// function formatText(tipo,fld)
//    Uso:  <a href="javascript:formatText('negrito','campo');">
// function function hotKey(trgt)
//    Uso:  onKeyDown="hotKey('texto');"
//**********************************************************************************************

function hotKey(trgt){
   //if (event.ctrlKey != true) return;
   strRange = document.selection.createRange().text;
   switch(event.keyCode){
     case 13:formatText('quebra',trgt);
   }
}

function MemoCounter(fld, countfld, maxlimit){
  if (fld.value.length > maxlimit){
     fld.value = fld.value.substring(0, maxlimit);
  }
  else {
     countfld.value = maxlimit - fld.value.length;
  }
}

function OnlyNumbers(fld, e) {
  var key = '';
  var strCheck = '0123456789';
  var whichCode = (window.Event) ? 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;
  else return true;  // Not a valid key
}

function OnlyAlpha(fld, e) {
  var key = '';
  var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var whichCode = (window.Event) ? 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;
  else return true;  // Not a valid key
}

function CurrencyFormat(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 (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 NumberFormat(fld, e, integers, decimals){
  var key = '';
  var strCheck = '0123456789,';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13) {
     if (fld.value.indexOf(',') > -1) {
        strdecimals = fld.value.substring(fld.value.indexOf(',')+1, fld.value.length);
        if (strdecimals.length == 0) fld.value = fld.value.substring(0,fld.value.length-1);
     }
     return true;  // Enter
  }
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if ((strCheck.indexOf(key) == -1) || ((fld.value.indexOf(',') > -1) && (whichCode == 46))) return false;
  else {
     if (fld.value.indexOf(',') > -1) {
        strdecimals = fld.value.substring(fld.value.indexOf(',')+1, fld.value.length) + 1;
        if (strdecimals.length > decimals) return false;
     }
     else if (((fld.value.length + 1) > integers) && (whichCode != 46)) return false;
     return true;
  }
}

function ValidEmail(campo) {
   var checkTLD=1;
   var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
   var matchArray=campo.match(emailPat);
   if (matchArray==null) {
      //alert("Email address seems incorrect (check @ and .'s)");
      return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   for (i=0; i<user.length; i++) {
      if (user.charCodeAt(i)>127) {
         //alert("Ths username contains invalid characters.");
         return false;
      }
   }
   for (i=0; i<domain.length; i++) {
      if (domain.charCodeAt(i)>127) {
         //alert("Ths domain name contains invalid characters.");
         return false;
      }
   }
   if (user.match(userPat)==null) {
      //alert("The username doesn't seem to be valid.");
      return false;
   }
   var IPArray=domain.match(ipDomainPat);
   if (IPArray!=null) {
      for (var i=1;i<=4;i++) {
         if (IPArray[i]>255) {
            //alert("Destination IP address is invalid!");
            return false;
         }
      }
      return true;
   }
   var atomPat=new RegExp("^" + atom + "$");
   var domArr=domain.split(".");
   var len=domArr.length;
   for (i=0;i<len;i++) {
      if (domArr[i].search(atomPat)==-1) {
         //alert("The domain name does not seem to be valid.");
         return false;
      }
   }
   if (checkTLD && domArr[domArr.length-1].length!=2 && 
      domArr[domArr.length-1].search(knownDomsPat)==-1) {
      //alert("The address must end in a well-known domain or two letter " + "country.");
      return false;
   }
   if (len<2) {
      //alert("This address is missing a hostname!");
      return false;
   }
   return true;
}

function formatText(tipo,fld){
  campo = eval("document.forms[0]." + fld);
  campo.focus();
  strRange = document.selection.createRange().text;
  switch(tipo){
     case "negrito":
        tag_i = "<b>";
        tag_f = "</b>";
        break;
     case "italico":
        tag_i = "<i>";
        tag_f = "</i>";
        break;
     case "sublinhado":
        tag_i = "<u>";
        tag_f = "</u>";
        break;
     case "quebra":
        tag_i = "<br>";
        tag_f = "<br>";
        break;
     case "link":
        tag_i = "<a href=\"http://www.\" target=\"_blank\">";
        tag_f = "</a>";
        break;
  }
  if (strRange == "") document.selection.createRange().text = strRange + tag_i + tag_f;
  else document.selection.createRange().text = tag_i + strRange + tag_f;
  return;
}