
/**
 * classe v�rification du form
 */
var FormRecrutement = new Class({
	initialize: function() {
		this.fields = new Array();
	}, 
	addField: function(id, type, message) {
		var field = new FieldRecrutement(id, type, message);
		this.fields.include(field);
	},
	check: function() {
		var j = this.fields.length;
		for (var i = 0; i < j; i++) {
			if (!this.fields[i].check()) return false;
		}
		return true;
	}
});
/**
 * classe v�rification d'un champ
 */
var FieldRecrutement = new Class({
	initialize: function(id, type, message, messageVide) {
		this.id = id;
		this.type = type;
		this.message = message;
		this.messageVide = messageVide;
	},
	check: function() {
		var rep;
		switch (this.type) {
			case 'emptyText': 
				rep = checkEmptyText(this.id);
				break;
			case 'emptySelect': 
				rep = checkEmptySelect(this.id);
				break;
			case 'emptySpecialSelect': 
				rep = checkEmptySelect(this.id);
				break;
			case 'Email': 
				rep = checkEmail(this.id);
				break;
			case 'Phone': 
				rep = checkPhone(this.id);
				break;
			case 'Int': 
				rep = checkInt(this.id);
				break;
			case 'Date': 
				rep = isDate($(this.id).getValue());
				break;
			case 'File': 
				rep = checkFile($(this.id).value, ['pdf', 'doc', 'rtf']);
				break;
			case 'Files':
				rep = checkFile($(this.id).value, ['pdf', 'doc', 'rtf','txt']);
				break;
			case 'cpFr': 
				rep = checkCP($(this.id).getValue());
				break;
			case 'radio': 
				rep = checkRadio(this.id);
				break;
			case 'singleCheck': 
				rep = singleCheck(this.id);
				break;
			case 'Insee':
				rep = checkInsee(this.id);
				if (rep !== true) {
					$(this.id[0]).focus();
					alert(this.message);
					return false;
				} 
				break;
			case 'tel': 
				rep = checkTel($(this.id[0]).getValue(), $(this.id[1]).getValue());
				if (rep !== true) {
					$(this.id[0]).focus();
					alert(this.message);
					return false;
				} 
				break;
			case 'cp': 
				rep = checkCodePostal($(this.id[0]).getValue(), $(this.id[1]).getValue());
				if (rep !== true) {
					$(this.id[0]).focus();
					alert(this.message);
					return false;
				} 
				break;
		}
		if (rep !== true) {
			if ($(this.id)) {
				if ($(this.id).disabled==false) { // Placer le focus sur un �;�ment disabled provoque une erreur sous ie.
					$(this.id).focus();
				}
			}
			alert(this.message);
			return false;
		} 
		return true;
	}

});

// la chackbox sp�cifi�e est check�e
function singleCheck(id) {
	return ($(id).checked);
}

// champs texte vide
function checkEmptyText(id) {
	var val = trim($(id).getValue());
	//var val = ($(id).getValue());
	return (val != '' && val != null);
}

// valeur a bien �t� choisie dans un select
function checkEmptySelect(id) {
	if ($(id).disabled == true) return false;
	var val = $(id).options[$(id).selectedIndex].value;
	return (val != null && val != 0  && val != '');
}

// select aliment� par l'internaute : v�rifie si liste vide 
function checkEmptySpecialSelect(id) {
	return ($(id).options.length > 0);
}

// @ mail
function checkEmail(id) {
	return ($(id).getValue().match(/^[a-z\d]+((\.|-|_)[a-z\d]+)*@((?![-\d])[a-z\d-]{0,62}[a-z\d]\.){1,4}[a-z]{2,6}$/gi)==$(id).getValue())&&($(id).getValue().substr($(id).getValue().lastIndexOf("@")).length<=256);
}

//Function to trim the space in the left side of the string
function ltrim ( s ){
  return s.replace( /^\s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
   return s.replace( /\s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
   var temp = s;
   return temp.replace(/^\s+/,'').replace(/\s+$/,'');
}

// n� de telephone fr
function checkPhone(id) {
	var regex = new RegExp(/^(01|02|03|04|05|06|07|08)(([\.\-\/])?[0-9][0-9]){4}/gi);
	id=trim(id);
	if(!(id.length==10)){
		return false;
	}else{
		return (regex.test($(id).getValue()))
	}
}

// nb entier
function checkInt(id) {
	var value = $(id).getValue();	
	return (value != '' && !isNaN(value) && (parseInt(value) === parseFloat(value)) && (value.indexOf("+") === -1) && (value.indexOf(".") === -1) );
}

// fichier
function checkFile(val, ext) {
	var fileExt = val.substring(val.lastIndexOf('.') + 1);
	var j = ext.length;
	for (var i = 0; i < j; i++) {
		if (ext[i].toLowerCase() == fileExt.toLowerCase()) {
			return true;
		}
	}
	return false;
}

// num�ro de s�curit� sociale 
function checkInsee(ids) {
	var numSS = $(ids[0]).getValue();
	var cleSS = $(ids[1]).getValue();
	
	str = new String(numSS);

	re = /^\s*(\d)\s*[-.]?\s*(\d\d)\s*[-.]?\s*(\d\d)\s*[-.]?\s*(\d\d)\s*[-.]?\s*(\d\d\d)\s*[-.]?\s*(\d\d\d)\s*$/;
	if ( ! re.test(str) ) {
		return false ;
	}
	
	//num = new Number(cleSS);
	var cle = document.getElementById('clesecu').value;
	cle = str.replace(re,"$1$2$3$4$5$6");
	num = new Number(str.replace(re,"$1$2$3$4$5$6"));
	
	
	key = 97-(num%97);
	// Has ECMAScript no sprintf function?
	if(key.toString().replace(/^(\d)$/,"0$1")!=cleSS) {
		return false ;
	}
	
	return true;
}

// code postal francais
function checkCP(val) {
	var re = /^(0[1-9]|[1-9][0-9])[0-9][0-9][0-9]$/;
	return re.test(val);
}
function checkRadio(name) {
	var rep = false;
	var field = $E('body').getElements('input[name=' + name + ']');
	var j = field.length;
	for (var i = 0; i < j; i++) {
		if (field[i].checked == true) {
			return true;
		}
	}
	return false;
}


// fonction g�rant la v�rification des n� de tel suivant le pays
function checkTel(valTel, valPays) {
	switch (valPays) {
		// france
		case '5':
			//var regex = new RegExp(/^(01|02|03|04|05|06|08)(([\.\-\/])?[0-9][0-9]){4}/gi);
			valTel = trim(valTel);
			if(!(valTel.length==10)){
				return false;
			}else{
				var regex = new RegExp(/^(01|02|03|04|05|06|07|08|09)([0-9][0-9]){4}/gi);
				return regex.test(valTel);
			}
			break;
		default: 
			return true;
			break;
	}
	return true;
}

// fonction g�rant la v�rification des cp suivant le pays
function checkCodePostal(valCP, valPays) {
	switch (valPays) {
		// france
		case '5':
			var regex = /^(0[1-9]|[1-9][0-9])[0-9][0-9][0-9]$/;
			return regex.test(valCP);
			break;
		default: 
			return true;
			break;
	}
	return true;
}
