/******************************************************************************
multi-language validation script
	version 1.00
	by 2xt Tecnologia em gestão de negocios
	www.2xt.com.br
	too much talent
Parametros extras
  FrmFormat                       = Formata campos 
  Autotab      maxlength           = Faz a tabulação automatica dos campos assim q cehga no maxlength
  FmtUpperCase                    = O campo so aceita valores em caixa auta
  FmtLowerCase                    = O campo so aceita valores em caixa baixa
  FmtFloatBrasil                  = Aplica o campo com Numerico float                ex.: FmtFloatBrasil=true
  FmtDate                         = Aplica o campo como data                         ex.: FmtDate=true
  FmtFilter                       = Aplica o campo com filtro                        ex.: Filter='0-9'
Functions existentes
  str_trim(aString)               = tira espaço da direita e da esquerta
  str_rtrim(aString)              = tira espaço da direita
  str_trim(aString)               = tira espalo da esquerda
  isDate(sdata)                   = testa se a data e valida                         ex.: isDate('11/19/1979')
  FtmDateBrasil(pdata)            = Formata um valor data                            ex.: 11/09/1679 
  FmtFloatBrasilToBrasilP(numero) = Coloca pontos de 3 em 3 casas                    ex.: 1.000.345,59
  FmtFloatBrasilPToBrasil(numero) = Tira os pontos de 3 em 3 dos campos              ex.:1000345,59
  FmtFloatAmericToBrasil(numero)  = Troca o padrao apericano por brasileiro          ex.: 1.123,45
  FmtFloatBrasilToAmeric(numero)  = Troca o padrao brasileiro pelo apericano         ex.:1123.45
  FrmValidationElement(oElement)  = Aplica os parametros extras no campo especifico  ex.:FrmValidationElement(document.form1.text1);
  FrmValidation_all()             = Aplica os pramentros esxtras em todos os campos  ex.:FrmValidation_all();
*******************************************************************************/

function str_ltrim(aString)
{
  return aString.replace(/^[\s]+/g,"");
}

function str_rtrim(aString)
{
  return aString.replace(/[\s]+$/g,"");
}

function str_trim(aString)
{
  var result = aString.replace(/^[\s]+/g,"");
  return result.replace(/[\s]+$/g,"");
}

function isDate(sdata)
{
  arraydata = sdata.split('/');
  day2 = arraydata[0];
  month2 = arraydata[1];
  year2 = arraydata[2]; 
  if((!day2)||(!month2)||(!year2)){
    return false;
    exit;
  }
  if((day2.length>2)||(month2.length>2)||(year2.length>4)){
    return false;
    exit;
  }
  while(day2.length<2){
    day2='0'+day2;
  }
  while(month2.length<2){
    month2='0'+month2;
  }
  if(year2.length!=4){
    return false;
    exit;
  }
  var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
  var inpDate = day2 + month2 + year2;
  var filter=/^[0-9]{2}[0-9]{2}[0-9]{4}$/;
  if(! filter.test(inpDate)){
    return false;
    exit;  
  }
  filter=/01|02|03|04|05|06|07|08|09|10|11|12/;
  if(! filter.test(month2)){
    return false;
    exit;
  }
  var N = Number(year2);
  if( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) ){
    DayArray[1]=29;
  }
  for(var ctr=0; ctr<=11; ctr++){
    if (MonthArray[ctr]==month2){ 
      if (day2<= DayArray[ctr] && day2 >0 ){
        inpDate = day2 + '/' + month2 + '/' + year2;
        return true;
        exit;
      }
      else{
        return false;
        exit;
      }
    }
  }
}

function FtmDateBrasil(pdata)
{
  var today = new Date();
  var anoatual = today.getFullYear() + '0';
  var tempn = pdata.split('/');
  if(!tempn[0]){
    tempn[0] =  '';
    return '';
    exit;
  }
  else if(tempn[0].match('[^0-9]')){
    tempn[0] =  '';
    return '';
    exit;
  }
  else{
    while(tempn[0].length<2)
      tempn[0] = '0' + tempn[0];
    tempn[0] = tempn[0].substr(0,2);
  }
  if(!tempn[1]){
    tempn[1] =  '';
    return '';
    exit;
  }
  else if(tempn[1].match('[^0-9]')){
    tempn[1] =  '';
    return '';
    exit;
  }
  else{
    while(tempn[1].length<2)
      tempn[1] = '0' + tempn[1];
    tempn[1] = tempn[1].substr(0,2);
  }
  if(!tempn[2]){
    tempn[2] = anoatual.substr(0,4);
  }
  else if(tempn[2].match('[^0-9]')){
    tempn[2] = anoatual.substr(0,4);
  }
  else{
    var num = tempn[2].length;
    num = 4 - num;
    tempn[2] = anoatual.substr(0,num) + tempn[2];
    tempn[2] = tempn[2].substr(0,4);
  }    
  return tempn[0] + '/' + tempn[1] + '/' + tempn[2];
}

function FmtFloatBrasilToBrasilP(numero)
{
  numero = ' ' + numero;
  numero = numero.replace(/^[\s]+/g,"");
  numero = numero.replace(/[\s]+$/g,"");
  while(numero.match('[.]')){
    numero = numero.replace('.','');
  }  
  var tempn = numero.split(',');
  if(!tempn[1]){
    tempn[1] =  '00';
  }
  else if(tempn[1].match('[^0-9]')){
    tempn[1] =  '00';
  }
  else{
    tempn[1] += '00';
    tempn[1] = tempn[1].substr(0,2);
  }
  if(!tempn[0]){
    tempn[0] = '0';
  }
  else if(tempn[0]==''){
    tempn[0] = '0';
  }
  else if(tempn[0].match('[^0-9]')){
    tempn[0] = '0';  
  }
  var j = 1;
  var parte1 = '';
  for(var i=tempn[0].length-1;i>=0;i--){
    parte1 = tempn[0].substr(i,1) + parte1;
    if((j==3)&&(i!=0)){
      parte1 = '.' + parte1;
      j = 0;
    }
    j++;
  } 
  return parte1 + ',' + tempn[1];
}

function FmtFloatBrasilPToBrasil(numero)
{
  numero = ' ' + numero;
  numero = numero.replace(/^[\s]+/g,"");
  numero = numero.replace(/[\s]+$/g,"");
  while(numero.match('[.]')){
    numero = numero.replace('.','');
  }  
  var tempn = numero.split(',');
  if(!tempn[1]){
    tempn[1] =  '00';
  }
  else if(tempn[1].match('[^0-9]')){
    tempn[1] =  '00';
  }
  else{
    tempn[1] += '00';
    tempn[1] = tempn[1].substr(0,2);
  }
  if(!tempn[0]){
    tempn[0] = '0';
  }
  else if(tempn[0]==''){
    tempn[0] = '0';
  }
  else if(tempn[0].match('[^0-9]')){
    tempn[0] = '0';  
  }
  return tempn[0] + ',' + tempn[1];
}

function FmtFloatAmericToBrasil(numero)
{
  numero = ' ' + numero;
  numero = numero.replace(/^[\s]+/g,"");
  numero = numero.replace(/[\s]+$/g,"");
  var tempn = numero.split('.');
  if(!tempn[1])
  {
    tempn[1] =  '00';
  }
  else
  {
    tempn[1] = tempn[1] + '00';
    tempn[1] = tempn[1].substr(0,2);
  }
  if(!tempn[0])
  {
    tempn[0] = '0';
  }
  if(tempn[0]=='')
  {
    tempn[0] = '0';
  }
  var j = 1;
  var parte1 = '';
  for(var i=tempn[0].length-1;i>=0;i--)
  {
    parte1 = tempn[0].substr(i,1) + parte1;
    if((j==3)&&(i!=0))
    {
      parte1 = '.' + parte1;
      j = 0;
    }
    j++;
  } 
  return parte1 + ',' + tempn[1];
}

function FmtFloatBrasilToAmeric(numero)
{
  numero = ' ' + numero;
  numero = numero.replace(/^[\s]+/g,"");
  numero = numero.replace(/[\s]+$/g,"");
  while(numero.match('[.]'))
  {
    numero = numero.replace('.','');
  } 
  numero = numero.replace(',','.');
  var tempn = numero.split('.');
  if(!tempn[1])
  {
    tempn[1] =  '00';
  }
  else
  {
    tempn[1] += '00';
    tempn[1] = tempn[1].substr(0,2);
  }
  if(!tempn[0])
  {
    tempn[0] = '0';
  }
  if(tempn[0]=='')
  {
    tempn[0] = '0';
  }
  var final = tempn[0] + '.' + tempn[1]; 
  return parseFloat(final);
}

function FrmValidationElement(oElement)
{
  if(!oElement.bProcessed){
    //*** BEGIN OnBlur ********************************************************
    if(oElement.onblur){
      oElement.fblur=oElement.onblur;
    } //end id
    oElement.onblur=function(){
      if(this.fblur && this.fblur()==false){
        event.returnValue=false;
      } //end id
      var sFmtUpperCase=this.getAttribute("FmtUpperCase");
      if(sFmtUpperCase){
        this.value = this.value.toUpperCase();
      } // end if
      var sFmtLowerCase=this.getAttribute("FmtLowerCase");
      if(sFmtLowerCase){
        this.value = this.value.toLowerCase();
      } // end if
      var sFmtfilter=this.getAttribute("Fmtfilter");
      if(sFmtfilter){
       var re=new RegExp('[^'+sFmtfilter+']'); 
       var sFrmFormat=oElement.getAttribute("FrmFormat");
       if(sFrmFormat){ 
        for(var i=0;i<sFrmFormat.length;i++){
         if(sFrmFormat.substr(i,1)!='#'){
          if(this.value.substr(i,1)!= sFrmFormat.substr(i,1)){
           this.value = '';
           break;
          }
         } 
         else{
          if(re.test(this.value.substr(i,1))){
           this.value = '';
           break;
          } // end if	   
         }
        }
       }
       else{
        if(re.test(this.value)){
         oElement.value = '';
        } // end if
        event.returnValue=false;
       } // end if
      } // end if
      var sFmtFloatBrasil=this.getAttribute("FmtFloatBrasil");
      if(sFmtFloatBrasil){
        this.value = FmtFloatBrasilToBrasilP(this.value);
      } // end if
      var sFmtDate=this.getAttribute("FmtDate");
      if(sFmtDate){
        this.value = FtmDateBrasil(this.value);
      } // end if	  
    } //*** END OnBlur ********************************************************

    //*** BEGIN OnFocus *******************************************************
    if(oElement.onfocus){
      oElement.ffocus=oElement.onfocus;
    } //end if
    oElement.onfocus=function(){
      if(this.ffocus && this.ffocus()==false){
        event.returnValue=false;
      } //end if
      var sFmtFloatBrasil=this.getAttribute("FmtFloatBrasil");
      if(sFmtFloatBrasil){
        this.value = FmtFloatBrasilPToBrasil(this.value);
        this.select();
      } // end if
    } // end function onfocus
    //*** END OnFocus *********************************************************

    //*** BEGIN OnKeyPress ****************************************************
    
    if(oElement.onkeypress){
      oElement.fKeypress=oElement.onkeypress;
    } // end if
    oElement.onkeypress=function(){
      if(this.fKeypress && this.fKeypress()==false){
        event.returnValue=false;
      } // end if

      var sFmtfilter=this.getAttribute("Fmtfilter");
      if(sFmtfilter){
        var sKey=String.fromCharCode(event.keyCode);
        var re=new RegExp('[^'+sFmtfilter+']');
        if(sKey!="\r" && re.test(sKey)){
          event.returnValue=false;
          return false; 
        } // end if
        event.keyCode=sKey.charCodeAt(0);
      } // end if

      var sFmtUpperCase=this.getAttribute("FmtUpperCase");
      if(sFmtUpperCase){
        var sKey=String.fromCharCode(event.keyCode);
        sKey = sKey.toUpperCase();
        event.keyCode=sKey.charCodeAt(0);
      } // end if

      var sFmtLowerCase=this.getAttribute("FmtLowerCase");
      if(sFmtLowerCase){
        var sKey=String.fromCharCode(event.keyCode);
        sKey = sKey.toLowerCase();
        event.keyCode=sKey.charCodeAt(0);
      } // end if

      var sFrmFormat=this.getAttribute("FrmFormat");
      if(sFrmFormat){
       var stext = this.value;
       var sKey=String.fromCharCode(event.keyCode);
       var strSelection = document.selection.createRange().text;
       if(strSelection!=''){
        this.value = '';
        event.returnValue=false;
        document.selection.clear();
       } 
       else{
        if(sFrmFormat.substr(stext.length,1)=='#'){
         if((sFrmFormat.substr(stext.length+1,1)!='#')&&(sFrmFormat.substr(stext.length+1,1)!='')){
          this.value = this.value + sKey + sFrmFormat.substr(stext.length+1,1);
          event.keyCode=0;
          event.returnValue=false;
         }
         else{
          event.returnValue=true;
         }
        }
        else{
         if(stext.length+2<=sFrmFormat.length+1){
          this.value = this.value + sFrmFormat.substr(stext.length,1) + sKey;
          event.keyCode=0;
         }
         event.returnValue=false;
        }
       }
      } // end if
      var sAutotab=this.getAttribute("Autotab");
      if(sAutotab){
       e = this.form.elements;
       if(this.value.length+1==this.getAttribute("maxlength")){
        for(i=0; i<e.length; i++){
         if(e[i] == this){
          if(event.keyCode!=0){
           var sKey=String.fromCharCode(event.keyCode);
           this.value = this.value + sKey;
          }
          event.returnValue=false;
          if(i+1<e.length){
           e[i+1].focus();
           e[i+1].select();
          }
          else{
           e[0].focus();
           e[0].select();
          }
         }
        }
       }
      } // end if

      var sFmtFloatBrasil=this.getAttribute("FmtFloatBrasil");
      if(sFmtFloatBrasil){
        var sKey=String.fromCharCode(event.keyCode);
        var re=new RegExp('[^0-9,]|(,[0-9]*,)');
        var atestar = this.value + sKey
        if(sKey!="\r" && re.test(atestar)){
          event.returnValue=false;
        } // end if
        event.keyCode=sKey.charCodeAt(0);
      } // end if

      var sFmtDate=this.getAttribute("FmtDate");
      if(sFmtDate){
        this.maxlength = '10';
        var sKey=String.fromCharCode(event.keyCode);
        var re=new RegExp('[^0-9/]|(/[0-9]*/[0-9]*/)');
        var atestar = this.value + sKey
        if(sKey!="\r" && re.test(atestar)){
          event.returnValue=false;
        } // end if
        else{
          if(document.selection.createRange().text==''){
            atestar = this.value;
            if(sKey!='/'){
              if((atestar.length==1)||(atestar.length==4)){
                this.value += sKey + '/';
                event.returnValue=false;
              } // end if
              else if((atestar.length==2)||(atestar.length==5)){
                this.value += '/';
              } // end else if
            } // end if
            else {
              if((atestar.length==0)||(atestar.length==3)){
                event.returnValue=false;
              } // end if
              else if(atestar.length==1){
                this.value = '0' + this.value;
              } // end else if
              else if(atestar.length==4){
                this.value = this.value.substr(0,3)+ '0' + this.value.substr(3,1);
              } // end else if
            } // end else
          } // end if
        } // end else
        event.keyCode=sKey.charCodeAt(0);
      } // end if data
    } //*** END OnKeyPress ******************************************************

    //*** BEGIN OnChange ******************************************************
    //*** END OnChange ********************************************************

    //*** BEGIN onChange ******************************************************
    //*** END onChange ********************************************************

    //*** BEGIN onKeyDown *****************************************************
    //*** END onKeyDown *******************************************************

    //*** BEGIN onKeyUp *******************************************************
    //*** END onKeyUp *********************************************************

    //*** BEGIN onSelect ******************************************************
    //*** END onSelect ********************************************************

  } // end if oElement.bProcessed
} // end function

function FrmValidation_all()
{
  var i,iForms=document.forms.length;
  for(i=0; i<iForms; i++){
    var oForm=document.forms[i];
    if(!oForm.bProcessed){
      var j, iElements=oForm.elements.length;
      for(j=0; j<iElements; j++){
	    var oElement=oForm.elements[j];
	    //alert(oElement.name);//localName
	    FrmValidationElement(oElement);	
      } //end for
    } //end if
  } //end for
} //end function
