﻿
// Definisce il debug, obbligatoria
var DEBUG = true;





// -----------------
// Funzioni GENERALI
// -----------------



// Parsing di una lista "key=value" tipo querystring
function parseGetVars(vars)
{
	if (vars && (vars.length > 0))
	{
		try
		{
			var getVars = new Array();
			var qString = unescape(vars);
			var pairs = qString.split(/\&/);
			var founded = false;
			for (var i in pairs)
			{
				founded = true;
				var nameVal = pairs[i].split(/\=/);
				getVars[nameVal[0]] = nameVal[1];
			}
			// Molto poco elegante...
			if (founded)
				return (getVars);
			else
				return (null);
		}
		catch (e) { return (null); }
	}
	else
	{
		return (null);
	}
}

// Da Array a String "key1=value1&keyN=valueN"
function composeGetVars(getVars)
{
	var vars = "";
	for (var i in getVars)
	{
		vars += "&" + i + "=" + getVars[i];
	}
	return (vars.substring(1));
}



// Carica la querystring in un array
function parseQueryString()
{
	return (parseGetVars(top.location.search.substring(1)));
}




// Carica il documento XML 
function loadXML(xmlfile)
{
	// Viene passato il nome dell'array...
	var db = eval("db_" + xmlfile.toLowerCase());
	if (db)
		return (db);
	else if (DEBUG) { alert("Impossibile leggere la collezione (34)."); }
}



// Ripulisce la lista principale.
function clearCollection(collection)
{
	collection.splice(0, 1);
	return (collection);
}


function getAttributeValue(item, attribute, cleanedCollection)
{
	if (attribute == "codice")
	{
		// L'elemento è già il nome dell'elemento...
		return (item);
	}
	else if (attribute == "posizione")
	{
		// Estrae la posizione nell'array
		for (i = 0; i < cleanedCollection.length; i++)
		{
			if (cleanedCollection[i] == item) { return i; }
		}
	}
	else
	{
		if (DEBUG) { alert("Errore di lettura (159)."); }
	}
}

// Cerca un gioiello
function findByCode(cleanedCollection, code)
{
	for (i = 0; i < cleanedCollection.length; i++)
	{
		if (cleanedCollection[i] == code) { return cleanedCollection[i]; }
	}
}




// -------------------------------------------
// Funzioni specifiche per l'elemento GIOIELLO
// -------------------------------------------

// La collezione delle immagini
function getImages(gioiello)
{
	return(gioiello);
}

