<!--
var arrErro  = new Array(1);
arrErro[0] = "Erro\n";

function selectIndex(cmbToSel,selected){
 with(cmbToSel){
 for(var i=0;i<options.length;i++)
  if(options[i].value == selected)
   options.selectedIndex = i;}}

function events(){
for(var i=0;i<document.forms.length;i++){
with(document.forms[i]){for(var j=0;j<length;j++){with(elements[j]){
if(type=='text' || type=='password') attachEvent('onkeyup',function x(){proxCampo();});
if(id.indexOf('Int') != -1) attachEvent('onkeypress',function x(){onlyNum();});
if(id.indexOf('Dat') != -1) attachEvent('onkeypress',function x(){onlyNum();maskDate();});
if(id.indexOf('HMS') != -1) attachEvent('onkeypress',function x(){onlyNum();maskTime();});
if(id.indexOf('IPv') != -1) attachEvent('onkeypress',function x(){onlyNum(46);});
if(id.indexOf('Txt') != -1) attachEvent('onkeyup',function x(){maxLengthTextArea();});}}}}
focoCampo();}

function maskDate(date){ 
 date = (date==null)?event.srcElement:date; 
 var len = date.value.length;
 if ( (len == 2) || (len == 5) )
  date.value += '/';} 

function maskTime(time){ 
 var time = (time==null)?event.srcElement:time; 
 var len = time.value.length;
 if ( (len == 2) || (len == 5))
  time.value += ':';} 

function maxLengthTextArea(){
 var textArea = event.srcElement;
 var spanMaxChars = document.getElementById('maxChars');
 getRange(textArea.id);
 maxlength = tMax;
 var currentLength = maxlength - textArea.value.length;
 if(currentLength>-1)
  spanMaxChars.innerHTML = currentLength;
 else
  textArea.value = textArea.value.substring(0,maxlength);}

function focoCampo(){
with(document.forms[0])
for(var i=0;i<length;i++)
 with(elements[i])
  if (type != 'image' && type != 'submit' && type != 'button' && type != 'reset' && type != 'hidden' && type != 'file'){
   focus();
   return;}}

function chkValue(campo){
for(var i=0;i<campo.length;i++)
 if(campo[i].checked) 
  return campo[i].value;}

function onlyNum(e){
 var tecla = (window.event) ? event.keyCode : e.which;
 if ( tecla > 47 && tecla < 58 || tecla == 8 || tecla == arguments[1])
  return;
 else 
  if (window.event) //IE
   window.event.returnValue = null;   
  else //Firefox
   e.preventDefault();}

function proxCampo(){
 cmpAtual = event.srcElement;
 frm = cmpAtual.form;
 if (cmpAtual.value.length == cmpAtual.maxLength){
  with(frm){
  for(var i=0;i<length;i++)
   if (cmpAtual.name == elements[i].name)
    if (i+1 != length)
	 elements[i+1].focus();}}}

// ********************************** Início Rotina de Validação ****************************************************

var tMax = 0, tMin = 0, tRange = "", ok = false, erro = 0;
var bCPadrao = "#7f9db9", bWPadrao = "1px", cPadrao = "#333333";
var bC = "red", bW = "1px", cor = "#E41A1A";
var MsgErro = -1;
var nomeCampoFocus = "NOTHING";
strErro = new String();

function corErro(campo){
 if(campo.type != 'radio' && campo.type != 'checkbox'){	
 with(campo.style){
  borderWidth = bW;
  borderColor = bC;
  color = cor;}}}

function getRange(nfoCampo){
 tRange = nfoCampo.substr(4,nfoCampo.indexOf('-',4)-nfoCampo.indexOf('-')-1);
 with(tRange){
 var sep = indexOf(';')
 if (sep == -1){
  tMin = 0;
  tMax = tRange;}
 else{
  tMin = parseInt(substring(0,sep)); 
  tMax = parseInt(substr(sep+1,length));}}}

function getRequerido(nfoCampo,posI){
 return parseInt(nfoCampo.substr(posI,1));}

function getMsgErro(nfoCampo,posI){
 return parseInt(nfoCampo.substr(posI,2));}

function NzL(campo){
 if (String(campo).length == 0)
  return false;
 else
  return true;}

function validaCampo(campo){
 var Requerido = 0
 tMax = 0;
 foco = 0;
 
 with(campo){
// ************************ Verificação dos Campos *****************************
   getRange(id);
   Requerido = getRequerido(id,id.indexOf('-'+tRange+'-') + 2 + String(tRange).length);
   MsgErro = getMsgErro(id,id.indexOf('-'+tRange+'-') + 2 + String(tRange).length + 2);
   if(campo.disabled)Requerido=0;

if (Requerido == 1 || value.length > 0){
    if (id.indexOf('Str') != -1 || id.indexOf('Txt') != -1)
     ok = validaStr(value);
    if (id.indexOf('Int') != -1)
	 ok = validaInt(value);
    if (id.indexOf('@@@') != -1)
     ok = validaEmail(value);
    if (id.indexOf('Sen') != -1)
     ok = validaSenha(campo);
	if (id.indexOf('Dat') != -1)
     ok = validaData(value);
	if (id.indexOf('HMS') != -1)
     ok = validaHora(value);
    if (id.indexOf('IPv') != -1)
     ok = validaIP(value);
	
	if(Requerido == 1){
 	 if (id.indexOf('Mlt') != -1)
	  ok = validaMlt(value);
     if (id.indexOf('Rad') != -1)
      ok = validaRadioGroup(campo);	 
     if (id.indexOf('Cmb') != -1)
      ok = validaCmb(campo);}	

    if (!ok){
	strErro += arrErro[MsgErro];
     if (foco == 0) {foco = 1;}
	 if (nomeCampoFocus == "NOTHING")
	  colocaFoco (campo,foco);}
	
    if (ok){
	 ok = NzL(value);
	 if (!ok)
	  corErro(campo);
	        
    }	   
    else
     corErro(campo);
   }else ok = true;

      

// ****************************************************************************
 }//End With
}

function validaEmail(campo){
 var lStrFormatted = new String(campo);
 lStrFormatted = lStrFormatted.replace(/^\s*/, '');
 lStrFormatted = lStrFormatted.replace(/\s*$/, '');
 		
 //Leitura do comando de validação
 //1	\w+				=> 1 ou mais caracteres inclusos em a-z,A-Z,0-9 + {'_'}
 //2	[\.-]?			=> apenas 1 caracter incluso em {'.','-'}
 //3	([\.-]?\w)*		=> 0 ou mais grupos de caracteres do tipo 2 seguido do tipo \w
 //4	(\.\w{2,3})+	=> 1 ou mais grupos de 2 ou 3 caracteres do tipo \w no fim da string
	
 if (! (/^\w+([\.-]?\w+)*(.)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(lStrFormatted)) )
  return false;
 if (!validaStr(campo)) 
  return false;
 
return true;}

function validaSenha(campo){
with(campo.form){
for(var i=0;i<length;i++)
 if (campo.name == elements[i].name){
  campoConf = elements[i+1];break;}}

 if (campo.value == campoConf.value)
  if(validaStr(campo.value))
   return true;
 else
  return false;}
  
function validaCmb(campo){
 if (campo.selectedIndex != 0)
  return true;
 else
  return false;}

function validaMlt(campo){
 if (campo.length != 0)
  return true;
 else
  return false;}

function validaInt(campo){
 if (!isNaN(campo)){
  if (campo.length >= tMin && campo.length <= tMax && campo != '')
   return true;
  else
   return false;}
 else
  return false;}

function validaMaxInt(campo){
 if (!isNaN(campo)){
  if (campo >= tMin && campo <= tMax)
   return true;
  else
   return false;}
 else
  return false;}
  
function validaStr(campo){
 if (campo.length >= tMin && campo.length <= tMax && campo.length != '')
  return true;
 else
  return false;}
  
function validaRadioGroup(campo){
 var RadioGroup = eval('campo.form.'+ campo.name);
 for(var r=0;r<RadioGroup.length;r++)
  if(RadioGroup[r].checked) 
   return true;
return false;}

function validaIP(campo) {
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = campo.match(ipPattern);

if ( ((campo == "0.0.0.0") || (campo == "255.255.255.255")) || (ipArray == null))
 return false;
else {
 for (i = 0; i < 4; i++) {
  thisSegment = ipArray[i];
  if (thisSegment > 255) {
   return false;
   i = 4;}
  if ((i == 0) && (thisSegment > 255)) {
   return false;
  i = 4;}}}
return true;}

function validaData(campo){
var BR = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
if (campo.match(BR) == null)
 return false;
else
 {
  var arrayDate = campo.split("/");
  var intDay = parseInt(arrayDate[0],10); 
  var intMonth = parseInt(arrayDate[1],10);
    if (intMonth == 2)
	 { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
	 }
	else
	 return true;
 }
return false;}

function validaHora(campo){
 if (! (/^\d+:[0-5]\d:[0-5]\d$/.test(campo)) )
  return false;
 return true;}

function colocaFoco(campo,ativo){if (ativo == 1){nomeCampoFocus = campo;}}
function SetFocus(campo){campo.focus();}

function Validafrm(frm){
 ok = false;
 erro = 0;
 //var foco = 0;
 nomeCampoFocus = "NOTHING";
 with(frm){
  for (var i=0;i<length;i++){
   with(elements[i]){
    if (type != 'image' && type != 'submit' && type != 'button' && type != 'reset' && type != 'hidden' && type != 'checkbox' && type != 'radio'){
	 style.borderWidth = bWPadrao;
	 style.borderColor = bCPadrao;
	 style.color = cPadrao;
	 if(id!=''){
 	  validaCampo(elements[i]);
	  if (!ok)
	   erro += 1;}}
   } //End With
  } //Next
  try{Extras(frm)}catch(x){};
  if (erro > 0){
	strErro = "Por favor, verifique o(s) seguinte(s) erro(s):\n\n"+ strErro
	alert(strErro);
	strErro = "";
   return false;
   
   }
  else{
   return true;}
 } //End With
}
-->
