function isDate( date )
{
	if (date.length!=10) return false;

	var dn1 = date.substr(0,2);
	var dn2 = date.substr(3,2);
	var dn3 = date.substr(6,4);

	if ( (date.indexOf("/",0) != 2) || (date.indexOf("/",3) != 5) || (date.charAt(7) == "") )
	{
		return false;
	}

	if ( (isNaN(dn1)) || (isNaN(dn2)) || (isNaN(dn3)) )	return false;

	if ( (dn1 > 31) || (dn2 > 12) )	return false;
	
	return true;
}

function mask(obj,e,mask )
{
   var whichCode = (window.Event) ? e.which : e.keyCode;

   // if a control character was typed accept it without check
   if ( whichCode < 32 ) return true;

   if ( obj.maxLength <= 0 ) obj.maxLength = mask.length;

   if ((obj.value.length >= obj.maxLength) || (obj.value.length >= mask.length)) return false;

   var strCheck  = '#ADMYHMS';
   var numbers   = '0123456789';
//   var whichCode = (window.Event) ? e.which : e.keyCode;
   var key       = String.fromCharCode(whichCode);
   var cChar     = mask.charAt(obj.value.length);

   while ((obj.value.length < obj.maxLength) && (obj.value.length < mask.length))
   {
      if (strCheck.indexOf(cChar) < 0)
      {
         obj.value += cChar;
      }
      else
      {
         if (('#DMYHMS'.indexOf(cChar) >= 0) && (numbers.indexOf(key) < 0)) return false;
         break;
      }
      cChar = mask.charAt(obj.value.length);
   }
   return true;
}
///
///  controls the input data in a text field
///
///  dependencies: none
///
///  usage: <input type=text name=text1 maxlenght=9 size=9 onkeypress="javascript:return(mask2(this,event,'####-####'));">
///         <input type=text name=text1 maxlenght=10 size=10 onkeypress="javascript:return(mask2(this,event,'AAAAAAAAAA',' !@+'));">
///
function mask2(obj,evt,msk,deny)
{
   var nCode = (window.Event) ? evt.which : evt.keyCode;
   
   // if a control character was typed accept it without check
   if ( nCode < 32 ) return true;

   // necessary to mozilla´s browsers
   if ( obj.maxLength <= 0 ) obj.maxLength = mask.length;
   
   // if maxlength value or the mask´s length was reached, abort the function
   if ((obj.value.length >= obj.maxLength) || (obj.value.length >= msk.length)) return false;

   var sKey  = String.fromCharCode(nCode);
   var sChar = msk.charAt(obj.value.length);

   // if a denied character was typed, abort the function
   if ( typeof(deny) != "undefined" )
   {
      if ( deny.indexOf(sKey) >= 0 ) return false;
   }

   while ((obj.value.length < obj.maxLength) && (obj.value.length < msk.length))
   {
      switch ( sChar )
      {
         case "*":
            if ( (nCode >= 48 && nCode <= 57) || (nCode >= 65 && nCode <= 90) || (nCode >= 97 && nCode <= 122) ) return true;
            return false;
         case "A": // accept only AaBbCc...Zz
            if ( (nCode >= 65 && nCode <= 90) || (nCode >= 97 && nCode <= 122) ) return true;
            return false;
         case "#": // accept only 123...9
            if ( nCode >= 48 && nCode <= 57 ) return true;
            return false;
         case "X": // accept any character
            return true;
         default:  // any character else in the msk variable must be added
            obj.value += sChar;
            sChar = msk.charAt(obj.value.length);
      }
   }
}
///
///  controls the data entrance in a text field
///
///  usage: <input type=text name=text1 maxlenght=9 size=9 onkeypress="javascript:return(mask2(this,event,'A'));">
///         <input type=text name=text1 maxlenght=10 size=10 onkeypress="javascript:return(mask2(this,event,'X',,' !@+'));">
///
///  obj  - object representing the text field
///  evt  - event (always this)
///  type - '*' = numeric & alphabetic characters only
///         'A' = alphabetic characters only
///         '#' = numeric characters only
///         'X' = any character type
///  acpt - string of characters that must be accepted
///  deny - string of characters that must be denied
///
function accepts(obj,evt,type,acpt,deny)
{
   var nCode = (window.Event) ? evt.which : evt.keyCode;
   
//   window.alert( nCode );

   // if a control character was typed accept it without check
   if ( nCode < 32 ) return true;
   
   var sKey  = String.fromCharCode(nCode);

   // if a accepted character was typed, abort the function
   if ( typeof(acpt) != "undefined" )
   {
      if ( acpt.indexOf(sKey) >= 0 ) return true;
   }

   // if a denied character was typed, abort the function
   if ( typeof(deny) != "undefined" )
   {
      if ( deny.indexOf(sKey) >= 0 ) return false;
   }

   if ( typeof(type) == "undefined" ) type = "X";

   switch ( type )
   {
      case "*":
         if ( (nCode >= 48 && nCode <= 57) || (nCode >= 65 && nCode <= 90) || (nCode >= 97 && nCode <= 122) ) return true;
         return false;
      case "A": // accept only AaBbCc...Zz
         if ( (nCode >= 65 && nCode <= 90) || (nCode >= 97 && nCode <= 122) ) return true;
         return false;
      case "#": // accept only 123...9
         if ( nCode >= 48 && nCode <= 57 ) return true;
         return false;
      case "X": // accept any character
         return true;
      default:  // deny all
         return false;
   }
}


/***
* This script and many more are available free online at -->
* The JavaScript Source!! http://javascript.internet.com -->
*
* V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
* Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
* Changes:
*
* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
* international characters) were allowed.
* 
* 1.1.3: Added the restriction to only accept addresses ending in two
* letters (interpreted to be a country code) or one of the known
* TLDs (com, net, org, edu, int, mil, gov, arpa), including the
* new ones (biz, aero, name, coop, info, pro, museum).  One can
* easily update the list (if ICANN adds even more TLDs in the
* future) by updating the knownDomsPat variable near the
* top of the function.  Also, I added a variable at the top
* of the function that determines whether or not TLDs should be
* checked at all.  This is good if you are using this function
* internally (i.e. intranet site) where hostnames don't have to 
* conform to W3C standards and thus internal organization e-mail
* addresses don't have to either.
* Changed some of the logic so that the function will work properly
* with Netscape 6.
* 
* 1.1.2: Fixed a bug where trailing . in e-mail address was passing
* (the bug is actually in the weak regexp engine of the browser; I
* simplified the regexps to make it work).
* 
* 1.1.1: Removed restriction that countries must be preceded by a domain,
* so abc@host.uk is now legal.  However, there's still the 
* restriction that an address must end in a two or three letter
* word.
* 
* 1.1: Rewrote most of the function to conform more closely to RFC 822.
* 
* 1.0: Original
*/
function isEmail (emailStr)
{
   /* The following variable tells the rest of the function whether or not
      to verify that the address ends in a two-letter country or well-known
      TLD.  1 means check it, 0 means don't. */

   var checkTLD=1;

   /* The following is the list of known TLDs that an e-mail address must end with. */

   var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

   /* The following pattern is used to check if the entered e-mail address
      fits the user@domain format.  It also is used to separate the username
      from the domain. */

   var emailPat=/^(.+)@(.+)$/;

   /* The following string represents the pattern for matching all special
      characters.  We don't want to allow special characters in the address. 
      These characters include ( ) < > @ , ; : \ " . [ ] */

   var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

   /* The following string represents the range of characters allowed in a 
      username or domainname.  It really states which chars aren't allowed.*/

   var validChars="\[^\\s" + specialChars + "\]";

   /* The following pattern applies if the "user" is a quoted string (in
      which case, there are no rules about which characters are allowed
      and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
      is a legal e-mail address. */

   var quotedUser="(\"[^\"]*\")";

   /* The following pattern applies for domains that are IP addresses,
      rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
      e-mail address. NOTE: The square brackets are required. */

   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

   /* The following string represents an atom (basically a series of non-special characters.) */

   var atom=validChars + '+';

   /* The following string represents one word in the typical username.
      For example, in john.doe@somewhere.com, john and doe are words.
      Basically, a word is either an atom or quoted string. */

   var word="(" + atom + "|" + quotedUser + ")";

   // The following pattern describes the structure of the user

   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

   /* The following pattern describes the structure of a normal symbolic
      domain, as opposed to ipDomainPat, shown above. */

   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

   /* Finally, let's start trying to figure out if the supplied address is valid. */

   /* Begin with the coarse pattern to simply break up user@domain into
      different pieces that are easy to analyze. */

   var matchArray=emailStr.match(emailPat);

   if (matchArray==null)
   {
      /* Too many/few @'s or something; basically, this address doesn't
         even fit the general mould of a valid e-mail address. */
//      alert("Email address seems incorrect (check @ and .'s)");
      return false;
   }

   var user=matchArray[1];
   var domain=matchArray[2];

   // Start by checking that only basic ASCII characters are in the strings (0-127).

   for (i=0; i<user.length; i++)
   {
      if (user.charCodeAt(i)>127)
      {
//         alert("Ths username contains invalid characters.");
         return false;
      }
   }
   
   for (i=0; i<domain.length; i++)
   {
      if (domain.charCodeAt(i)>127)
      {
//         alert("Ths domain name contains invalid characters.");
         return false;
      }
   }

   // See if "user" is valid 

   if (user.match(userPat)==null)
   {
      // user is not valid
//      alert("The username doesn't seem to be valid.");
      return false;
   }

   /* if the e-mail address is at an IP address (as opposed to a symbolic
      host name) make sure the IP address is valid. */

   var IPArray=domain.match(ipDomainPat);

   if (IPArray!=null)
   {
      // this is an IP address
      for (var i=1;i<=4;i++)
      {
         if (IPArray[i]>255)
         {
//            alert("Destination IP address is invalid!");
            return false;
         }
      }
      return true;
   }

   // Domain is symbolic name.  Check if it's valid.
    
   var atomPat=new RegExp("^" + atom + "$");
   var domArr=domain.split(".");
   var len=domArr.length;

   for (i=0;i<len;i++)
   {
      if (domArr[i].search(atomPat)==-1)
      {
//         alert("The domain name does not seem to be valid.");
         return false;
      }
   }

   /* domain name seems valid, but now make sure that it ends in a
      known top-level domain (like com, edu, gov) or a two-letter word,
      representing country (uk, nl), and that there's a hostname preceding 
      the domain or country. */

   if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
   {
//      alert("The address must end in a well-known domain or two letter " + "country.");
      return false;
   }

   // Make sure there's a host name preceding the domain.

   if (len<2)
   {
 //     alert("This address is missing a hostname!");
      return false;
   }

   // If we've gotten this far, everything's valid!
   return true;
}


function checkSelect( frm, fld, idx, nowarn )
{
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }

   idx = (typeof(idx)=="number"?idx:0);
   idx = (idx<0?0:idx);
   idx = (idx>=frm.elements[fld].length?frm.elements[fld].length:idx);

   nowarn = (typeof(nowarn)=="boolean"?nowarn:false);

   if ( (frm.elements[fld].selectedIndex < idx) && (! nowarn) )
   {
      window.alert( "Selecione uma das opcoes da lista de " + 
		            (frm.elements[fld].title.length>0?frm.elements[fld].title:fld) );
      frm.elements[fld].focus();
   }
   return (frm.elements[fld].selectedIndex >= idx);
}

function checkText( frm, fld, nowarn )
{
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }
   else
   {
      if (frm.elements[fld].type != "text" && frm.elements[fld].type != "hidden" && frm.elements[fld].type != "password" && frm.elements[fld].type != "textarea")
      {
         window.alert( "\"" + fld + "\" não é uma caixa de texto!" );
         return false;
      }
   }

   nowarn = (typeof(nowarn)=="boolean"?nowarn:false);

   if ( (frm.elements[fld].value == "") && (! nowarn) )
   {
      window.alert("O campo " + 
		           (frm.elements[fld].title.length>0?frm.elements[fld].title:fld) + 
		           " deve ser preenchido!");
      if ( frm.elements[fld].type != "hidden" ) frm.elements[fld].focus();
   }

   return (frm.elements[fld].value != "");
}
///
///  check if the value of a text field contains a valid email address
///
function checkEmail( frm, fld, nowarn )
{
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }
   else
   {
      if (frm.elements[fld].type != "text" && frm.elements[fld].type != "hidden")
      {
         window.alert( "\"" + fld + "\" não é uma caixa de texto!" );
         return false;
      }
   }

   nowarn = (typeof(nowarn)=="boolean"?nowarn:false);

   var flag = isEmail( frm.elements[fld].value );

   if ( (!flag) && (!nowarn) )
   {
      alert("O endereço de e-mail informado não é válido!");
      if ( frm.elements[fld].type != "hidden" ) frm.elements[fld].focus();
   }
   return flag;
}

function checkRadio( frm, fld, nowarn )
{
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }

   nowarn = (typeof(nowarn)=="boolean"?nowarn:false);

   var rtn = false;
   var fid = "";

   for (var i=0;i<frm.elements[fld].length;i++)
   {
      if (frm.elements[fld][i].checked)
      {
         rtn = true;
         fid = (frm.elements[fld][i].title.length>0?frm.elements[fld][i].title:fld);
         break;
      }
   }
   
   if ( (! rtn) && (! nowarn) )
   {
      window.alert( "Você deve marcar uma das " + frm.elements[fld].length + 
		            " opções do item " + (frm.elements[fld][0].title.length>0?frm.elements[fld][0].title:fld) );
   }
   return rtn;
}
///
///  extract only charcters numbers of a given string
///
function numbersOnly( s )
{
   var r = "";
   for ( var i=0;i<s.length;i++ )
   {
      if ( "0123456789".indexOf( s.substr(i,1) ) >= 0 ) r += "" + s.substr(i,1);
   }
   return r;
}
///
///  check if a given cpf number is valid
///
///  dependecies: numbersOnly
///
function isCPF(c)
{
   var i;
   // this function works only with character number
   c = numbersOnly( c );
   // cpfs must be eleven character numbers
   if ( c.length != 11 ) return false;
   // numbers like 11111111111, 22222222222, ..., 999999999999 are not valid
   if ( (parseInt(c) / (parseInt(c) % 10)) == 11111111111 ) return false
   s = c;
   var c = s.substr(0,9);
   var dv = s.substr(9,2);
   var d1 = 0;
   var v = false;
   for (i = 0; i < 9; i++)
   {
      d1 += c.charAt(i)*(10-i);
   }
   if (d1 == 0)
   {
      return false;
   }
   d1 = 11 - (d1 % 11);
   if (d1 > 9) d1 = 0;
   if (dv.charAt(0) != d1) return false;
   d1 *= 2;
   for (i = 0; i < 9; i++)
   {
      d1 += c.charAt(i)*(11-i);
   }
   d1 = 11 - (d1 % 11);
   if (d1 > 9) d1 = 0;
   if (dv.charAt(1) != d1) return false;
   return true;
}
///
///  check if a form field contains a valid cpf number
///
function checkCPF(frm,fld)
{
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }
   else
   {
      if (frm.elements[fld].type != "text" && frm.elements[fld].type != "hidden")
      {
         window.alert( "\"" + fld + "\" não é uma caixa de texto!" );
         return false;
      }
   }
   if ( !isCPF(frm.elements[fld].value) )
   {
      window.alert( "O nº de CPF informado não é válido" );
      if ( frm.elements[fld].type != "hidden" ) frm.elements[fld].focus();
      return false;
   }
   return true;
}

function checkDate(frm,fld,nowarn)
{
   var fRet = true
   if (typeof(frm.elements[fld])=="undefined")
   {
      window.alert( "O campo \"" + fld + "\" não existe!" );
      return false;
   }
   else
   {
      if (frm.elements[fld].type != "text" && frm.elements[fld].type != "hidden")
      {
         window.alert( "\"" + fld + "\" não é uma caixa de texto!" );
         return false;
      }
   }

   if ( (!isDate(frm.elements[fld].value)) && (!nowarn) )
   {
	  fRet = false;
      window.alert("O data informada no campo \"" + 
		           frm.elements[fld].title + 
		           "\" não é válida");
	  if ( frm.elements[fld].type != "hidden" ) frm.elements[fld].focus();
   }

   return fRet;
}