// 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, sLang){
	var o = document.getElementById(o_id);
	if(o.value.length < minimum_length){
		if (sLang == 'fr' )
		{
			alert("Veuillez entrer " + str_error_field_name + "; Ce champ est obligatoire et doit contenir au minimum " + minimum_length + " caracteres.");
		}
		else
		{
			alert("Please enter a valid " + str_error_field_name + "; This field is mandatory and must contain at least " + minimum_length + " characters.");
		}		
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_email(o_id, str_error_field_name, sLang){
	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)) {
		if (sLang == 'fr' )
		{
			alert("Veuillez entrer " + str_error_field_name + ". Ce champ est obligatoire.\nVerifiez bien le format: abc@abc.com.");
		}
		else
		{
			alert("Please enter a valid " + str_error_field_name + ". This field is mandatory.\nVerify the format properly: abc@abc.com.");
		}		
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		return false;
	}
	return true;
}

function validate_phone(o_id, str_error_field_name, sLang){
	var o = document.getElementById(o_id);
	var re = new RegExp(/^(1\-)?\d{3}\-\d{3}\-\d{4}$/);
	if(!re.test(o.value)) {
		if (sLang == 'fr' )
		{
			alert("Veuillez entrer " + str_error_field_name + ". Ce champ est obligatoire.\nVerifiez bien le format: 514-222-3333 ou 1-888-222-3333");
		}
		else
		{
			alert("Please enter a valid " + str_error_field_name + ". This field is mandatory.\nVerify the format properly: 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 + ". Ce champ est obligatoire.\nVerifiez bien le 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 + ". Ce champ est obligatoire.\nVerifiez bien le 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 + ". Ce champ est obligatoire.\nVerifiez bien le 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 + ". Ce champ est obligatoire.\nVerifiez le format.");
		o.style.backgroundColor = '#' + invalid_field_color;
		o.focus();
		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 = '';
			}
		}
	}
}
