/* Completion */
var currentLiCompletion=0;
var liMaxCompletion=0;

function initCompletion(idInput,maxLi)
{
	var idPseudoSelect=idInput+"_NovaCompletion";
	liMaxCompletion=maxLi;
	currentLiCompletion=1;
	novUtils.$(idPseudoSelect).style.left=novUtils.getLeft(novUtils.$(idInput));
	novUtils.$(idPseudoSelect).style.top=novUtils.getTop(novUtils.$(idInput))+25;
	var les_lis = novUtils.$(idPseudoSelect).getElementsByTagName('li');
	var nb_lis=les_lis.length;
	for (var i = 0; i < nb_lis;i++)
	{
		les_lis[i].onmouseover=function () {
				this.className='over';
			}
		les_lis[i].onmouseout=function () {
				this.className='';
			}
		les_lis[i].onclick=function () {
				novUtils.$(idInput).value=this.firstChild.nodeValue;
				novUtils.$(idPseudoSelect).parentNode.removeChild(novUtils.$(idPseudoSelect));
				currentLiCompletion=1;
				liMaxCompletion=0;
			}
	}
	var bool=1;
	if (novUtils.$(idPseudoSelect))
	{
		novUtils.addEvent(novUtils.$(idPseudoSelect),'mouseover',function() {
				bool=0;
			}
		);
		novUtils.addEvent(novUtils.$(idPseudoSelect),'mouseout',function() {
				bool=1;
			}
		);
	}
	novUtils.addEvent(novUtils.$(idInput),'blur',function() {
			if (bool==1)
				if (novUtils.$(idPseudoSelect))
					novUtils.$(idPseudoSelect).style['display']="none";
		}
	);	
}

function handlerInputCompletion(e)
{
	var touche=0;
	var targ=novUtils.getEvtTarget(e); 
	var idInput=targ.getAttribute('id');
	var idPseudoSelect=idInput+"_NovaCompletion";
	
	if (window.ActiveXObject)
		touche=window.event.keyCode;
	else
		touche=e.keyCode;
	switch(touche)
	{
		case 38: break;
		case 40: break;
		case 9:  break;
		case 13: validate(); break;
		case 37: break;
		case 16: break;
		case 27: break;
		case 39: break;
		default:  xajax.call("getNovaCompletion",new Array(idInput,targ.value),1);break;
	}
	function validate()
	{
		if (novUtils.$(idPseudoSelect))
		{
			novUtils.$(idInput).value=novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).firstChild.nodeValue;
			novUtils.$(idPseudoSelect).parentNode.removeChild(novUtils.$(idPseudoSelect));
		}
		currentLiCompletion=1;
		liMaxCompletion=0;
		return false;
	}
}
function TabulateKeyDown(e)
{
	var touche=0;
	var targ=novUtils.getEvtTarget(e);
	var idInput=targ.getAttribute('id');
	var idPseudoSelect=idInput+"_NovaCompletion";
	if (window.ActiveXObject)
		touche=window.event.keyCode;
	else
		touche=e.keyCode;
	switch(touche)
	{
		case 9: tabu(); break;
		case 13: return false; break;
		case 38: haut();  break;
		case 40: bas();    break;
	}
	function tabu()
	{
		if (novUtils.$(idPseudoSelect))
		{
			novUtils.$(idInput).value=novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).firstChild.nodeValue;
			novUtils.$(idPseudoSelect).parentNode.removeChild(novUtils.$(idPseudoSelect));
		}
		currentLiCompletion=1;
		liMaxCompletion=0;
	}
	function haut()
	{
		if (currentLiCompletion>1)
		{
			novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).className="";
			currentLiCompletion-=1;
			novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).className="over";
			if (novUtils.getTop(novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion))< novUtils.getTop(novUtils.$(idPseudoSelect))+parseInt(novUtils.$(idPseudoSelect).scrollTop))
				novUtils.$(idPseudoSelect).scrollTop=parseInt(novUtils.$(idPseudoSelect).scrollTop)-parseInt(novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).offsetHeight);
			
		}else
			novUtils.$(idPseudoSelect).scrollTop=0;
	}
	function bas()
	{
		if (currentLiCompletion<liMaxCompletion)
		{
			if (currentLiCompletion!=0)
				novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).className="";
			currentLiCompletion+=1;
			novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).className="over";
			if (novUtils.getTop(novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion))+parseInt(novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).offsetHeight)> novUtils.getTop(novUtils.$(idPseudoSelect))+parseInt(novUtils.$(idPseudoSelect).offsetHeight)+parseInt(novUtils.$(idPseudoSelect).scrollTop))
				novUtils.$(idPseudoSelect).scrollTop=parseInt(novUtils.$(idPseudoSelect).scrollTop)+parseInt(novUtils.$(idPseudoSelect+'_li_'+currentLiCompletion).offsetHeight);
				
		}else
			novUtils.$(idPseudoSelect).scrollTop=9999;
	}
}

function setCompletion(idInput)
{
	novUtils.addEvent(novUtils.$(idInput),'keyup',handlerInputCompletion);
	novUtils.addEvent(novUtils.$(idInput),'keydown',TabulateKeyDown);
}
/* Fin - completion */


/* SortableList */
 var DragUtils = {
	swap : function(item1, item2) {
		var parent = item1.parentNode;
		parent.removeChild(item1);
		parent.insertBefore(item1, item2);
		item1.style["top"] = "0px";
		item1.style["left"] = "0px";
	},
	nextItem : function(item) {
		var sibling = item.nextSibling;
		while (sibling != null) {
			if (sibling.nodeName == item.nodeName) return sibling;
			sibling = sibling.nextSibling;
		}
		return null;
	},
	previousItem : function(item) {
		var sibling = item.previousSibling;
		while (sibling != null) {
			if (sibling.nodeName == item.nodeName) return sibling;
			sibling = sibling.previousSibling;
		}
		return null;
	}
};


function item(elmtHTML,infos) {
	var currentY = 0;
	var currentObject= false;
	var decalageTopElement=0;
	/**/
	novUtils.addEvent(elmtHTML,'mousedown',onMouseDown);
	elmtHTML.style["position"]="relative";
	elmtHTML.style["top"]="0px";
	/**/
	function onMouseDown(e) {
		var targ=novUtils.getEvtTarget(e); 
		if (targ!=elmtHTML) return false;
		currentObject=elmtHTML;
		currentY = (window.ActiveXObject) ? (window.event.clientY + document.body.scrollTop)  : e.pageY ;
		decalageTopElement=(window.ActiveXObject) ? (window.event.offsetY)  : e.layerY ;
		novUtils.addEvent(document,'mousemove',onMouseMove);
		novUtils.addEvent(document,'mouseup',onMouseUp);
		infos.ondown(currentObject);
		return false;
	}
	/**/
	function onMouseMove(e) {
			var difY = (window.ActiveXObject) ? (window.event.clientY+document.body.scrollTop - novUtils.getTop(currentObject)) : e.pageY-novUtils.getTop(currentObject) ;
			var newY1 = parseInt(currentObject.style["top"])+parseInt(difY)-decalageTopElement;
			currentY = (window.ActiveXObject) ? (window.event.clientY + document.body.scrollTop)  : e.pageY ;
			var item = currentObject;
			var next = DragUtils.nextItem(item);
			while (next != null && currentObject.offsetTop >= next.offsetTop - 2) {
				var item = next;
				var next = DragUtils.nextItem(item);
			}
			if (currentObject != item) {
				DragUtils.swap(currentObject, next);
				return;
			}
			var item = currentObject;
			var previous = DragUtils.previousItem(item);
			while (previous != null && currentObject.offsetTop <= previous.offsetTop + 2) {
				var item = previous;
				var previous = DragUtils.previousItem(item);
			}
			if (currentObject != item) {
				DragUtils.swap(currentObject, item);
				return;
			}
			currentObject.style["top"]=newY1+"px";
		return false;
	}
	/**/
	function onMouseUp(e) {
		infos.onup(currentObject);
		currentObject.style["top"] = "0px";
		currentObject.style["left"] = "0px";
		novUtils.removeEvent(document,'mousemove',onMouseMove);
		novUtils.removeEvent(document,'mouseup',onMouseUp);
		infos.send();
		return false;
	}
}
function ListeDragAndSort(divConteneur,classForFils,tagForFils,TableSQL,IDTableSQL,IDTriTableSQL,inputTriName,FormId,ajaxRegistryFunction,FnDown,FnUp)
{
	this.Items=new Array();
	this.infosAjax = {
		SqlTable    : TableSQL,
		IdSqlTable  : IDTableSQL,
		IdTriTable  : IDTriTableSQL,
		nameFormTri : inputTriName,
		IdForm : FormId,
		registry : ajaxRegistryFunction,
		ondown : FnDown,
		onup : FnUp,
		register : function() { xajax.call(ajaxRegistryFunction, arguments, 1) },
		send :function() { this.register(this.SqlTable,this.IdSqlTable,this.IdTriTable,this.nameFormTri,xajax.getFormValues(this.IdForm));}
	}
	var tab=novUtils.getElementsByClassName(classForFils,tagForFils,novUtils.$(divConteneur));
	for(var i=0;i<tab.length;i++) this.Items.push(new item(tab[i],this.infosAjax));
}
/* Fin Sort list Ajax */


/* Anim Add Panier */
var move2panierStruct = {
	objDepart : false,
	objDestination : false,
	xajaxFunctionName : false,
	xajaxFunctionArgs :false,
	vitesse : 25,
	alpha : 50,
	move : function() {
		if (this.flyingDiv)	this.update();
		if ((this.objDepart)&&(this.objDestination))
			this.moveFunction();
		else
			return false;
	},
	moveFunction : function() { 
		if(!this.isMoving){
			this.flyingDiv = this.objDepart.cloneNode(true); 
			document.body.appendChild(this.flyingDiv);
			this.flyingDiv.style['position'] = 'absolute';
			if (window.ActiveXObject) this.flyingDiv.style['filter']="alpha(opacity="+(this.alpha)+")";
		else this.flyingDiv.style['opacity']=(this.alpha/100);
			this.flyingDiv.style.left =novUtils.getLeft(this.objDepart) + 'px';
			this.flyingDiv.style.top = novUtils.getTop(this.objDepart) + 'px';
			this.curX=novUtils.getLeft(this.objDepart);
			this.curY=novUtils.getTop(this.objDepart);
			this.flyingDiv.style.display='block';
			this.flyingDiv.style.width = this.objDepart.offsetWidth + 'px';
		}
		this.diffX=novUtils.getLeft(this.objDestination)-novUtils.getLeft(this.objDepart);
		this.diffY=novUtils.getTop(this.objDestination)-novUtils.getTop(this.objDepart);
		this.flyToBasket();
	},
	isMoving : false,
	flyingDiv : false,
	diffY : 0,
	diffX : 0,
	curX  : 0,
	curY  : 0,
	flyToBasket: function() {
		if (this.flyingDiv)
		{
			var maxDiff = Math.max(Math.abs(this.diffX),Math.abs(this.diffY));
			var moveX = (this.diffX / maxDiff) * this.vitesse;
			var moveY = (this.diffY / maxDiff) * this.vitesse;
			this.curX+=moveX;this.curY+=moveY;
			this.flyingDiv.style.left = Math.round(this.curX) + 'px';
			this.flyingDiv.style.top = Math.round(this.curY) + 'px';
			if(moveX>0 && this.curX > novUtils.getLeft(this.objDestination)) this.update();
			if(moveX<0 && this.curX < novUtils.getLeft(this.objDestination)) this.update();
			if((this.flyingDiv)&&(this.flyingDiv.style.display=='block')) setTimeout('move2panierStruct.flyToBasket()',10); 
		}
	},
	update : function() {
		this.clean();
		if (this.xajaxFunctionName) xajax.call(this.xajaxFunctionName,this.xajaxFunctionArgs,1);
	},
	clean : function() {
		this.flyingDiv.style.display='none';
		this.isMoving=false;
		this.diffX=0;
		this.diffY=0;
		this.curY=0;
		this.curX=0;
		this.flyingDiv=false;
	}
}
/* Fin - Anim Add Panier */