/*
'------------------------------------------------------------------------------------------------------------------
'Autor: Brener Ferreira de Araújo
'Data de Criação: 27/10/2003
'Descrição: Include com funções em JS
'Data de Alteração:
'Responsável pela Alteração:
'------------------------------------------------------------------------------------------------------------------
*/

function tecla() { 
		if((event.keyCode < 48) || (event.keyCode > 57)) { 
				event.returnValue = false; 
			} 
		}

function setColor(el, bg) {
		  if (el.style){
			el.style.backgroundColor = bg;
		}
	}

function lower(fld,str,st) {
  var field = fld;
  if (st==0) {
   field.value=str.toLowerCase();
  } else {
   field.value=str.toUpperCase();
  }
}

function validaEmail(email)
{
	if (typeof(email) != "string")
		return false;
	else if (!email.match(/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/))
		return false;
	return true;
}



function stripSpaces(form_item){

  var mytext = new String("");

  mytext = form_item.value;

  while (mytext.charAt(0) == ' ') {

    mytext = mytext.substring(1,mytext.length);

  };

  while (mytext.charAt(mytext.length - 1) == ' ') {

    mytext = mytext.substring(0, (mytext.length - 1));

  };

  form_item.value = mytext;

}



function FormataData(Campo,teclapres) { 

var tecla = teclapres.keyCode; 

vr = document.form[Campo].value; 

vr = vr.replace( ".", "" ); 

vr = vr.replace( "/", "" ); 

vr = vr.replace( "/", "" ); 

tam = vr.length + 1; 



if ( tecla != 9 && tecla != 8 ){ 

  if ( tam > 2 && tam < 5 ) 

   document.form[Campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam ); 

  if ( tam >= 5 && tam <= 10 ) 

   document.form[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); } 

} 



function ValidaData(campo)

// Entrada: Campo Saída: 1=OK 0=Falha

 {

  var valor = campo.value;

  

  if ( valor != '' )

  {

   now = new Date();

   anohoje = now.getYear();

   Mes   = new Array ("01","02","03","04","05","06","07","08","09","10","11","12");

   meshoje = now.getMonth();

   Dia   = new Array ("31","29","31","30","31","30","31","31","30","31","30","31");

   diahoje = now.getDay();

   Data  = new Date();

      

   Barra1 = valor.indexOf("/");

   Barra2 = valor.lastIndexOf("/");



   DiaCampo = parseInt(valor.substring(0,Barra1),10);

   MesCampo = parseInt(valor.substring(Barra1 + 1,Barra2),10);

   AnoCampo = parseInt(valor.substring(Barra2 + 1,valor.length),10);

    

   var Ano = parseInt(Data.getYear(),10);

   var AnoBissexto = 0;

   

   var j = -1;

   for (var i = 0; i <= 11; i++)

    {

    if (Mes[i] == parseInt(MesCampo,10))

     { j = i; }

   }

   if (valor.length < 10)

    {

     alert('Data inválida.');

     campo.value='';

     campo.focus();

     return false;

    }

   else

    { 

     if (AnoCampo < 1900)

        {

       alert('Data inválida.');

      campo.value='';

      campo.focus();

      return false;

      }

     else

       {

         if (j == -1)

        {

        alert ('Data inválida.');

          campo.value='';

        campo.focus();

        return false;

       }

       else

        {

         /*if ((parseInt(DiaCampo,10) < diahoje) || (parseInt(MesCampo,10) < meshoje) || (parseInt(AnoCampo,10) < anohoje))

         {

         alert ('A data não pode ser menor que hoje.');

          campo.value='';

         campo.focus();

         return false;

         }*/

         

         if ((parseInt(DiaCampo,10) < 1) || (parseInt(DiaCampo,10) > parseInt(Dia[j],10)) )

          { alert ('Data inválida.');

           campo.value='';

          campo.focus();

          return false;

         }

        else

         {

            if (AnoCampo % 4 == 0)

              AnoBissexto = 1;



           if (AnoBissexto != 1 && parseInt(DiaCampo,10) == 29 && parseInt(MesCampo,10) == 2)

             {

           alert ('Data inválida. Ano não é bissexto!');

            campo.value='';

           campo.focus();

           return false;

          }

          else

           { return true; }

         }

        }

      }

    }

  }

}



function popup(mypage, myname, w, h, scroll) 

{

	var winl = (screen.width - w) / 2;

	var wint = (screen.height - h) / 2;

	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no';

	win = window.open(mypage, myname, winprops);

	if (parseInt(navigator.appVersion) >= 4) 

	{ 

		win.window.focus();

	}

}



