// JavaScript Document

Forms = {} ;

// Errors
Forms.error = new Array() ;
Forms.error.add_error = function(error) {
	Forms.error[Forms.error.length] = error ;
}
Forms.error.add_error('required') ;
Forms.error.add_error('too_long') ;
Forms.error.add_error('too_short') ;
Forms.error.add_error('invalid') ;
Forms.error.add_error('invalid_password') ;
Forms.error.add_error('password_mismatch') ;
Forms.error.add_error('wrong_pseudo') ;
Forms.error.add_error('group_taken') ;
Forms.error.add_error('already_exist') ;
Forms.error.add_error('unknown') ;

// Validate functions
Forms.validation = {} ;
Forms.validation.email = function(word) {
	return (/^\w[\w+.-]*@[\w\-]+.\w[\w+.-]*\w$/).test(word) ;
}
Forms.validation.url = function(word) {
	return (/^[\w\d][\w\d\+\.\-\/]*[\w\d]$/).test(word) ;
}
Forms.validation.age = function(word) {
	return (/[123456789]\d?\d?/).test(word) ;
}
Forms.validation.name = function(word) {
	return (/^[a-zA-Z][a-zA-Z\s-]*[a-zA-Z]$/).test(word) ;
}
Forms.validation.french_name = function(word) {
	return (/^[a-zéèêïîàâA-Z0-9\.\;\:\!\'][0-9a-zéèêïîàâA-Z\s-\.\;\:\!\']*[0-9a-zéèêïîàâA-Z\.\;\:\!\']$/).test(word) ;
}
Forms.validation.french_text = function(word) {
	return (/^[a-zA-Z0-9\u0020\u0021\u0022\u0027\u0028\u0029\u002C\u002D\u002E\u002F\u003A\u003B\u003F\u0040\u005B\u005C\u005D\u005E\u005F\u0060\u007B\u007C\u007D\u00A8\u00AB\u00B0\u00B4\u00B8\u00BB\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D2\u00D3\u00D4\u00D6\u00D9\u00DA\u00DB\u00DC\u00E0\u00E1\u00E2\u00E4\u00E7\u00E8\u00E9\u00EA\u00EB\u00EE\u00EF\u00F4\u00F6\u00F9\u00FA\u00FB\u00FC\u0152\u0153][a-zA-Z0-9\u0020\u0021\u0022\u0027\u0028\u0029\u002C\u002D\u002E\u002F\u003A\u003B\u003F\u0040\u005B\u005C\u005D\u005E\u005F\u0060\u007B\u007C\u007D\u00A8\u00AB\u00B0\u00B4\u00B8\u00BB\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D2\u00D3\u00D4\u00D6\u00D9\u00DA\u00DB\u00DC\u00E0\u00E1\u00E2\u00E4\u00E7\u00E8\u00E9\u00EA\u00EB\u00EE\u00EF\u00F4\u00F6\u00F9\u00FA\u00FB\u00FC\u0152\u0153]*[a-zA-Z0-9\u0020\u0021\u0022\u0027\u0028\u0029\u002C\u002D\u002E\u002F\u003A\u003B\u003F\u0040\u005B\u005C\u005D\u005E\u005F\u0060\u007B\u007C\u007D\u00A8\u00AB\u00B0\u00B4\u00B8\u00BB\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D2\u00D3\u00D4\u00D6\u00D9\u00DA\u00DB\u00DC\u00E0\u00E1\u00E2\u00E4\u00E7\u00E8\u00E9\u00EA\u00EB\u00EE\u00EF\u00F4\u00F6\u00F9\u00FA\u00FB\u00FC\u0152\u0153]$/).test(word) ;
}
Forms.validation.game_name = function(word) {
	return (/^[0-9a-zA-Zéèêïîàâ][0-9a-zéèêïîàâA\'-Z\s-]*[0-9a-zéèêïîàâA-Z]$/).test(word) ;
}
Forms.validation.code = function(word) {
	return (/^[\w\d][\w\d]*[\w\d]$/).test(word) ;
}
Forms.validation.pseudo = function(word) {
	return (/^[\w\d_\-\(\)\[\]\.\!\\\|\/\@éêèàâîïù\<\>\^][\s\w\d_\-\(\)\[\]\.\!\\\|\/\@éêèàâîïù\<\>\^]*[\w\d_\-\(\)\[\]\.\!\\\|\/\@éêèàâîïù\<\>\^]$/).test(word)
}

// Form
Forms.form = function(callback, form) {
	this.elements = new Array() ;
	this.form = form ;
	this.success = callback ;
}

Forms.form.prototype.validate = function() {
	validated = true ;
	for (i in this.elements) {
		this.elements[i].validate() ;	
	}
	for (i = 0 ; i < this.elements.length ; i++) {
		if (!this.elements[i].validated) {
			validated = false ;
			break ;
		}
	}
	if (validated) {
		this.success() ;	
	}
}

Forms.form.prototype.get_form = function() {
	return this.form ;	
}

Forms.form.prototype.get_element = function(id) {
	for (i in this.elements) {
		if (this.elements[i].span_id == id) {
			return this.elements[i] ;	
		}
	}
}

Forms.form.prototype.add_element = function(element) {
	this.elements.push(element) ;	
}

// Base
Forms.base = function(span_id, options) {
	this.error = 0 ;
	this.span_id = span_id ;
	this.validated = false ;
	this.element = null ;
	
	this.validations = new Array() ;
}

Forms.base.prototype.add_validation = function(valid) {
	this.validations.push(valid) ;	
}

Forms.base.prototype.validate = function() {
	this.error = 0 ;
	for (i = 0 ; i < this.validations.length ; i++) {
		this.validations[i](this.element, this) ;
	}
	
	this.show_state() ;
}

Forms.base.prototype.add_error = function(error) {
	flag = 1 ;
	for (i = 0 ; i < Forms.error.length ; i++) {
		if (Forms.error[i] == error) {
			this.error |= flag ;
			break ;
		}
		flag *= 2 ;
	}
	return this ;
}

Forms.base.prototype.show_state = function() {
	flag = 1 ;
	for (i = 0 ; i < Forms.error.length ; i++) {
		if (this.error & flag) {
			$('#'+this.span_id+' .'+Forms.error[i]).removeClass('ui-helper-hidden') ;	
		}
		else {
			$('#'+this.span_id+' .'+Forms.error[i]).addClass('ui-helper-hidden') ;	
		}
		flag *=2 ;
	}
	
	if (this.error != 0) {
		$('#'+this.span_id+' input').removeClass('ui-state-highlight').addClass('ui-state-error');
		$('#'+this.span_id+' select').removeClass('ui-state-highlight').addClass('ui-state-error');
		$('#'+this.span_id+' textarea').removeClass('ui-state-highlight').addClass('ui-state-error');
		this.validated = false ;
	}
	else {
		$('#'+this.span_id+' input').removeClass('ui-state-error').addClass('ui-state-highlight');
		$('#'+this.span_id+' select').removeClass('ui-state-error').addClass('ui-state-highlight');
		$('#'+this.span_id+' textarea').removeClass('ui-state-error').addClass('ui-state-highlight');
		this.validated = true ;
	}
}

// TextField
Forms.textField = function (span_id, options) {
	//base constructor
	this.error = 0 ;
	this.span_id = span_id ;
	this.validated = false ;
	this.element = null ;
	var required = (options.required == null) ? true : options.required ;
	
	this.validations = new Array() ;
	// end
	
	this.element = $('#'+span_id+' input').get()[0] ;
	
	this.min_size = (options['min_size']==null) ? 0 : options['min_size'] ;
	this.max_size = (options['max_size']==null) ? 10000 : options['max_size'] ;
	if (required) {this.add_validation(Forms.textField.validate_required) ;}
	this.add_validation(Forms.textField.validate_size) ;
}

Forms.textField.prototype = Forms.base.prototype ;

Forms.textField.validate_required = function(input, js_input) {
	if (input.value.length == 0) {
		js_input.add_error('required') ;	
	}
}

Forms.textField.validate_size = function(input, js_input) {
	if (js_input.min_size != 0 && input.value.length < js_input.min_size) {
		js_input.add_error('too_short') ;	
	}
	if (js_input.max_size != 10000 && input.value.length > js_input.max_size) {
		js_input.add_error('too_long') ;	
	}
}

// Select
Forms.select = function(span_id, options) {
	//base constructor
	this.error = 0 ;
	this.span_id = span_id ;
	this.validated = false ;
	this.element = null ;
	
	this.validations = new Array() ;
	// end
	
	this.element = $('#'+span_id+' select').get()[0] ;
	this.invalid_item = (options['invalid_item'] == null)? null : options['invalid_item'] ;
	this.add_validation(Forms.select.validate_item) ;
}

Forms.select.prototype = Forms.base.prototype ;

Forms.select.validate_item = function(input, js_input) {
	if (input.options[input.selectedIndex].value == js_input.invalid_item) {
		js_input.add_error('invalid') ;	
	}
}

// Checkbox
Forms.checkbox = function(span_id, options) {
	if (typeof options == 'undefined') { options = {} ;}
	//base constructor
	this.error = 0 ;
	this.span_id = span_id ;
	this.validated = false ;
	this.element = null ;
	var required = (typeof options.required == 'undefined') ? true : options.required ;
	
	this.validations = new Array() ;
	// end
	
	this.element = $('#'+span_id+' input').get()[0] ;
	if (required) {
		this.add_validation(Forms.checkbox.validate_item) ;
	}
}

Forms.checkbox.prototype = Forms.base.prototype ;

Forms.checkbox.validate_item = function(input, js_input) {
	if (input.checked == false) {
		js_input.add_error('required') ;
	}
}