// variables
var mouseover = false;
var scrolldirection;
var scrollratio;
var theheartbeat, scrolling;
var menuOffsetTop = menuOffsetLeft = menuMaxHeight = 0;
var currentX = currentY = 0;
var whichIt = null;

// switch direction scroller will move while pressing a button
function switchit(obj, cmd)
{
	if (cmd == 'in')
	{
		mouseover = true;
		if (obj.getAttribute('id') == "scrollup")
		{
			scrolldirection = -1;
		}
		else
		{
			scrolldirection = 1;
		}
	}
	else
	{
		mouseover = false;
	}
}

function scroll()
{
	if (((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
	{
		scrollbox.style.pixelTop += scrolldirection;
	}
}

function heartBeat()
{
	data.style.pixelTop = -1 * scrollratio * (scrollbox.style.pixelTop + menuOffsetTop - menuOffsetTop);
}

function grabIt(e) {
	if (event.button == 1)
	{
		if (mouseover)
			scrolling = window.setInterval("scroll();",1);
		else
		{
			whichIt = event.srcElement;
			while (whichIt.id.indexOf("scrollbox") == -1) {
				whichIt = whichIt.parentElement;
				if (whichIt == null) { return true; }
		    }
//			whichIt.style.pixelLeft = whichIt.offsetLeft;
		    whichIt.style.pixelTop = whichIt.offsetTop;
			currentX = (event.clientX + document.body.scrollLeft);
			currentY = (event.clientY + document.body.scrollTop);
		}
	
		theheartbeat = window.setInterval("heartBeat()",1);
	}
}

function moveIt(e) {
	if (whichIt == null) { return false; }
	newX = (event.clientX + document.body.scrollLeft);
    newY = (event.clientY + document.body.scrollTop);
    distanceX = (newX - currentX);
	distanceY = (newY - currentY);
    currentX = newX;
	currentY = newY;
	if (((scrollbox.style.pixelTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
	{
    	scrollbox.style.pixelTop += distanceY;
	}
	event.returnValue = false;
	return false;
}

function dropIt() {
	clearInterval(scrolling);
	clearInterval(theheartbeat);
	whichIt = null;
}

function init()
{
	// events
	document.onmousedown = grabIt;
	document.onmousemove = moveIt;
	document.onmouseup = dropIt;
	menuOffsetTop = floater.style.pixelTop;
	menuMaxHeight = floater.style.pixelHeight;
	
	getscrollinc();
}

function getscrollinc()
{
	var contentinc;
	var scrollerinc;
	var contentHeight = data.offsetHeight;
	var scrollerHeight = content.style.pixelHeight;

	if (scrollerHeight < contentHeight)
	{
		contentinc = content.style.pixelHeight / contentHeight;
		scrollerinc = floater.style.pixelHeight*contentinc;
		scrollratio = contentHeight / floater.style.pixelHeight;
	}
	else
	{
		scrollerinc = floater.style.pixelHeight;
		contentinc = 1;
	}
	
	scrollbox.style.height = scrollerinc;
}

//******************************************************************************************//
	
function Tovr(id)
	{
		document.getElementById('tr'+id).style.backgroundColor = '#CCCCFF';
	}
	
function Tout(id, color)
	{
		document.getElementById('tr'+id).style.backgroundColor = color;
	}

function soumettre(nomform,nomobj)
	{
	if (document.forms[nomform].elements[nomobj].selectedIndex != 0)	
		document.forms[nomform].submit();
	else
		alert (document.forms[nomform].elements[nomobj].options[document.forms[nomform].elements[nomobj].selectedIndex].text);

	}
	
function soumettre_bis(nomform,nomobj,radio)
	{
	if (document.forms[nomform].elements[nomobj].selectedIndex != 0)	
		{
		document.forms[nomform].elements[radio].click();
		
		//alert("Bouton_Radio: " + document.forms[nomform].elements[radio].name + "|   NomFormulaire: " + nomform+ "|   NomObjet: " + nomobj+ "|   Radio: " + radio);
		}
	else
		alert (document.forms[nomform].elements[nomobj].options[document.forms[nomform].elements[nomobj].selectedIndex].text);

	}

function soumettre_ter(nomform,nomobj,val)
	{
	
		document.liste4.niveau0.value = val;
		document.liste4.submit();
	
	}
function soumettre_codeae(val)
	{
	
		document.liste4.codeae.value = val;
		document.liste4.submit();
	}
function submitbyyear(nomform)
	{
	if (document.forms[nomform].niveau0.selectedIndex != 0)	
		{	
			document.forms[nomform].niveau0.value = document.liste0.niveau0.value;
			document.forms[nomform].submit();	
		}
	else
		alert (document.forms[nomform].niveau0.options[document.forms[nomform].niveau0.selectedIndex].text);
	}
	
function ordonner(val)
	{
		if (document.donnees.ordre.value == 0)
			document.donnees.ordre.value = 1;
		else
			document.donnees.ordre.value = 0;
			
		document.donnees.trie.value = val;
		
		document.donnees.submit();
	}
	
function go(num)
	{
		document.donnees.aPartir.value = (num-1)*20;
		document.donnees.submit();
	}
	
function selection(nomform,obj,variable,val)
	{
		nomform.obj.variable.value = val;
		nomform.submit();
	}
	
function debutpage(num)
	{
		document.donnees.aPartir.value = ((num-1)*20)*10;
		document.donnees.page.value = num;
		document.donnees.submit();
	}
	
function finpage(num,fin)
	{
		document.donnees.aPartir.value = ((num-1)*20);
		document.donnees.page.value = fin;
		document.donnees.submit();
	}
	
function movepic(img_name,img_src) 
	{
	document[img_name].src=img_src;
	}
	
function test_browser()
	{
	var nav=navigator.appName;
	return nav;
	}

function verification_choix() {
	var i;
	var premier = 0;
	var maChaine;
	
	maChaine = "";
	
	for (i=0; i< document.liste2.niveau2.length;i++)
	{
	 if(document.liste2.niveau2.options[i].selected == true)
	 {
	 	if (premier == 0)
	 		maChaine = document.liste2.niveau2.options[i].value;
	 	else
	  	maChaine = maChaine + "," + document.liste2.niveau2.options[i].value;
	  
	  premier = 1;
	 }
	}
	
	document.liste2.niveau_choix.value=maChaine;
	
	//soumettre("liste2","niveau2");
	//document.liste2.submit();
 	return true;
}	