/**
 * Elements to check with checkForm
 * 
 * Each element in the array is an object to check.
 * Each object have 3 properties: name, type and text.
 * object.name = The name of the element to check
 * object.type = The type of value to check for
 * object.text = The text to display if the check fails
 *
 * The type can be one of these:
 * <li>
 *   <ul>text - The value can be any text but empty</ul>
 *   <ul>email - The value has to be an valid email</ul>
 *   <ul>regExp - Use object.regExp to check the value</ul>
 *   <ul>number - The value has to be a number</ul>
 *   <ul>checked - The element has to be "checked" as in a checkbox or radio</ul>
 *   <ul>match - Match object.name element's value with object.match element's value</ul>
 * </li>
 *
 * 
 */
function checkForm(form, elementList, headerText)
{
	// Check that the form exists
	form = ( 'text' != typeof(form) ? form : document.forms[form] );
	if (!form)
	{
		alert('System Error: Form not found. form: ' + form + '.');
		return false;
	}
	
	// Elements to check
	if (!elementList) elementList = checkElementList;
	
	// Header text
	headerText = ( headerText ? headerText : 'Missing information' );
	
	// The errors to alert
	var errorList = new Array();
	
	// Loop the elements and check'em
	var element = null;
	var regEx = null;
	for(var i = 0; i < elementList.length; ++i)
	{
		regEx = element = null;
		// Some simple tests
		if (!elementList[i].name || 'string' != typeof(elementList[i].name))
				{ errorList.push('System Error: elementList name not specified. name: ' + elementList[i].name + '. name: ' + typeof(elementList[i].name) + '.'); continue; }
		if (!elementList[i].type || 'string' != typeof(elementList[i].type))
				{ errorList.push('System Error: elementList type not specified. type: ' + elementList[i].type + '.'); continue; }
		if (!elementList[i].text || 'string' != typeof(elementList[i].text))
				{ errorList.push('System Error: elementList text not specified. text: ' + elementList[i].text + '.'); continue; }
		// RegExp test
		if ('regExp' == elementList[i].type && 'string' == typeof(elementList[i].regExp))
				elementList[i].regExp = new RegExp(elementList[i].regExp);
		if ('regExp' == elementList[i].type && (!elementList[i].regExp || 'object' != typeof(elementList[i].regExp)))
				{ errorList.push('System Error: elementList regExp not specified or not a RegExp. element: ' + elementList[i].toSource() + '.'); continue; }
		// Html Element
		element = form.elements[elementList[i].name];
		if (!element)
				{ errorList.push('System Error: Element not found. Name: ' + elementList[i].name + '.'); continue; }
		
		// Make regexp
		switch(elementList[i].type.toLowerCase())
		{
			case 'text': regEx = new RegExp('.+'); break;
			case 'email': regEx = new RegExp('^.+@.+\\..{2,4}$'); break;
			case 'regExp': regEx = elementList[i].regExp; break;
			case 'number': regEx = new RegExp('\\d+'); break;
			case 'word': regEx = new RegExp('\\w+'); break;
			case 'checked': regEx = null; break;
			case 'match':
				if (form.elements[elementList[i].match])
				regEx = new RegExp(form.elements[elementList[i].match]);
				break;
			default:
				errorList.push('System Error: elementList type is unknown. Type: ' + elementList[i].type + '.');
				continue;
				break;
		}
		
		// Check the value
		var result = false;
//					if ('input' == element.tagName && '' == element.type)
//					{
//						result = element.value.match(regEx);
//					}
//					else if ('textarea' == element.tagName)
//					{
//					}
		switch(element.type)
		{
			case 'textarea':
			case 'select-one':
			case 'text':
			case 'password':
				if ('match' == elementList[i].type.toLowerCase())
				{
					if (form.elements[elementList[i].match])
					{
						result = element.value == form.elements[elementList[i].match].value;
					}
					else
					{
						errorList.push('System Error: Can\'t find match element. match: ' + elementList[i].match + '.');
					}
					
				}
				else
				{
					result = element.value.match(regEx);
				}
				break;
			case 'checkbox':
				result = true == element.checked;
				break;
//						case 'radio':
//							for (var x = 0; x < 
//							break;
			default:
				errorList.push('System Error: element.type is unknown. Type: ' + element.type + '.');
				continue;
				break;
		}
		
		// Check the result
		if (!result)
		{
			errorList.push(elementList[i].text);
		}
	}
	
	// Alert the errors
	if (errorList.length)
	{
		alertError(errorList, headerText);
		return false;
	}
	
//				alert('form ok');
	return true;
}

function alertError(errorList, headerText)
{
	errorList = ( 'string' != typeof(errorList) ? errorList : errorList.split(/\n/) );
	headerText = ( headerText ? headerText : 'Errors:' );
	if (!errorList.length) return;
	
	// Find the longest text
	var textLength = headerText.length;
	for (var i = 0; i < errorList.length; ++i)
	{
		textLength = Math.max(errorList[i].length, textLength);
	}
	// Build a line
	var line    = '';
	textLength *= 1.3; // Output text are not mono-spatiated, therefore this
	for (var i = 0; i < textLength; i++) { line += '-'; }
	// Make the error text
	var errorText = '';
	for (var i = 0; i < errorList.length; ++i)
	{
		errorText += errorList[i] + '\n';
	}
	
	// Alert the error list
	alert(headerText + '\n' + line + '\n' + errorText + line + '\n');
}
