

function criaMascaraData(_RefObjeto, _Modelo){

var valorAtual = _RefObjeto.value;
var valorNumerico = '';
var nIndexModelo = 0;
var nIndexString = 0;
var valorFinal = '';
var adicionarValor = true;
 

// limpa a string valor atual para verificar
// se todos os caracteres s�o n�meros
for (i=0;i<_Modelo.length;i++){
  if (_Modelo.substr(i,1) != '#'){
    valorAtual = valorAtual.replace(_Modelo.substr(i,1),'');
}}
 
// verifica se todos os caracteres s�o n�meros
for (i=0;i<valorAtual.length;i++){
  if (!isNaN(parseFloat(valorAtual.substr(i,1)))){
    valorNumerico = valorNumerico + valorAtual.substr(i,1);
}}
 
// aplica a m�scara ao campo informado usando
// o modelo de m�scara informado no script
for (i=0;i<_Modelo.length;i++){
 
  if (_Modelo.substr(i,1) == '#'){
    if (valorNumerico.substr(nIndexModelo,1) != ''){
      valorFinal = valorFinal + valorNumerico.substr(nIndexModelo,1);
      nIndexModelo++;nIndexString++;
    }
      else {
        adicionarValor = false;
  }}
 
    else {
      if (adicionarValor && valorNumerico.substr(nIndexModelo,1) != ''){
      valorFinal = valorFinal + _Modelo.substr(nIndexString,1)
      nIndexString++;
    }}
}
 
_RefObjeto.value = valorFinal; 

}


function validaletra(f) {
//!/^[A-z]*$/.test(f.value)?f.value = f.value.replace(/[^A-z]/g,''):null;
//!/^[A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]*$/i.test(f.value)?f.value = f.value.replace(/[^A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]/ig,''):null;
!/^[A-zÇçÀàÁÉÍÓáéíóÂÊÔâêôÃã ]*$/i.test(f.value)?f.value = f.value.replace(/[^A-zÇçÀàÁÉÍÓáéíóÂÊÔâêôÃã ]/ig,''):null;
}

function validanumero(f){
!/^[0-9]*$/i.test(f.value)?f.value = f.value.replace(/[^0-9]/ig,''):null;	
}

function validapreco(f){
!/^[0-9.]*$/i.test(f.value)?f.value = f.value.replace(/[^0-9.]/ig,''):null;	
}

/*
function validDate(obj){
	 date=obj.value
	if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return }
	if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (/^\d{2}\/\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date)) return
	
	 test1=(/^\d{1,2}\/?\d{1,2}\/\d{4}$/.test(date));
	 date=date.split('/');
	 d=new Date(date[2],date[1]-1,date[0]);
	 test2=(1*date[0]==d.getDate() && 1*date[1]==(d.getMonth()+1) && 1*date[2]==d.getFullYear());
	 if (test1 && test2) 
	 	return true
	 alert("Data inválida");
	 obj.select();
	 obj.focus();
	 obj.value = "";
	 return false
	 
}
*/

function validDate(datax) {
//validação de data by 
//Carmo Renato E. Silva - 2008
//websites@carmorenato.com

	if(datax.value.length < 10) {
	
		alert("Formato inválido. Favor preencher no formato dd/mm/aaaa");
		//datax.value = "";
		datax.focus();
		//return false;

	}
	else {
		diax = datax.value.substr(0,2);
		//alert(diax);
		mesx = datax.value.substr(3,2);
		//alert(mesx);
		anox = datax.value.substr(6,4);
		//alert(anox);
		
		if(anox < "1900" || anox > "2100") {
			alert("Ano inválido!");
			//datax.value = "";
			datax.focus();
		}
		else {
			if(mesx < "01" || mesx > "12") {
				alert("Mês inválido!");
				//datax.value = "";
				datax.focus();
			}
			else {
				if(diax < "01" || diax > "31") {
					alert("Dia inválido!");
					//datax.value = "";
					datax.focus();
				}
				else {
					if((mesx == "04" || mesx == "06" || mesx == "09" || mesx == "11") && diax == "31") {
						switch(mesx) {
							case "04":
							var nomemes = "Abril";
							break;
							case "06":
							var nomemes = "Junho";
							break;
							case "09":
							var nomemes = "Setembro";
							break;
							case "11":
							var nomemes = "Novembro";
							break;
		
						}
						alert(nomemes+" não tem 31 dias!");
						//datax.value = "";
						datax.focus();
					}
					else {
						if(mesx == "02" && diax > "29") {
							alert("Dia incorreto para Fevereiro!");
							//datax.value = "";
							datax.focus();
						}
						else {
							if(anox%4 !== 0 && diax == "29") {
								alert(anox+" não é bissexto!");
								//datax.value = "";
								datax.focus();
							}
							else {
								//alert("Data válida");
							}
						}
					}
				}
			} 
		}
	
	}
}


function check_date(date) {
   var err = 0
   string = date
   var valid = "0123456789/"
   var ok = "yes";
   var temp;
   for (var i=0; i< string.length; i++) {
     temp = "" + string.substring(i, i+1);
     if (valid.indexOf(temp) == "-1") err = 1;
   }
   if (string.length != 10) err=1
   b = string.substring(3, 5)        // month
   c = string.substring(2, 3)        // '/'
   d = string.substring(0, 2)        // day
   e = string.substring(5, 6)        // '/'
   f = string.substring(6, 10)    // year
   if (b<1 || b>12) err = 1
   if (c != '/') err = 1
   if (d<1 || d>31) err = 1
   if (e != '/') err = 1
   if (f<1850 || f>2050) err = 1
   if (b==4 || b==6 || b==9 || b==11){
     if (d==31) err=1
   }
   if (b==2){
     var g=parseInt(f/4)
     if (isNaN(g)) {
         err=1
     }
     if (d>29) err=1
     if (d==29 && ((f/4)!=parseInt(f/4))) err=1
   }
   if (err==1) {
       alert("Data Inválida");
    return false;
   }
   else {
      // alert("Data correta");
    return true;
   }
}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

