//////////////////////////////////////////////////////////////
// Start forms management
//////////////////////////////////////////////////////////////

function vs_check_forms(w){

	f = w.document.dajajhallal;
	fields_quantity = f.length;

	if(f.nf.value!="" && f.nf.type == "hidden"){
		numeric_fields = f.nf.value;
	}else{
		numeric_fields = "";
	}
	
	if(f.nidid.value!="" && f.nidid.type == "hidden"){
		mandatory_fields = f.nidid.value;
	}else{
		mandatory_fields = "";
	}

	debug="";
	errors = "";
	errors_qty = 0;
	mandatory_array = mandatory_fields.split(",");
	numeric_array = numeric_fields.split(",");
	
	for(i=0;i<fields_quantity;i++){
		n = f[i].name;
		t = f[i].type;
		v = f[i].value;
		//item[i] = n +","+ t +","+ v;
		if(!v || v==""){
			for(j=0;j<mandatory_array.length;j++){
			// Boucle for des champs obligatoires
				mi = mandatory_array[j];
				if(mi == n){
					errors += "- Le champ "+ n +" est necessaire au bon traitement de ce formulaire\r\n";
					errors_qty++;
				}
			// Fin de la boucle champs obligatoires // p = filtrePhrase(p);	//	document.forms[myF].phrase.value = p;
			}
			
		}else{
		
			for(k=0;k<numeric_array.length;k++){
			// Boucle for des champs numeriques
				ni = numeric_array[k];
				if(ni == n){
					tempv = v.replace(/\s/,'');
					if(tempv.search(/[\D]/)!=-1){
						errors += "- Le champ "+ n +" doit etre de type numerique uniquement\r\n";
						errors_qty++;
					}
				}
			// Fin de la boucle champs numeriques
			}
			
			if(n=="email"){
				if(!emailCheck(v) || emailCheck(v)==false) {
					errors += "- Le champ "+ n +" doit etre une adresse email valide (domaine ou IP)\r\n";
					errors_qty++;
				}
			}
			
		}
			
	}
	
	if(errors_qty>0){
		alert("Certains champs sont en erreur :\r\n"+ errors);
		return false;
	}
	
	return true;
}

function emailCheck(emailStr) {
	// I'm working on this part so it will be commented soon..
	// THANXXXXXXX
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		  }
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
	   }
	}

	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
   			}
		}
	return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;

	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return false;
	}

	if (len<2) {
		return false;
	}
return true;
}

function filtrePhrase(phrase){

	var count = phrase.length;
	var	s = phrase;
	var charToVal = "";
	
	for (i=0; i<count; i++){
		
		var tmp = s.charAt(i);
				
		if (estDansLimite(s.charCodeAt(i))){
			var tmp_charToVal = tmp;
		}else{
			var tmp_charToVal = "\&#"+ s.charCodeAt(i);
		}
		charToVal += tmp_charToVal;
	}
	return charToVal;
}

function estDansLimite(c){
		if((c>47 && c<57) || (c>54 && c<91) || (c>96 && c<123) || c==32 || c==64){
			return true;
		}else{
			return false;
		}
}

//////////////////////////////////////////////////////////////
// End forms management
//////////////////////////////////////////////////////////////