/*
    +-----------------------------------------------------------------------------------------------------+
    |                                 MOW - Modelador Online para Web                                     |
    |                                                                                                     |
    |   Date-written:    04/07/2002                                                                       |
    |   Authors:         Adalberto Ferreira Filho, adalberto@mow.com.br                                   |
    |                    Marcio Martini, martini@mow.com.br                                               |
    |                                                                                                     |
    |   Este programa serve para trocar os caracteres especiais por seus respectivos codigos para o HTML. |
    |   Alterações:                                                                                       |
    |                                                                                                     |
    |   04-02-2004 - Inclusão do tipo para que possa tambem destrocar, ou seja, receber código e voltar   |
    |                caracter. Para seu melhor entendimento veja em:                                      |
    |                www.mow.com.br/mow-js/modelos/Troca_caracter.html                                    |
    |                                                                                                     |
    |   28-05-2004 - Inclusão da chamada para caracteres "eml". (Troca_caracter_eml)                      |
    |                                                                                                     |
    |                                                                                                     |
    +-----------------------------------------------------------------------------------------------------+
*/

function Cria_array(n) {
  this.length                          =   n;
  for (var i = 1;  i <= n; i++) {
    this[i]                            =   0;
  }
  return this;
}

//  +-----------------------------------------------------------------------------------------------------+
//  |    Troca_caracter                                                                                   |
//  +-----------------------------------------------------------------------------------------------------+
function Troca_caracter(campo, tipo, naobr) {

  tb_byte                              =   new Cria_array(12);
  tb_troca                             =   new Cria_array(12);

  tb_byte[1]       =   "\"";           tb_troca[1]         =   "&quot;";
  tb_byte[2]       =   "\&";           tb_troca[2]         =   "&amp;";
  tb_byte[3]       =   "\>";           tb_troca[3]         =   "&gt;";
  tb_byte[4]       =   "\<";           tb_troca[4]         =   "&lt;";
  tb_byte[5]       =   "<BR>";         tb_troca[5]         =   "\n<BR>";
  tb_byte[6]       =   "\n";           tb_troca[6]         =   "<br>";
  tb_byte[7]       =   "\n";           tb_troca[7]         =   "<BR>";
  tb_byte[8]       =   "\,";           tb_troca[8]         =   "&#44;";
  tb_byte[9]       =   "*";            tb_troca[9]         =   "&#42;";
  tb_byte[10]      =   "'";            tb_troca[10]        =   "&#39;";
  tb_byte[11]      =   "'";            tb_troca[11]        =   "&#39;";
  tb_byte[12]      =   "\“";          tb_troca[12]        =   "&laquo;";
  tb_byte[13]      =   "\”";          tb_troca[13]        =   "&raquo;";

  if ((naobr       !=  null)    &&     (naobr   ==  "naobr")) { 
    tb_byte[6]     =   "\n";
    tb_troca[6]    =   tb_byte[6];
    tb_troca[7]    =   tb_byte[7];
  }else{
    if ((naobr     !=  null)    &&     (naobr   ==  "<BR>")) {
      tb_byte[6]   =   "\n<BR>";
      tb_troca[6]  =   "<BR>";
      tb_troca[7]  =   "<BR>";
    }else{
      tb_byte[6]   =   "\n";
      tb_troca[6]  =   "<br>";
      tb_troca[7]  =   "<BR>";
    }
  }

  if (campo                            !=  '') {
    campo_limite                       =   campo.length;
    if ((tipo                          ==  null)  ||  (tipo       ==  'codigo')) {
      for (x1=0; x1<campo_limite; x1++) {
        for (x2=1; x2<=tb_byte.length; x2++) {
          byte_aux                     =   campo.substring(x1,x1+tb_byte[x2].length);
          if (byte_aux                 ==  tb_byte[x2]) {
            campo                      =   campo.substring(0,x1-0) + tb_troca[x2]  +  campo.substring(x1+tb_byte[x2].length,campo.length);
            campo_limite               =   campo_limite + tb_troca[x2].length - tb_byte[x2].length;
            x1                         =   x1  +   tb_troca[x2].length - 1;
            x2                         =   tb_byte.length + 1;
          }
        }
      }
    }else{
      if (tipo                         ==  "caracter") {
        for (x1=0; x1<campo_limite; x1++) {
          for (x2=1; x2<=tb_byte.length; x2++) {
            byte_aux                   =   campo.substring(x1,x1+tb_troca[x2].length);
            if (byte_aux               ==  tb_troca[x2]) {
              if (tb_troca[x2]         ==  "<br>") { 
                campo                  =   campo.substring(0,x1) + campo.substring(x1+tb_troca[x2].length,campo.length);
                campo_limite           =   campo.length;
              }else{
                campo                  =   campo.substring(0,x1) + tb_byte[x2] + campo.substring(x1+tb_troca[x2].length,campo.length);
                campo_limite           =   campo_limite - tb_troca[x2].length + tb_byte[x2].length;
              }
              x2                       =   tb_byte.length + 1;
            }
          } 
        }
      }else{
        alert("PARAMETRO INVALIDO = " + tipo);
      }
    }
  }
  return campo;
}



//  +-----------------------------------------------------------------------------------------------------+
//  |    Troca_caracter_eml                                                                               |
//  +-----------------------------------------------------------------------------------------------------+
function Troca_caracter_eml(campo, tipo) {

  tb_byte                              =   new Cria_array(41);
  tb_troca                             =   new Cria_array(41);

//  +-----------------------------------------------------------------------------------------------------+
//  |    MINÚSCULAS                                                                                       |
//  +-----------------------------------------------------------------------------------------------------+ 
  tb_byte[1]       =   "á";           tb_troca[1]         =   "=E1";
  tb_byte[2]       =   "à";           tb_troca[2]         =   "=E0";
  tb_byte[3]       =   "ä";           tb_troca[3]         =   "=E4";
  tb_byte[4]       =   "â";           tb_troca[4]         =   "=E2";
  tb_byte[5]       =   "é";           tb_troca[5]         =   "=E9";
  tb_byte[6]       =   "è";           tb_troca[6]         =   "=E8";
  tb_byte[7]       =   "ê";           tb_troca[7]         =   "=EA";
  tb_byte[8]       =   "ë";           tb_troca[8]         =   "=EB";
  tb_byte[9]       =   "í";           tb_troca[9]         =   "=ED";
  tb_byte[10]      =   "ì";           tb_troca[10]        =   "=EC";
  tb_byte[11]      =   "ï";           tb_troca[11]        =   "=EF";
  tb_byte[12]      =   "ó";           tb_troca[12]        =   "=F3";
  tb_byte[13]      =   "ò";           tb_troca[13]        =   "=F2";
  tb_byte[14]      =   "ö";           tb_troca[14]        =   "=F6";
  tb_byte[15]      =   "ú";           tb_troca[15]        =   "=FA";
  tb_byte[16]      =   "ù";           tb_troca[16]        =   "=F9";
  tb_byte[17]      =   "ü";           tb_troca[17]        =   "=FC";
  tb_byte[18]      =   "ç";           tb_troca[18]        =   "=E7";
  tb_byte[19]      =   "ñ";           tb_troca[19]        =   "=F1";
  tb_byte[20]      =   "ý";           tb_troca[20]        =   "=FD";

//  +-----------------------------------------------------------------------------------------------------+
//  |    MAIÚSCULAS                                                                                       |
//  +-----------------------------------------------------------------------------------------------------+ 
  tb_byte[21]      =   "Á";           tb_troca[21]        =   "=C1";
  tb_byte[22]      =   "À";           tb_troca[22]        =   "=C0";
  tb_byte[23]      =   "Ä";           tb_troca[23]        =   "=C4";
  tb_byte[24]      =   "Ã";           tb_troca[24]        =   "=C3";
  tb_byte[25]      =   "Â";           tb_troca[25]        =   "=C2";
  tb_byte[26]      =   "É";           tb_troca[26]        =   "=C9";
  tb_byte[27]      =   "È";           tb_troca[27]        =   "=C8";
  tb_byte[28]      =   "Ê";           tb_troca[28]        =   "=CA";
  tb_byte[29]      =   "Ë";           tb_troca[29]        =   "=CB";
  tb_byte[30]      =   "Í";           tb_troca[30]        =   "=CD";
  tb_byte[31]      =   "Ì";           tb_troca[31]        =   "=CC";
  tb_byte[32]      =   "Ï";           tb_troca[32]        =   "=EF";
  tb_byte[33]      =   "Ó";           tb_troca[33]        =   "=D3";
  tb_byte[34]      =   "Ò";           tb_troca[34]        =   "=D2";
  tb_byte[35]      =   "Ö";           tb_troca[35]        =   "=D6";
  tb_byte[36]      =   "Ú";           tb_troca[36]        =   "=DA";
  tb_byte[37]      =   "Ü";           tb_troca[37]        =   "=DC";
  tb_byte[38]      =   "Ç";           tb_troca[38]        =   "=C7";
  tb_byte[39]      =   "Ñ";           tb_troca[39]        =   "=D1";
  tb_byte[40]      =   "Ý";           tb_troca[40]        =   "=DD";

//  +-----------------------------------------------------------------------------------------------------+
//  |    CARACTERES ESPECIAIS                                                                             |
//  +-----------------------------------------------------------------------------------------------------+ 
  tb_byte[41]      =   "¿";           tb_troca[41]        =   "=BF";


  if (campo                            !=  '') {
    campo_limite                       =   campo.length;
    if ((tipo                          ==  null)  ||  (tipo       ==  'codigo')) {
      for (x1=0; x1<campo_limite; x1++) {
        byte_aux                       =   campo.substring(x1,x1+1);
        for (x2=1; x2<=tb_byte.length; x2++) {
          if (byte_aux                 ==  tb_byte[x2]) {
            campo                      =   campo.substring(0,x1) + tb_troca[x2]  +  campo.substring(x1+1,campo.length);
            campo_limite               =   campo_limite + tb_troca[x2].length - 1;
            x1                         =   x1  +   tb_troca[x2].length - 1;
            x2                         =   tb_byte.length + 1;
          }
        }
      }
    }else{
      if (tipo                         ==  "caracter") {
        for (x1=0; x1<campo_limite; x1++) {
          for (x2=1; x2<=tb_byte.length; x2++) {
            byte_aux                   =   campo.substring(x1,x1+tb_troca[x2].length);
            if (byte_aux               ==  tb_troca[x2]) {
              if (tb_troca[x2]         ==  "<br>") { 
                campo                  =   campo.substring(0,x1) + campo.substring(x1+tb_troca[x2].length,campo.length);
                campo_limite           =   campo.length;
              }else{
                campo                  =   campo.substring(0,x1) + tb_byte[x2] + campo.substring(x1+tb_troca[x2].length,campo.length);
                campo_limite           =   campo_limite - tb_troca[x2].length + 1;
              }
              x2                       =   tb_byte.length + 1;
            }
          } 
        }
      }else{
        alert("PARAMETRO INVALIDO = " + tipo);
      }
    }
  }
  return campo;
}


