

//-------browser sniff--------------

var nome=(navigator.appName=="Microsoft Internet Explorer")?true:false;
var wt=nome?(navigator.appVersion.split(';').toString().split(" ").toString().split(',')[4] ):null;
var isie5=((wt)>=5 && nome)?true:false;
var isie4=((wt <5 && wt >=4) && nome)?true:false;
var isNS4=(document.layers)?true:false;
var isNS6=document.getElementById && (navigator.appName=="Netscape")?true:false;
var container = "";
var imgClosed;
var imgOpen;
var imgLeaf;
var imgDownload;
var spacer;

var idCat;

mm=(isNS4)?'show':'visible';
nn=(isNS4)?'hide':'hidden';


//--------preload------------------
imgClosed=new Image;
imgOpen=new Image;
imgLeaf=new Image;
imgDownload=new Image;
spacer=new Image;
imgClosed.src="albero/images/cart_ch.gif";
imgOpen.src="albero/images/cart_op.gif";
//imgLeaf.src="albero/images/link.gif";
//imgLeaf.src="albero/images/cart_op.gif";
imgLeaf.src="albero/images/spacer.gif";
imgDownload.src="albero/images/download.gif";
spacer.src="albero/images/spacer.gif";


//----------constant----------------
var DESCRIPTION = 0;
var LEVEL = 1;
var STATUS = 2;
var URLSCHEDA = 3;
var DIMENSIONESCHEDA = 4;
var NOME = 5;
var POPUP = 6;

//----------------getContainer----------------------

function getContainer(name){
	 return document.getElementById(name); 
}


// style array must contain
// { foreground, background, selectedforeground, selectedbackground, face, size} 


//----------------scrivi menu----------------------
function writeTree(nomeContenitore,arrayValori){
	basicWriteTree(nomeContenitore, arrayValori, 0, 0, 300, 'black', '3', 'Arial','white','blue');
}

var undefined;
getNodeStyle= undefined;

//----------------scrivi menu----------------------
function basicWriteTree(nomeContenitore, arrayValori){
	var openScript;
	lev="";
	container=getContainer(nomeContenitore); 
	container.treeData= arrayValori;
	livello=0;
	for(i=0; i<container.treeData.length; i++)	{
		if (container.treeData[i][LEVEL]<livello) {
			livello=container.treeData[i][LEVEL]; 
		}

		if (container.treeData[i][LEVEL]==livello)
		{
		// controlla se cartella aperta(a) o cartella chiusa(c) o link(l)
		    var type= container.treeData[i][STATUS];
		    var isSelected= (container.selezionato==i);
			openScript= "cambia('" + nomeContenitore + "'," + i + "); return false;";
			var nodeStyle;
			if(getNodeStyle) {
				nodeStyle= getNodeStyle(type,livello);
			} else {
				nodeStyle= getDefaultNodeStyle(type);
			}
			if (type=='l') {
				lev+=printLeafNode(container.treeData[i], livello, isSelected, nodeStyle, openScript, imgLeaf);
			} else if (type=='c') {
				lev+=printFolderfNode(container.treeData[i], livello, isSelected, nodeStyle, openScript);
			} else if (type=='a') {
				lev+=printFolderfNode(container.treeData[i], livello, isSelected, nodeStyle, openScript);
				livello++; 
			}
		}
	}
	container.innerHTML=lev;	
}


function getDefaultNodeStyle(nodeType) {
	if (nodeType=='l') {
		return treeLinkNodeStyle;
	} else if (nodeType=='c') {
		return treeClosedNodeStyle; 
	} else if (nodeType=='a') {
		return treeOpenNodeStyle;
	}
	return treeLinkNodeStyle;
}

function printFont(foreground, background, fontface, fontsize) {
	var l= '<font color=\"'+ foreground +
			'\" size=\"'+fontsize
			+'\" face=\"'+fontface;
	if(background!='none') {		
		l= l +'\" style=\"background-color:'+background;
    }			
	l= l+ '\">';
	return l;
}


function printFolderfNode(row, level, isSelected, rowStyle, openScript) {
		var lev= '';

		var fg= rowStyle[0];
		var bg= rowStyle[1];
		var face=rowStyle[4];
		var size=rowStyle[5];
		var image=rowStyle[6];
		
		lev += '<img src="'+spacer.src+'" height="1" width="' + (10*(level+1)) + '" border="0">';
		lev += '<a href="#" onClick ="' + openScript + ' "style="text-decoration:none;" onFocus="this.blur()">';
		if(image!="") {
			lev += '<img src="'+image+'" height="10" width="10" border="0">&nbsp;';
		}
		
		if (isSelected) {
			fg= rowStyle[2];
			bg= rowStyle[3];
		}
		lev += printFont(fg, bg, face, size);
		
		lev += row[DESCRIPTION] + '</a>';
		if(row[URLSCHEDA] && row[URLSCHEDA]!='null') {
			lev += '&nbsp;&nbsp; <a href="' + row[URLSCHEDA] + '" target="blank">';
			lev += '<img src="'+imgDownload.src+'" height="10" width="62" border="0">';
			lev += '</A>'+ row[DIMENSIONESCHEDA];
		}
		lev += '</font>';
		if (row[POPUP]!='null' && row[URLSCHEDA] && row[URLSCHEDA]!='null') {
			lev += '&nbsp;&nbsp;<a href="#" onclick="popup(\'' + row[POPUP] + '\')" >';
			lev += '<img src="'+imgDownload.src+'" height="10" width="62" border="0">';
			lev += '</a>';
		}
		lev += '<br>';
		return lev;
}

function popup(url)	{
	window.open(url,"docList","resizable=no,scrollbars=no,width=283px,height=20px");
}

function printLeafNode(row, level, isSelected, rowStyle, openScript, iconImg) {
		var lev= '';
		
		lev += '<img src="'+spacer.src+'" height="1" width="' + (10*(level+1)) + '" border="0">';
		if (row[URLSCHEDA] && row[URLSCHEDA]!='null') {
			var target= "_top";
			if(row.length>5) {
				target= row[6];
			} else {
				target= "_top";
			}
			lev +='<a href=\"'+row[URLSCHEDA]+'\" style=\"text-decoration:none;\" target='+target+'>';
			
		} else { 
			lev += '<a href="download.jsp/#" onClick ="' + openScript + ' "style="text-decoration:none;" onFocus="this.blur()">';
		}
		lev += '<img src="'+iconImg.src+'" height="10" width="10" border="0">&nbsp;';

		var fg= rowStyle[0];
		var bg= rowStyle[1];
		var face=rowStyle[4];
		var size=rowStyle[5];
		
		if (isSelected) {
			fg= rowStyle[2];
			bg= rowStyle[URLSCHEDA];
		}
		lev += printFont(fg, bg, face, size);
		lev += row[DESCRIPTION] + '</a>';
		if(row[URLSCHEDA] && row[URLSCHEDA]!='null') {
			lev += '&nbsp;&nbsp; <a href="' + row[URLSCHEDA] + '" target="blank">';
			lev += '<img src="'+imgDownload.src+'" height="10" width="62" border="0"></A>'+ row[DIMENSIONESCHEDA];
		}
		lev += '</font>';
		if (row[POPUP]!='null') {
			lev += '&nbsp;&nbsp;<a href="#" onclick="popup(\'' + row[POPUP] + '\')" >';
			lev += '<img src="'+imgDownload.src+'" height="10" width="62" border="0">';
			lev += '</a>';
		}
		lev += '<br>';
		return lev;
}




function aggiungi(da,a)
{

	containerDa=getContainer(da);
	containerA=getContainer(a);
	
	if (containerDa.treeData[containerDa.selezionato][STATUS]=='l' && containerA.treeData[containerA.selezionato][STATUS]!='l')
	{
		newArray = new Array();
		j=0;
	  for (i=0;i<containerA.treeData.length;i++)
		{
			newArray[j]=containerA.treeData[i];
			j++;
			if (i == containerA.selezionato)
			{
				newArray[j]=new Array(containerDa.treeData[containerDa.selezionato][DESCRIPTION],containerA.treeData[containerA.selezionato][LEVEL]+1,containerDa.treeData[containerDa.selezionato][STATUS],containerDa.treeData[containerDa.selezionato][URLSCHEDA]);
				containerA.selezionato=j-1;
				j++;
			}
		}
		writeTree(a,newArray);
	//debug
	stampaDebug(da);
	stampaDebug(a);
	}
	else
	{
		alert('Puoi associare solo un prodotto ad una cartella');
	}
}

function togli(da)
{
	containerDa=getContainer(da);
	if (containerDa.treeData[containerDa.selezionato][STATUS]=='l')
	{
		newArray = new Array();
		j=0;
	  for (i=0;i<containerDa.treeData.length;i++)
		{
			if (i!=containerDa.selezionato)
			{
				newArray[j]=containerDa.treeData[i];
				j++;
			}
		}
		containerDa.selezionato=containerDa.selezionato-1;
		writeTree(da,newArray);
	}
	else
	{
		alert('Non puoi disassociare una cartella');
	}
}

function stampaDebug(x)
{
	z=document.getElementById(x);
	if (x=='categorie') y=document.getElementById('consoleCat');
	else y=document.getElementById('consoleProd');		
	debug='';
  for (i=0;i<z.treeData.length;i++)
	 {
		debug += 'new array('+z.treeData[i][DESCRIPTION]+','+z.treeData[i][LEVEL]+','+z.treeData[i][STATUS]+','+z.treeData[i][URLSCHEDA]+')';
	 }
	y.innerHTML=debug;
}



function cambia(nomeContenitore,pos) {
	var mustCallServer= false;
	container=getContainer(nomeContenitore);
	var level = parseInt(container.treeData[pos][LEVEL]);
	if (container.treeData[pos][STATUS]=='a') {
		container.treeData[pos][STATUS]='c';
	} else if (container.treeData[pos][STATUS]=='c')	{
		container.treeData[pos][STATUS]='a';
		var nextNodeLevel;
		var lastElementPosition = (container.treeData.length-1);
		if(pos < lastElementPosition ) {
			nextNodeLevel = parseInt(container.treeData[pos+1][LEVEL]);
			if(nextNodeLevel<=level) {
				mustCallServer = true;
			}
		} else {
			mustCallServer= true;
		}
	}
	container.selezionato=pos;
	var stringa = "";
	if(mustCallServer) {
		for (i=0;i<container.treeData.length;i++){
			 if(container.treeData[i][STATUS] == 'a' ){
			 	stringa += container.treeData[i][NOME] +';';
			 }
		}	
	}
	++ level;
	if(mustCallServer) {
//che cosa serve???
		window.location = getTreeServerUrl()
							+'?IDCATEGORIA='
							+container.treeData[pos][NOME]
							+'&LEVEL='+level
							+'&POS='
							+pos+'&UPDATE='+stringa;
	} else {
		writeTree(nomeContenitore,container.treeData);
	}
}

