// All credits goes to www.youngpup.net for this excellent script
// (c) www.youngpup.net & http://www.dynamicdrive.com/dynamicindex11/domdrag/index.htm
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

// ----------------------------------------------------------------------------------------------------------
function ajasm_create() {
	var ajasm_obj = false
	if (window.XMLHttpRequest) {
		ajasm_obj = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		try { ajasm_obj = new ActiveXObject("Msxml2.XMLHTTP") } 
		catch (e) {
			try { ajasm_obj = new ActiveXObject("Microsoft.XMLHTTP")	}
			catch (e) {}
		}
	}	else return false;
	return ajasm_obj;
}
// ----------------------------------------------------------------------------------------------------------
function ajasm_showbox(cibleid) {
	document.getElementById(cibleid).style.display='block';
}
function ajasm_closebox(cibleid) {
	document.getElementById(cibleid).style.display='none';
}

// ----------------------------------------------------------------------------------------------------------

function ajasm_SetCookie(formobj, cname, cval) {
	ajasm_obj=ajasm_create();
	ajasm_obj.open('GET', 'ajasm_setcookie.php?cname='+cname+'&cval='+cval, true);
	ajasm_obj.onreadystatechange = function() {   
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			//formobj.submit();
		}
	}   
	ajasm_obj.send(null);
}


function ajasm_verify() {
	kw=document.getElementById('q10').value;
	ajasm_obj=ajasm_create();
	ajasm_obj.open('GET', 'ajasm_inventory.php?uxd='+uxd+'&s='+kw, true);
	ajasm_obj.onreadystatechange = function() {   
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			return true;
		}
	}   
	ajasm_obj.send(null);
}


// ----------------------------------------------------------------------------------------------------------

function ajasm_preview(num) {
	ajasm_obj=ajasm_create();
	ajasm_obj.open('GET', 'ajasm_preview.php?uxd='+uxd+'&num='+num, true);

	ajasm_obj.onreadystatechange = function() {   
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			document.getElementById("box04_content").innerHTML='';
			document.getElementById("box04_content").innerHTML=ajasm_obj.responseText;
			ajasm_showbox("box04_root");
		}
	}   
	ajasm_obj.send(null);
}

function ajasm_movepreview(source_id) {
	mx=ajasm_getoffset(source_id, "left")+80;
	my=ajasm_getoffset(source_id, "top")-60;

	document.getElementById("box04_root").style.position='absolute';
//	document.getElementById("box04_root").style.left=mx+"px";
	document.getElementById("box04_root").style.top=my+source_id.offsetHeight+"px";
}


// ----------------------------------------------------------------------------------------------------------

function ajasm_updatecat(f, srub, scat, current_cat) {
	document.getElementById(scat).style.backgroundColor="#ffffd0";
	var l1    = f.elements[srub];
	var l2    = f.elements[scat];

	var index = l1.selectedIndex;
	if(index < 1) {
		l2.options.length = 0;
		return false;
	}

	ajasm_obj=ajasm_create();
	ajasm_obj.open("POST", "ajasm_categorie.php", true);
		
	ajasm_obj.onreadystatechange = function anonymous() {
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			eval(ajasm_obj.responseText);
			document.getElementById(scat).value=current_cat;
			document.getElementById(scat).style.backgroundColor="#ffffff";
		}
	}
	
	ajasm_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "uxd="+uxd+"&srub="+escape(l1.options[index].value)+"&frm="+f.name+"&scat="+scat;
	ajasm_obj.send(data);
}

// ----------------------------------------------------------------------------------------------------------


function ajasm_updateregions(f, spays, sregion, current_region) {
	document.getElementById(sregion).style.backgroundColor="#ffffd0";
	var l1    = f.elements[spays];
	var l2    = f.elements[sregion];

	var index = l1.selectedIndex;
	if(index < 1) {
		l2.options.length = 0;
		return false;
	}

	ajasm_obj=ajasm_create();
	ajasm_obj.open("POST", "ajasm_regions.php", true);
		
	ajasm_obj.onreadystatechange = function anonymous() {
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			eval(ajasm_obj.responseText);
			document.getElementById(sregion).value=current_region;
			document.getElementById(sregion).style.backgroundColor="#ffffff";
		}
	}
	
	ajasm_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "uxd="+uxd+"&sp="+escape(l1.options[index].value)+"&frm="+f.name+"&select="+sregion+"&cr="+current_region;
	ajasm_obj.send(data);
}

function ajasm_setregion(formobj, coderegion) {
	ajasm_obj=ajasm_create();
	ajasm_obj.open('GET', 'ajasm_setregion.php?uxd='+uxd+'&cval='+coderegion, true);
	ajasm_obj.onreadystatechange = function() {   
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			formobj.submit();
		}
	}   
	ajasm_obj.send(null);
}



// --------------------------------------------

function ajasm_inject(ajasm_obj, dest_id) {
	reponse=ajasm_obj.responseText;
	cible=document.getElementById(dest_id);
	cible.innerHTML=reponse;
}

function ajasm_display(cible) {
		document.getElementById(cible).display='block';
}
function ajasm_hide(cible) {
		document.getElementById(cible).display='hidden';
}

function ajasm_getoffset(what, offsettype) {
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null) {
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}


function ajasm_send(keyword, dest_id) {
	// suggest
	ajasm_obj=ajasm_create();

	ajasm_obj.open('GET', 'ajasm_suggest.php?uxd='+uxd+'&s='+keyword, true); // mode asynchrone 

	ajasm_obj.onreadystatechange = function() {   
		if ((ajasm_obj.readyState == 4)&&(ajasm_obj.status==200)) {
			if (ajasm_obj.responseText!='') {
				ajasm_showbox('suggest_box');
				ajasm_display(dest_id);
				ajasm_inject(ajasm_obj, dest_id);
			} else {
				ajasm_closebox('suggest_box');
			}
		}
	}   
	ajasm_obj.send(null);
}

function ajasm_suggest_click(obj) {
	document.getElementById('q10').value=obj.innerHTML;
}

function ajasm_suggest_select(obj) {
	obj.style.backgroundColor="#DFF2E2";
}

function ajasm_suggest_unselect(obj) {
	obj.style.backgroundColor="";
}

function ajasm_suggest(source_id) {
	cible='suggest_box';
	query=source_id.value;
	dropmenuobj=document.getElementById(cible);
	
	document.onclick = function() { dropmenuobj.style.visibility='hidden'; }
	document.body.onresize = function() { dropmenuobj.style.visibility='hidden'; }
	source_id.onclick =	function() { setTimeout("dropmenuobj.style.visibility='hidden'",20) }
	dropmenuobj.onclick =		function() { dropmenuobj.style.visibility='hidden'; }

	dropmenuobj.style.visibility='visible';

	dropmenuobj.x=ajasm_getoffset(source_id, "left");
	dropmenuobj.y=ajasm_getoffset(source_id, "top");

	dropmenuobj.style.left=dropmenuobj.x+"px";
	dropmenuobj.style.top=dropmenuobj.y+source_id.offsetHeight+"px";

	ajasm_send(query, cible);
	//document.getElementById('suggest_box').innerHTML=qe;	
}
