// JS validation microlibrary
// Programmed by Ben for Egomedia in 2008

// Configuration
var invalid_field_color = 'FFFF00';

function validate_minimum_length(o_id, str_error_field_name, minimum_length){
	var o = document.getElementById(o_id);
	if(o.value.length < minimum_length){
		alert("Please enter " + str_error_field_name + "; this field is mandatory and must contain a minimum of " + minimum_length + " characters.");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_email_duplicates(o_id_m, o_id_f, o_id_s, o_id_t)
{
	var o_m = document.getElementById(o_id_m).value;
	var o_f = document.getElementById(o_id_f).value;
	var o_s = document.getElementById(o_id_s).value;
	var o_t = document.getElementById(o_id_t).value;
	
	
	if (o_m == o_f)
	{
		alert("Le premier courriel d'un de vos amis est identique au votre. Veuillez corriger.");
		document.getElementById(o_id_f).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_f).focus();
		return false;
	}
	if (o_m == o_s)
	{
		alert("Le deuxieme courriel d'un de vos amis est identique au votre. Veuillez corriger.");
		document.getElementById(o_id_s).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_s).focus();
		return false;
	}
	if (o_m == o_t)
	{
		alert("Le troisieme courriel d'un de vos amis est identique au votre. Veuillez corriger.");
		document.getElementById(o_id_t).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_t).focus();
		return false;
	}
	if (o_f == o_s)
	{
		alert("Le premier courriel d'un de vos amis est identique au deuxieme courriel d'un de vos amis. Veuillez corriger.");
		document.getElementById(o_id_s).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_s).focus();
		return false;
	}
	if (o_f == o_t)
	{
		alert("Le premier courriel d'un de vos amis est identique au troisieme courriel d'un de vos amis. Veuillez corriger.");
		document.getElementById(o_id_t).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_t).focus();
		return false;
	}
	if (o_s == o_t)
	{
		alert("Le deuxieme courriel d'un de vos amis est identique au troisieme courriel d'un de vos amis. Veuillez corriger.");
		document.getElementById(o_id_t).style.backgroundColor = '#' + invalid_field_color;
		document.getElementById(o_id_t).focus();
		return false;
	}
	return true;
}

function validate_choice(o_id, str_error_field_name)
{
	var o = document.getElementById(o_id);
	if (o.value == '')
	{
		alert("Please " + str_error_field_name + ". This field is mandatory.");
		o.style.backgroundColor = '#' + invalid_field_color;
		return false;
	}
	return true;
}
function validate_email(o_id, str_error_field_name){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
	if(!re.test(o.value)) {
		alert("Please enter " + str_error_field_name + ". This field is mandatory.\nPlease verify the format : abc@abc.com.");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_phone(o_id, str_error_field_name){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^(1\-)?\d{3}\-\d{3}\-\d{4}$/);
	if(!re.test(o.value)) {
		alert("Veuillez entrer " + str_error_field_name + ". This field is mandatory.\nPlease verify the format : 514-222-3333 ou 1-888-222-3333");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_postal_code(o_id, str_error_field_name){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i);
	if(!re.test(o.value)) {
		alert("Veuillez entrer " + str_error_field_name + ". This field is mandatory.\nPlease verify the format : H1H 1H1 ou H1H1H1");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_float(o_id, str_error_field_name){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^\-?(\+?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))$/);
	if(!re.test(o.value)) {
		alert("Veuillez entrer " + str_error_field_name + ". This field is mandatory.\nPlease verify the format : 123 ou 12.34 ou -1.23");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_positive_integer(o_id, str_error_field_name){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^([0-9]+)$/);
	if(!re.test(o.value)) {
		alert("Veuillez entrer " + str_error_field_name + ". This field is mandatory.\nPlease verify the format : 123");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	o.style.backgroundColor = '#FFFFFF';
	return true;
}

function validate_regexp(o_id, str_error_field_name, str_regexp){
	var o = document.getElementById(o_id);
	var re = new RegExp(str_regexp);
	if(!re.test(o.value)) {
		alert("Veuillez entrer " + str_error_field_name + ". This field is mandatory.\nPlease verify the format .");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}
function validate_checkboxes (aBoxes, sMessage)
{
	var iNBChecked = 0;
	
	if (aBoxes.length == 0)
	{
		return false;
	}
	else
	{
		
		for (i=0; i< aBoxes.length; i++)
		{
			
			var o = document.getElementById(aBoxes[i]);
			if ( o.checked == true)
			{
				iNBChecked++;
			}
		}
		
		if (iNBChecked <= 0)
		{
			alert("Please select a check box "+ sMessage);
			return false;
		}
	}	
	return true;
}

function init_validation(){
	// Reprototype ONCHANGE event and make each field reset its background color
	var arr_inputs = document.getElementsByTagName('input');
	for(var i=0; i < arr_inputs.length; i++){
		if(typeof(arr_inputs[i]) == 'object'){
			arr_inputs[i].onkeydown = function(){
				this.style.backgroundColor = '';
			}
		}
	}
}
