// JavaScript Document

function verif(n)
{
	if(n<10) value = "gar0" + n;
	else value = "gar" + n;
	if(eval("document.theform." + value + ".checked") == false) return;

	nb = 0;
	for(i=1;i<20;i++)
	{
		if(i<10) value = "gar0" + i;
		else value = "gar" + i;
	
		if(i != n && eval("document.theform." + value + ".checked") == true) nb += 1;
	}

	if(n<10) value = "gar0" + n;
	else value = "gar" + n;

	if(nb > 1) eval("document.theform." + value + ".checked = false");
	
	return;
}

function ddValue(p_dd)
{
	return p_dd[p_dd.selectedIndex].value;
}

function radioButtonValue(p_radiob)
{
	for(var i=0; i<p_radiob.length; i++)
	{
		if(p_radiob[i].checked) return p_radiob[i].value;
	}
	
	return null;
}

function valueIsUndefined(p_anObject)
{
	return (""+p_anObject.value)=="undefined";
}

function strReplace(p_sourceStr, p_searchStr, p_replaceStr)
{
	var cut=0;
	var l=p_searchStr.length;

	while(cut!=-1)
	{
		if((cut=p_sourceStr.indexOf(p_searchStr,0))!=-1)
		{
			p_sourceStr= p_sourceStr.substring(0,cut) + p_replaceStr + p_sourceStr.substring(cut+l,p_sourceStr.length) + "";
		}
	}
	
	return p_sourceStr;
}

function isNumber(p_str)
{
	var r=parseInt(p_str);

	if(isNaN(r)) return false;

	if(r==0)
	{
		if(p_str.length==0) return false;
		else
		{
			var l=p_str.length;
			var countZeroes=0;
			var countDecimal=0;

			for(var i=0;i<l;i++)
			{
				if(p_str.charAt(i)=='.') countDecimal++;
				else if(p_str.charAt(i)=='0') countZeroes++;
			}
				
			if((countZeroes<1) || (countDecimal>1) || (countDecimal+countZeroes != l)) return false;
		}
	}

	return true;
}

function are2Numbers(p_str)
{
	var m=p_str.indexOf(";",0);
	var first;
	var second;
	
	if(m==-1) return false;

	first=p_str.substring(0,m);
	second=p_str.substring(m+1,p_str.length);

	if(isNumber(first) && isNumber(second)) return true;

	return false;
}

function xor(a,b)
{
	return a!=b; //( ( a || b) && !(a && b))
}

function FormFieldsA(p_n,p_formName)
{
	for(var i=0; i<p_n; i++)
	{
		this[i]="";
	}
	this.totalFields=p_n;
	this.formName=p_formName;
}

function DependenciesA(p_n)
{
	for(var i=0; i<p_n; i++)
	{
		this[i]="";
	}
	this.totalDependencies=p_n;
}

function FieldDescriptor(p_fieldName, p_fieldLabel, p_fieldType, p_validityExpr, p_dependencies)
{
	this.fieldName=p_fieldName;
	this.fieldLabel=p_fieldLabel;
	this.fieldType=p_fieldType;
	this.validityExpr=p_validityExpr;
	this.dependencies=p_dependencies;
}

function evalFormFields(p_ff)
{
	var thisField;
	var validity;

	for(var i=0; i<p_ff.totalFields; i++)
	{
		thisField=p_ff.formName +"." +p_ff[i].fieldName;
		
		validity=strReplace(p_ff[i].validityExpr,"$$FO",""+p_ff.formName);
		validity=strReplace(validity,"$$FI",""+p_ff[i].fieldName);
		validity=strReplace(validity,"$$F",thisField);
		//test validity
		if(0==eval("("+ validity +") ? 1 : 0"))
		{
			if(p_ff[i].fieldType=="T")
			{
				alert("Veuillez remplir le champ "+p_ff[i].fieldLabel+", svp.");
				eval(thisField +".focus()");
				eval(thisField +".select()");
			}
			if(p_ff[i].fieldType=="TS")
			{
				alert("Veuillez faire un choix dans la liste "+p_ff[i].fieldLabel+", svp.");
				eval(thisField +".focus()");
			}
			else if(p_ff[i].fieldType=="TC")
			{
				alert("Vous devez être d'accord avec les "+p_ff[i].fieldLabel+" et la politique de protection de la vie privée");
				eval(thisField +".focus()");
				eval(thisField +".select()");
			}
			else if(p_ff[i].fieldType=="TZ")
			{
				alert("You are not obliged to fill the "+p_ff[i].fieldLabel+" field, " + "but if you do, do it right (please, see Help)");
				eval(thisField +".focus()");
				eval(thisField +".select()");
			}
			else if(p_ff[i].fieldType=="TN")
			{
				alert("You must enter a number in the "+p_ff[i].fieldLabel+" field, please.");
				eval(thisField +".focus()");
				eval(thisField +".select()");
			}
			else if(p_ff[i].fieldType=="TNZ")
			{
				alert("You are not obliged to fill the "+p_ff[i].fieldLabel+" field, " + "but if you do, must enter a number in the "+p_ff[i].fieldLabel+" field, please.");
				eval(thisField +".focus()");
				eval(thisField +".select()");
			}
			else if(p_ff[i].fieldType=="R")
			{
				alert("Vous devez faire un choix pour "+p_ff[i].fieldLabel+", svp.");
				//eval(thisField +"[0].focus()"); //Not supported by IE
				//eval(thisField +"[0].select()");//Not supported by IE
			}
	
			return false
		}
		//test dependencies
		
		if(p_ff[i].dependencies != null && ( eval( "("+thisField+".value !='') ? 1 : 0")==1 ) )
		{
			for(var j=0; j<p_ff[i].dependencies.totalDependencies; j++)
			{
				if( eval( "("+p_ff.formName+"." + p_ff[p_ff[i].dependencies[j]].fieldName+".value =='')? 1 : 0")==1 )
				{
					alert("If you fill the "+p_ff[i].fieldLabel +" field, " + "the "+p_ff[p_ff[i].dependencies[j]].fieldLabel + " field must also be filled.");
					return false; //could accumulate field names...
				}
			}
		}
	}

	return true;
}

function isValidEMail(string)
{
	var first;
	var second;
	var last;
	
	if((last=string.length-1) < 4) return false;
	
	if( (first=string.indexOf("@",1)) !=-1 )
	{
		if( ((last-first) >= 3) && (second=string.indexOf(".",first+2)) !=-1 ) return true;
	}

	return false;
}

function isValidSIC(string)
{
	var l=string.length;

	if(l!=4) return false;
	for(var i=0;i<4;i++)
	{
		c=string.charAt(i);
		if(c<'0' || c>'9') return false;
	}

	return true;
}

function checkEMail()
{
	if((document.theform.email.value!="") && ! isValidEMail(document.theform.email.value))
	{
		alert("L'adresse e-mail entrée est incorrecte !");
		document.theform.email.focus();
		document.theform.email.select();
	}	
}

function my_submit()
{
	return evalFormFields(formFields)
}

/*******************************************************************************/
function OnlyNumber() {
if(event.keyCode < 47 || event.keyCode > 57) event.returnValue = false;
	 if(event.which < 47 || event.which > 57) return false;
}
		
function Decimals() {
if(event.keyCode < 44 || event.keyCode > 57) event.returnValue = false;
	 if(event.which < 44 || event.which > 57) return false;
}
/*******************************************************************************/

function PopupCentrer(page,largeur,hauteur,options) {
  var top=(screen.height-hauteur)/2;
  var left=(screen.width-largeur)/2;
  window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}













