var questionWeight = new Array();
questionWeight[0] = new Array(); // for Water
questionWeight[1] = new Array(); // for Energy
questionWeight[2] = new Array(); // for Transport
questionWeight[3] = new Array(); // for Waste
questionWeight[4] = new Array(); // for Food
//Water question weight
questionWeight[0][1] = 5
questionWeight[0][2] = 5
questionWeight[0][3] = 3
questionWeight[0][4] = 3
questionWeight[0][5] = 2
questionWeight[0][6] = 5
questionWeight[0][7] = 3
questionWeight[0][8] = 1
//Energie question weight
questionWeight[1][1] = 5
questionWeight[1][2] = 5
questionWeight[1][3] = 5
questionWeight[1][4] = 1
questionWeight[1][5] = 5
questionWeight[1][6] = 5
questionWeight[1][7] = 5
questionWeight[1][8] = 5
questionWeight[1][9] = 4
questionWeight[1][10] = 5
questionWeight[1][11] = 4
questionWeight[1][12] = 3
questionWeight[1][13] = 0
questionWeight[1][14] = 3
//transport question weight
questionWeight[2][1] = 5
questionWeight[2][2] = 4
questionWeight[2][3] = 4

questionWeight[2][4] = 3
questionWeight[2][5] = 4
questionWeight[2][6] = 2
questionWeight[2][7] = 2
questionWeight[2][8] = 4
//Waste question weight
// 1
questionWeight[3][1] = 3	// 1
questionWeight[3][2] = 3	// 1a
questionWeight[3][3] = 3	// 1b
questionWeight[3][4] = 3	// 1c
// 2
questionWeight[3][5] = 4
// 3
questionWeight[3][6] = 4
// 4
questionWeight[3][7] = 5	// a
questionWeight[3][8] = 5
questionWeight[3][9] = 5
questionWeight[3][10] = 5
questionWeight[3][11] = 5	// e
questionWeight[3][12] = 0
questionWeight[3][13] = 0
questionWeight[3][14] = 5
questionWeight[3][15] = 5
questionWeight[3][16] = 5	// l
questionWeight[3][17] = 5
questionWeight[3][18] = 5
questionWeight[3][19] = 5	// o
//Food question weight
questionWeight[4][1] = 4
questionWeight[4][2] = 4
questionWeight[4][3] = 3
questionWeight[4][4] = 5
questionWeight[4][5] = 4
questionWeight[4][6] = 3
questionWeight[4][7] = 3
questionWeight[4][8] = 2
questionWeight[4][9] = 4

var questionMsg = new Array();
questionMsg[0] = new Array(); // for Water
questionMsg[1] = new Array(); // for Energy
questionMsg[2] = new Array(); // for Transport
questionMsg[3] = new Array(); // for Waste
questionMsg[4] = new Array(); // for Food
//Water question tip/messages
questionMsg[0][1] = "";
questionMsg[0][2] = "Per favore conta solo il tempo in cui il rubinetto è aperto";
questionMsg[0][3] = 'Con un orologio, misura il tempo necessaio per riempire un contenitore da 1 litro, aprendo il rubinetto come fai di solito; il flusso (litri/min) che corrisponde al tuo rubinetto è il valore che si ottiene dividendo 60 per il tempo misurato';
questionMsg[0][4] = questionMsg[0][3];
questionMsg[0][5] = 'Per accertartene, metti un po\' di carta igienica nella cassetta dello scarico o nel lavandino prima di andare a letto; se il mattino dopo la carta è bagnata è ovvio che c\’è una perdita';
questionMsg[0][6] = 'C\'è un modo semplice per misurare la capienza della tua cassetta esterna. Segna con un pennarello il livello dell\'acqua dentro la cisterna, chiudi il rubinetto esterno che la alimenta e tira l\'acqua. Una volta che la cisterna è vuota, con un contenitore graduato (ma va bene anche una bottiglia da litro), riempilo fino al livello segnato. Contando quanti contenitori hai versato, saprai quanta acqua contiene la cisterna';
questionMsg[0][7] = "";
questionMsg[0][8] = "";
//Energy question messages
questionMsg[1][1] = "";
questionMsg[1][2] = "";
questionMsg[1][3] = "";
questionMsg[1][4] = "";
questionMsg[1][5] = "";
questionMsg[1][6] = "";
questionMsg[1][7] = "";
questionMsg[1][8] = "";
questionMsg[1][9] = "";
questionMsg[1][10] = "";
questionMsg[1][11] = "";
questionMsg[1][12] = "";
questionMsg[1][13] = "";
questionMsg[1][14] = "";
//Transport question messages
questionMsg[2][1] = "";
questionMsg[2][2] = "";
questionMsg[2][3] = "";
questionMsg[2][4] = "";
questionMsg[2][5] = "";
questionMsg[2][6] = "";
questionMsg[2][7] = "";
questionMsg[2][8] = "";
//Waste question messages
questionMsg[3][1] = "";
questionMsg[3][2] = "";
questionMsg[3][3] = "";
questionMsg[3][4] = "";
questionMsg[3][5] = "";
questionMsg[3][6] = "";
questionMsg[3][7] = "";
questionMsg[3][8] = "";
questionMsg[3][9] = "";
questionMsg[3][10] = "";
questionMsg[3][11] = "";
questionMsg[3][12] = "";
questionMsg[3][13] = "";
questionMsg[3][14] = "";
questionMsg[3][15] = "";
questionMsg[3][16] = "";
questionMsg[3][17] = "";
questionMsg[3][18] = "";
questionMsg[3][19] = "";
//Food question messages
questionMsg[4][1] = "";
questionMsg[4][2] = "";
questionMsg[4][3] = "";
questionMsg[4][4] = "";
questionMsg[4][5] = "";
questionMsg[4][6] = "";
questionMsg[4][7] = "";
questionMsg[4][8] = "";
questionMsg[4][9] = "";

// create new object for each domain
var water = new NewDomain("water", "Acqua", "w", "frmWater", questionWeight[0], questionMsg[0], 0, 54, 95, 135);
var energy = new NewDomain("energy","Energia", "e", "frmEnergy", questionWeight[1], questionMsg[1], 0, 111, 193, 275);
var transport = new NewDomain("transport","Trasporti", "t", "frmTransport", questionWeight[2], questionMsg[2], 0, 63, 101, 140);transport.hasCar = true;	// to transport is dynamiccally added property hasCar
var waste = new NewDomain("waste", "Smaltimento Rifiuti", "we", "frmWaste", questionWeight[3], questionMsg[3], 0, 176, 281, 385);
var food = new NewDomain("food","Cibo & Acquisti", "f", "frmFood", questionWeight[4], questionMsg[4], 0, 70, 110, 150);
var results = new NewDomain("results", "Risultati", "r", "frmResults", '', '', '', '', '', '');
/*	PORTUGAL
var water = new NewDomain("water", "Acqua", "w", "frmWater", questionWeight[0], questionMsg[0], 0, 58, 96, 135);
var energy = new NewDomain("energy","Energia", "e", "frmEnergy", questionWeight[1], questionMsg[1], 0, 103, 182, 260);
var transport = new NewDomain("transport","Trasporti", "t", "frmTransport", questionWeight[2], questionMsg[2], 0, 53, 87, 120);
var waste = new NewDomain("waste", "Smaltimento Rifiuti", "we", "frmWaste", questionWeight[3], questionMsg[3], 0, 169, 270, 370);
var food = new NewDomain("food","Cibo & Acquisti", "f", "frmFood", questionWeight[4], questionMsg[4], 0, 65, 103, 140);
var results = new NewDomain("results", "Risultati", "r", "frmResults", '', '', '', '', '', '');
*/

var domains = new Array();
domains[0] = water;
domains[1] = energy;
domains[2] = transport;
domains[3] = waste;
domains[4] = food;
domains[5] = results;


//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Create a new "domain" object with several properties
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function NewDomain(id, name, prefix, formName, questionWeigths, questionMsg, result, minvalue, medvalue, maxvalue)
{
	this.id = id;
	this.name = name;
	this.prefix = prefix;
	this.formName = formName;
	this.questionWeigths = questionWeigths;
	this.questionMsg = questionMsg;
	this.result = result;
	this.minvalue = minvalue;
	this.medvalue = medvalue;
	this.maxvalue = maxvalue;
}


//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Hide all layers; Draw chart images; Show "results" layer.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function ShowResults()
{
	var allAnswered = false;
	
	//check if at least one domain have answers
	for (var i = 0; i < domains.length-1; i++)
	{
		if (!AllUnanswered(domains[i]))
		{
			// check if all questions in that domain are answered
			if (AllAnswered(domains[i]))
			{
				allAnswered = true;
			}
			else
			{
				ValidateAllDomains(domains[i]);
				return;
			}
		}
	}
	if (allAnswered)
	{
		HideAll();
		ShowGraph(water);
		ShowGraph(energy);
		ShowGraph(transport);
		ShowGraph(waste);
		ShowGraph(food);
		show(results.id);
		
		// APA
		var resultList = new String('Risultati numerici:\n=====================\n');
		for(var i = 0; i < domains.length - 1; i++)
			resultList += domains[i].name + ': ' + domains[i].result + '\n';
		//alert(resultList);		
		//
	}
	else
	{
		alert("Per ottenere un risultato devi rispondere a tutte le domande di almeno un dominio.");
		return;
	}

}


//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Select and resizes the image to show in the chart.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function ShowGraph(domain)
{
	domain.result = CalculateDomain(domain);
	var imageId = domain.prefix + "_graph";
	Image1= new Image(domain.result,20);
	
	if (domain.result < domain.minvalue)
	{
		Image1.src = "images/green.gif";
	}
	else if (domain.result < domain.medvalue)
	{
		Image1.src = "images/yellow.gif";
	}
	else 
	{
		Image1.src = "images/red.gif";
	}

	var temp = document.getElementById(imageId);

	temp.src = Image1.src;
	temp.width = CalcWidth(domain.result,domain.maxvalue);
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Equalizes chart bar width to an maximum of 399 pixels
/// </summary>
///<returns>integer</returns>
/// <creationdate>2005-08-20</creationdate>
function CalcWidth (value, maxi)
{
	var result = 0;
	result = (399 * value)/maxi;
	return result;
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Calculate the result of the specified domain.
/// </summary>
/// <returns>integer</returns>
/// <creationdate>2005-08-20</creationdate>
function CalculateDomain(domain)
{
	var frm = document.forms[domain.formName];
	var formLen = frm.elements.length;
	var itemValue, questionNumber;
	var sum = 0;
	for(var i = 0; i < formLen; i++)
	{
		if(frm.elements[i].checked)
		{
			itemValue = frm.elements[i].value;
			questionNumber = IndexQuestion(i, domain.formName);
			sum = sum + (domain.questionWeigths[questionNumber] * itemValue);
		}
	}
	//alert(domain.name + ' - sum: ' + sum);
	return sum;
}


//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Return the index position of the question within the specified form
/// </summary>
///<returns>integer</returns>
/// <creationdate>2005-08-20</creationdate>
function IndexQuestion (itemNum, formName)
{
	var j = 1;
	var frm = document.forms[formName];
	var formLen = frm.elements.length;
	if (itemNum == 0)
	{
		return 1;
	}
	else
	{
		for (x=1; x < formLen; x++)
		{
			if ( x <= itemNum)
			{
				if (frm.elements[x].name != frm.elements[x-1].name)
				{
					j++;
				}
			}
		}
		return j;
	}
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Show instant image result to the user anwser.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
/// <lastmodifiedby>Parodi Alessandro</lastmodifiedby>
/// <lastmodifieddate>2005-08-20</lastmodifieddate>
/// <modification>added alt and title to images</modification>
function ShowInstantResult(radio,img)
{
	if (radio.value == 0 || radio.value == 1)
	{
		img.src = "images/foot01.gif";
		img.alt = 'bravissimo';
		img.title = 'bravissimo';
	}
	else if (radio.value == 2)
	{
		img.src = "images/foot02.gif";
		img.alt = 'sei sulla buona strada';
		img.title = 'sei sulla buona strada';
	}
	else if (radio.value == 3)
	{
		img.src = "images/foot03.gif";
		img.alt = 'puoi fare meglio';
		img.title = 'puoi fare meglio';
	}
	else if (radio.value == 4)
	{
		img.src = "images/foot04.gif";
		img.alt = 'così non va';
		img.title = 'così non va';
	}
	else 
	{
		img.src = "images/foot05.gif";
		img.alt = 'non ci siamo proprio';
		img.title = 'non ci siamo proprio';
	}
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Hide all specified layers
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function HideAll(){
	document.getElementById(water.id).style.display = "none";
	document.getElementById(energy.id).style.display = "none";
	document.getElementById(transport.id).style.display = "none";
	document.getElementById(waste.id).style.display = "none";
	document.getElementById(food.id).style.display = "none";
	document.getElementById(results.id).style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Hide all layers; Show specified layer; Goto the anchor.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function show(name){
	HideAll();
	document.getElementById(name).style.display = "block";
	window.location.hash ="top";
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Hide or show specified layer.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function hideShow(answer) {     
	var element = document.getElementById(answer);
    if (null!=element)
	{
		element.style.display = ""==element.style.display ? "block" : "";
	}
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Parodi Alessandro</author>
/// <summary>
/// Hide or show specified object.
/// </summary>
/// <creationdate>2005-10-13</creationdate>
function HideShow(obj, show) {  
    if(obj != null) obj.style.display = show?'':'none';
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Checks if all the questions are unanswered for the specified domain
/// </summary>
/// <returns>bool</returns>
/// <creationdate>2005-08-20</creationdate>
///	<modified>Parodi Alessandro 2005-10-13</modified>
/// <descption>id domain is transport and user does not have a car, only first 3 radiogroups are considered</descption>
function AllUnanswered(domain)
{
	var frm = document.forms[domain.formName];
	var radioGroupPrefix = domain.prefix;
	var radioGroups = GetAllRadioGroups(domain);
	var allUnchecked = true;

	// modified by APA
	var radioGroupsNum = radioGroups.length ;
	if(domain == transport && !transport.hasCar)
	{
		radioGroupsNum = 3;
		//alert('no car');
	}
	//

	for (var i = 0; i < radioGroupsNum; i++)
	{
		var checkbuttons = false; 
		radioelement = document.getElementsByName(radioGroups[i]); 

		for (var j = 0; j < radioelement.length; j++) 
		{
			if (radioelement[j].checked) 
			{
				checkbuttons=true;
			} 
		} 
		if (checkbuttons==true) 
		{	
			allUnchecked = false;
		} 
	}
	return allUnchecked;
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Checks if all the questions, in a specified domain, are answered.
/// </summary>
///<returns>bool</returns>
/// <creationdate>2005-08-20</creationdate>
function AllAnswered(domain)
{
	var frm = document.forms[domain.formName];
	var radioGroupPrefix = domain.prefix;
	var radioGroups = GetAllRadioGroups(domain);
	var checkAllGroups = true;

	// modified by APA
	var radioGroupsNum = radioGroups.length ;
	if(domain == transport && !transport.hasCar)
	{
		radioGroupsNum = 3;
		//alert('no car');
	}
	//

	for (var i = 0; i < radioGroupsNum; i++)
	{
		var checkbuttons = false; 
		radioelement = document.getElementsByName(radioGroups[i]); 
		for (var j = 0; j < radioelement.length; j++) 
		{
			if (radioelement[j].checked) 
			{
				checkbuttons=true;
			} 
		} 
		if (checkbuttons==false) 
		{
			//
			//	alert('domain ' + domain.name + '\n' + radioGroups[i] + ' not checked');
			//
			checkAllGroups = checkbuttons;
		} 
	}
	if (!checkAllGroups)
	{
		return false
	}
	else
	{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Checks if a domain have all the questions answered or unanswered.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function ValidateDomain(activeDomain, nextDomain)
{
	if (AllUnanswered(activeDomain))
	{
		show(nextDomain.id);
	}
	else if (AllAnswered(activeDomain))
	{
		show(nextDomain.id);
	}
	else
	{
		alert("Si prega di rispondere a tutte le domande del dominio: " + activeDomain.name);
	}	
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>PArodi Alessandro</author>
/// <summary>
/// Detects flash movie
/// </summary>
/// <creationdate>2005-11-11</creationdate>
function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
	
//////////////////////////////////////////////////////////////////////////////////
/// <author>PArodi Alessandro</author>
/// <summary>
/// calls flash function
/// </summary>
/// <creationdate>2005-11-11</creationdate>
function changeNavigationDomain(domainID) {
	thisMovie('navigationMovie').asFunc(domainID);
}


//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Checks if all domains have all the questions answered or unanswered.
/// Shows the specified domain if the above conditions are true.
/// </summary>
/// <creationdate>2005-08-20</creationdate>
function ValidateAllDomains(domainToShow)
{
	var allUnanswered = false;
	var allAnswered = false;
	for (var i = 0; i < domains.length-1; i++)
	{
		if (AllUnanswered(domains[i]))
		{
			allUnanswered = true;
		}
		else if (AllAnswered(domains[i]))
		{
			allAnswered = true;
		}
		else
		{
			alert("Si prega di rispondere a tutte le domande del dominio: " + domains[i].name);

			return;
		}	
	}
	if ((allUnanswered) || (allAnswered))
	{
		show(domainToShow.id);
	}
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Daniel Serra</author>
/// <summary>
/// Returns an array with the names of all radio groups of a domain.
/// </summary>
///<returns>array</returns>
/// <creationdate>2005-08-20</creationdate>
function GetAllRadioGroups(domain)
{
	var frm = document.forms[domain.formName];
	var radioGroupPrefix = domain.prefix;
	var radioGroups = new Array();
	var tempItem = "";
	var j = 0;
	for (var i = 0; i < frm.length; i++)
	{
		if (tempItem != frm.elements[i].name)
		{
			tempItem = frm.elements[i].name;
			radioGroups[j] = frm.elements[i].name;
			j++;			
		}
	}
	return radioGroups;
}

//////////////////////////////////////////////////////////////////////////////////
/// <author>Alessandro Parodi</author>
/// <summary>
/// Manages other transport-concening answers if no-car choiche is selected
/// </summary>
/// <creationdate>2005-10-13</creationdate>
function SetNoCar(sender)
{
	if(sender.tagName.toUpperCase() != 'INPUT' || sender.type.toLowerCase() != 'radio') return;
	
	var divHasCar = document.getElementById('noCar');
	if(divHasCar == null) return;

	var senderRadioGroupName = sender.name;
	var radioGroup = sender.form[senderRadioGroupName];

	if(radioGroup[radioGroup.length - 1].checked == true)
		transport.hasCar = false;
	else
		transport.hasCar = true;
	
	HideShow(divHasCar, transport.hasCar);
}