//--- 11/07/2000 --- Jerome CHAGNOUX
//--- Verification of a form ---
//--- These javascripts operates a differents function over form and select box  ---

//--- moveSelect: move elements from one select list to another ---
function moveSelect(formName,from,to)
//--- from, to: integer, name of the two select box.
{
	var index;
	var text;
	var value;
	var l;
	var a;
	var j;
	index=eval("document."+formName+"."+from+".options.selectedIndex");
	while (index>=0)
	{
		text=eval("document."+formName+"."+from+".options["+index+"].text");
		value=eval("document."+formName+"."+from+".options["+index+"].value");
		eval("document."+formName+"."+from+".options["+index+"]=null");
		a = new Option(text,value);
		l=eval("document."+formName+"."+to+".length");
		eval("document."+formName+"."+to+".options["+l+"]= a");
		for (j=0;j<eval("document."+formName+"."+to+".length");j++)
			eval("document."+formName+"."+to+".options["+j+"].selected=false;");
		eval("document."+formName+"."+to+".options["+l+"].selected=true;");
		index=eval("document."+formName+"."+from+".options.selectedIndex");
	}
	if (eval("document."+formName+"."+from+".length")!=0)
	{
		for (j=0;j<eval("document."+formName+"."+from+".length");j++)
			eval("document."+formName+"."+from+".options["+j+"].selected=false;");
		eval("document."+formName+"."+from+".options[0].selected=true;");
	}
}

//--- clearSelect: function, delete all elements of a select list ---
function clearSelect(formName,selectName)
{
	var j;
	for (j=eval("document."+formName+"."+selectName+".length")-1;j>=0;j--)
		eval("document."+formName+"."+selectName+".options["+j+"]=null;");
}

//--- deleteSelect: function, delete the selected items in a list ---
function deleteSelect(formName,listName)
//--- formName: string, name of the form ---
//--- listName: string, name of the list in the form ---
{
	var index;
	var text;
	var value;
	var l;
	var j;
	index=eval("document."+formName+"."+listName+".options.selectedIndex");
	while (index>=0)
	{
		eval("document."+formName+"."+listName+".options["+index+"]=null");
		index=eval("document."+formName+"."+listName+".options.selectedIndex");
	}
	if (eval("document."+formName+"."+listName+".length")!=0)
	{
		for (j=0;j<eval("document."+formName+"."+listName+".length");j++)
			eval("document."+formName+"."+listName+".options["+j+"].selected=false;");
		eval("document."+formName+"."+listName+".options[0].selected=true;");
	}
}

//--- allMultiSelect: function, select all elements of a select list with multiple selection ---
function allMultiSelect(formName,selectName)
{
	var j;
	for (j=0;j<eval("document."+formName+"."+selectName+".length");j++)
		eval("document."+formName+"."+selectName+".options["+j+"].selected=true;");
}

//--- existText: function, verify in a list if a text already exists ---
function existText(myList,myString)
//--- myList: string, representing the list, not the name of the list nor the name of the form ---
//--- myString: string, what we look for ---
{
	var l=myList.length;
	var result=false;
	var j;
	for(j=0;j<l;j++)
	{
		result=(myList.options[j].text==myString);
		if (result)
			break;
	}
	return (result);
}

//--- isOneCheckBox: function, verify if there is at least one checkbox checked ---
function isOneCheckBox(myForm)
{
	var j;
	var l=eval("document."+myForm+".length");
	var result=false;
	for(j=0;j<l;j++)
	{
		result=eval("document."+myForm+".elements["+j+"].checked");
		if (result)
			break;
	}
	return result;
}

//--- upcas: function ---
function upcas(myfield,myform)
{
champ=eval("document."+myform.name+"."+myfield);
champ.value=champ.value.toUpperCase();
}

function upcas2(myfield)
{
myfield.value=myfield.value.toUpperCase();
}

//--- upcasPrenom: function ---
function upcasPrenom(myfield,myform)
{
champ=eval("document."+myform.name+"."+myfield);
champ.value=champ.value.substring(0,1).toUpperCase()+champ.value.substring(1,champ.value.length).toLowerCase();
}

function convToNum(myfield)
{
	myfield.value=myfield.value.replace(",",".");
	if(isNaN(myfield.value))myfield.value='';
}

function Valid_Immatriculation(ObjImmat,exclu,aff,cyl50)
{	
	ObjImmat.value = ObjImmat.value.toUpperCase();
	var monImmat = ObjImmat.value;
	
	var reImmat      = /^[0-9]{1,4}([A-Z]{2,3}|WW[A-Z]{1,2})(([0-9]{2,3})|2A|2B)$/;
	var reImmatCorse = /^[0-9]{1,4}[A-Z]{2,3}(2A|2B)$/;
	var reImmatDom   = /^[0-9]{1,4}[A-Z]{2,3}97[0-8]{1}$/;
	var reImmatFront = /^[0-9]{1,4}(TTQ|TTW)$/;
	var reImmatTemp  = /^[0-9]{1,4}WW[A-Z]{1,2}(([0-9]{2,3})|2A|2B)$/;
	var reImmat50   = /(^[A-Z]{1,2})([0-9]{2,3})([A-Z]{1}$)/;
	var reNewImmat   = /(^[A-Z]{2})([0-9]{3})([A-Z]{2}$)/;
	var reNewImmat2   = /^[A-Z]{2}-[0-9]{3}-[A-Z]{2}$/;
  if(monImmat!='')
  {
  	if(cyl50){
  		if(!reImmat50.test(monImmat)&&!reNewImmat.test(monImmat)&&!reNewImmat2.test(monImmat)){if(aff) alert("L'immatriculation 50 n'est pas valide"); return false;}
		}
  	else{
	  	if(!reImmat.test(monImmat)&&!reImmatFront.test(monImmat)&&!reNewImmat.test(monImmat)&&!reNewImmat2.test(monImmat)){if(aff) alert("L'immatriculation n'est pas valide"); return false;}
	  }
  	
  	if(reNewImmat.test(monImmat)){
  		ObjImmat.value =monImmat.replace(reNewImmat, "$1-$2-$3") ;
  		}
  		
  	if(exclu){
	  	if(exclu.indexOf('C',0)>=0) if(reImmatCorse.test(monImmat)){if(aff)alert(""); return false;}
	  	if(exclu.indexOf('D',0)>=0) if(reImmatDom.test(monImmat))  {if(aff)alert(""); return false;}
	  }

  }
  return true;
}

