
function js_getParentNode(obj,name,className){
	//return the first parent node with nodeName 'name' and className 
	var node=obj
	while (node) {
		if (node.nodeName.toUpperCase()==name.toUpperCase() && (className=='' || className==node.className)) return node;
		node=node.parentNode;
	}
	return false;
}
function js_getNextNode(obj,name,className){
	//return the next sibling node with nodeName 'name' and className 
	var node=obj.nextSibling;
	while (node) {
		if (node.nodeName.toUpperCase()==name.toUpperCase() && (className=='' || className==node.className)) return node;
		node=node.nextSibling;
	}
	return false;
}
function js_isChild(child,parent){
	//Return true if 'child' is a child node of 'parent'
	//PAS POSSIBLE?? A VOIR
	var node=child;
	while(node) {
		if(node===parent) {alert(true);return true;}
		node=node.parentNode;
	}
	alert(false);
	return false;
}
function js_menuClick(obj){
	//If modal mode, close all other menus
	var node=js_getParentNode(obj,"div","");
	if (node && node.getAttribute("modal")=="yes") {
		//Removing the "selected" class on all p and put selected on obj node
		ps=node.getElementsByTagName('p');
		for (i=0;i<ps.length;i++) if (ps[i].className=="selected") {ps[i].className=""}
		obj.className="selected";
		uls=node.getElementsByTagName('ul');
		for (i=0;i<uls.length;i++) if (uls[i].parentNode.nodeName!="DIV") {uls[i].style.display='none';}
		node=obj;
		while(node) {
			if (node.nodeName=="UL") node.style.display='';
			node=node.parentNode
		}
	}
	var nodes=obj.parentNode.childNodes;		//Click on the P -> parent node is LI
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].nodeName.toUpperCase()=='UL') nodes[i].style.display=(nodes[i].style.display=='none')?'':'none';
	}
	return false;
}
function js_getNodePath(node) {
	var path="";
	while (node) {
		path=node.nodeName+" > "+path;
		node=node.parentNode;
	}
	return path;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function js_setInputEvents(obj) {
	//Set events to a 'input' element where when user click, clear the value, etc.. like a search on 
	obj.setAttribute('default',obj.getAttribute('value'));
	obj.className='inputWait';
	obj.onclick=function () {if (this.value==this.getAttribute('default')) {this.value="";this.className='inputType'}}
	obj.onblur=function () {if (this.value=="") {this.value=this.getAttribute('default');this.className='inputWait'}}
	obj.onchange=function () {if (this.value!=this.getAttribute('default')) {js_getParentNode(this,"form","").submit()}}
}

function js_changeOpac(obj,opacity)  {  
	obj.style.opacity = (opacity / 100);  
	obj.style.MozOpacity = (opacity / 100);  
	obj.style.KhtmlOpacity = (opacity / 100);  
	obj.style.filter = "alpha(opacity=" + opacity + ")";
}

function js_changeFontSize(increment) {
	if(document.getElementsByTagName('body')[0].style.fontSize)
		fs=parseInt(document.getElementsByTagName('body')[0].style.fontSize);
	else
		fs=11;
	fs=fs+increment;
	document.getElementsByTagName('body')[0].style.fontSize=""+fs+"px";
}

function js_showhideNextNode(obj) {
	//obj must have a attribute 'node' which will be the main 'parent node'
	//if obj does not have an attribute 'childnode', the show/hide will address next node of the 'parent node' (nextSibling)
	//if obj has an attribute 'childnode', the show/hide will address the first childnode of the 'parent node'
	//if obj has a class name 'stats_down' or 'stats_up', there is an automatic switch
	//if obj
	if (obj.className=='stats_down') obj.className='stats_up'; else if (obj.className=='stats_up') obj.className='stats_down';
	if (node=js_getParentNode(obj,obj.getAttribute('node'),'')) {
		if (obj.getAttribute('childnode')) {
			nextnode=obj.getAttribute('childnode')
			node=node.firstChild
		} else {
			nextnode=obj.getAttribute('node')
		}
		while (node=node.nextSibling) {
			if(node.nodeName.toLowerCase()==nextnode) {if(node.nodeName=='TBODY2') js_slide(node); else node.style.display=(node.style.display=='none')?'':'none';break;}
		}
		if (obj.getAttribute('node')=='tr') {
			if (node.getElementsByTagName('img').length && node.getElementsByTagName('img')[0].getAttribute('location')) node.getElementsByTagName('img')[0].src=node.getElementsByTagName('img')[0].getAttribute('location');
		}
	}
}
function js_slide(obj){
	var display=obj.style.display;
	obj.setAttribute('C',50)
	obj.style.display='';
	obj.style.height='auto';
	//alert(obj.offsetHeight)
	obj.setAttribute('H',obj.offsetHeight)
	obj.style.overflow='hidden'
	if (display=='none') {
		obj.style.height='0px'; 
		obj.setAttribute('D',1)
	} else { 
		obj.style.height=obj.getAttribute('H')+'px'
		obj.setAttribute('D',-1)
	}
	obj.timer = setInterval(function(){js_sliding(obj)},10);
}
function js_sliding(obj) {
	var curH=parseInt(obj.style.height)
	C=parseInt(obj.getAttribute('C'))		//Count (max of iteration)
	H=parseInt(obj.getAttribute('H'))		//Max height
	D=parseInt(obj.getAttribute('D'))		//Direction
	obj.setAttribute('C',C-1)
	if (D==1)
		step=(H-curH)/10;
	else
		step=curH/10;
	step=Math.round(step+1)
	//alert(curH+' '+step+' '+(curH+(D*step))+'px')
	obj.style.height=(curH+(D*step))+'px'
	if (C<0) if (D==-1) {obj.style.height='0px';obj.style.display='none';clearInterval(obj.timer);} else {obj.style.height=H+'px';clearInterval(obj.timer);}
	if (D==-1 && curH<2) {obj.style.height='0px';obj.style.display='none';clearInterval(obj.timer);}
	if (D==1 && curH>H-2) {obj.style.height='auto';clearInterval(obj.timer);}
}
function js_showhideAllNodes(tagName,showhide) {
	var nodes=document.getElementsByTagName(tagName);
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].hasAttribute('node')) {
			if (nodes[i].className=='stats_down' || nodes[i].className=='stats_up') nodes[i].className=(showhide)?'stats_up':'stats_down';
			if (node=js_getParentNode(nodes[i],nodes[i].getAttribute('node'),'')) {
				if (nodes[i].getAttribute('childnode')) {
					nextnode=nodes[i].getAttribute('childnode')
					node=node.firstChild
				} else {
					nextnode=nodes[i].getAttribute('node')
				}
				while (node=node.nextSibling) {
					if(node.nodeName.toLowerCase()==nextnode) {node.style.display=(showhide)?'':'none';;break;}
				}
				if (nodes[i].getAttribute('node')=='tr') {
					if (node.getElementsByTagName('img').length && node.getElementsByTagName('img')[0].getAttribute('location')) node.getElementsByTagName('img')[0].src=node.getElementsByTagName('img')[0].getAttribute('location');
				}
			}
		}
	}
}

function js_checkMouseOver(e,obj,flag) {
	//return false;
	// returns true if the mouseover/mouseout event comes from the element itself or not
	// flag=1 -> mouseover, flag=-1 -> mouseout
	if (!e) e=window.event;
	var target=0;
	//if (e.relatedTarget) target=e.relatedTarget; else (flag==1)?target=e.toElement:target=e.fromElement;
	if (e.relatedTarget) target=e.relatedTarget; else target=e.toElement;
	alert('obj='+js_getNodePath(obj)+', target='+js_getNodePath(target))
	while (target) {
		if (target==obj) {return false;}		//The target was 'inside' the obj -> is a child -> not a real mouseout
		target=target.parentNode;
	}
	return false;
//	var t=(event)?event.relatedTarget:window.event.toElement;document.getElementById('debug').innerHTML+=''+t.nodeType+' onmouseout<br/>';
}

function js_eventHandler() {
	var e=window.event;
	var target=(window.event)?e.srcElement:e.target;
	document.getElementById('debug').innerHTML+=''+target.nodeName+' onmouseover<br/>';
	this.nextSibling.style.display=''
}

function js_adminMenuSave(obj,flag) {
	//Flag: -1: reset, 1: preview, 2: test, 3:save
	//After a preview or all 'testing', all 'p onclick' have been replaced to post the form
	//if (flag==-1) {document.location=document.location;return false}		//Reset
	xml=document.getElementById('cmsWebmenus');
	form=document.getElementById('admin_menu_form');
	if (obj.nodeName=='P') {	//If we clicked on a 'P' (within the menu)
		form.setAttribute('action',obj.getAttribute('location'))
	} else	{					//Else, we click on a 'INPUT' in the 'Edit menu', get the first menu with a 'file' as location
		var location="";
		nodes=xml.getElementsByTagName('div');
		for (i=0;i<nodes.length;i++) {
			if (nodes[i].getAttribute('admin_file') && nodes[i].getAttribute('admin_file')!='') {location=nodes[i].getAttribute('admin_file');i=nodes.length}
			if (nodes[i].getAttribute('admin_cid') && nodes[i].getAttribute('admin_cid')!='') {location="/content.php?cid="+nodes[i].getAttribute('admin_cid');i=nodes.length}
		}
		if (location=="") {
			alert("Error: all menus are empty");
			return false;
		} else {
			form.setAttribute('action',location)
		}
	}
	inputs=form.getElementsByTagName('input');
	switch(flag) {
		case 1:			//preview
			inputs[0].setAttribute('value','preview')
			tmp=js_adminMenuParse(xml);
			inputs[1].setAttribute('value',tmp)
			preview=window.open("","preview","width=1110,height=680,scrollbars=yes,resize=yes");
			form.setAttribute('target','preview');
			form.submit();
			break;
		case 2:			//testing
			inputs[0].setAttribute('value','testing')
			form.setAttribute('target','');
			form.submit();
			break;
		case 3:			//save
			inputs[0].setAttribute('value','save')
			tmp=js_adminMenuParse(xml);
			inputs[1].setAttribute('value',tmp)
			if (form.getAttribute('target')) form.removeAttribute('target');
			form.submit();
			break;
		case -1:			//reset
			document.location=form.getAttribute('action');
			break;
	}
	//var form2=document.createElement("form2")
	//form2.appendChild(form.cloneNode(true))
	//alert(form2.innerHTML)
}
function js_adminMenuParse(node) {
	var nodes=node.childNodes
	var result=document.createElement('tmp');
	for (var i=0;i<nodes.length;i++) {
		switch (nodes[i].nodeName) {
			case "UL":
				var newnode=document.createElement('menus');
				div_node=nodes[i].getElementsByTagName('div')[0];
				for (var j=0;j<div_node.attributes.length;j++) {
					if (div_node.attributes[j].nodeName.indexOf("admin_")==0) newnode.setAttribute(div_node.attributes[j].nodeName.replace("admin_",""),div_node.attributes[j].nodeValue)
				}
				newnode.innerHTML=js_adminMenuParse(nodes[i])
				result.appendChild(newnode);
				break;
			case "LI":
				var newnode=document.createElement('menu');
				div_node=nodes[i].getElementsByTagName('div')[0];
				for (var j=0;j<div_node.attributes.length;j++) {
					if (div_node.attributes[j].nodeName.indexOf("admin_")==0) newnode.setAttribute(div_node.attributes[j].nodeName.replace("admin_",""),div_node.attributes[j].nodeValue)
				}
				newnode.innerHTML=js_adminMenuParse(nodes[i])
				result.appendChild(newnode);
				break;
		}
	}
	//alert(result.innerHTML)
	return result.innerHTML;
}


