/**
 * Function for validating form field.
 * To use this function :
 *  - your XHTML document must have the following DOCTYPE: <!DOCTYPE html PUBLIC "-//TheFloatingStone//DTD Tomos//FR" "http://srv.tomos.fr/dtd/xml1-tomos.dtd">
 *  - your form field must have the two new attribute: mandatory(true/false) and label(string)
 *
 * Param: the form id
 * Return an empty string if no error, the error message otherwise.
 **/

function validateFormFields(formId){
	var formFieldElements = $(formId).getElementsBySelector(
		'input[type="text"]'
		, 'input[type="checkbox"]'
		, 'input[type="radio"]'
		, 'input[type="file"]'
		, 'input[type="password"]'
		, 'select'
		, 'textarea'
	);
	var myLabel = '';
	var myOutput = '';
	for (i=0 ; i<formFieldElements.length ; i++){       
		myElement = formFieldElements[i];
		if( myElement.readAttribute('mandatory')=='true' && $F(myElement)=='' ) {
			if (myElement.readAttribute('label')!=null) {
				myLabel = myElement.readAttribute('label');
			} else if (myElement.readAttribute('name')!=null) {
				myLabel = myElement.readAttribute('name');
			} else if (myElement.readAttribute('id')!=null) {
				myLabel = myElement.readAttribute('id');
			}
			myOutput += ' - '+ myLabel +"\n";
		}
	}	
	return myOutput;
}

/**
 * Email validation
 *
 * Param: a string - the email to validate
 * Return: true is the email is valide, false otherwise
 **/

function isEmail (email){

	if (email != '' && !email.match (/^[a-z0-9\-\._]+@[a-z0-9\-_\.]+\.[a-z]{2,4}$/gi)){
	 	return false;
	}

	return true;
}