
function init_faq() 
{
	Ajax.request('/php-bin/controler.php?id=faq', response_category, '');
}



function response_category() 
{
	// If server responded
	if (Ajax.checkReadyState(Ajax.xmlHttp))
	{		
		var display = document.getElementById('faq');
		var response = Ajax.xmlHttp.responseXML.documentElement;
		var categories = response.getElementsByTagName('category');		
		
		// Remove any duplicate categories
		var categories = _remove_duplicates(categories);
		
		var p = document.createElement('p');
		var ul = document.createElement('ul');
		ul.id = 'faq_category';
		
		for (i = 0; i < categories.length; i++) {
			var category = categories[i];
			var li = document.createElement('li');
			var div = document.createElement('div');
			
			var li_txt = document.createTextNode(category);
			
			div.className = 'fadein';
			div.id = 'questions' + category;	// Will hold questions when category expands
			li.id = category;
			li.className = 'faq_category';
			li.onclick = function () { return click_category(this, response); }
			li.onmouseover = li.style.cursor = 'pointer';
			
			li.appendChild(li_txt);
			ul.appendChild(li);
			ul.appendChild(div);

			
		}
		
		p.appendChild(ul);
		display.appendChild(p);

		// Done with calculations and server activity - hide server busy icon
		Display.hide_busy();
	}
	// If server's busy
	else { Display.show_busy(); }
}



function click_category(list, response) 
{
	var id_faqs = response.getElementsByTagName('id_faq');		
	var categories = response.getElementsByTagName('category');		
	var questions = response.getElementsByTagName('question');
	var answers = response.getElementsByTagName('answer');
	
	// Get layer that will hold questions
	var div_questions = document.getElementById('questions' + list.id);	
	div_questions.style.height = '100%';	// height adjustement for fade in in IE
	
	// Display questions for a particular category
	for (i = 0; i < questions.length; i++) {
		
		var curr_id_faq = id_faqs.item(i).firstChild.data;	// id_faq read from xml file
		var curr_cat = categories.item(i).firstChild.data;	// category read from xml file
		var curr_ques = questions.item(i).firstChild.data;	// question read from xml file
		var curr_ans = answers.item(i).firstChild.data;	// answer read from xml file
		
		// Display only questions pertinent to the category of an expanded item
		if (curr_cat == list.id) {
			
			var li = document.createElement('li');
			var li_txt = document.createTextNode(questions.item(i).firstChild.data);
			var a = document.createElement('a');
			
			// When question's clicked, show layer with the answer
			li.onclick = function () { click_question(this, response); }
			li.onmouseover = function() { this.style.color = '#8f1f1f'; }
			li.onmouseout = function() { this.style.color = '#3366CC'; }
			li.className = 'faq_questions';
			li.id = curr_id_faq;
			
			li.appendChild(li_txt);
			a.appendChild(li);

			div_questions.appendChild(a);
		}
	}
		
	Display.fadein(div_questions.id);

	var categories = document.getElementsByTagName('li');
	
	// Change all images to plus and hide layers showing questions under those categories
	for (i = 0; i < categories.length; i++) {
		var questions = document.getElementById('questions' + categories[i].id)
		
		if (categories[i].className == 'faq_category_exp' && categories[i].id != list.id && questions != null) {
			questions.innerHTML = "";
			questions.style.height = '1px';
			categories[i].className = 'faq_category';

		}
	}

	// Toggle same link
	if (list.className == 'faq_category') {
		list.className = 'faq_category_exp';
		div_questions.style.height = '100%';
	}
	else {
		var questions = document.getElementById('questions' + list.id);
		list.className = 'faq_category';
		questions.innerHTML = "";
		div_questions.style.height = '1px';
	}
	
}

function click_question(list, response) 
{
		var size = response.getElementsByTagName('id_faq').length;
		var faq_answer = document.getElementById('faq_answer');
		faq_answer.style.width = '95%';
		
		// Clear the layer if an answer already present in the layer
		if (faq_answer.hasChildNodes) { faq_answer.innerHTML = ""; }
		
		for (i = 0; i < size; i++) {
			
			var curr_id = response.getElementsByTagName('id_faq').item(i).firstChild.data;	
			
			if (curr_id == list.id) {
				var question = response.getElementsByTagName('question').item(i).firstChild.data;	// question read from xml file
				var answer = response.getElementsByTagName('answer').item(i).firstChild.data;	// answer read from xml file
				
				// Display category
				var p = document.createElement('p');
				var h4 = document.createElement('h4');
				var h4_txt = document.createTextNode(question);
				
				h4.appendChild(h4_txt);
				p.appendChild(h4);
			
			}

		}
		// Need to use innerHTML directly on faq_answer (not p) in order to avoid unspecified error exception
		answer = p.innerHTML + answer;
		faq_answer.innerHTML = answer;

		Display.fadein(faq_answer.id);
	
}






function _remove_duplicates(arr) 
{
	var no_dupes = new Array();
	var duplicate = false;
	
	for (i = 0; i < arr.length; i++) {
		var curr = arr.item(i).firstChild.data;
		
		// Traverse through array of unique values to check if curr is already stored in there
		for (j = 0; j < no_dupes.length; j++) {
			if (curr == no_dupes[j]) {
				var duplicate = true;
			}
		}
		
		// Add if not present, reset flag if it is
		if (!duplicate) {
			no_dupes[no_dupes.length] = curr;
		}
		else {
			duplicate = false;
		}
	}
	
	return no_dupes;
}

