
/**
 * Klasa wygenerowana automagicznie. Nie modyfikować ręcznie!
 *
 * @date 2010-08-30+02:00 07:31:20+02:00+02:00
 */
function Validator_pl(_formName)
{
    /**
     * @access private
     */
    var formName = _formName;

    /**
     * @access private
     */
    var hasError = false;

    /**
     * Rejestr validatorów definiowanych przez użytkownika, wykonywanych po walidacji pola
     * @access private
     */
    var postValidatorRegistry = {};

    /**
     * Rejestr validatorów definiowanych przez użytkownika, wykonywanych przed walidacja pola
     * @access private
     */
    var preValidatorRegistry = {};

    /**
     * Funkcja uruchamiana przed walidacją.
     *
     * @access private
     */
    var preExecute = null;

    /**
     * Funkcja uruchamiana po walidacji.
     *
     * @access private
     */
    var postExecute = null;

    /**
     * Mapa gromadząca komunikaty o błędach wraz z kodami tych błedów
     */
    var errorMessages = {};

    /**
     * Mapa gromadząca przetłumaczone komunikaty o błędach
     */
    var errorMessagesI18N =
    {
        
        'DOMAINTOTRANSFER' : null
    };

    
    this.domainToTransfer = null;


    var errorCodes =
    {
        
        'DOMAINTOTRANSFER' : 0x063D01
    };

    this.form = null;

    
    /**
     *
     */
    this.validateDomainToTransfer = function(mixed)
    {
        

		var getFieldPattern =

		
			function()
			{
				return /^(?:(?:(?:[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b][\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b\-]{0,61}[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b]|[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b])|(?:[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7][\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7\-]{0,61}[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7]|[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7])|(?:[\x30-\x39\u05d0-\u05ea][\x30-\x39\u05d0-\u05ea\-]{0,61}[\x30-\x39\u05d0-\u05ea]|[\x30-\x39\u05d0-\u05ea])|(?:[\x30-\x39\u0430-\u045f\u0491-\u04c2][\x30-\x39\u0430-\u045f\u0491-\u04c2\-]{0,61}[\x30-\x39\u0430-\u045f\u0491-\u04c2]|[\x30-\x39\u0430-\u045f\u0491-\u04c2]))\.){1,4}[a-z]{2,4}$/;
			}

		var regexp = new RegExp(getFieldPattern());

	

            if(mixed == null || !regexp.test(mixed))
            {
                
            hasError = true;
            setError('DOMAINTOTRANSFER', errorCodes['DOMAINTOTRANSFER']);

                return false;
            }

            return true;

    }


    /**
     * @access public
     */
    this.setErrorMessage = function(code, message)
    {
        errorMessagesI18N[code] = message;
    }

    function getPostValidator(fieldName)
    {
        return (!postValidatorRegistry[fieldName] ? null : postValidatorRegistry[fieldName]);
    }

    function getPreValidator(fieldName)
    {
        return (!preValidatorRegistry[fieldName] ? null : preValidatorRegistry[fieldName]);
    }

    this.registerPostValidator = function(fieldName, functionName)
    {
        postValidatorRegistry[fieldName] = functionName;
    }

    this.registerOnChangeEvent = function(fieldName, functionName)
    {
        if(this.form.elements[fieldName].type == undefined && this.form.elements[fieldName].length != typeof undefined)
        {
            for(var i = 0; i < this.form.elements[fieldName].length; i++)
            {
                if(this.form.elements[fieldName][i].type == 'radio')
                {
                    this.form.elements[fieldName][i].onclick = functionName;
                }
            }
        } else if(this.form.elements[fieldName].type == 'checkbox') {

          this.form.elements[fieldName].onclick = functionName;

        } else {

          this.form.elements[fieldName].onchange = functionName;
        }
    }

    this.registerPreValidator = function(fieldName, functionName)
    {
        preValidatorRegistry[fieldName] = functionName;
    }

    this.registerPostExecute = function(functionName)
    {
        postExecute = functionName;
    }

    this.registerPreExecute = function(functionName)
    {
        preExecute = functionName;
    }

    function validateHelper(validateFunctionName, field)
    {
       var preValidate    = field.prevalidator;
       var value          = field.value;
       var postValidate   = field.postvalidator;
       return (  ( preValidate != null && preValidate(this, value) ) || true ) && (validateFunctionName(value) && ( ( postValidate != null && postValidate(this, value) ) || true ) );
    }

    function setError(code, msg)
    {
        errorMessages[code] = msg;
    }

    function hasErrors()
    {
        return hasError;
    }

    /**
     * @access private
     */
    function checkControlSum(str, weights, modulo, allow_high)
    {
        str = str.replace(/[^\d]/g,"");
        if(!str.length) return false;

        var nsize = str.length;
        var j = 0, sum = 0, control = 0;
        var csum = str.substring(nsize - 1);

        for (var i = 0; i < nsize - 1; i++)
        {
                j = parseInt(str.charAt(i));
                sum += j * weights[i];
        }
        if(sum == 0 || str.length < 9)
        {
            return false;
        }
        control = sum % modulo;

        if(allow_high == false) {
                control = 10 - control;
        }

        if (control == 10) {
            control = 0;
        }
        return control == csum;
    }

    /**
     * @access private
     */
    function trim(str, trimValue)
    {
    	var arrayRegex = trimValue.split(',');
   	
   	
    	for(i = 0; i < arrayRegex.length; i++)
    	{
			if(arrayRegex[i] == ' ')
			{
				str = str.replace(/^\s*/, '').replace(/\s*$/, '');   
			}
			else
			{
				find = '/' + arrayRegex[i] + '/g';
				str = str.replace(eval(find),'');
			}	
 		}
		return str;
    }

    /**
     * @access private
     */
    function normalizeValueHelper(field, trimValue)
    {
        var str = trimValue ? trim(field.value, trimValue) : field.value;
        
        field.value = str;
        
        if(str == '')
        {
            return getNullPattern();
        }
        return getNotNullObject(field.name, str);
    }

    /**
     * @access private
     */
    function getNotNullObject(name, value)
    {
        return {'prevalidator' : getPreValidator(name), 'value' : value, 'postvalidator' : getPostValidator(name)};
    }

    /**
     * @access private
     */
    function normalizeValue(field, trimValue)
    {
        if(typeof field == typeof undefined && field.disabled)
        {
            return getNullPattern();
        }
        switch(field.type)
        {

            case 'text' :
                return normalizeValueHelper(field, trimValue);
            break;

            case 'textarea' :
                return normalizeValueHelper(field, trimValue);
            break;

            case 'password' :
                return normalizeValueHelper(field);
            break;

            case 'hidden' :
                return normalizeValueHelper(field);
            break;

            case 'checkbox' :
                if(!field.checked)
                {
                    return getNullPattern();
                }
                return getNotNullObject(field.name, field.value);
            break;

            case 'select-one' :

                if(field.options[field.selectedIndex] == undefined || field.options[field.selectedIndex].value == 0 || field.options[field.selectedIndex].value == '')
                {
                    return getNullPattern();
                }
                return getNotNullObject(field.name, field.options[field.selectedIndex].value);
            break;

            default:
                // gdy mamy doczynienia z radiobuttonami
                if(field.length != typeof undefined)
                {
                    for(var i = 0; i < field.length; i++)
                    {
                        if(field[i].type == 'radio' && field[i].checked)
                        {
                            return getNotNullObject(field[i].name, field[i].value);
                        }
                    }
                }

            break;

        }
        return getNullPattern();
    }

    /**
     * @access private
     */
    function getNullPattern()
    {
        return {'prevalidator' :null, 'value' : null, 'postvalidator' : null};
    }

    function getErrors()
    {
        var str = '';

        for(var key in errorMessages)
        {
            if(errorMessages[key] != null)
            {
                str += errorMessagesI18N[key] + '\n';
            }
            delete errorMessages[key];
        }

        return str;
    }

    /**
     * @access public
     */
    this.execute = function(data)
    {
        this.normalize();

        if(preExecute != null && !preExecute(this))
        {
            if(hasErrors()) alert(getErrors());
            hasError = false;
            return false;
        }

        
        validateHelper(this.validateDomainToTransfer, this.domainToTransfer);


        if(postExecute != null && !hasErrors() && !postExecute(this))
        {
            if(hasErrors()) alert(getErrors());
            hasError = false;
            return false;
        }

        if(hasErrors()) alert(getErrors());
        var ret = !hasErrors();
        hasError = false
        return ret;
    }

    /**
     * @access private
     */
    this.normalize = function()
    {
        
    this.domainToTransfer = normalizeValue(this.form.elements['domainToTransfer']);

    }

    /**
     * @access public
     */
    this.callTriggers = function()
    {
        var validator = eval(this.form.elements['componentInstance'].value);
        validator.normalize();
        
        validator.normalize();
    }

    /**
     * @access public
     */
    this.initialize = function()
    {
        if(this.form == null) this.form = document.forms[formName];

        this.normalize();

        

        this.normalize();
    }
}

Date.prototype.after = function(p_date) {
    return this.valueOf() > p_date.valueOf();
}

Date.prototype.before = function(p_date) {
    return this.valueOf() < p_date.valueOf();
}

Date.prototype.equal = function(p_date) {
    return this.valueOf() == p_date.valueOf();
}


function NetartPeselAnalyzer(p_PESEL, p_now) {

  this.PESEL = null;

  this.now = null;

  this.minAllowAge = 13;

  this.maxAllowAge = 100;

  this.testedBornDate = null;

  this.minBornDate = null;

  this.maxBornDate = null;

  this.isAdult = function() {
    var bornDate = this.testedBornDate.getCalBornDate();
    if (bornDate == null) {
      return false;
    }

    if (bornDate.after(this.now)) {
      return false;
    }

    var enoughYoung = this.testedBornDate.after(this.minBornDate);
    var enoughOld = this.testedBornDate.before(this.maxBornDate);

    return enoughYoung && enoughOld;
  }

  this.getMinAllowBornDate = function() {
    var minAllowBornDate = null;
    if (this.maxAllowAge != null) {
      minAllowBornDate = cloneDate(this.now);
      minAllowBornDate.setFullYear(minAllowBornDate.getFullYear() - this.maxAllowAge);
      minAllowBornDate = this.truncateToDay(minAllowBornDate);
    }
    return minAllowBornDate;
  }

  this.getMaxAllowBornDate = function() {
    var maxAllowBornDate = null;
    if (this.minAllowAge != null) {
      maxAllowBornDate = cloneDate(this.now);
      maxAllowBornDate.setFullYear(maxAllowBornDate.getFullYear() - this.minAllowAge);
      maxAllowBornDate = this.truncateToDay(maxAllowBornDate);
    }
    return maxAllowBornDate;
  }

  this.isValid = function() {
    var weights = [1,3,7,9,1,3,7,9,1,3];
    if (this.PESEL == null || !checkControlSum(this.PESEL, weights, 10, false)) {
      return false;
    }
    return this.isAdult();
  }

  this.truncateToDay = function(date) {
    var pB = cloneDate(date);
    pB.setHours(0);
    pB.setMinutes(0);
    pB.setSeconds(0);
    return pB;
  }

  this.getPESEL = function() {
    return PESEL;
  }

  this.getNow = function() {
    return now;
  }

  this.setNow = function(now) {
    this.now = now;
  }

  this.getMinAllowAge = function() {
    return minAllowAge;
  }

  this.getMaxAllowAge = function() {
    return maxAllowAge;
  }

    function checkControlSum(str, weights, modulo, allow_high)
    {
        str = str.replace(/[^\d]/g,"");
        if(!str.length) return false;

        var nsize = str.length;
        var j = 0, sum = 0, control = 0;
        var csum = str.substring(nsize - 1);

        for (var i = 0; i < nsize - 1; i++)
        {
                j = parseInt(str.charAt(i));
                sum += j * weights[i];
        }
        if(sum == 0 || str.length < 9)
        {
            return false;
        }
        control = sum % modulo;

        if(allow_high == false) {
                control = 10 - control;
        }

        if (control == 10) {
            control = 0;
        }
        return control == csum;
    }


    this.PESEL = p_PESEL.replace("\\D*", "");
    this.now = p_now;

    this.testedBornDate = new BornDate(peselToCalDate(this.PESEL));
    this.minBornDate = new BornDate(this.getMinAllowBornDate());
    this.maxBornDate = new BornDate(this.getMaxAllowBornDate());
};


function BornDate(p_date) {

    var calBornDate = null;

  this.after = function(p_bornDate) {
    var after = false;
    var checkCalendarDate = p_bornDate.getCalBornDate();
    if (this.calBornDate != null && checkCalendarDate != null) {
      after = this.calBornDate.after(checkCalendarDate);
    }
    return after;
  }

  this.before = function(p_bornDate) {
    var before= false;
    var checkCalendarDate = p_bornDate.getCalBornDate();
    if (this.calBornDate != null && checkCalendarDate != null) {
      before = this.calBornDate.before(checkCalendarDate);
    }
    return before;
  }

  this.getCalBornDate = function() {
    return this.calBornDate;
  }

  this.setCalBornDate = function(calBornDate) {
    this.calBornDate = calBornDate;
  }

  this.calBornDate = p_date;
}


function peselToCalDate(p_pesel) {

    var calBornDate = null;
    var dayDigit = p_pesel.substring(4, 6);
    var mountDigit = p_pesel.substring(2, 4);
    var yearDigit = p_pesel.substring(0, 2);
    var day = parseInt(dayDigit);
    var month = -1;
    var monthCode = parseInt(mountDigit);
    var year = -1;

    if (monthCode>=1 && monthCode<=12) {
      month = monthCode;
      yearDigit = "19" + yearDigit;
    } else if (monthCode>=81 && monthCode<=92) {
      month = monthCode - 80;
      yearDigit = "18" + yearDigit;
    } else if (monthCode>=21 && monthCode<=32) {
      month = monthCode - 20;
      yearDigit = "20" + yearDigit;
    } else if (monthCode>=41 && monthCode<=52) {
      month = monthCode - 40;
      yearDigit = "21" + yearDigit;
    } else if (monthCode>=61 && monthCode<=72) {
      month = monthCode - 60;
      yearDigit = "22" + yearDigit;
    }

	// Date object's months start from 0
    month--;

    var sMount = month.toString();

    if (sMount.length == 1) {
      sMount = "0" + sMount;
    }

    year = parseInt(yearDigit);
    return new Date(year, sMount, dayDigit);
}


function cloneDate(p_date) {

    return new Date(p_date);
}


