function fJS_InsTextBox(selecao, obj) {
	 data = "";
	 for (i=1; i <= selecao; i++)
      data = data + "<input type=file class=oText name="+obj+i+"><br>";

	 if (document.layers) {
      document.layers.cust.document.write(data);
      document.layers.cust.document.close();
   }
   else {
      if (document.all) {
         cust.innerHTML = data;
      }
   }
}

<!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site:  http://members.xoom.com/cyanide_7 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Adaptado para dar foco para o próximo campo com <enter> -->



function getElementIndex(obj) {
	var theform = obj.form;
	for (var i=0; i<theform.elements.length; i++) {
		if (obj.name == theform.elements[i].name) {
			return i;
			}
		}
	return -1;
	}

function fTabNext(obj) {
	if (navigator.platform.toUpperCase().indexOf("SUNOS") != -1) {
		obj.blur(); return; // Sun's onFocus() is messed up
		}
	var theform = obj.form;
	var i = getElementIndex(obj);
	var j=i+1;
	if (j >= theform.elements.length) { j=0; }
	if (i == -1) { return; }
	while (j != i) {
		if ((theform.elements[j].type!="hidden") &&
				(theform.elements[j].type!="button") &&
				(theform.elements[i].type!="textarea") &&
			  (theform.elements[j].name != theform.elements[i].name) &&
				(!theform.elements[j].disabled) ||
				(theform.elements[j].name == "oFocus")) {
			theform.elements[j].focus();
			break;
		}
		j++;
		if (j >= theform.elements.length) { j=0; }
	}
}

function fJS_RemoveEspacos(campo) {
  var tmp = "";
  var campo_length = campo.value.length;
  var campo_length_menos_1 = campo.value.length - 1;
  for (index = 0; index < campo_length; index++) {
    if (campo.value.charAt(index) != ' ') {
      tmp += campo.value.charAt(index);
    }
    else {
      if (tmp.length > 0) {
        if (campo.value.charAt(index+1) != ' ' && index != campo_length_menos_1) {
          tmp += campo.value.charAt(index);
        }
      }
    }
  }
  campo.value = tmp;
}

function fJS_TextoMaiusculo() {
   event.keyCode = String.fromCharCode(event.keyCode).toUpperCase().charCodeAt(0);
}

function fJS_TextoMinusculo() {
   event.keyCode = String.fromCharCode(event.keyCode).toLowerCase().charCodeAt(0);
}

function fJS_IgnoraAcentos(campo) {
	campo.value = campo.value.
									 replace(/á/gi,"a").
									 replace(/à/gi,"a").
									 replace(/ä/gi,"a").
									 replace(/ã/gi,"a").
									 replace(/â/gi,"a").
									 replace(/é/gi,"e").
									 replace(/è/gi,"e").
									 replace(/ë/gi,"e").
									 replace(/ê/gi,"e").
									 replace(/í/gi,"i").
									 replace(/ì/gi,"i").
									 replace(/ï/gi,"i").
									 replace(/î/gi,"i").
									 replace(/ó/gi,"o").
									 replace(/ò/gi,"o").
									 replace(/ö/gi,"o").
									 replace(/õ/gi,"o").
									 replace(/ô/gi,"o").
									 replace(/ú/gi,"o").
									 replace(/ù/gi,"u").
									 replace(/ü/gi,"u").
									 replace(/û/gi,"û").toLowerCase();
}


function fJS_FormataFone(sCampo){
	var p;
	p 	= sCampo.value;
	// Impede que o usuário digite letras e símbolos
   if (event.keyCode < 48 || event.keyCode > 57) event.returnValue=false;

	//Formatação de DDD (colocar os () em volta do DDD)
	d1=p.indexOf('(');
	d2=p.indexOf(')');
	if(p.length==2){
		pp=p;
		if(d1==-1){ pp="("+pp;	}
		if(d2==-1){ pp=pp+")";  }
		sCampo.value="";
		sCampo.value=pp;
	}

	//Adiciona o traço(-) no lugar correto(separando o telefone das 4 últimas casas)
	if (p.length>9){
		d3=p.indexOf("-");
		if ( d3!=-1 ) {
			n1=p.substring(0,d3);
			n2=p.substring(d3+1,p.length);
			pp=n1+n2;
			p="";
			p=pp;
		}
		n1=p.substring(0,(p.length - 3));
		n2=p.substring((p.length - 3),p.length);
		pp=n1+"-"+n2;
		sCampo.value="";
		sCampo.value=pp;
	}
 }

function fJS_FormataCEP(sCampo,iTamMax,iPosTraco,tTeclaPres){

// Impede que o usuário digite letras e símbolos
   if (event.keyCode < 48 || event.keyCode > 57) event.returnValue=false;

	var iTecla, sValorTxt, iTam;

 	iTecla = tTeclaPres.keyCode;
 	sValorTxt = sCampo.value;
 	sValorTxt = sValorTxt.toString().replace( "-", "" );
 	iTam = sValorTxt.length ;

 	if (iTam < iTamMax && iTecla != 8) {
  	iTam = sValorTxt.length + 1 ;
 	}

 	if (iTecla == 8 ) {
  	iTam = iTam - 1 ;
 	}

 	if ( iTecla == 8 || iTecla == 88 || iTecla >= 48 && iTecla <= 57 || iTecla >= 96 && iTecla <= 105 ){
  	if ( iTam <= 3 ){
    	sCampo.value = sValorTxt ;
  	}
  	if (iTam > iPosTraco && iTam <= iTamMax) {
    	sValorTxt = sValorTxt.substr(0, iTam - iPosTraco) + '-' + sValorTxt.substr(iTam - iPosTraco, iTam);
  	}
  	sCampo.value = sValorTxt;
 	}
}
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 (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 fJS_FormataMoeda_2(str, sepM, sepU, nc) {
	var novo	= str.value;
		//
	// Remove os "zeros" e a "vírgula" do início da string
	while ((novo.indexOf('0') == 0) || (novo.indexOf(',') == 0))
		novo = novo.substring(1, novo.length);

	temp = novo;

	// Formata o número em centavos
	if (novo.length < nc) {
		zero = "";
		for (i = 1; i < (nc - novo.length); i++)
			zero += '0';
		novo = '0' + sepU + zero + 	novo;
	}

	// Remove os separadores de milhar
	while ((i = novo.indexOf('.')) != -1)
		novo = novo.substring(0, i) + novo.substring(i + 1, novo.length);

	if (temp.length >= nc) {
		// Remove a vírgula
		i = novo.indexOf(',');
		novo = novo.substring(0, i) + novo.substring(i + 1, novo.length);

		// Coloca os separadores de milhar
		s1 = novo.substring(0, novo.length - nc + 1);
		for (i = s1.length - 3; i > 0; i = i - 3)
			s1 = s1.substring(0, i) + sepM + s1.substring(i, s1.length);

		// Coloca o separador de vírgula
		s2 = novo.substring(novo.length - nc + 1, novo.length);
			//
		str.value = s1 + sepU + s2;
	}
	else str.value = novo;
}

function fJS_OnlyNumbers(e)
{
   var key       = '';
   var strCheck  = '0123456789';
   var whichCode = (window.Event) ? e.which : e.keyCode;

   if (whichCode == 13) return false;  // 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 )
   {
        if(fJS_IsIE())e.keyCode = 0;
        else e.preventDefault();

        return false;  // Not a valid key
   }
   else return true;

}  // fJS_OnlyNumbersDDL


function fJS_FormatCNPJ(oObjt ,e)
{
    var whichCode = (window.Event) ? e.which : e.keyCode;

    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace

    if(fJS_OnlyNumbers(e))
    {
        var iValue = oObjt.value;
        switch(iValue.length)
        {
            case 2 : oObjt.value = iValue + "."; break;
            case 6 : oObjt.value = iValue + "."; break;
            case 10 : oObjt.value = iValue + "/"; break;
            case 15 : oObjt.value = iValue + "-"; break;
        }

    }
}

function fJS_FormataCPF_CGC(pForm,pCampo,pTamMax,pPos1,pPos2,pPosBarra,pPosTraco,pTeclaPres){
 var wTecla, wVr, wTam;
		//
 wTecla = pTeclaPres.keyCode;
 wVr = pForm[pCampo].value;
 wVr = wVr.toString().replace( "-", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( "/", "" );
 wTam = wVr.length ;
	//
 if (wTam < pTamMax && wTecla != 8) {
    wTam = wVr.length + 1 ;
 }
 if (wTecla == 8 ) {
    wTam = wTam - 1 ;
 }
 if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
  if ( wTam <= 2 ){
    pForm[pCampo].value = wVr ;
  }
  if (wTam > pPosTraco && wTam <= pTamMax) {
        wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
				if (pPosBarra) wVr = wVr.substr(0, wTam - pPosBarra) + '/' + wVr.substr(wTam - pPosBarra, wTam);
	}
  if ( wTam == pTamMax){
        wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
  }
  pForm[pCampo].value = wVr;
 }
}

function fJS_ValidaCPF(pForm, pCampo, string) {
	var StrData = string;
	var CPFPat  = /^(\d{3}).(\d{3}).(\d{3})-(\d{2})/;
	var CPFPat2 = /^(\d{11})/;
	var matchCPFArray    = StrData.match(CPFPat);
	var matchCPFArray2   = StrData.match(CPFPat2);
		//
	if (!StrData) return true;
	else {
		if (matchCPFArray == null && matchCPFArray2 == null) {
		    alert('CPF Inválido!\n');
				pForm[pCampo].focus();
				return false;
		}
		else if(matchCPFArray != null) {
			StrData = matchCPFArray[1] + matchCPFArray[2] + matchCPFArray[3] + matchCPFArray[4];
			if (!check_cpf(StrData)) {
		    	alert('CPF Inválido!\n');
					pForm[pCampo].focus();
			    return false;
	   		}
		   	else return true;
		}
		else if(matchCPFArray2 != null) {
			StrData = matchCPFArray2[1];
			if (!check_cpf(StrData)) {
		    	alert('CPF Inválido!\n');
					pForm[pCampo].focus();
			    return false;
	   		}
		   	else return true;
		}
	}
}

function check_cpf (StrCPF) {
	x = 0;
	soma = 0;
	dig1 = 0;
	ig2 = 0;
	texto = "";
	trCPF1="";
	len = StrCPF.length;
	x = len -1;
	for (var i=0; i <= len - 3; i++) {
		y = StrCPF.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	StrCPF1 = StrCPF.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (StrCPF1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	if ((dig1 + "" + dig2) == StrCPF.substring(len,len-2)) return true;
	return false;
}

function fJS_ValidaCNPJ(pForm, pCampo) {
	 if (pForm[pCampo].value.length == 0) return true;
	 if (pForm[pCampo].value.substr(0,1) != 0 && pForm[pCampo].value.length < 19) {
			pForm[pCampo].value = '0' + pForm[pCampo].value;
			pForm[pCampo].value = pForm[pCampo].value.replace('/', '');
			pForm[pCampo].value = pForm[pCampo].value.replace('.', '');
			pForm[pCampo].value = pForm[pCampo].value.replace('-', '');
			var tempCNPJ = '';
			tempCNPJ += pForm[pCampo].value.substr(0,3) + '.';
			tempCNPJ += pForm[pCampo].value.substr(3,3) + '.';
			tempCNPJ += pForm[pCampo].value.substr(6,3) + '/';
			tempCNPJ += pForm[pCampo].value.substr(9,4) + '-';
			tempCNPJ += pForm[pCampo].value.substr(13,2);
			pForm[pCampo].value = tempCNPJ;
	 }
	 if (pForm[pCampo].value.length >= 18) {
	   if (VerifyCNPJ(pForm[pCampo].value) == 1) return true;
	   else {
	      alert("CNPJ inválido!");
	      pForm[pCampo].focus();
	      return false;
	   }
	 }
	 else {
	 	alert('O número do CNPJ deve ser informado incluindo-se os 15 dígitos.');
		pForm[pCampo].focus();
		return false;
	 }
}

function isNumb(c) {
	if((cx=c.indexOf(","))!=-1) c = c.substring(0,cx)+"."+c.substring(cx+1);
	if((parseFloat(c) / c != 1)) {
		if(parseFloat(c) * c == 0) return(1);
		else return(0);
	}
	else return(1);
}

function LIMP(c) {
	while((cx=c.indexOf("-"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf("/"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf(","))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf("."))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf("("))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf(")"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	while((cx=c.indexOf(" "))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
	return(c);
}

function VerifyCNPJ(CNPJ) {
	CNPJ = LIMP(CNPJ);
	if(isNumb(CNPJ) != 1) return(0);
	else {
		if(CNPJ == 0) return(0);
		else {
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1) {
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1) return(1);
				else return(0);
			}
			else return(0);
		}
	}
}

function RealTestaCNPJ(CNPJ,g) 	{
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--) {
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8) ind=2;
		else ind++;
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1) VerCNPJ=0;
	else VerCNPJ=11-VerCNPJ;
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))) return(0);
	else return(1);
}

<!-- ##### FORMATA DATAS ##### -->
<!-- Original:  Richard Gorremans (RichardG@spiritwolfx.com) -->
<!-- Web Site:  http://www.spiritwolfx.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// Check browser version
var isNav4 = false, isNav5 = false, isIE4 = false
var strSeperator = "/";

// If you are using any Java validation on the back side you will want to use the / because
// Java date validations do not recognize the dash as a valid date separator.

var vDateType = 3; // Global value for type of date format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm  (Unable to do date check at this time)
//                3 = dd/mm/yyyy
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 2; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero

if(navigator.appName == "Netscape") {
  if (navigator.appVersion < "5") {
    isNav4 = true;
    isNav5 = false;
  }
  else
    if (navigator.appVersion > "4") {
      isNav4 = false;
      isNav5 = true;
    }
}

else {
  isIE4 = true;
}


function fJS_FormataData(vDateName, vDateValue, e, dateCheck, dateType) {
  vDateType = dateType;

  // vDateName = object name
  // vDateValue = value in the field being checked
  // e = event
  // dateCheck
  // True  = Verify that the vDateValue is a valid date
  // False = Format values being entered into vDateValue only
  // vDateType

  // 1 = mm/dd/yyyy
  // 2 = yyyy/mm/dd
  // 3 = dd/mm/yyyy
  //Enter a tilde sign for the first number and you can check the variable information.

	if (vDateValue == "~") {
    alert("AppVersion = "+navigator.appVersion+" \nNav. 4 Version = "+isNav4+" \nNav. 5 Version = "+isNav5+" \nIE Version = "+isIE4+" \nYear Type = "+vYearType+" \nDate Type = "+vDateType+" \nSeparator = "+strSeperator);
    vDateName.value = "";
    vDateName.focus();
    return true;
  }

  var whichCode = (window.Event) ? e.which : e.keyCode;
  // Check to see if a seperator is already present.
  // bypass the date if a seperator is present and the length greater than 8
  if (vDateValue.length > 8 && isNav4) {
    if ((vDateValue.indexOf("-") >= 1) || (vDateValue.indexOf("/") >= 1))
      return true;
  }

  //Eliminate all the ASCII codes that are not valid
  var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
  if (alphaCheck.indexOf(vDateValue) >= 1) {
    if (isNav4) {
      vDateName.value = "";
      vDateName.focus();
      vDateName.select();
      return false;
    }
    else {
      vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
      return false;
   }
  }

  if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
    return false;
  else {
    //Create numeric string values for 0123456789/
    //The codes provided include both keyboard and keypad values

    var strCheck = '13,47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
    if (strCheck.indexOf(whichCode) != -1) {
      if (isNav4) {
        if (((vDateValue.length < 6 && dateCheck) || (vDateValue.length == 7 && dateCheck)) && (vDateValue.length >=1)) {
          alert("Data Iválida!");
          vDateName.value = "";
          vDateName.focus();
          vDateName.select();
          return false;
        }
        if (vDateValue.length == 6 && dateCheck) {
          var mDay = vDateName.value.substr(2,2);
          var mMonth = vDateName.value.substr(0,2);
          var mYear = vDateName.value.substr(4,4)

          //Turn a two digit year into a 4 digit year
          if (mYear.length == 2 && vYearType == 4) {
            var mToday = new Date();

            //If the year is greater than 30 years from now use 19, otherwise use 20
            var checkYear = mToday.getFullYear() + 30;
            var mCheckYear = '20' + mYear;
            if (mCheckYear >= checkYear)
              mYear = '19' + mYear;
            else
              mYear = '20' + mYear;
          }

          var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
          if (!dateValid(vDateValueCheck)) {
            alert("Data Inválida!");
            vDateName.value = "";
            vDateName.focus();
            vDateName.select();
            return false;
          }
          return true;
        }

        else {
          // Reformat the date for validation and set date type to a 1
          if (vDateValue.length >= 8  && dateCheck) {
            if (vDateType == 1) { // mmddyyyy
              var mDay = vDateName.value.substr(2,2);
              var mMonth = vDateName.value.substr(0,2);
              var mYear = vDateName.value.substr(4,4)
              vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
            }
            if (vDateType == 2) { // yyyymmdd
              var mYear = vDateName.value.substr(0,4)
              var mMonth = vDateName.value.substr(4,2);
              var mDay = vDateName.value.substr(6,2);
              vDateName.value = mYear+strSeperator+mMonth+strSeperator+mDay;
            }
            if (vDateType == 3) { // ddmmyyyy
              var mMonth = vDateName.value.substr(2,2);
              var mDay = vDateName.value.substr(0,2);
              var mYear = vDateName.value.substr(4,4)
              vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
            }

            //Create a temporary variable for storing the DateType and change
            //the DateType to a 1 for validation.
            var vDateTypeTemp = vDateType;
            vDateType = 1;
            var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
            if (!dateValid(vDateValueCheck)) {
              alert("Data Inválida!");
              vDateType = vDateTypeTemp;
              vDateName.value = "";
              vDateName.focus();
              vDateName.select();
              return false;
            }

            vDateType = vDateTypeTemp;
            return true;
          }
          else {
            if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
              alert("Data Inválida!");
              vDateName.value = "";
              vDateName.focus();
              vDateName.select();
              return false;
            }
          }
        }
      }
    else {
      // Non isNav Check
      if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
        alert("Data Inválida!");
        vDateName.value = "";
        vDateName.focus();
        return true;
      }

      // Reformat date to format that can be validated. mm/dd/yyyy
      if (vDateValue.length >= 8 && dateCheck) {
      // Additional date formats can be entered here and parsed out to
      // a valid date format that the validation routine will recognize.
        if (vDateType == 1) { // mm/dd/yyyy
          var mMonth = vDateName.value.substr(0,2);
          var mDay = vDateName.value.substr(3,2);
          var mYear = vDateName.value.substr(6,4)
        }
        if (vDateType == 2) { // yyyy/mm/dd
          var mYear = vDateName.value.substr(0,4)
          var mMonth = vDateName.value.substr(5,2);
          var mDay = vDateName.value.substr(8,2);
        }
        if (vDateType == 3) { // dd/mm/yyyy
          var mDay = vDateName.value.substr(0,2);
          var mMonth = vDateName.value.substr(3,2);
          var mYear = vDateName.value.substr(6,4)
        }
        if (vYearLength == 4) {
          if (mYear.length < 4) {
            alert("Data Inválida!");
            vDateName.value = "";
            vDateName.focus();
            return true;
          }
        }

        // Create temp. variable for storing the current vDateType
        var vDateTypeTemp = vDateType;

        // Change vDateType to a 1 for standard date format for validation
        // Type will be changed back when validation is completed.
        vDateType = 1;

        // Store reformatted date to new variable for validation.
        var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;

        if (mYear.length == 2 && vYearType == 4 && dateCheck) {
          //Turn a two digit year into a 4 digit year
          var mToday = new Date();

          //If the year is greater than 30 years from now use 19, otherwise use 20
          var checkYear = mToday.getFullYear() + 30;
          var mCheckYear = '20' + mYear;
          if (mCheckYear >= checkYear)
            mYear = '19' + mYear;
          else
            mYear = '20' + mYear;
          vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
          // Store the new value back to the field.  This function will
          // not work with date type of 2 since the year is entered first.
          if (vDateTypeTemp == 1) // mm/dd/yyyy
            vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
          if (vDateTypeTemp == 3) // dd/mm/yyyy
            vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
        }

        if (!dateValid(vDateValueCheck)) {
          alert("Data Inválida!");
          vDateType = vDateTypeTemp;
          vDateName.value = "";
          vDateName.focus();
          return true;
        }

        vDateType = vDateTypeTemp;
        return true;
      }
      else {
        if (vDateType == 1) {
          if (vDateValue.length == 2) {
            vDateName.value = vDateValue+strSeperator;
          }
          if (vDateValue.length == 5) {
            vDateName.value = vDateValue+strSeperator;
           }
        }
        if (vDateType == 2) {
          if (vDateValue.length == 4) {
            vDateName.value = vDateValue+strSeperator;
          }
          if (vDateValue.length == 7) {
            vDateName.value = vDateValue+strSeperator;
          }
        }
        if (vDateType == 3) {
          if (vDateValue.length == 2) {
            vDateName.value = vDateValue+strSeperator;
          }
          if (vDateValue.length == 5) {
            vDateName.value = vDateValue+strSeperator;
          }
        }
        return true;
      }
    }

    if (vDateValue.length == 10&& dateCheck) {
      if (!dateValid(vDateName)) {
        // Un-comment the next line of code for debugging the dateValid() function error messages
        //alert(err);
        alert("Data Inválida!");
        vDateName.focus();
        vDateName.select();
      }
    }
    return false;
    }
    else {
      // If the value is not in the string return the string minus the last
      // key entered.
      if (isNav4) {
        vDateName.value = "";
        vDateName.focus();
        vDateName.select();
        return false;
      }
      else {
        vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
        return false;
      }
    }
  }
}


function dateValid(objName) {
  var strDate;
  var strDateArray;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var booFound = false;
  var datefield = objName;
  var strSeparatorArray = new Array("-"," ","/",".");
  var intElementNr;

  // var err = 0;
  var strMonthArray = new Array(12);

  strMonthArray[0] = "Jan";
  strMonthArray[1] = "Feb";
  strMonthArray[2] = "Mar";
  strMonthArray[3] = "Apr";
  strMonthArray[4] = "May";
  strMonthArray[5] = "Jun";
  strMonthArray[6] = "Jul";
  strMonthArray[7] = "Aug";
  strMonthArray[8] = "Sep";
  strMonthArray[9] = "Oct";
  strMonthArray[10] = "Nov";
  strMonthArray[11] = "Dec";

  //strDate = datefield.value;
  strDate = objName;
  if (strDate.length < 1) {
    return true;
  }

  for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
    if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      if (strDateArray.length != 3) {
        err = 1;
        return false;
      }
      else {
        strDay = strDateArray[0];
        strMonth = strDateArray[1];
        strYear = strDateArray[2];
      }
      booFound = true;
    }
  }

  if (booFound == false) {
    if (strDate.length>5) {
      strDay = strDate.substr(0, 2);
      strMonth = strDate.substr(2, 2);
      strYear = strDate.substr(4);
   }
  }

  //Adjustment for short years entered
  if (strYear.length == 2) {
    strYear = '20' + strYear;
  }

  strTemp = strDay;
  strDay = strMonth;
  strMonth = strTemp;
  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    err = 2;
    return false;
  }

  intMonth = parseInt(strMonth, 10);

  if (isNaN(intMonth)) {
    for (i = 0;i<12;i++) {
      if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
        intMonth = i+1;
        strMonth = strMonthArray[i];
        i = 12;
      }
    }
    if (isNaN(intMonth)) {
      err = 3;
      return false;
    }
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    err = 4;
    return false;
  }

  if (intMonth>12 || intMonth<1) {
    err = 5;
    return false;
  }

  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
    err = 6;
    return false;
  }

  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
    err = 7;
    return false;
  }

  if (intMonth == 2) {
    if (intday < 1) {
      err = 8;
      return false;
    }
    if (LeapYear(intYear) == true) {
      if (intday > 29) {
        err = 9;
        return false;
      }
    }
  else {
    if (intday > 28) {
    err = 10;
    return false;
  }
}
}
return true;
}

function LeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) { return true; }
  }
  else {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
}
//  ### FORMATA DATAS -- End -->

function fJS_ChecaCampo(ini,formulario) {
	var campovazio = true;
	numcampos = formulario.length - 1;
	for (i = ini; i < numcampos; i++) {
		var tempobj = formulario.elements[i];
		if (tempobj.value != '') {
			campovazio = false;
			break;
        }
 	}
	if (campovazio == true)
		alert('É necessário preencher os campos para consulta!');
	else
		formulario.submit();
}

function fJS_ValidaHora(sCampo) {
	sValorTxt = sCampo.value;
	if ((sValorTxt.substr(0,2) > 23) || (sValorTxt.substr(3,2) > 59)) {
		alert("Hora inválida! Digite novamente!");
		sCampo.focus();
		sCampo.value = "";
	}
}

function fJS_FormataHora(sCampo,iTamMax,iPosTraco,tTeclaPres){

// Impede que o usuário digite letras e símbolos
	if (event.keyCode < 48 || event.keyCode > 57) event.returnValue=false;

	var iTecla, sValorTxt, iTam;

 	iTecla = tTeclaPres.keyCode;
 	sValorTxt = sCampo.value;
 	sValorTxt = sValorTxt.toString().replace( ":", "" );
 	iTam = sValorTxt.length ;

 	if (iTam < iTamMax && iTecla != 8) {
  	iTam = sValorTxt.length + 1 ;
 	}

 	if (iTecla == 8 ) {
  	iTam = iTam - 1 ;
 	}

 	if ( iTecla == 8 || iTecla == 88 || iTecla >= 48 && iTecla <= 57 || iTecla >= 96 && iTecla <= 105 ){
  	if ( iTam <= 2 ){
    	sCampo.value = sValorTxt ;
  	}
  	if (iTam > iPosTraco && iTam <= iTamMax) {
    	sValorTxt = sValorTxt.substr(0, iTam - iPosTraco) + ':' + sValorTxt.substr(iTam - iPosTraco, iTam);
  	}
  	sCampo.value = sValorTxt;

		if(sCampo.value.length==sCampo.maxLength){
    	for(var i=0;i<sCampo.form.length;i++){
      	if(sCampo.form[i]==sCampo){sCampo.form[i+1].focus();break}
      }
    }
	}
}

function fJS_CheckaTodos(formulario) {
  for (i=0; i<formulario.length; i++) {
      if (formulario.elements[i].type=="checkbox")
        if (formulario.oSA.checked)
            formulario.elements[i].checked = true
        else
            formulario.elements[i].checked = false
  }
}

function fJS_IdentificaNavegador() {
	var navegador;
		//
	if (document.layers)
		navegador = "nc";
	else {
		if (document.all)
			navegador = "ie";
		else {
			if (document.getElementById)
				navegador = "n6";
			else
				navegador = "não identificado!";
		}
	}
		//
	return navegador;
}

function fJS_Foco(sCampo) {
	var j = 0;
	var i = 0;
	var sForm = document.forms[0];

	if ((sForm != "undefined") && (sForm.elements.length > 0)) {
		if (sCampo != "") {
			sCampo.focus();
		}
		else {
			while (j != 1) {
				if (i == sForm.elements.length) break;
				if ((sForm.elements[i].type != "hidden") && (!sForm.elements[i].disabled) && (sForm.elements[i].name != "codigo")) {
					sForm.elements[i].focus();
					j = 1;
				}
				i++;
			}
		}
	}
}

function fJS_Erro() {
	alert("Erro ao executar um comando javascript\nContate os desenvolvedores: web.tecnico@planae.com.br");
  return true;
}

function fJS_ConverteValorMoeda(num, iTipo) {
	if (iTipo==1) {
		if(isNaN(num))
			num = "0";

		sign 	= (num == (num = Math.abs(num)));
		num 	= Math.floor(num*100+0.50000000001);
		cents = num%100;
		num 	= Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;

		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));

		return (((sign)?'':'-') + num + ',' + cents);
	}
	else {
		for (i=0; i<=num.length; i++) { //Retira todos os pontos da string para o cálculo correto
			num = num.replace('.', '');
		}
		num = num.replace(',','.');
		num = parseFloat(num);
		num ? num : num = 0;

		return num;
	}
}

function fJS_LimiteTextarea(oCampo, iMax, iCont) {
	if (oCampo.value.length > iMax) {
		alert('Esse campo não pode conter mais que '+iMax+' caracteres!');
		oCampo.value = oCampo.value.substr(0, iMax);
		oCampo.focus();
	}
	if (iCont == 1) { // Se possui contador
		oForm 			= oCampo.form;
		oCampoCont	= eval('oForm.cont_'+oCampo.name+'');
			//
		oCampoCont.value = iMax - oCampo.value.length;
	}
}

function fJS_Modulo(iNumero) {
	if (iNumero >= 0)
		return iNumero;
	else
		return -iNumero;
}

function fJS_RetornaMunicipios(oForm) {
	oForm.action = '';
	oForm.target = '';
	oForm.icogemunic.value = 1;
	oForm.submit();
}

function fJS_BloqueiaGravacao() {
	Gravou = true;
	HideLayer('gravar');
	ShowLayer('gravar_d');
}

function fJS_LiberaGravacao() {
	Gravou = false;
	HideLayer('gravar_d');
	ShowLayer('gravar');
}


//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;

    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" );

    var posicaoCampo = 0;
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;;

    if (Digitato != 8) { // backspace
        for(i=0; i<= TamanhoMascara; i++) {
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                  TamanhoMascara++;
            }else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
              }
          }
        campo.value = NovoValorCampo;
          return true;
    }else {
        return true;
    }
}
//............................................................................
function fJS_IsIE()
{
    var sBrowser   = navigator.appName;
	var sUserAgent = navigator.userAgent;			
	
	if (sBrowser == "Microsoft Internet Explorer")return true;
	else return false;	
}
function IsIE8()
{
    if(fJS_IsIE())
    {
        if(navigator.appVersion.split(';')[1] == " MSIE 8.0")
        {
            return true;
        }
        else return false;
    }
    else return false;
}
//............................................................................
function fJS_OnlyNumbers(e)
{     
   var key       = '';
   var strCheck  = '0123456789';
   var whichCode;
   
   if(fJS_IsIE()) whichCode = e.keyCode;    
   else  whichCode = (window.Event) ? e.which : e.keyCode;
      
   if (whichCode == 13) return false;  // 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)
   {                       
        if(fJS_IsIE())e.keyCode = 0;         
        else e.preventDefault();
        
        return false;  // Not a valid key        
   }
   else return true;

}  // fJS_OnlyNumbersDDL

function fJS_MascaraData(oObjt ,e)
{	
    var whichCode;	
	
    if(fJS_IsIE()) whichCode = e.keyCode;    
    else  whichCode = (window.Event) ? e.which : e.keyCode;
    
    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace
    
    if(fJS_OnlyNumbers(e))
    {
        var iValue = oObjt.value;
				
        switch(iValue.length)        
        {
            case 2 : oObjt.value = iValue + "/"; break;        
            case 5 : oObjt.value = iValue + "/"; break;        
        } 
    } 
}
//............................................................................
function fJS_FormatPhone(oObjt ,e)
{   
    var whichCode;
  
    if(fJS_IsIE()) whichCode = e.keyCode;    
    else  whichCode = (window.Event) ? e.which : e.keyCode;
        
    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace
           
    if(fJS_OnlyNumbers(e))
    {
        var iValue = oObjt.value;
        switch(iValue.length)        
        {
            case 0 : oObjt.value = iValue + "("; break;        
            case 3 : oObjt.value = iValue + ")"; break;        
            case 8 : oObjt.value = iValue + "-"; break;        
        }       
    }          
}
//............................................................................
function fJS_IsPossible(iKey)
{
    var bReturn   = false;
    
    switch(parseInt(iKey))
    {
        case 0  : bReturn = true;  break; //--> Tab
        case 8  : bReturn = true;  break; //--> Backspace
        case 13 : bReturn = true;  break; //--> Enter
        case 32 : bReturn = true;  break; //--> BarSpace
        case 127: bReturn = true;  break; //--> Delete
        default : bReturn = false; break; 
    }
        
    return bReturn;
}
//............................................................................
function fJS_FormatCNPJ(oObjt ,e)
{
    var whichCode;

    if(fJS_IsIE()) whichCode = e.keyCode;    
    else  whichCode = (window.Event) ? e.which : e.keyCode;
        
    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace
    
    if(fJS_OnlyNumbers(e))
    {   
        var iValue = oObjt.value;
        switch(iValue.length)        
        {
            case 2 : oObjt.value = iValue + "."; break;        
            case 6 : oObjt.value = iValue + "."; break;        
            case 10 : oObjt.value = iValue + "/"; break;        
            case 15 : oObjt.value = iValue + "-"; break;
        }
               
    }    
}
//............................................................................
function fJS_FormatCPF(oObjt ,e)
{
    var whichCode;

    if(fJS_IsIE()) whichCode = e.keyCode;    
    else  whichCode = (window.Event) ? e.which : e.keyCode;
        
    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace
    
    if(fJS_OnlyNumbers(e))
    {
        var iValue = oObjt.value;
        switch(iValue.length)        
        {
            case 3 : oObjt.value = iValue + "."; break;        
            case 7 : oObjt.value = iValue + "."; break;                    
            case 11 : oObjt.value = iValue + "-"; break;
        }       
    }
}
//............................................................................
function fJS_FormatCEP(oObjt ,e)
{
    var whichCode;
    
    if(fJS_IsIE()) whichCode = e.keyCode;    
    else  whichCode = (window.Event) ? e.which : e.keyCode;
        
    if (whichCode == 13) return false;  // Enter
    if (whichCode == 0) return true;    // Delete
    if (whichCode == 8) return true;    // Backspace
    
    if(fJS_OnlyNumbers(e))
    {
        var iValue = oObjt.value;
        switch(iValue.length)        
        {                               
            case 5 : oObjt.value = iValue + "-"; break;
        }       
    }    
}
