var g_COLUMNCLASS="npcol";
var g_COMPONENTCLASS="npcomp";
var g_CONTENTCLASS="npcontent";
var g_ActiveComp;
var g_ActiveContent;
var g_UpdatePosition = false;
///////// Net.Portal DOM function ///////////////////
function getComponentParent(div)
{
	var ndiv = div;
	while(ndiv && ndiv.className!=g_COMPONENTCLASS)
		ndiv = jsParentNode(ndiv);

	return ndiv;
}

function getColumnParent(div)
{
	var ndiv = div;
	while(ndiv && ndiv.className!=g_COLUMNCLASS)
		ndiv = jsParentNode(ndiv);
	return ndiv;
}

function getChildContent(div)
{
	var divs = div.getElementsByTagName("DIV");
	var child = null;
	for(var i=0;i<divs.length && child==null;i++)
		if(divs[i].className == g_CONTENTCLASS)
			child = divs[i];
	return child;
}

function getAssociatedContent(div)
{
	return getChildContent(getComponentParent(div));
}

function getColNum(comp)
{
	var col=getColumnParent(comp);
	var divs = document.getElementsByTagName("DIV");
	var num = -1;
	var k=0;
	for(var i=0;i<divs.length && num<0;i++)
	{
		if(divs[i] == col) num = k;
		if(divs[i].className == g_COLUMNCLASS) k++;
	}
	return num;
}

function getOrder(comp)
{
	var col =getColumnParent(comp);
	var divs = col.getElementsByTagName("DIV");
	var num = -1;
	var k=0;
	for(var i=0;i<divs.length && num<0;i++)
	{
		if(divs[i] == comp) num = k;
		if(divs[i].className == g_COMPONENTCLASS) k++;
	}
	return num;
}

////////  Component Drag and Drop function //////////
function componentDnD(evt, div)
{
	if(!evt)
		evt = window.event;

	var ndiv = getComponentParent(div);
	if(ndiv)
	{
		//assign the dnd functions
		ndiv.dragStart = startComponentDnd;
		ndiv.dragMove = moveComponentDnd;
		ndiv.dragEnd = endComponentDnd;
		//launch the dnd
		g_DnD.start(ndiv,evt);
		g_UpdatePosition = true;
	}
	return false;
}

function startComponentDnd(dnd)
{
	var styl = dnd.objlnk.style;
	//define the dynamic properties
	styl.position = "absolute";
	styl.pixelWidth = 250;
	//define the new position
	styl.pixelTop = dnd.y-15;
	styl.pixelLeft = dnd.x-125;
	styl.filter = "alpha(opacity=50)";

}

function moveComponentDnd(dnd)
{
	moveLayerBy(dnd.objlnk,dnd.dx,dnd.dy);
}

function endComponentDnd(dnd)
{
	//move the component, to avoid problem if the function elementFromPoint
	//return the actual dragged element
	dnd.objlnk.style.pixelTop = dnd.y+10;
	dnd.objlnk.style.pixelLeft = dnd.x+10;

	var ndiv = document.elementFromPoint(dnd.cx,dnd.cy);
	var comp = getComponentParent(ndiv);
	if(!comp)
	{
		var col = getColumnParent(ndiv);
		if(col)
			col.appendChild(dnd.objlnk.removeNode(true));
	}
	else
	{
		var node = dnd.objlnk.removeNode(true);
		var col = getColumnParent(comp);

		//check the component not the drag element and a moveable element
		if(!comp.ISMOVEABLE)
		{
			var divs = col.getElementsByTagName("DIV");
			comp = null;
			for(var i=0;i<divs.length && !comp;i++)
				if(divs[i].ISMOVEABLE)
					comp = divs[i];
		}
		if(comp)
			col.insertBefore(node,comp);
		else
			col.appendChild(node);
	}

	dnd.objlnk.style.position = "static";
	dnd.objlnk.style.width = dnd.objlnk.parentNode.style.width;
	dnd.objlnk.style.filter = "alpha(opacity=100)";
	document.selection.empty();
}

///////////////////  Reload functions /////////////////////

function reload(elm,idcomptab,idcomp,bReloadContainer,optionalS)
{
	var div = (bReloadContainer) ? getComponentParent(elm) : getAssociatedContent(elm);
	var content = getAssociatedContent(elm);
	var fctStr = "window.NP_" + content.getAttribute('name') +"_Init";
	var fct = eval(fctStr) ? fctStr + "(" + idcomptab + ")" : null;
	
	//Use for components info and publication for navigation (next and previous)
	var adds = "";
	if (optionalS != null)
		adds = "&options=" + optionalS;
		
	var url=g_AbsPath+"flush.jsp?idtabcomp="+idcomptab + ((bReloadContainer) ? "&reloadcontainer=" : "") + adds;
	div.innerHTML = "<IMG SRC=\""+g_AbsPath +"shared/img/clock.gif\">&nbsp;loading...";
setTimeout(function(){
	g_HttpRequest.queuing(div,url,bReloadContainer,fct);
},110);	
}

function openDynamicProfile(elm,url,name,w,h,size,scroll)
{
	g_ActiveComp = getComponentParent(elm);
	g_ActiveContent = getAssociatedContent(elm);
	openCenterWnd(url,name,w,h,size,scroll);
}

function save_display(wndsrc,wndfrm,nexttab,url,bforce)
{
	if(g_UpdatePosition || bforce)
	{
		var frm = wndfrm.document.forms['defaultpage_saving'];
		var str ="<root>\n";
		str+="<tab id=\""+frm.idtab.value+"\">\n";
		var divs = wndsrc.document.getElementsByTagName("DIV");
		var divsComp;
		var colnum=0;
		for(var i=0;i<divs.length;i++)
		{
			if(divs[i].className==g_COLUMNCLASS)
			{
				var order=0;
				var col = divs[i];
				divsComp = col.getElementsByTagName("DIV");
				for(var j=0;j<divsComp.length;j++)
				{
					if(divsComp[j].className==g_COMPONENTCLASS)
					{
						var state;
						if(divsComp[j].style.display == "none")
							state = "close";
						else
						{
							var divContent = getChildContent(divsComp[j]);
							state = (divContent.style.display=="none") ? "hidden" : "visible";
						}
						str+="<comp idtabcomp=\""+divsComp[j].getAttribute('IDTABCOMP')+"\" order=\""+order+"\" col=\""+colnum+"\" state=\""+state+"\" idcomp=\""+divsComp[j].getAttribute('IDCOMP')+"\"/>\n";
						order++;
					}
				}
				colnum++;
			}
		}

		str+="</tab>\n";
		str+="</root>";

		frm.elements['xml'].value = str;
		if(nexttab!=null) frm.elements['taborder'].value = nexttab;
		if(url!=null) frm.elements['url'].value = url;
		frm.submit();
	}
	else
	{
		window.location = g_AbsPath +"defaultpage.jsp?idtab="+nexttab;
	}
}


//////////////////// Javascript http requester //////////////////////
var g_HttpRequest = new HttpRequest;

function HttpRequest_checkState()
{
	var obj = g_HttpRequest;
	var state = obj.xmldoc.readyState;
	var ok;
	var text;

	ok = false;
	if (state == 4)
	{
		var err = obj.xmldoc.parseError;

		if(obj.inProcess.div)
		{
			if (err.errorCode != 0)
				 text = "<FONT COLOR=\"#FF0000\">" + err.reason+"</FONT>";
			else
			{
				ok = true;

//alert("obj.inProcess.outer= "+obj.inProcess.outer);

				if(!obj.inProcess.outer)
					text = obj.xmldoc.text;
				else
					text = HttpRequest_innerDivCode(obj.xmldoc.text);
			}
//alert(text);

			if(text=="#REFRESH#")
				window.location.reload(true);
			else
			{
				//alert("ramen"+obj.inProcess.div.innerHTML);
				obj.inProcess.div.innerHTML = text;
				//alert("lamend");
				if(ok && obj.inProcess.onload)
					try
					{
						eval(obj.inProcess.onload);
					}
					catch(ex){}
			}
		}
		else if (err.errorCode != 0)
			alert(err.reason);
		obj.inProcess = null;
		obj.load();
	}
}
function HttpRequest_innerDivCode(cod)
{
	var text = cod;
	var end1,end2;
	text = text.substring(text.indexOf(">")+1);
	end1 = text.indexOf("</div>");
	while(end1>=0)
	{
		end2 = end1;
		end1 = text.indexOf("</div>",end2+1);
	}
	return text.substring(0,end2);
}

function HttpRequest()
{
	//this.xmldoc = new ActiveXObject("Microsoft.XMLDOM");
	this.xmldoc = getXMLDOM();
	this.xmldoc.onreadystatechange = HttpRequest_checkState;
	this.queue = new Array();
	this.inProcess = null;
}

HttpRequest.prototype.queuing = function(div,url,outer,onload)
{
	this.queue.addElement(new HttpRequestElement(div,url,outer,onload));
	this.load();
}

HttpRequest.prototype.load = function()
{
	if(this.inProcess==null && this.queue.size()>0)
	{
		this.inProcess = this.queue.elementAt(0);
		this.queue.removeElementAt(0);
		this.xmldoc.load(this.inProcess.url);
	}
}

function HttpRequestElement(div,url,outer,onload)
{
	this.div = div;
	this.url = url;
	this.outer = outer;
	this.onload = onload;
}