/*
 *  Form
 */
function Form()
{
	this.controls = new Array();

	this.addControl = function(control)
	{
		this.controls.push(control);
	}

	this.check = function()
	{
		var res = true;

		for (i = 0; i < this.controls.length; i++)
			this.controls[i].displayError(false);

		for (i = 0; i < this.controls.length; i++)
		{
			if (this.controls[i].errorBox.style.display == 'none')
				if (!this.controls[i].check())
					res = false;
		}

		return res;
	}
}

/*
 *  Control
 */
function Control(id, msg)
{
	this.id = id;
	this.msg = msg;

	this.field = document.getElementById(this.id);
	this.errorBox = document.getElementById(id + '_error');

	if (this.field)
		this.baseClass = this.field.className;

	this.check = function()
	{
		return true;
	}

	this.displayError = function(value)
	{
		if (value)
		{
			this.field.className = this.baseClass + ' error';

			this.errorBox.innerHTML = msg;
			this.errorBox.style.display = 'block';
		}
		else
		{
			this.field.className = this.baseClass;

			this.errorBox.style.display = 'none';
		}
	}

	this.getValue = function()
	{
		return this.field.value;
	}
}

/*
 *  ControlNotEmpty
 */
//ControlNotEmpty.prototype = new Control();

function ControlNotEmpty(id)
{
	this.parent = Control;
	this.parent(id, 'Champ obligatoire');

	this.check = function()
	{
		if (this.getValue() == '')
		{
			this.displayError(true);
			return false;
		}

		this.displayError(false);
		return true;
	}
}

/*
 *  ControlNumeric
 */

function ControlNumeric(id)
{
	this.parent = Control;
	this.parent(id, 'Valeur numérique attendue');

	this.check = function()
	{
		if (isNaN(this.getValue()))
		{
			this.displayError(true);
			return false;
		}

		this.displayError(false);
		return true;
	}
}

/*
 *  ControlInteger
 */

function ControlInteger(id)
{
	this.parent = Control;
	this.parent(id, 'Valeur numérique entière attendue');

	this.check = function()
	{
		if (isNaN(this.getValue()) || isNaN(Math.round(this.getValue() * 100) / 100))
		{
			this.displayError(true);
			return false;
		}

		this.displayError(false);
		return true;
	}
}

/*
 *  ControlValidDate
 */

function ControlValidDate(id)
{
	this.parent = Control;
	this.parent(id, 'Date incorrecte');

	this.check = function()
	{
		var day = this.getValue().substr(0, 2);
		var month = this.getValue().substr(3, 2);
		var year =  this.getValue().substr(6, 4);

		if (day != '' ||
			month != '' ||
			year != '')
		{
			month = month - 1;

			date = new Date(year, month, day);

			var annee = date.getYear();

			if ((Math.abs(annee) + "").length < 4)
				annee = annee + 1900;

			if (day != date.getDate() ||
				month != date.getMonth() ||
				year != annee)
			{
				this.displayError(true);
				return false;
			}
		}

		this.displayError(false);
		return true;
	}
}

function ControlDateAntNow(id)
{
	this.parent = Control;
	this.parent(id, 'La date doit être antérieure à la date du jour');

	this.check = function()
	{
		var day = this.getValue().substr(0, 2);
		var month = this.getValue().substr(3, 2);
		var year =  this.getValue().substr(6, 4);

		if (day != '' ||
			month != '' ||
			year != '')
		{
			month = month - 1;

			date1 = new Date(year, month, day);
			date2 = new Date();

			if (date1 > date2)
			{
				this.displayError(true);
				return false;
			}
		}

		this.displayError(false);
		return true;
	}
}

/*
 *  ControlValidEmail
 */

function ControlValidEmail(id)
{
	this.parent = Control;
	this.parent(id, 'Format de l\'adresse incorrect');

	this.check = function()
	{
		if (this.getValue() != '' &&
			(this.getValue().indexOf("@") == "-1" ||
			this.getValue().indexOf(".") == "-1"))
		{
			this.displayError(true);
			return false;
		}

		this.displayError(false);
		return true;
	}
}

/*
 * EntryList
 */
function addItem(e)
{
	var text = document.getElementById(e + '_val');
	var list = document.getElementById(e + '_list');

	var val = text.value;

	text.value = '';
	text.focus();

	if (val != '')
	{
		list.options[list.options.length] = new Option(val);

		updateList(e);
	}
}

function removeItem(e)
{
	var list = document.getElementById(e + '_list');

	var sel = list.options.selectedIndex;

	if (sel >= 0)
	{
		var noeud = list.options[sel];
		noeud.parentNode.removeChild(noeud);
	}

	updateList(e);
}

function updateList(e)
{
	var text = document.getElementById(e);
	var list = document.getElementById(e + '_list');

	text.value = '';

	for (i = 0; i < list.options.length; i++)
	{
		if (text.value == '')
			text.value = list.options[i].text;
		else
			text.value += ',' + list.options[i].text;
	}
}
