var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// whitespace characters
var whitespace = " \t\n\r";

// Seqüências proibidas
var proibidos = "www:@:.com:.cjb:.net:http://:href:<a";

// decimal point character differs by language and culture
var decimalPointDelimiter = ","

// non-digit characters which are allowed in credit card numbers
var creditCardDelimiters = " "

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

var mPrefix = "Não foi informado um valor para o campo "
var mSuffix = ", que é obrigatório. Por favor, informe agora."

var iEmail = "Este campo deve conter um endereço Email válido(como foo@bar.com). Informe novamente."
var iCreditCardPrefix = "Este não é um número válido para cartão de crédito "
var iCreditCardSuffix = ". Informe novamente."
var iDay = "Dia inválido. Informe novamente."
var iMonth = "Mês inválido. Informe novamente."
var iYear = "Ano inválido. Informe novamente."
var iDatePrefix = "A data informada em "
var iDateSuffix = " não é válida. Informe novamente."

// p is an abbreviation for "prompt"

var pEntryPrompt = "Informe um "
var pEmail = "endereço Email válido (como foo@bar.com)."
var pCreditCard = "número de cartão de crédito válido."
var pDay = "dia entre 1 e 31."
var pMonth = "mês entre 1 e 12."
var pYear = "ano com 4 dígitos."

var defaultEmptyOK = false

// Attempting to make this library run on Navigator 2.0,
// so I'm supplying this array creation routine as per
// JavaScript 1.0 documentation.  If you're using 
// Navigator 3.0 or later, you don't need to do this;
// you can use the Array constructor instead.

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(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;

// Check whether string s is empty.

// Returns true if string s is empty or 
// whitespace characters only.

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
function isProhibited (s)
{
	var i;
	var arrProibidos = proibidos.split(':');
	var s1 = s.toLowerCase();

    // Is s empty?
    if (isEmpty(s)) return false;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
	
    for (i = 0; i < arrProibidos.length; i++)
    {   
        // Checa se o string tem o caracter
		
        if (s1.indexOf(arrProibidos[i]) != -1) return true;
    }

    // All characters are ok.
    return false;
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isLetter (c)
{
   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{
   return ((c >= "0") && (c <= "9"))
}

function isCaracterValido (c)
{
	return ( ((c >= "a") && (c <= "z")) || 
	((c >= "A") && (c <= "Z")) || ((c >= "0") && (c <= "9")))
}


// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isAllNumber (STRING s)
// 
// Returns true if a string s are only numbers.

function isAllNumber (s)
{
   for(i = 0; i < s.length; i++)
    {
		if(!isDigit(s.charAt(i)))
		{
			return false;
		}
    }
    return true
}

function isAllCaracterValidos (s)
{
	for(i = 0; i < s.length; i++)
    {
		if(!isCaracterValido(s.charAt(i)))
		{
			return false;
		}
    }
    return true
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.

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 = parseInt (s, 10);
    return ((num >= a) && (num <= b));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate(strData)
{
	var intPosic1;
	var intPosic2;
	var year;
	var month;
	var day;

	if (strData.length < 8 || strData.length > 10)
	{
		return false;
	}
	
	intPosic1 = strData.indexOf('/', 0);
	if (intPosic1 < 1)
	{
		return false;
	}
		
	intPosic2 = strData.indexOf('/', intPosic1 + 1);
	if (intPosic2 <= (intPosic1 + 1))
	{
		return false;
	}
		
	year = strData.substr(intPosic2 + 1, 4);
	month = strData.substr(intPosic1 + 1, intPosic2 - (intPosic1 + 1));
	day = strData.substr(0, intPosic1);
	
	// 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;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Removes all characters which do NOT appear in string bag 
// from string s.

function stripCharsNotInBag (s, bag)
{
   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is 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;
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)
{
   var i;
    var returnString = "";

    // 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;
}

// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)
{
   return stripCharsInBag (s, whitespace);
}

function checkFormat (strValor, strFormat)
	{
	var isOk = true;

	isOk = (strValor.length == strFormat.length);

	if (isOk)
		{
		for (var i = 0; i < strFormat.length && isOk; i++)
			{
			switch (strFormat.charAt (i))
				{
				case '9':
					// Numeral
					isOk = isDigit (strValor.charAt (i));
					break;

				case 'A':
					// Letra
					isOk = isLetter (strValor.charAt (1));
					break;

				case '#':
					// Alfanumérico
					isOk = (isDigit (strValor.charAt (i)) || isLetter (strValor.charAt (i)));
					break;

				case '/':
					// Alfanumérico e "/"
					isOk = (isDigit (strValor.charAt (i)) || isLetter (strValor.charAt (i)) || strValor.charAt (i) == '/');
					break;
									
				default:
					// Caracteres especiais
					isOk = (strValor.charAt (i) == strFormat.charAt (i));
					break;
				}
			}
		}
	return isOk;
	}
function isRGValid(s, t)
{
	var offset;
	s = s.toUpperCase();
	switch (t)
	{
		case 1:
			if (s.length < 15 || s.length > 16)
			{
				return false;
			}
			if (s.substr(0,2) != "RG")
			{
				return false;
			}
			if (s.substr(2,1) != "/")
			{
				return false;
			}
			if (s.length == 15)
			{
				if (!checkFormat(s.substr(3,3), "AAA"))
				{
					return false;
				}
				if (s.substr(6,1) != "/")
				{
					return false;
				}
				if (!isYear(s.substr(7,2)))
				{
					return false;
				}
				if (s.substr(9,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(10,5),"#9999"))
				{
					return false;
				}
			}
			else
			{
				if (!checkFormat(s.substr(3,4), "AAA#"))
				{
					return false;
				}
				if (s.substr(7,1) != "/")
				{
					return false;
				}
				if (!isYear(s.substr(8,2)))
				{
					return false;
				}
				if (s.substr(10,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(11,5),"#9999"))
				{
					return false;
				}
			}
			break;
		case 2:
			if (s.length != 11 && (s.length < 15 || s.length > 16))
			{
				return false;
			}
			if (s.substr(0,5) != "CBKCE")
			{
				return false;
			}
			if (s.substr(5,1) != "/")
			{
				return false;
			}
			if (s.length == 11)
			{
				if (!checkFormat(s.substr(6,5),"99999"))
				{
					return false;
				}
			}
			else
			{
				if (s.length == 15)
				{
					if (!checkFormat(s.substr(6,3), "AAA"))
					{
						return false;
					}
					if (s.substr(9,1) != "/")
					{
						return false;
					}
					if (!checkFormat(s.substr(10,5),"#9999"))
					{
						return false;
					}
				}
				else
				{
					if (!checkFormat(s.substr(6,4), "AAA#"))
					{
						return false;
					}
					if (s.substr(10,1) != "/")
					{
						return false;
					}
					if (!checkFormat(s.substr(11,5),"99999"))
					{
						return false;
					}
				}
			}
			break;
		case 3:
			if (isWhitespace(s))
			{
				return false;
			}
			for (var i = 0; i < s.length; i++)
			{
				if (!(isDigit (s.charAt (i)) || isLetter (s.charAt (i)) || s.charAt (i) == '/'))
				{
					return false;
				}
			}
			break;
		case 4:
			if (s.length < 15 || s.length > 16)
			{
				return false;
			}
			if (s.substr(0,2) != "RG")
			{
				return false;
			}
			if (s.substr(2,1) != "/")
			{
				return false;
			}
			if (s.length == 15)
			{
				if (!checkFormat(s.substr(3,3), "AAA"))
				{
					return false;
				}
				if (s.substr(6,1) != "/")
				{
					return false;
				}
				if (s.substr(7,2) != "TR")
				{
					return false;
				}
				if (s.substr(9,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(10,5),"99999"))
				{
					return false;
				}
			}
			else
			{
				if (!checkFormat(s.substr(3,4), "AAA#"))
				{
					return false;
				}
				if (s.substr(7,1) != "/")
				{
					return false;
				}
				if (s.substr(8,2) != "TR")
				{
					return false;
				}
				if (s.substr(10,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(11,5),"99999"))
				{
					return false;
				}
			}
			break;
		case 5:
			if (s.length < 15)
			{
				return false;
			}
			if (s.substr(0,2) != "NR")
			{
				return false;
			}
			if (s.length <= 16)
			{
				offset = s.length - 15
				if (s.substr(2,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(3,3), "AAA"))
				{
					return false;
				}
				if (s.substr(6 + offset,1) != "/")
				{
					return false;
				}
				if (!isYear(s.substr(7 + offset,2)))
				{
					return false;
				}
				if (s.substr(9 + offset,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(10 + offset,5),"99999"))
				{
					return false;
				}
			}
			else
			{
				offset = s.length - 17
				if (!checkFormat(s.substr(2,2), "99"))
				{
					return false;
				}
				if (s.substr(4,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(5,3), "AAA"))
				{
					return false;
				}
				if (s.substr(8 + offset,1) != "/")
				{
					return false;
				}
				if (!isYear(s.substr(9 + offset,2)))
				{
					return false;
				}
				if (s.substr(11 + offset,1) != "/")
				{
					return false;
				}
				if (!checkFormat(s.substr(12 + offset,5),"99999"))
				{
					return false;
				}
			}
			break;
		default:
			return false;
			break;
	}
	return true;
}
function onRemover(List1,List2)
// List1 Nome do List ou Combo 1
// List2 Nome do List ou Combo 2

{
  var IdList;
  var mnList;
 
  if (List2.selectedIndex == -1)
    return;

  var tam = List2.options.length-1;
  for (var i=tam; i >= 0; i--)
  {
      if (List2.options[i].selected)
      {
         IdList = List2.options[i].value;
         mnList = List2.options[i].text;
 // Inclui a linha no combo.
  
         var objOption = document.createElement("OPTION");
           
         objOption.value = IdList;
         objOption.text  = mnList;
           
         List1.add( objOption );
  
         List2.options[i] = null;
      
      }
  }
  
}  

function onRemoverTodos(List1,List2)
// List1 Nome do List ou Combo 1
// List2 Nome do List ou Combo 2

{
  var IdList;
  var mnList;
 
  var tam = List2.options.length-1;
  for (var i=tam; i >= 0; i--)
  {
         IdList = List2.options[i].value;
         mnList = List2.options[i].text;
 // Inclui a linha no combo.
  
         var objOption = document.createElement("OPTION");
           
         objOption.value = IdList;
         objOption.text  = mnList;
           
         List1.add( objOption );
  
         List2.options[i] = null;      

  }
  
}  

function onAdicionar(List1,List2)
// List1 Nome do List ou Combo 1
// List2 Nome do List ou Combo 2
{

  var IdList;
  var mnList;
 
  if (List1.selectedIndex == -1)
    return;

  var tam = List1.options.length-1;
  for (var i=tam; i >= 0; i--)
  {
      if (List1.options[i].selected)
      {
         IdList = List1.options[i].value;
         mnList = List1.options[i].text;
  
         // Inclui a linha no combo.
  
         var objOption = document.createElement("OPTION");
           
         objOption.value = IdList;
         objOption.text  = mnList;
         
         List2.add( objOption );
  
         List1.options[i] = null;
      
      }
  }
  
}

