
var req;
var seldocId;
var fieldswidget='fieldswidget';

function showPopupMod (index, doc, liid){
	var oldseldoc = seldocId;
	changeColorObject(liid,"red");
	seldocId=liid;
	changeColorObject(oldseldoc,"black");
	showFields(index, doc);
}

function changeColorObject(objectId, color){
	if(objectId != seldocId){
		var styleObject = getStyleObject(objectId);
		styleObject.color=color;
	}
}


function showFields(index,doc){
	var fieldsUrl = 'viewDocumentFields?index='+index+'&doc='+doc;
	processAjax(fieldsUrl);
	targetDiv(fieldswidget);
}

function processAjax(url) {
	if (window.XMLHttpRequest) { // Non-IE browsers
		req = new XMLHttpRequest();
		req.onreadystatechange = targetDiv;
		try {
			req.open("GET", url, true);
		} catch (e) {
			alert(e);
		}
		req.send(null);
	} else if (window.ActiveXObject) { // IE
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = targetDiv;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function targetDiv(divname) {
	if (req.readyState == 4) { // Complete
		if (req.status == 200) { // OK response
			getObject(fieldswidget).innerHTML = req.responseText;
		} else {
			alert("Problem: " + req.statusText);
		}
	}
} 

// DRAG fields div

var x,y;
var tx, ty;
var isDrag=false;

var firefox=document.getElementById&&!document.all;

//document.onmousemove=mouseMove;
//document.onmousedown=selectMouse;
document.onmouseup= mouseUp;
function mouseUp(){
	isDrag=false;
}

function selectMouse(e) 
{	
	if (firefox){
		//verify that user clicked the 'titlebar' element 
		var p=e.target;	
		if (p.attributes['id'] && p.attributes['id'].value=="titlebar"){
			//get the widget coordinates from the root, 'htmlfields' element
			tx=parseInt(getStyleObject(fieldswidget).right);
			ty=parseInt(getStyleObject(fieldswidget).top);
			//getObject(fieldswidget).innerHTML+="<strong> tx ty = "+tx+" : "+ty+"</strong>"+"<br/>"+"<strong> x et y = "+x+" : "+y+"</strong>";
			isDrag=true;
			x=e.clientX;
			y=e.clientY;
		}
			
		
	}
	else{		//IE
		//verify that user clicked the 'titlebar' element
		var p=event.srcElement;		
		if (p.attributes['id'] && p.attributes['id'].value=="titlebar"){
			//get the widget coordinates from the root, 'htmlfields' element
			tx=parseInt(getStyleObject(fieldswidget).right);
			ty=parseInt(getStyleObject(fieldswidget).top);
			isDrag=true;
			x=event.clientX;
			y=event.clientY;
		}	
	}
}
	


function mouseMove(e)
{
  if (isDrag)	//only move the box if the user is dragging it
  {
  	
	var box=getObject(fieldswidget);
	
	if (firefox){
		var newX = tx - (e.clientX - x);
		var newY = e.clientY + (ty - y);
		moveObjectRightTop(fieldswidget,newX  , newY);
	}
	else{		//IE
		var newX = tx - (event.clientX - x);
		var newY = event.clientY + (ty - y);
		moveObjectRightTop(fieldswidget, newX , newY);
		
	}    
  }
}

function closeWidget(id){	
	var widget1=getObject(fieldswidget);
	var node=getObject(id);
	widget1.removeChild(node);
}

function moveObjectRightTop(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.right = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} //
