// SimpleChek.js
// Esta e uma vers&atilde;o simplificado do arquivo FormChek.js
// contendo apenas as funcoes mais usuais, com isso sendo um arquivo bem menor
// contra os 57K do FormChek.js
// 
//  Lista das funcoes utilizadas e suas dependencias
//
// 		isInteger
// 		   |_____isEmpty
// 		   |_____isDigit
// 		
// 		isEmpty
// 		
// 		isLetter
// 		
// 		isDigit
// 		
// 		isLetterOrDigit
// 		   |_____isLetter
// 		   |_____isDigit
// 		
// 		isFloat
// 		   |_____isEmpty
// 		   |_____decimalPointDelimiter
// 		   |_____isDigit
// 		
// 		isWhitespace
// 		   |_____isEmpty
// 		
// 		isEmail
// 		   |_____isEmpty
// 		   |_____isWhitespace
// 		   |_____HasInvalidChar
// 		
// 		isDate
// 		   |_____isYear
// 		   |_____isMonth
// 		   |_____isDay
// 		   |_____daysInMonth
// 		   |_____daysInFebruary
// 		
// 		daysInFebruary
// 		
// 		isDay
// 		   |_____isEmpty
// 		   |_____isIntegerInRange
// 		
// 		isMonth
// 		   |_____isIntegerInRange
// 		
// 		isYear
// 		   |_____isEmpty
// 		   |_____isNonnegativeInteger
// 		
// 		isIntegerInRange
// 		   |_____isEmpty
// 		   |_____isInteger
// 		
// 		isNonnegativeInteger
// 		   |_____isSignedInteger
// 		   |_____isEmpty
// 		
// 		isSignedInteger
// 		   |_____isEmpty
// 		   |_____isInteger
//
// 		isZIPCode
// 		   |_____isEmpty
// 		   |_____isInteger
// 		   |_____stripCharsInBag
// 		   |_____ZIPCodeDelimiters
//

		
var dadosOk=false;
var VerifiqueTAB=true;

var defaultEmptyOK = false
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";          // whitespace characters
var invalidchars = " \t\n\r\\,/";
var decimalPointDelimiter = ","      // decimal point character differs by language and culture
var phoneNumberDelimiters = "- ";  // non-digit characters which are allowed in phone numbers
var ZIPCodeDelimiters = "- ";         // non-digit characters which are allowed in ZIP Codes

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 8

var digitsInUSPhoneNumber = 8;

function makeArray(n)
{
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var daysInMonth = new Array(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;



function isInteger (s)
{   var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}


function isEmpty(s){
   return ((s == null) || (s.length == 0))
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


function isFloat (s)
{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s))
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
    else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}



function isWhitespace (s)

{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
    else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;
	if (HasInvalidChar(s)) return false;

    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else if ((s.charAt(i) == "@") && (s.charAt(i+1) == ".")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    //if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
	if (! (isYear(year) && isMonth(month) && isDay(day))) return false;
    var intYear = Number(year);
    var intMonth = Number(month);
    var intDay = Number(day);

    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


function daysInFebruary (year)
{
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDay (s)
{   if (isEmpty(s))
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);
    return isIntegerInRange (s, 1, 31);
}


function isMonth (s)
{   if (isEmpty(s))
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isYear (s)
{   if (isEmpty(s))
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}


function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
    else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = Number (s);
    return ((num >= a) && (num <= b));
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (Number(s) >= 0) ) );
}


function isSignedInteger (s)
{   if (isEmpty(s))
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
}


function isPhoneNumber (s)
{
	if (!isEmpty(s))
		var normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters)
	else
		normalizedPhone = s
	
    if (isEmpty(normalizedPhone)) 
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    return (isInteger(normalizedPhone) && normalizedPhone.length <= digitsInUSPhoneNumber)
}


function isZIPCode (s)
{  

	if (!isEmpty(s))
		var normalizedZIP = stripCharsInBag(s, ZIPCodeDelimiters)
	else
		normalizedZIP = s
	

	if (isEmpty(normalizedZIP)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(normalizedZIP) && 
            ((normalizedZIP.length == digitsInZIPCode1) ||
             (normalizedZIP.length == digitsInZIPCode2)))
}
 
// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

  if (!isEmpty(s)){
	// Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
  }

    return returnString;
}

function HasInvalidChar (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (invalidchars.indexOf(c) != -1) return true;
    }
    return false;
}


function Mostra(quem, tammax) {
   if ( (quem.value.length == tammax) && (VerifiqueTAB) ) { 
     var i=0,j=0, indice=-1;
     for (i=0; i<document.forms.length; i++) { 
       for (j=0; j<document.forms[i].elements.length; j++) { 
          if (document.forms[i].elements[j].name == quem.name) { 
            indice=i;
            break;
          } 
       } 
       if (indice != -1) break; 
     } 
     for (i=0; i<=document.forms[indice].elements.length; i++) { 
       if (document.forms[indice].elements[i].name == quem.name) { 
          while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
                  (i < document.forms[indice].elements.length) ) { 
             i++;
          } 
          document.forms[indice].elements[(i+1)].focus();
          VerifiqueTAB=false;
          break;
       } 
     } 
   } 
} 


function PararTAB(quem) { VerifiqueTAB=false; } 
function ChecarTAB() { VerifiqueTAB=true; } 

function Range(v,p1,p2)
	{
		if (v>=p1 && v<=p2)
			{return true;}
		else {return false;}
	}





