//Auto tabulação do formulario
VerifiqueTAB=true;

function checkTAB(){
	VerifiqueTAB= true;
}

function pulaCampo(campo,f){
	//document.form1.loja.tagName 
	if ((campo.tagName=="INPUT" || campo.tagName=="SELECT") && VerifiqueTAB==true){
		if (campo.maxLength==campo.value.length){
			next = 0;
			while (next<f.length && campo.name!=f[next].name) next++;
			next++
			while (next<f.length && f[next].type!="text" && f[next].type!="password" && f[next].type!="checkbox" && f[next].type!="radio" && f[next].nodeName!="SELECT") next++;

			if (f[next].disabled == false)
				if (next<f.length) f[next].focus();	
	
		}
		VerifiqueTAB= false;
	}
}

//Formatação de numeros
//DE		1200.00 
//Para	R$ 1.200,00
function formatCurrency(number){
  var num = new String (number);
  if (num.indexOf (".") == -1){
		intLen = num.length;
    toEnd = intLen;
    var strLeft = new String (num.substring (0, toEnd));
    var strRight = new String ("00");
  }else {
		pos = eval (num.indexOf ("."));
		var strLeft = new String (num.substring (0, pos));
		intToEnd = num.length;
		intThing = pos + 1;
		var strRight = new String (num.substring (intThing, intToEnd));
		if (strRight.length > 2){
			nextInt = strRight.charAt(2);
			if (nextInt >= 5){
				strRight = new String (strRight.substring (0, 2));
				strRight = new String (eval ((strRight * 1) + 1));
				if((strRight * 1) >= 100){
					strRight = "00";
					strLeft = new String (eval ((strLeft * 1) + 1));
				}
				if (strRight.length <= 1){
					strRight = new String ("0" + strRight);
				}
			}else{
				strRight = new String (strRight.substring (0, 2));
			}
		}else{
			if (strRight.length != 2){
				strRight = strRight + "0";
			}
		}
	}
	if (strLeft.length > 3){
		var curPos = (strLeft.length - 3);
		while (curPos > 0){
			var remainingLeft = new String (strLeft.substring (0, curPos));
			var strLeftLeft = new String (strLeft.substring (0, curPos));
			var strLeftRight = new String (strLeft.substring (curPos, strLeft.length));
			strLeft = new String (strLeftLeft + "." + strLeftRight);
			curPos = (remainingLeft.length - 3);
		}
	}
	strWhole = strLeft + "," + strRight;
	finalValue = 'R$ '+ strWhole;
	return (finalValue);
}

//DE		R$ 1.200,00 ou 1.200,00 
//Para	1200.00
function formatEnglish(number){
	number = number + "";
	number = number.replace(/\./g,"");
	number = number.replace(/,/g,".");
	number = number.replace(/ /g,"");
	number = number.replace(/R\$/gi,"");
	return (number);
}


//Auto formatacao para Numero enquanto digita
function verificaValor(campo,e){


  //problema com o tab
  if (campo.value.length == 0)
  {
      campo.value = "R$ ";
      return false;
  }    
  
  //se nao tem virgula
  if ((e==8 && (campo.value=="R$" || campo.value=="R$ ")) ||
		 (e==9 || e==16) ||
		 (campo.value == "R$" || campo.value == "R$ ") ||
		 (campo.value.substring(1,campo.value.length).indexOf("R")>0)){
    campo.value="R$ ";
    return false;
  }
  
  if(campo.value.substring(0,3)=="R$ ")
		valor = campo.value.substring(3,campo.value.length);
  else if(campo.value.substring(0,3)!="R$ "){
		valor = campo.value;
		campo.value = "R$ "+campo.value;
  }

  valorU = valor.substring(valor.length-1);


  if(valorU!="1" && valorU!="2" && valorU!="3" && valorU!="4" &&
valorU!="5" && valorU!="6"
  && valorU!="7" && valorU!="8" && valorU!="9" && valorU!="0" &&
valorU!="." && valorU!=","){

    campo.value=campo.value.substring(0,campo.value.length-1);
    return false;
   }




  if(valor.indexOf(",")<0){

     if(valor.indexOf(".")>0 && valor.length==4){
        part = valor.split(".");
        camp="";
        for(m=0;m<part.length;m++)
          camp+=part[m];
      campo.value ="R$ "+ camp;
       }
     tmp = "";
     for(i=valor.length;i>=0;i--){
       if(valor.charAt(i)!=".")
      tmp += valor.charAt(i);
     }


     tmp2="";
     fim ="";
     partes = new Array(parseInt(tmp.length/3)+2);
     for (j=0;j<parseInt((tmp.length-1)/3);j++){
       partes[j] = tmp.substring(3*j,3*j+3);
       fim = tmp.substring(3*j+3);

     }



     for(k=0;k < j;k++){
       tmp2 += partes[k]+ ".";
     }

     tmp2+=  (fim);
     tmp3="";
     for(i=tmp2.length;i>=0;i--){
      tmp3 += tmp2.charAt(i);
     }


     if( parseInt(tmp2.indexOf("."))==3){
        campo.value ="R$ "+ tmp3;
       }
  }
  //se tem virgula
  else{
    tmp = campo.value.substring(campo.value.lastIndexOf(",")+1,campo.value.length);
    if(tmp.indexOf(".")>0 || tmp.indexOf(",")>0){
      campo.value = campo.value.substring(0, campo.value.length-1);
    }
    if(tmp.length==3)
      campo.value = campo.value.substring(0, campo.value.length-1);
  }
}


function limpa(campo){
  if(campo.value == "R$" || campo.value=="R$ ")
    campo.value="";
  else if(campo.value.length>3 && campo.value.indexOf(",")<0)
    campo.value+=",00";
  else if(campo.value.indexOf(",")>0 && campo.value.substring(campo.value.indexOf(",")).length==2 )
    campo.value+="0";  
}

function limpaRFC(campo){
	if(campo.value.length>3 && campo.value.indexOf(",")<0)
    		campo.value+=",00";
  	else if(campo.value.indexOf(",")>0 && campo.value.substring(campo.value.indexOf(",")).length==2 )
    		campo.value+="0";  
}

function justNumber(campo){
	
	buffer = campo.value
	lastChar = buffer.substring(buffer.length - 1)
		

	if(lastChar != "1" && lastChar != "2" && lastChar != "3" && lastChar != "4" &&
	   lastChar != "5" && lastChar != "6" && lastChar != "7" && lastChar != "8" &&
	   lastChar != "9" && lastChar != "0" && lastChar != ".")

			campo.value = campo.value.substring(0, campo.value.length - 1)    
}

function ValidaCampoNumerico(strNum)
{
    var i = 0;
    var j = 0;
    var teste = "0123456789";
    
    if (strNum=="") return true;
    
    while (j < strNum.length)
    {
      i=0;
      while (i < 10)
      {
        if (strNum.charAt(j) == teste.charAt(i))
            break;
        else
            i++;
            
        if (i == 10)
            return false;
      }
      j++;
    }                
    return true;
}
 


//Auto formatacao para Numero enquanto digita no formato 9.999.999 (Inteiro)
function verificaValorInt(campo,e){

  //problema com o tab
  if (campo.value.length == 0)
  {
      campo.value = "R$ ";
      return false;
  }    
  
  //se nao tem virgula
  if ((e==8 && (campo.value=="R$" || campo.value=="R$ ")) ||
		 (e==9 || e==16) ||
		 (campo.value == "R$" || campo.value == "R$ ") ||
		 (campo.value.substring(1,campo.value.length).indexOf("R")>0)){
    campo.value="R$ ";
    return false;
  }
  
  if(campo.value.substring(0,3)=="R$ ")
		valor = campo.value.substring(3,campo.value.length);
  else if(campo.value.substring(0,3)!="R$ "){
		valor = campo.value;
		campo.value = "R$ "+campo.value;
  }

  valorU = valor.substring(valor.length-1);

  if(valorU!="1" && valorU!="2" && valorU!="3" && valorU!="4" &&
valorU!="5" && valorU!="6"
  && valorU!="7" && valorU!="8" && valorU!="9" && valorU!="0" &&
valorU!="."){

    campo.value=campo.value.substring(0,campo.value.length-1);
    return false;
   }




  if(valor.indexOf(",")<0){

     if(valor.indexOf(".")>0 && valor.length==4){
        part = valor.split(".");
        camp="";
        for(m=0;m<part.length;m++)
          camp+=part[m];
      campo.value ="R$ "+ camp;
       }
     tmp = "";
     for(i=valor.length;i>=0;i--){
       if(valor.charAt(i)!=".")
      tmp += valor.charAt(i);
     }


     tmp2="";
     fim ="";
     partes = new Array(parseInt(tmp.length/3)+2);
     for (j=0;j<parseInt((tmp.length-1)/3);j++){
       partes[j] = tmp.substring(3*j,3*j+3);
       fim = tmp.substring(3*j+3);

     }



     for(k=0;k < j;k++){
       tmp2 += partes[k]+ ".";
     }

     tmp2+=  (fim);
     tmp3="";
     for(i=tmp2.length;i>=0;i--){
      tmp3 += tmp2.charAt(i);
     }


     if( parseInt(tmp2.indexOf("."))==3){
        campo.value ="R$ "+ tmp3;
       }
  }
  //se tem virgula
  else{
    tmp = campo.value.substring(campo.value.lastIndexOf(",")+1,campo.value.length);
    if(tmp.indexOf(".")>0 || tmp.indexOf(",")>0){
      campo.value = campo.value.substring(0, campo.value.length-1);
    }
    if(tmp.length==3)
      campo.value = campo.value.substring(0, campo.value.length-1);
  }
}


/*Limpa campo para valores inteiros 9.999.999 */
function limpaInt(campo){
  if(campo.value == "R$" || campo.value=="R$ ")
    campo.value="";
  else if(campo.value.length>3 && campo.value.indexOf(",")<0)
    campo.value+="";
  else if(campo.value.indexOf(",")>0 && campo.value.substring(campo.value.indexOf(",")).length==2 )
    campo.value+="0";  
}


    // Função para retirar os espaços em branco do início e do fim da string.
    function Trim(strTexto)
        {
            // Substitúi os espaços vazios no inicio e no fim da string por vazio.
            return strTexto.replace(/^\s+|\s+$/g, '');
        }

    // Função para validação de CEP.
    function IsCEP(strCEP, blnVazio)
        { 
            // Caso o CEP não esteja nesse formato ele é inválido!
            var objER = /^[0-9]{8}$/;

            strCEP = Trim(strCEP)
            if(strCEP.length > 0)
                {
                    if(objER.test(strCEP))
                        return true;
                    else
                        return false;
                }
            else
                return blnVazio;
        }

function verificaValorRFC(campo,e){


  //problema com o tab
  if (campo.value.length == 0)
  {
      campo.value = "R$ ";
      return false;
  }    
  

  //se nao tem virgula
  if ((e==8 && (campo.value=="R$" || campo.value=="R$ ")) ||
		 (campo.value == "R$" || campo.value == "R$ ") ||
		 (campo.value.substring(1,campo.value.length).indexOf("R")>0)){
    campo.value="R$ ";
    return false;
  }
  
  if(campo.value.substring(0,3)=="R$ ")
		valor = campo.value.substring(3,campo.value.length);
  else if(campo.value.substring(0,3)!="R$ "){
		valor = campo.value;
		campo.value = "R$ "+campo.value;
  }

  valorU = valor.substring(valor.length-1);


  if(valorU!="1" && valorU!="2" && valorU!="3" && valorU!="4" &&
valorU!="5" && valorU!="6"
  && valorU!="7" && valorU!="8" && valorU!="9" && valorU!="0" &&
valorU!="." && valorU!=","){

    campo.value=campo.value.substring(0,campo.value.length-1);
    return false;
   }




  if(valor.indexOf(",")<0){

     if(valor.indexOf(".")>0 && valor.length==4){
        part = valor.split(".");
        camp="";
        for(m=0;m<part.length;m++)
          camp+=part[m];
      campo.value ="R$ "+ camp;
       }
     tmp = "";
     for(i=valor.length;i>=0;i--){
       if(valor.charAt(i)!=".")
      tmp += valor.charAt(i);
     }


     tmp2="";
     fim ="";
     partes = new Array(parseInt(tmp.length/3)+2);
     for (j=0;j<parseInt((tmp.length-1)/3);j++){
       partes[j] = tmp.substring(3*j,3*j+3);
       fim = tmp.substring(3*j+3);

     }



     for(k=0;k < j;k++){
       tmp2 += partes[k]+ ".";
     }

     tmp2+=  (fim);
     tmp3="";
     for(i=tmp2.length;i>=0;i--){
      tmp3 += tmp2.charAt(i);
     }


     if( parseInt(tmp2.indexOf("."))==3){
        campo.value ="R$ "+ tmp3;
       }
  }
  //se tem virgula
  else{
    tmp = campo.value.substring(campo.value.lastIndexOf(",")+1,campo.value.length);
    if(tmp.indexOf(".")>0 || tmp.indexOf(",")>0){
      campo.value = campo.value.substring(0, campo.value.length-1);
    }
    if(tmp.length==3)
      campo.value = campo.value.substring(0, campo.value.length-1);
  }
}
   