<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Jesus de Oliveira Saad <jesus@evasta.com.br>                |
// +----------------------------------------------------------------------+
//
// $Id: Brazilian.php,v 1.6 2003/11/03 16:08:24 avb Exp $

require_once 'HTML/QuickForm/Rule.php';
require_once
$_SERVER['DOCUMENT_ROOT'].'/Jesus/Testes/PEAR/Validate/ptBR.php';

/**
* Data validation class for Brazil
* @version     1.0
*/
class HTML_QuickForm_Rule_Brazilian extends HTML_QuickForm_Rule
{
    
/**
     * Validates a value using an code function of Brazil
     *
     * @param     string    $value      Value to be checked
     * @param     mixed     $options    String name functions, array for phoneNumber
     * @access    public
     * @return    boolean   true if value is valid
     */
    
function validate($value, $opcao)
    {
        if (
is_array($opcao))
        {
            if (!isset(
$opcao['function']))
                return
false;
            
$exp = $opcao['function'];
        }
        else
            
$exp = $opcao;
        switch (
$exp)
        {
            case
'cpf':
                return
Validate_ptBR::cpf($value);
            case
'cnpj':
                return
Validate_ptBR::cnpj($value);
            case
'region':
                return
Validate_ptBR::region($value);
            case
'postalCode':
                return
Validate_ptBR::postalCode($value);
            case
'phoneNumber':
                if (isset(
$opcao['requireAreaCode']))
                    return
Validate_ptBR::phoneNumber($value , $opcao['requireAreaCode']);
                   return
Validate_ptBR::phoneNumber($value , false);
            case
'default':
                return
false;
        }
    }
// end func validate


    
function getValidationScript($options)
    {
        if (
is_array($options))
        {
            if (!isset(
$options['function']))
                return
false;
            if (isset(
$options['requireAreaCode']))
                return array(
$this->_getJSFunctions($options['function'] , $options['requireAreaCode']) , "{jsVar} != '' && !brazilian_".$options['function']."({jsVar})");
            return array(
$this->_getJSFunctions($options['function']) , "{jsVar} != '' && !brazilian_".$options['function']."({jsVar})");
        }
           return array(
$this->_getJSFunctions($options), "{jsVar} != '' && !brazilian_$options({jsVar})");
    }
// end func getValidationScript
    
function _getJSFunctions( $function_name , $requireAreaCode = false)
    {
        switch (
$function_name)
        {
            case
'cpf':
                return
'function brazilian_cpf( cpf )
{
    if(cpf.length != 11)
        return false;
        
    var i;
      var c = cpf.substr(0,9);
      var dv = cpf.substr(9,2); //digito verificador
    var d1 = 0;
  
    for (i = 0; i < 9; i++)
        d1 += c.charAt(i)*(10-i);
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" ||
        cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" ||
        cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" ||
        cpf == "99999999999")
        return false;
        
    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;
}'
;
            case
'cnpj':
                return
'function brazilian_cnpj( CNPJ ) {

    if (CNPJ.length != 14)
        return false;
    if (CNPJ == "00000000000000")
        return false;
    var nonNumbers = /\D/;
    if (nonNumbers.test(CNPJ))
        return false;
    var a = [];
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
    for (i=0; i<12; i++){
        a[i] = CNPJ.charAt(i);
        b += a[i] * c[i+1];
    }
    if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
    b = 0;
    for (y=0; y<13; y++) {
        b += (a[y] * c[y]);
    }
    if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
    if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
        return false;
    return true;
}'
;
            case
'region':
                return
"function brazilian_region( region )
{
    var test = new String(region);
    switch (test.toUpperCase())
    {
        case 'AC':
        case 'AP':
        case 'AL':
        case 'AM':
        case 'BA':
        case 'CE':
        case 'DF':
        case 'ES':
        case 'GO':
        case 'MA':
        case 'MT':
        case 'MS':
        case 'MG':
        case 'PA':
        case 'PB':
        case 'PR':
        case 'PE':
        case 'PI':
        case 'RJ':
        case 'RN':
        case 'RS':
        case 'RO':
        case 'RR':
        case 'SC':
        case 'SP':
        case 'SE':
        case 'TO':
            return true;
    }
    return false;
}"
;
            case
'postalCode':
                return
"function brazilian_postalCode(postalCode)
{
    var ereg = /(^[0-9]{5})[- ]?([0-9]{3})$/;
    return ereg.test(postalCode);
}"
;
            case
'phoneNumber':
                if(
$requireAreaCode)
                    return
'function brazilian_phoneNumber( number )
{
    var ereg =    /^(\()?[1-9]{2}(?(1)\))[\- ]?(\d{4})[\- ]?(\d{4})$/;
    return ereg.test(number);
}'
;
                return
'function brazilian_phoneNumber( number )
{
    var ereg = /^(\d{4})[- ]?(\d{4})$/;
    return ereg.test(number);
}'
;
           case
'default':
                return
false;
        }
    }

}
// end class HTML_QuickForm_Rule_Brazilian
?>