/***************************************************	

	fValidate
	Copyright (c) 2000-2003
	by Peter Bailey
	www.peterbailey.net/fValidate/

	fValidate.international.js

	Included Validators
	-------------------
	cazip
	ukpost
	germanpost
	swisspost

	This file is only part of a larger validation
	library	and will not function autonomously.

	Created at a tab-spacing of four (4)

****************************************************/

fValidate.prototype.cazip = function()
{
	var elem = this.elem;
	if ( this.typeMismatch( 'text' ) ) return;
	elem.value = elem.value.toUpperCase();
	if ( !( /^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/.test( elem.value ) ) )
	{
		this.throwError();
	}
}
fValidate.prototype.capost = fValidate.prototype.cazip;

fValidate.prototype.ukpost = function()
{
	var elem = this.elem;
	if ( this.typeMismatch( 'text' ) ) return;
	elem.value = elem.value.toUpperCase();
	if ( !( /^[A-Z]{1,2}\d[\dA-Z] ?\d[A-Z]{2}$/.test( elem.value ) ) )
	{
		this.throwError();
	}
}

fValidate.prototype.germanpost = function()
{
	var elem = this.elem;
	if ( this.typeMismatch( 'text' ) ) return;
	elem.value = elem.value.toUpperCase();
	if ( !( /^(?:CH\-)\d{4}$/.test( elem.value ) ) )
	{
		this.throwError();
	}
}

fValidate.prototype.swisspost = function()
{
	var elem = this.elem;
	if ( this.typeMismatch( 'text' ) ) return;
	elem.value = elem.value.toUpperCase();
	if ( !( /^(?:D\-)\d{5}$/.test( this.elem.value ) ) )
	{
		this.throwError();
	}
}

fValidate.prototype.brzip = function()
{
	var elem = this.elem;
	if ( this.typeMismatch( 'text' ) ) return;
	elem.value = elem.value.toUpperCase();
	if ( !( /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test( elem.value ) ) )
	{
		this.throwError();
	}
}
fValidate.prototype.brpost = fValidate.prototype.brzip;


fValidate.prototype.cpf = function()
{
	var elem = this.elem;
	var numeros, digitos, soma, i, resultado, digitos_iguais; 
  digitos_iguais = 1; 
  var valor = elem.value;
  if (valor.length < 11) {
    this.throwError(); 
  }
	else {
	  for (i = 0; i < valor.length - 1; i++) 
	    if (valor.charAt(i) != valor.charAt(i + 1)) 
	    { 
	      digitos_iguais = 0; 
	      break; 
	    } 
	  if (!digitos_iguais) 
	  { 
	    numeros = valor.substring(0,9); 
	    digitos = valor.substring(9); 
	    soma = 0; 
	    for (i = 10; i > 1; i--) 
	          soma += numeros.charAt(10 - i) * i; 
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
	    if (resultado != digitos.charAt(0)) 
	      this.throwError(); 
	    numeros = valor.substring(0,10); 
	    soma = 0; 
	    for (i = 11; i > 1; i--) 
	      soma += numeros.charAt(11 - i) * i; 
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
	    if (resultado != digitos.charAt(1)) 
	      this.throwError(); 
	    return; 
	  } 
	  else 
	    this.throwError();
	}
}

fValidate.prototype.cnpj = function()
{
	var elem = this.elem;
  var valor = elem.value;
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais; 
  digitos_iguais = 1; 
  if (valor.length < 14 && valor.length < 15) {
    this.throwError(); 
  }
	else {
	  for (i = 0; i < valor.length - 1; i++) 
      if (valor.charAt(i) != valor.charAt(i + 1)) 
      { 
		    digitos_iguais = 0; 
		    break; 
      } 
	  if (!digitos_iguais) 
    { 
	    tamanho = valor.length - 2 
	    numeros = valor.substring(0,tamanho); 
	    digitos = valor.substring(tamanho); 
	    soma = 0; 
	    pos = tamanho - 7; 
	    for (i = tamanho; i >= 1; i--) 
      { 
		    soma += numeros.charAt(tamanho - i) * pos--; 
		    if (pos < 2) 
          pos = 9; 
      } 
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
	    if (resultado != digitos.charAt(0)) 
        this.throwError(); 
	    tamanho = tamanho + 1; 
	    numeros = valor.substring(0,tamanho); 
	    soma = 0; 
	    pos = tamanho - 7; 
	    for (i = tamanho; i >= 1; i--) 
      { 
	      soma += numeros.charAt(tamanho - i) * pos--; 
	      if (pos < 2) 
          pos = 9; 
      } 
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
	    if (resultado != digitos.charAt(1)) 
        this.throwError(); 
	    return true; 
    } 
	  else 
      this.throwError(); 
	}	        
}

//	EOF