RequiredChecker = function() {
	this.check = function(value, required) {
		if (required && (value == null || value.toString() == ""))
			return {isValid:false, error:new ERequiredViolation()}			
		
		return {isValid:true}
	}
}

//----------------

ConstraintsCheckerConstructor = function(typeName, config) {
	var result = null;
	switch (typeName) {
		case 'integer'	: result = new ConstraintsCheckerInteger()	; break;
		case 'boolean'	: result = new ConstraintsCheckerBoolean()	; break;
		case 'date'		: result = new ConstraintsCheckerDate()		; break;
		case 'memo'		: result = new ConstraintsCheckerMemo()		; break;
		case 'real'		: result = new ConstraintsCheckerReal()		; break;		
		case 'set'		: result = new ConstraintsCheckerSet()		; break;
		case 'string'	: result = new ConstraintsCheckerString()	; break;
		case 'time'		: result = new ConstraintsCheckerTime()		; break;
	}

	if (result) 
		for(var item in config) result[item] = config[item];
		
	return result;
}

ConstraintsChecker = function() {
	this.check = function(value) {}
}

ConstraintsCheckerInteger = function(min, max) {
	this.min = min;
	this.max = max;
	
	this.check = function(value) {
		if (this.min && parseInt(value) < this.min) 
			return {isValid:false, error:new EIntegerConstraintsViolation(this.min, this.max, 1)}
			
		if (this.max && parseInt(value) > this.max) 
			return {isValid:false, error:new EIntegerConstraintsViolation(this.min, this.max, 2)}			
		
		return {isValid:true}
	}
}

ConstraintsCheckerString = function(maxLength) {
	this.maxLength = maxLength;
	
	this.check = function(value) {
		if (this.maxLength && value.length > this.maxLength) 
			return {isValid:false, error:new EStringConstraintsViolation(this.maxLength)}
		
		return {isValid:true}
		
	}
}

ConstraintsCheckerReal = function(min, max) {
	this.min = min;
	this.max = max;
		
	this.check = function(value) {
	    value = value.replace(',', '.');
	    
		if (this.min && parseFloat(value) < this.min) 
			return {isValid:false, error:new EFloatConstraintsViolation(this.min, this.max, 1)}
			
		if (this.max && parseFloat(value) > this.max) 
			return {isValid:false, error:new EFloatConstraintsViolation(this.min, this.max, 2)}			
		
		return {isValid:true}
	}
}

ConstraintsCheckerMemo = function(maxLength) {
	this.maxLength = maxLength;
	
	this.check = function(value) {
		if (this.maxLength && value.length > this.maxLength) 
			return {isValid:false, error:new EMemoConstraintsViolation(this.maxLength)}
		
		return {isValid:true}
		
	}
}

ConstraintsCheckerDate = function(min, max) {
	this.min = min;
	this.max = max;

	this.check = function(value) {
		var date = new Date(value);
		if (typeof this.min == "string")  this.min = Date.parse(this.min) ; //new Date(this.min);
		if (typeof this.max == "string")  this.max = Date.parse(this.max) ; //new Date(this.max);
		
		if (this.min && Date.parse(date) <= Date.parse(this.min)) 
			return {isValid:false, error:new EDateConstraintsViolation(this.min, this.max, 1)}
			
		if (this.max && Date.parse(date) >= Date.parse(this.max)) 
			return {isValid:false, error:new EDateConstraintsViolation(this.min, this.max, 2)}			
		
		return {isValid:true}
	}
}

ConstraintsCheckerTime = function() {
	this.check = function(value) {alert('ConstraintsCheckerTime not implemented!');}
}

ConstraintsCheckerSet = function(values, caseSensitive) {
	this.values = values
	this.caseSensitive = caseSensitive;
	
	this.check = function(value) {
		var found = false;
		if (this.values && this.values.length > 0) {
			for(var i = 0; i<this.values.length; i++){
				if (!this.caseSensitive && typeof value == "string") {
					found = (value.toLowerCase() == this.values[i].toString().toLowerCase())
				} else {
					found = (value == this.values[i]);
				}
				
				if (found) return {isValid:true}
			}

			return {isValid:false, error:new ESetConstraintsViolation(this.values, this.caseSensitive)}
		} else {
			return {isValid:true}			
		}
		
	}
}

ConstraintsCheckerBoolean = function(mustValue) {
	this.mustValue = mustValue;
	
	this.check = function(value) {
		if (this.mustValue && this.mustValue != value)
			return {isValid:false, error: new EBooleanConstraintsViolation(this.mustValue)}
			
		return {isValid:true}
	}
}

//----------------

TypeCheckerConstructor = function(typeName) {
	var result = null;
	switch (typeName) {
		case 'integer'	: result = new TypeCheckerInteger()	; break;
		case 'boolean'	: result = new TypeCheckerBoolean()	; break;
		case 'date'		: result = new TypeCheckerDate()		; break;
		case 'memo'		: result = new TypeCheckerMemo()		; break;
		case 'real'		: result = new TypeCheckerReal()		; break;		
		case 'set'		: result = new TypeCheckerSet()		; break;
		case 'string'	: result = new TypeCheckerString()	; break;
		case 'time'		: result = new TypeCheckerTime()		; break;
	}

	return result;
}

TypeChecker = function() {
	this.check = function(value) {}
}

TypeCheckerInteger = function() {
	this.check = function(value) {
		if (parseInt(value) != value) 
			return {isValid:false, error:new EIntegerTypeViolation(value)}

		return {isValid:true}
	}
}

TypeCheckerString = function() {
	this.check = function(value) {
		return {isValid:true}
	}
}

TypeCheckerReal = function() {
	this.check = function(value) {
	    value = value.replace(',', '.');
		if (parseFloat(value) != value) 
			return {isValid:false, error:new ERealTypeViolation(value)}

		return {isValid:true}
	}
}

TypeCheckerMemo = function() {
	this.check = function(value) {
		return {isValid:true}
	}
}

TypeCheckerDate = function() {
	this.check = function(value) {
		var date = new Date(value);

		if (! date.toString() != value) 
			return {isValid:false, error:new EDateConstraintsViolation(value)}

		return {isValid:true}
	}
}

TypeCheckerTime = function() {
	this.check = function(value) {alert('TypeCheckerTime not implemented!');}
}

TypeCheckerSet = function() {
	this.check = function(value) {	
		alert('TypeCheckerSet not implemented!');
	}
}

TypeCheckerBoolean = function() {
	this.check = function(value) {
		if (value != 1 || value != 0 || value.toLowerCase() != 'true' || value.toLowerCase() != 'false' || value.toLowerCase() != 'yes' || value.toLowerCase() != 'no' || value.toLowerCase() != 'y' || value.toLowerCase() != 'n')
			return {isValid:false, error: new EBooleanConstraintsViolation(value)}
			
		return {isValid:true}
	}
}

