function alignTop()
{
	window.moveTo(0,0);
	window.focus();
}

function findForm(formName)
{
	for(i=0;i<document.forms.length;i++)
	{
		if(document.forms[i].name==formName) return document.forms[i]
	}
	return null
}

function alterForm(varToChange, actionToDO)
{
	//alert(varToChange+' : '+actionToDO);
	variable = document.getElementById(varToChange);
	variable.value = actionToDO;
}

function needSelection(action)
{
	switch(action)
	{
		//Commencer par les cas qui necessitent une sélection
		case 'edit'			:	
		case 'delete'		:	
		case 'activate'		:	
		case 'deactivate'	:	
		case 'modify'		:	return true;
								break;
		//Les cas qui ne necessitent pas de selection
		default				:	return false;
								break;
	}
}

function verifySelection(inputName, FormName)
{
	Form = findForm(FormName);
	if(Form == null) return false;
	if(!needSelection(Form.action.value)) return true;
	for(i=0; i<Form.elements.length; i++)
	{
		if((Form.elements[i].type=="checkbox") && (Form.elements[i].name==inputName)&& (Form.elements[i].checked==true)) return true;
		if((Form.elements[i].type=="hidden") && (Form.elements[i].name==inputName)&& (Form.elements[i].value>0)) return true;		
	}
	return false;
}

function submitForm(varToChange, actionToDO, FormName)
{
	Form = findForm(FormName);
	if(Form == null) return;
	//alert(FormName);
	alterForm(varToChange, actionToDO);
	if(verifySelection('selectedItem[]', FormName) || verifySelection('selectedItem', FormName)) Form.submit();
	else 
	{
		alert("Action annulée car aucun élément n'est sélectionné.");
		alterForm(varToChange, 'list');
	}
}

function confirmSubmitForm(message, varToChange, actionToDO, FormName)
{
	if(!confirm(message)) return;
	submitForm(varToChange, actionToDO, FormName);
}

function chekAllUsers(FormName, checkValue)
{
	Form = findForm(FormName);
	if(Form == null) return;
	for(i=0; i<Form.elements.length; i++)
	{
		if(Form.elements[i].type=="checkbox") Form.elements[i].checked=checkValue;
	}
}

function chekUser(checkBoxId)
{
	checkBox = document.getElementById(checkBoxId);
	if(!checkBox.checked ) checkBox.checked = true;
}

function toggleEnabling(myID, fieldID)
{
	fieldToToggleEnabling = document.getElementById(fieldID);
	if(myID.type=="checkbox") 	fieldToToggleEnabling.disabled = !myID.checked;
	else fieldToToggleEnabling.disabled = (myID.value==null || myID.value=="");
}
function linkForm(checkBoxId, varToChange, actionToDO, FormName)
{
	chekAllUsers(FormName, false)
	chekUser(checkBoxId);
	submitForm(varToChange, actionToDO, FormName);
}

function confirmLinkForm(message, checkBoxId, varToChange, actionToDO, FormName)
{
	if(!confirm(message)) return;
	linkForm(checkBoxId, varToChange, actionToDO, FormName)
}

function validateDate(year, month, day, date)
{
	yearText = document.getElementById(year);
	monthSelect = document.getElementById(month);
	daySelect = document.getElementById(day);

	day=parseInt(String(daySelect.options[daySelect.selectedIndex].value));
	month=parseInt(String(monthSelect.options[monthSelect.selectedIndex].value));
	year=parseInt(String(yearText.value));
	switch(month)
	{
		default		:	
						break;//if(day<0) return false;
		case	2	:	if(day>29) return false;
						if((day==29) && (!isBisextile(year))) return false;
						break;
		case	4	:
		case	6	:
		case	9	:
		case	11	:	if(day>30) return false;
						break;

	}
	
	alterForm(date, String(year)+"-"+String(month)+"-"+String(day));
	return true;
}

//Determine si l'année est bisextile ou pas (29 jours pour février)
function isBisextile(year)
{
	if(!(year % 4)) //Divisible par 4
	{
		if(!(year % 100)) //Divisible par 100
			return ((year % 400)==0) //Si divisible par 400
		else return true
	}
	else return false;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function popUp(arg)
{
	window.open("/popup.php?path="+arg,null,"location=no, menubar=no, toolbar=no; resizable=no, scrollbars, width=666, height=540");
}

function popUpLegals(arg)
{
	window.open("/popup.php?path="+arg,null,"location=no, menubar=no, toolbar=no; resizable=no, scrollbars, width=470, height=340");
}

function printMe()
{
	print();
}

function closeMe()
{
	opener.focus();
	close();
}


function doConfirm(message,action) {
	if (confirm(message)) {
		submitForm(action,'ND')
	}
}
