// JavaScript Document
// Apre una finestra con determinate proprietà e dimensioni

function apriFinestra(nome,proprieta)
{
  miaFinestra=open(nome, "displayWindow", proprieta)
}

function go()
{
 if (document.selecter.select1.options[document.selecter.select1.selectedIndex].value != "none")
 {
  if (document.selecter.select1.options[document.selecter.select1.selectedIndex].value != "")
  {
  locatio = document.selecter.select1.options[document.selecter.select1.selectedIndex].value
  window.location = locatio
  }
 }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

//*******//
var Contatti = new Array();
var PrimoPiano = new Array();
var TipoLettura;
var TipoContatto = new Array("INTERCOMPANY", "DISTRIBUTORS", "AGENTE", "RIVENDITORE", "TARATURE");
var TipologiaContatto;
var Continente;
var Nazione;
var Regione;
var Provincia;

var XMLFile = "../xml/elenco.xml"
var xml = null;

function LeggiElenco(dato, tipol, cont, naz, reg, prov)
{
	TipoLettura = dato;
	Continente = cont;
	Nazione = naz;
	Regione = reg;
	Provincia = prov;
	TipologiaContatto = TipoContatto[tipol];
	if (document.implementation && document.implementation.createDocument)
		{
			xml = document.implementation.createDocument("","",null)
			xml.onload=Leggi
			xml.load(XMLFile);
		}
		else if (window.ActiveXObject)
		{
			xml = new ActiveXObject("Microsoft.XMLDOM")
			xml.onreadystatechange = IEGo
			xml.load(XMLFile);
		}
}

function IEGo()
{
	if (xml.readyState == 4)
		Leggi();
}

function OrdinaContatti(cont)
{
		var i,j;
	
		for (i=0; i < cont.length-1; i++)
		{
			j=i+1;
			if (cont[i].denominazione > cont[j].denominazione)
			{
				temp=cont[i];
				cont[i]=cont[j];
				cont[j]=temp;
			}
		}
}

function Leggi()
{
// Effettua la lettura dei contatti
// 0 = continente
// 1 = nazione
// 2 = regione
// 3 = provincia
// 4 = contatto
// 5 = tipologia contatto

	var coordinate=document.selecter.select1.options[document.selecter.select1.selectedIndex].value;
	//var coordinate = document.getElementById("scelta").document.selecter.select1.data;
	//document.write(coordinate);
	var posizionegeo=new CoordinateGeo(Continente, Nazione, coordinate.split(",")[2], coordinate.split(",")[3]);

	if (TipoLettura == 4)
	{

		var Nodo = xml.getElementsByTagName("contatti")[0].getElementsByTagName("contatto");
		LeggiContatti(Nodo);
	//document.write(posizionegeo.continente+"<br>"+posizionegeo.nazione+"<br>"+posizionegeo.regione+"<br>"+posizionegeo.provincia);

		var testofin = "";
		//ordina i contatti
		OrdinaContatti(Contatti);
		
		for (var i = 0 ; i < Contatti.length ; i++ )
		{
			
			var testo = "";
				testo +='<table width=350 border=0><tr><td bgcolor=#D7D7D7><font class="menu">';
				testo +=Contatti[i].denominazione;
				testo +='</font></td></tr><tr><td bgcolor=#FFFFFF><H1 class="desc">';
				if (Contatti[i].indirizzo != "*")
					testo += Contatti[i].indirizzo+'<br>';
				if (Contatti[i].citta != "*")
					testo += Contatti[i].citta+'<br>';
				//if (Contatti[i].nazione != "*")
					//	testo += " - " + Contatti[i].nazione+'<br>';
				//if (Contatti[i].provincia != "*")
					//testo += " - " + Contatti[i].provincia;
				if (Contatti[i].tel != "*")
					testo += "Tel: "+Contatti[i].tel+'<br>';
				if (Contatti[i].fax != "*")
					testo += "Fax: "+Contatti[i].fax+'<br>';
					
				if (Contatti[i].email != "*")
					testo+='Em@il: <A class="title" href="mailto:'+Contatti[i].email+'">'+Contatti[i].email+'</A><br>';
				if (Contatti[i].internet != "*")
					testo +='Internet: <A class="title" href="'+Contatti[i].internet+'" target="_parent">'+Contatti[i].internet+'</A>';
				
				if (testo != "")
					testo+='</H1></td></tr></table><br>';
			
			if (posizionegeo.provincia != "ALL")
			{
				if (Contatti[i].provincia == posizionegeo.provincia & 
					Contatti[i].regione == posizionegeo.regione &
					Contatti[i].tipo == TipologiaContatto)
					testofin +=testo;
			}
			else 
			if (Contatti[i].regione == posizionegeo.regione &
				Contatti[i].tipo == TipologiaContatto)
				testofin += testo;
		}
		document.getElementById("livello").innerHTML=testofin;
	}
	
	else if (TipoLettura == 0)	// lettura per continente
	{
		var Nodo = xml.getElementsByTagName("contatti")[0].getElementsByTagName("contatto");
		LeggiContatti(Nodo);
		var testo = "";
		//Ordina i contatti
		OrdinaContatti(Contatti);

		// Tipo di contatto
		// 0: intercompany
		// 1: distributors

		for (var i = 0 ; i < Contatti.length ; i++ )
		{
			if (Contatti[i].continente == posizionegeo.continente &&
				Contatti[i].tipo == TipologiaContatto)
			{
				testo +='<table width=350 border=0><tr><td bgcolor=#D7D7D7><font class="menu">';
				testo+=Contatti[i].denominazione;
				testo+='</font></td></tr><tr><td bgcolor=#FFFFFF><H1 class="desc">'+
						Contatti[i].indirizzo+'<br>'+Contatti[i].citta;
				if (Contatti[i].provincia != "*")
						testo += " - " + Contatti[i].provincia;						
				if (Contatti[i].nazione != "*")
						testo += " - " + Contatti[i].nazione+'<br>';
				if (Contatti[i].tel != "*")	
					testo += 'Tel: '+Contatti[i].tel+'<br>';
				if (Contatti[i].fax != "*") 
					testo += 'Fax: '+Contatti[i].fax+'<br>';
				if (Contatti[i].email != "*")
					testo+='Em@il:<A class="title" href="mailto:'+Contatti[i].email+'">'+Contatti[i].email+'</A><br>';
				
				if (Contatti[i].internet != "*")
					testo +='Internet: <A class="title" href="'+Contatti[i].internet+'" target="_parent">'+Contatti[i].internet+'</A>';
				
				if (testo != "")
					testo+='</H1></td></tr></table><br>';
			}
		}
		document.getElementById("distreur").innerHTML=testo;
	}
}

function LeggiContatti(obj)
{
	for (i = 0 ; i < obj.length ; i++)
		Contatti[i] = new dati(
									getValue(obj[i],"continente") ,
									getValue(obj[i],"nazione") ,
									getValue(obj[i],"regione") ,
									getValue(obj[i],"provincia") ,
									getValue(obj[i],"denominazione") ,
									getValue(obj[i],"citta") ,
									getValue(obj[i],"indirizzo") ,
									getValue(obj[i],"tel") ,
									getValue(obj[i],"fax") ,
									getValue(obj[i],"email") ,
									getValue(obj[i],"internet"),
									getValue(obj[i],"tipo")
									)
}

function getValue(vobj,tag,param)
{
	if (param)
		return vobj.getElementsByTagName(tag)[0].getAttribute(param);
	else
		return vobj.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
}

// Crea il record contenente tutte le informazioni relative al rivenditore
function dati(continente, nazione, regione, provincia, denominazione, citta, indirizzo, tel, fax, email, internet, tipo)
{
		this.continente=continente;
		this.nazione=nazione;
		this.regione=regione;
		this.provincia=provincia;
		this.denominazione=denominazione;
		this.citta=citta;
		this.indirizzo=indirizzo;
		this.tel=tel;
		this.fax=fax;
		this.email=email;
		this.internet=internet;
		this.tipo=tipo;
}

// Crea il record contenente le informazioni relative alla posizione geografica
function CoordinateGeo(continente, nazione, regione, provincia)
{
	this.continente = continente;
	this.nazione = nazione;
	this.regione = regione;
	this.provincia = provincia;
}

