function doSubmit(){
	var retval = false;
	var f = $$('form#frmAttivazione input[type=checkbox]');
	var s = "";
	for(var i=0; i<f.length; i++){
		if(f[i].checked){
			var nome = f[i].name;
			var parts = nome.split('_');
			if(s == "")
				s = parts[1];
			else
				s += "," + parts[1];
		}
	}
	if(s == ""){
		window.alert("Selezionare almeno una rivista");
	} else {
		$('rivista').value = s;
		retval = true;
	}
	if($('email').value == ""){
		window.alert("Specificare un indirizzo di posta elettronica");
		retval = false;
	}
	return retval;
}

/*function setComuni(){
	var pars = "id=" + $('provincia').value;
	new Ajax.Request("/servlet/GetComuni",
					{	method: 'post',
						parameters: pars,
						onComplete: mostraComuni});
}

function mostraComuni(req){	
	var dom = req.responseXML.documentElement;
	if(errorHandle(dom))
		return;
	var elem = $('comune');

	var nr_elem = elem.options.length;
	if(nr_elem){
		for(var i=(nr_elem - 1); i>=0; i--){
			elem.options[i] = null;
		}
	}
	var opts = dom.getElementsByTagName('comune');
	for(var i=0; i<opts.length; i++){
		var txtElem = opts.item(i).firstChild.data;
		var valore = opts.item(i).getAttribute('id');
		elem.options[i] = new Option(txtElem, valore);
	}
	elem.selectedIndex = 0;
}
*/
function checkInputStep1(){
	var retval = true;
	if($('codice_cliente').value == ""){
		window.alert("Immettere il codice cliente");
		retval = false;
	}
	return retval;
}

function prepareStep2(req){
	var dom = req.responseXML.documentElement;
	if(errorHandle(dom))
		return;
	var accounts = dom.getElementsByTagName('account');
	var stOut = "";
	if(accounts.length > 0){
		var newTable = document.createElement("table");
		var newTBody = document.createElement("tbody");
		newTable.setAttribute("id", "tblAccounts");
		var row = document.createElement('tr');
		var col = document.createElement('th');
		col.innerHTML = "Rivista";
		row.appendChild(col);
		var colRivista = document.createElement('th');
		colRivista.innerHTML = "Ente";
		row.appendChild(colRivista);
		var colReferente = document.createElement('th');
		colReferente.innerHTML = "Referente";
		row.appendChild(colReferente);
		var colAction = document.createElement('th');
		colAction.innerHTML = "Attiva";
		row.appendChild(colAction);
		newTBody.appendChild(row);
		for(var i=0; i<accounts.length; i++){
			var id = accounts.item(i).getAttribute("id");
			var id_abb = accounts.item(i).getAttribute("id_abb");
			var row = document.createElement('tr');

			var colRivista = document.createElement('td');
			colRivista.innerHTML = accounts.item(i).getElementsByTagName("rivista").item(0).firstChild.data;
			row.appendChild(colRivista);

			var colEnte = document.createElement('td');
			colEnte.innerHTML = accounts.item(i).getElementsByTagName("ente").item(0).firstChild.data;
			row.appendChild(colEnte);

			var colReferente = document.createElement('td');
			colReferente.innerHTML = accounts.item(i).getElementsByTagName("referente").item(0).firstChild.data;
			row.appendChild(colReferente);

			var colInput = document.createElement('td');
			if(id_abb == 0){
				var cb = document.createElement('input');
				cb.setAttribute("type", "checkbox");
				cb.setAttribute("name", "cbr_" + id);
				cb.setAttribute("title", "Attiva l'account");
				colInput.appendChild(cb);
			} else {
				colInput.innerHTML = "Account attivo";
			}
			row.appendChild(colInput);
			newTBody.appendChild(row);
		}
		newTable.appendChild(newTBody);
		$('idAccounts').appendChild(newTable);
		displayStep(2);
	} else {
		$('idAccounts').innerHTML = $('idNoAccount').innerHTML;
		displayStep(3);
	}
}

function displayStep(numStep){
	var someNodeList = $('frmAttivazione').getElementsByClassName('step1');
	var nodes = $A(someNodeList);
	nodes.each( function(node){
		node.style.display = numStep == 1 ? 'block':'none';
	});
	var someNodeList = $('frmAttivazione').getElementsByClassName('step2');
	var nodes = $A(someNodeList);
	nodes.each( function(node){
		node.style.display = numStep >= 2 ? 'block' : 'none';
	});
	var someNodeList = $('frmAttivazione').getElementsByClassName('step3');
	var nodes = $A(someNodeList);
	nodes.each( function(node){
		node.style.display = numStep == 3 ? 'none' : (node.tagName == 'P' ? 'block' : 'inline');
	});
}

function backToStep1(){
	$('idAccounts').innerHTML = "";
	$('codice_cliente').value = "";
	displayStep(1);
}
function step1() {
	if(!checkInputStep1())
		return;
	var pars = "codice_cliente=" + $('codice_cliente').value;
	new Ajax.Request("/servlet/AttivazioneStep1",
					{	method: 'post',
						parameters: pars,
						onComplete: prepareStep2});
}