var loading;

function loadProds(p) {
	showLoading();
	
	var area	= _.gid('area').selectedIndex;
	var opt;
	var ajax	= _.ajax();
	var fields	= new Array();
	
	
	_.gid('prod').selectedIndex	= -1;
	_.gid('prod').disabled		= true;
	
	
	if (area > -1) {
		_.gid('area').disabled = true;
		
		while(_.gid('prod').length) {
			_.gid('prod').remove(0);
		}
		
		fields['area']	= _.gid('area').options[area].value;
		
		
		ajax.oncomplete = function(strResponseText, objResponseXML) {
			if (strResponseText != '') {
				var opts = strResponseText.split('|');
				
				var parts;
				var opt;
				
				for (var i = 0; i < opts.length; i++) {
					parts		= opts[i].split(';');
					
					opt		= _.elm('option');
					opt.value	= parts[0];
					opt.text	= parts[1];
					
					try {
						_.gid('prod').add(opt, null);
					}
					catch(ex) {
						_.gid('prod').add(opt);
					}
				}
				
				_.gid('prod').selectedIndex	= -1;
				_.gid('prod').disabled		= false;
			}
			
			_.gid('area').disabled = false;
			
			
			if (!isNaN(p))
				_.gid('prod').value = p;
			
			showSel();
		}
		
		ajax.onerror = function(intStatus, strStatusText) {
			alert('Errore [' + intStatus + '] [' + strStatusText + '].');	
			
			_.gid('area').disabled = false;
		}
		
		
		ajax.removeAllFields();
		
		for (var field in fields) {
			ajax.addField(field, fields[field]);
		}
		
		ajax.get('/ajax.prodotti.elenco.asp');
	}
	else {
		hideLoading();
	}
}

function showSel() {
	showLoading();
	
	var idpc;
	var idc;
	var rows;
	var rowdata;
	
	try {
		_.gid('reset_area').disabled = true;
		_.gid('reset_prod').disabled = true;
	}
	catch(e) {
	}
	
	if (_.gid('area').selectedIndex > -1) {
		idpc = _.gid('area').options[(_.gid('area').selectedIndex)].value;
		
		try {
			_.gid('reset_area').disabled = false;
		}
		catch(e) {
		}
		
		if (_.gid('prod').selectedIndex > -1) {
			idc = _.gid('prod').options[(_.gid('prod').selectedIndex)].value;
			
			try {
				_.gid('reset_prod').disabled = false;
			}
			catch(e) {
			}
		}
		else
			idc = 0;
	}
	else {
		idpc	= 0;
		idc	= 0;
	}
	
	rows = _.gcn('r');
	
	for (var i = 0; i < rows.length; i++) {
		rowdata = rows[i].id.split('|');
		
		if ((idpc == 0 || rowdata[0] == idpc) && (idc == 0 || rowdata[1] == idc || rowdata[2] == idc || rowdata[3] == idc)) {
			rows[i].style.visibility	= 'visible';
			rows[i].style.display		= 'block';
		}
		else {
			rows[i].style.visibility	= 'hidden';
			rows[i].style.display		= 'none';
		}
	}
	
	hideLoading();
}


function showLoading() {
	if (!loading.isVisible()) {
		loading.moveToDeadCentre();
		loading.show();
	}
}

function hideLoading() {
	if (loading.isVisible())
		loading.hide();
}

_.chain(window,	'load', function() {
	loading = _.dyl('loading');
	
	var i;
	
	if (location.search != '') {
		if (location.search.indexOf('&') > -1) {
			_.gid('area').value = location.search.split('&')[0].split('=')[1];
			loadProds(location.search.split('&')[1].split('=')[1]);
		}
		else {
			_.gid('area').value = location.search.split('=')[0];
			loadProds(location.search.split('=')[0]);
		}
	}
	else {
		_.gid('area').selectedIndex	= -1;
		_.gid('area').disabled		= false;
		_.gid('prod').selectedIndex	= -1;
		_.gid('prod').disabled		= true;
	}
	
	_.chain(_.gid('area'), 'change', loadProds, true);
	_.chain(_.gid('prod'), 'change', showSel, true);
	
	_.chain(_.gid('reset_area'), 'click', function() {
		_.gid('area').selectedIndex	= -1;
		_.gid('area').disabled		= false;
		_.gid('prod').selectedIndex	= -1;
		_.gid('prod').disabled		= true;
		
		showSel();
	}, true);
	
	_.chain(_.gid('reset_prod'), 'click', function() {
		_.gid('prod').selectedIndex	= -1;
		_.gid('prod').disabled		= false;
		
		showSel();
	}, true);
}, true);