var FormValidator = Class.create({
	
	initialize: function() { },
	
	addElementToCheck: function(aContentElement, aValidation) {
		var anElement = new Array();
		anElement.element = aContentElement;
		anElement.validation = aValidation;
		this.elementToCheck.push(anElement);
	},
	
	checkElement: function(aContentElement, aValidation) {
		aContentElement = aContentElement.replace(/\'/g,' ');
		return eval('this.' + aValidation+ '(\'' + aContentElement + '\');');
	},
	
	_checkEmpty: function(aValue) {
		if (aValue == null || aValue.length == 0) return true;
		return false;
	},
	
	_checkText: function(aValue) {
		if (aValue == null) return false;
		if (aValue.length == 0) return false;
		//if (this._checkNumber(aValue)) return false;
		if (this._isWhiteSpaces(aValue)) return false;
		return true;
	},
	
	_checkNumber: function(aValue) {
		if (isNaN(aValue)) return false;
		return true;
	 },
	
	_checkPort: function(aValue) {
		if (aValue == null) return false;
		if (aValue.length == 0) return false;
		if (!this._checkNumber(aValue)) return false;
		if (aValue < 1 || aValue > 65535) return false;
		return true;
	},
	
	_checkIpAddress: function(aValue) {
		var reg = /^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$/;
		if (reg.exec(aValue) == null) return false;
		else {
			var tab = aValue.split('.');
			var error = 0;
			for(var i = 0; i < tab.length; i++){
				if ((tab[i] - '0') > 255) error++;
			}
			if(error > 0) return false;
		}
		return true;
	},
	
	_checkHostName: function(aValue){
		var labelRegex = /^[a-z,0-9]$|^[a-z,0-9][a-z,0-9,\-,_]{0,61}[a-z,0-9]$/i;
		if (aValue.length > 255){
			return false;
		} else {
			var labels = aValue.split('.');
			for ( var i =0; i< labels.length;i++){
				if (labelRegex.exec(labels[i]) == null) return false;
			}
		}
		return true;
	},
	
	_checkDateCalendar: function(aValue) {
		if (!this._checkText(aValue)) return false;
		if (aValue.length != 10 || aValue.charAt(2) != '.' || aValue.charAt(5) != '.') return false;
		var splits = aValue.split('.');
		if (splits[0].length == 2 && splits[0].charAt(0) == '0') splits[0] = splits[0].charAt(1);
		if (splits[1].length == 2 && splits[0].charAt(0) == '0') splits[0] = splits[0].charAt(1);
		if (parseInt(splits[0]) > 31 && parseInt(splits[0]) < 1) return false;
		if (parseInt(splits[1]) > 12 && parseInt(splits[1]) < 0) return false;
		if (parseInt(splits[2]) > 3000 && parseInt(splits[2]) < 1950) return false;
		return true;
	},
	
	_isWhiteSpaces: function(aValue) {
		if (aValue.replace(/^\s+|\s+$/g, '').length == 0) return true;
		return false;
	}
	
});

FormValidator.EMPTY = '_checkEmpty';
FormValidator.TEXT = '_checkText';
FormValidator.NUMBER = '_checkNumber';
FormValidator.PORT = '_checkPort';
FormValidator.IP_ADDRESS = '_checkIpAddress';
FormValidator.HOST_NAME = '_checkHostName';
FormValidator.DATE_CALENDAR = '_checkDateCalendar';