function isNumeric(sVal){
	var point = false
	for ( var i = 0 ; i < sVal.length ; i ++ ){
		if (isNaN(parseInt(sVal.charAt(i)))){
			if (sVal.charAt(i)=='.' && !point){
				point=true;
			}
			else{
				return false;
			}
		}
	}
	return true;
}

function isNumeric_impose(sVal){
	var point = false
	for ( var i = 0 ; i < sVal.length ; i ++ ){
		if (isNaN(parseInt(sVal.charAt(i)))){
			if (sVal.charAt(i)=='.' && !point){
				point=true;
			}
			else{
				return false;
			}
		}
	}
	return true;
}


function isInt(sVal){
	for ( var i = 0 ; i < sVal.length ; i ++ ){
		if (isNaN(parseInt(sVal.charAt(i)))){
			return false;
		}
	}
	return true;
}
function isDate(dt){
	if (dt.length==0){
		return true;
	}
	
	var lg = 1
	var part = dt.split('/')  
	var nbPart = part.length ;
	var jour = 0 ;
	var mois = 0 ;
	var annee = 0 ;
	
	if (nbPart!=3){
		return false;
	}
	
	for ( var i = 0 ; i < 3 ; i ++ ){
		if (!isInt(part[i])){
				return false;
		}	
	}
	
	if (lg==1){			
		jour = part[0];
		mois = part[1];
		annee = part[2];
	}
	else{
		if (lg==2){
			jour = part[1];
			mois = part[0];
			annee = part[2];
		}
	}
	
	if (annee.length != 2 ){
		return false 
	}
	var testDt = new Date(annee, mois-1, jour);
	if (testDt.getDate() != jour ){
		return false;
	}
	
	if (testDt.getMonth() != (mois -1) ){
		return false;
	}			
	
//	if (testDt.getYear().toString().length==2){
//		if ("19" + testDt.getYear().toString() != annee ){	
//			return false;
//		}
//	}
//	else{
//	if (testDt.getYear()<1000){
//		if ((1900 + testDt.getYear()) != annee ){
//			return false;
//		}
//	}
//	else{
		if (testDt.getYear() != annee ){
			return false;
		}
//	}
//	}			
		
	return true;		
	
}

function toDate(dt){

	if (dt.length==0){
		return true;
	}
	
	var lg = 1;
	var part = dt.split('/')  
	var nbPart = part.length ;
	var jour = 0 ;
	var mois = 0 ;
	var annee = 0 ;
	
	if (nbPart!=3){
		return 0;
	}
	
	for ( var i = 0 ; i < 3 ; i ++ ){
		for ( var j = 0 ; j < part[i].length ; j ++ ){
			if (isNaN(parseInt(part[i].charAt(j)))){
				return 0;
			}
		}
	
	}
	
	if (lg==1){			
		jour = part[0];
		mois = part[1];
		annee = part[2];
	}
	else{
		if (lg==2){
			jour = part[1];
			mois = part[0];
			annee = part[2];
		}
	}
	if (annee.length != 2 ){
		return 0;
	}
	return new Date(annee, mois-1, jour);
}

function isTime(ti){

	if (ti.length==0){
		return true;
	}
	var lg = 1
	var part = ti.split(':')  
	var nbPart = part.length ;
	var heure = 0 ;
	var minute = 0 ;
	var seconde = 0 ;
	
	if (nbPart < 2 || nbPart > 3){
		return false;
	}
	
	for ( var i = 0 ; i < nbPart ; i ++ ){
		for ( var j = 0 ; j < part[i].length ; j ++ ){
			if (isNaN(parseInt(part[i].charAt(j)))){
				return false;
			}
		}			
	}
	heure = part[0];
	minute = part[1];
	
	if (nbPart==3){
		seconde = part[2];
	}
	var testTime = new Date();
	testTime.setHours( heure ) ;
	testTime.setMinutes( minute ) ;
	testTime.setSeconds( seconde ) ;
	
	if (testTime.getHours() != heure){
		return false;
	}
	
	if (testTime.getMinutes() != minute){
		return false;
	}
	
	if (testTime.getSeconds() != seconde){
		return false;
	}
	
	return true;
}


function isPhone(sPhone){
	var allowed='0123456789()+-. ';
	
	if (sPhone.length > 0 ){
		for ( var j = 0 ; j < sPhone.length ; j ++ ){
				if (allowed.indexOf(sPhone.charAt(j))==-1){
					return false;
				}
		}
	}
	return true;
}


function isEmail (myEmail){
	var okList = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-@+';
	var l = myEmail.length ;
	if (l==0){ return true;}
	for ( var i = 0 ; i < l ; i++){
		if ( okList.indexOf(myEmail.charAt(i))== -1 ){
			return false;
		}
	}
	var posAt = myEmail.indexOf('@');
	if ( posAt == -1 ) {
		return false;
	}
	else{
		var machaine = myEmail.substring(posAt);
		var posPoint = machaine.indexOf('.');
		if (posPoint == -1 ){
			return false;			
		}
		/*else{
			machaine = machaine.substring(posPoint+1);
			if ( machaine.indexOf('.') != - 1){
				return false;			
			}
		}*/
	}
	return true;
}

function isAlpha(sStr){
	var okList=' _-\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzŠšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ';
	for (var i = 0 ; i < sStr.length ; i++){
		if (okList.indexOf(sStr.charAt(i))==-1){
			return false;
		}
	}
	return true;
}

function isAlphaNumeric(sStr){
	 return true;
	var okList=' 0123456789_-\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzŠšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ';
	for (var i = 0 ; i < sStr.length ; i++){
		if (okList.indexOf(sStr.charAt(i))==-1){
			return false;
		}
	}
	return true;
}

function isSearch(sStr){
//	 return true;
	var okList=' 0123456789_-\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	for (var i = 0 ; i < sStr.length ; i++){
		if (okList.indexOf(sStr.charAt(i))==-1){
			return false;
		}
	}
	return true;
}

function isFile(sStr){
	// return true;
	var okList=' .\\:0123456789_-\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzŠšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ';
	for (var i = 0 ; i < sStr.length ; i++){
		if (okList.indexOf(sStr.charAt(i))==-1){
			return false;
		}
	}
	var posPoint = sStr.lastIndexOf('.');
	if ( posPoint == -1 ) {
		return false;
	}
	else{
		var machaine = sStr.substring(posPoint);
		machaine = machaine.toUpperCase();
		if (machaine == ".JPG" || machaine == ".JPEG" || machaine == ".GIF"){
			return true;
		} else {
			return false;
		}
	}
	return true;
}

function isImmatriculation(sStr){
	var findChar = -1;
	var findInt = -1;
	var findAlphaNumeric = false;
	var f_vResult=false;
	for (var i = 0 ; i < sStr.length ; i++){
		if (isNaN(parseInt(sStr.charAt(i))) && (findChar == -1))
			findChar = i;
		if (!isNaN(parseInt(sStr.charAt(i))) && (findInt == -1))
			findInt = i;
	}
	if ( isAlphaNumeric(sStr) ) 
		findAlphaNumeric=true;
	
	if ( (findChar != -1) && (findInt != -1) ){
		if ( (findChar > findInt) && findAlphaNumeric )
			f_vResult = true;
	}
	
	return f_vResult;
}

function isDepartement(sStr){
	if ( !isInt(sStr) || (sStr.length != 2) )
		return false;
	return true;	
}




function getValue(){
	//alert(this.name);
	//alert('var container = self.document.' + this.form + '.' + this.name +';');
	eval('var container = self.document.' + this.form + '.' + this.name +';');
	var type = container.type ? container.type : container[0].type
	if (type=='text'){
		return (container.value);
	}
	if (type=='file'){
		return (container.value);
	}
	if (type=='hidden'){
		return (container.value);
	}
	if (type=='select-one'){
		if(container.selectedIndex!=-1){
			return (container.options[container.selectedIndex].value);
		}
	}
	if (type=='textarea'){
		return (container.value);
	}						
	if (type=='password'){
		return (container.value);
	}
	if (type=='checkbox'){
		if (container.checked){
			return container.value;
		}
	}
	if (type=='radio'){
		var i = 0 ;
		while (true){
			if (!container[i]){
				break;
			}
			else{
				if (container[i].checked){
					return container[i].value;			
				}
			}
			i++;
		}
		return '';
	}
	return '';
}

function validateElement(){
	if (!validate(this.value(),this.dataType)){
		if (this.msg){
			alert(this.msg);
		}
		//eval('self.document.' + this.form + '.' + this.name +'.focus();');
		return false;
	}
	return true;
}
function validate(buffer,datatype){
	//alert('valeur: '+buffer + '\ntype: ' +datatype);
	if (buffer==''){		
		return true;
	}	
	if(datatype==1){
		//alpha
		return isAlpha(buffer);
	}
	if(datatype==2){
		//numeric
		return isNumeric(buffer);
	}
	if(datatype==3){
		//int
		return isInt(buffer);
	}
	if(datatype==4){
		// date
		return isDate(buffer);
	}
	if(datatype==5){
		// Time
		return isTime(buffer);
	}
	if(datatype==6){
		//Phone
		return isPhone(buffer);
	}
	if(datatype==7){
		//Mail
		return isEmail(buffer);
	}
	if(datatype==8){
		//alphaNumeric
		return isAlphaNumeric(buffer);
	}
	if(datatype==9){
		//Departement
		return isDepartement(buffer);
	}
	if(datatype==10){
		//Immatriculation
		return isImmatriculation(buffer);
	}
	if(datatype==11){
		//File
		return isFile(buffer);
	}
	if(datatype==12){
		//Numerique taille imposé 
		return isNumeric_impose(buffer);
	}
	if(datatype==13){
		//Moteur de recherche 
		return isSearch(buffer);
	}
	return true;

}
function FrmElement(form,name,size,dataType,msg,label,mandatory){
	this.form  		= form ;	
	this.name 		= name ;
	this.size 		= size ;
	this.dataType 	= dataType ;	
	this.mandatory 	= mandatory || false ;
	this.value 		= getValue;
	this.msg 		= msg;
	this.label 		= label;	
	this.validate 	= validateElement;
}

function frmValidation(){

	var flag = 0;
	var message = '';
	var valfocus = '';

	for ( var i = 0 ; i < this.elements.length ; i++){
		if (this.elements[i].value() && (this.elements[i].value().length<this.elements[i].size && this.elements[i].dataType==12)){
				message = message + this.elements[i].label + " " + lib_msg[1] +" " + this.elements[i].size + " " + lib_msg[2] +" \n";
				flag = 1;
				//if (message == '' ) valfocus = this.elements[i].name;
		}

		if (this.elements[i].mandatory && !this.elements[i].value()){
			if (message=="") valfocus = this.elements[i].name;
			message = message + lib_msg[0] +' ' + this.elements[i].label + " \n";
			flag = 1;	
		}
			
		if (this.elements[i].value().length>this.elements[i].size){
			if (this.elements[i].size>0){
				if (message == '' ) valfocus = this.elements[i].name;
				message = message + this.elements[i].label + " "+ lib_msg[3] +" " + this.elements[i].size + " " + lib_msg[2] +" \n";
				flag = 1;
			}
		}
		if (!validate(this.elements[i].value(),this.elements[i].dataType)){
			if (this.elements[i].msg){
				if (message == '' ) valfocus = this.elements[i].name;
				message = message + this.elements[i].msg + '\n';
				flag = 1;
			}		
		}	
	}
	if (flag == 1) {
		alert(message + "\n"+ lib_msg[4] );
		eval('var container = self.document.' + this.form + '.' + valfocus +';');
		var type = container.type ? container.type : container[0].type;
		if (type=='radio'){
			eval('self.document.' + this.form + '.' + valfocus +'[0]' ).focus();
		}else{
			if(valfocus.indexOf("ville")==0){
				//alert(eval('self.document.' + this.form + '.' + valfocus ).visibility);
				eval('self.document.' + this.form + '.' + valfocus ).focus();
			}
		}
		return false;
	}
	
	if (this.userValidation!=null){
		if (!this.userValidation()){
			return false;
		}		
	}

	return true;
}

function addElement(name,size,dataType,msg,label,mandatory){
	this.elements[this.elements.length] = new FrmElement(this.form,name,size,dataType,msg,label,mandatory);
	eval(this.proto +'.prototype.' + name + ' = this.elements[this.elements.length-1];');
}


function addElement2(name,label,size,not_use,dataType,not_use_bis,mandatory){
	this.elements[this.elements.length] = new FrmElement(this.form,name,size,dataType,label + ' est invalide.',label,mandatory);
	eval(this.proto +'.prototype.' + name + ' = this.elements[this.elements.length-1];');	
}


function createForm(){
	//return new Function('objName','frm','userValidation','this.proto= objName ;this.form = frm ; this.elements=new Array(0) ; this.addElement= addElement ; this.frmValidation = frmValidation; this.userValidation = userValidation || null');
	return new Function('objName','frm','userValidation','this.proto= objName ;this.form = frm ; this.elements=new Array(0) ; this.addElement= addElement ; this.addElement2 = addElement2 ; this.frmValidation = frmValidation; this.userValidation = userValidation || null');
}

var type_alpha				= 1
var type_numeric			= 2
var type_int				= 3
var type_date				= 4
var type_time				= 5
var type_phone				= 6
var type_mail				= 7
var type_alphaNumeric		= 8
var type_Departement		= 9
var type_Immatriculation	= 10
var type_File				= 11		
var type_numeric_impose		= 12
var type_search_engine		= 13

/*
addElement(name,size,dataType,msg,label,mandatory){
*/

function remplacecomat(val){
	var temp =val.value; 
	val.value= temp.replace(",",".");
}