﻿var xmlhttp = false;
var id_sel;
var gl_listbox;
var tipo_geo;

function onChangeSel(l)
{
	tipo_geo = document.getElementById('tipo_ricerca_geo').value;

	//alert(tipo_geo);

	var lSettore = document.getElementById('settore');
	var lSottoSettore = document.getElementById('sottosettore');
	var lCategoria = document.getElementById('categoria');

	if (l.id == 'area' && tipo_geo == 'nosett') id_sel = document.getElementById('settorecorrente').value;
	else id_sel = l[l.selectedIndex].value;

	if (l.id == 'area') {
		if (tipo_geo != 'nosett') lSettore.options.length = 1;
		lSottoSettore.options.length = 1;
		if (tipo_geo != 'nocat') lCategoria.options.length = 1;
		
		setElementValue('form_area', 'dv', l[l.selectedIndex].text);
		
		if (tipo_geo != 'nosett') gl_listbox = lSettore;
		else gl_listbox = lSottoSettore;
	}
	if (l.id == 'settore') {
		lSottoSettore.options.length = 1;
		if (lCategoria != null) lCategoria.options.length = 1;
		
		gl_listbox = lSottoSettore;
	}
	if (l.id == 'sottosettore') {
		if (lCategoria != null) lCategoria.options.length = 1;

		gl_listbox = lCategoria;
	}
	//alert('Listbox da popolare: ' + gl_listbox.id);
	if (l.id != 'sottosettore' || lCategoria != null) getValues();
	
	setVCC(lSettore, lSottoSettore, lCategoria);
}

function setElementValue(FormName, ElementName, Value)
{
  	//alert('FormName: ' + FormName + ', ElementName:' + ElementName + ', Value:' + Value);
	if (Value == 'Area') Value = '';
	
	document.forms[FormName].elements[ElementName].value = Value;
}

function getValues()
{
	xmlhttp=false;
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}

	url2call = "/b2b/action?msez=2100&tl=3&liv=";
	
	//alert(gl_listbox.id);
	
	if (gl_listbox.id == 'settore') url2call += 'area';
	if (gl_listbox.id == 'sottosettore') url2call += 'sett';
	if (gl_listbox.id == 'categoria') url2call += 'ssett';
	
	var lArea = document.getElementById('area');
	url2call += '&area=' + lArea[lArea.selectedIndex].value;
	
	url2call += ('&cod=' + id_sel);
	
	xmlhttp.onreadystatechange = statusCheck;
	//alert("testImportFile, open()");
	xmlhttp.open ('POST', url2call, true);
	xmlhttp.send (null);
}

function statusCheck()
{
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
		//alert(xmlhttp.responseText);
		CaricaMenu(gl_listbox, xmlhttp.responseText);
		} else {
		alert ('There was a problem with the request.');
		}
	}
}

function CaricaMenu(listbox, dati)
{
	//alert(dati);

	var p = eval("(" + dati + ")").cod_valore;
	
	listbox.options.length = 1;

	nSettore = new nodo();

	for (n in p) {
		//alert(p[n][0] + ' ' + p[n][1]);
		nSettore.id = p[n][0];
		nSettore.nome = p[n][1];
		creaNodo(listbox, nSettore);
	}
}

function nodo()
{
	this.id = "";
	this.nome = "";
}

function creaNodo(tipo, n, initValue)
{
	//alert('tipo ' + tipo + '; n = ' + n);

	//if (tipo == null) return false;
	//sel = document.getElementById(tipo);
	opt = document.createElement("option");
	opt.innerHTML = n.nome;
	opt.id = n.id;
	opt.value = n.id;
	tipo.appendChild(opt);
	if ((initValue != null) && (initValue == n.id))
		return true;
	return false;
}

function addCat2VCC() {
	var lSettore = document.getElementById('settore');
	var lSottoSettore = document.getElementById('sottosettore');
	var lCategoria = document.getElementById('categoria');
	
	setVCC(lSettore, lSottoSettore, lCategoria);
}

function setVCC(s, ss, c)
{
	var sv = '';
	if (s != null) sv = s[s.selectedIndex].value;
	var ssv = ss[ss.selectedIndex].value;
	
	if (ssv != '') sv = ssv;

	if (c != null) {
		var cv = c[c.selectedIndex].value;
		if (cv != '') sv = sv + '-' + cv;
	}
	
	//alert('vcc=' + sv);
	
	setElementValue('form_area', 'vcc', sv);
}
