//------------------------------------------------------------------------------------------------//
// CONTACT US FUNCTIONS SECTION                                                                   //
//------------------------------------------------------------------------------------------------//
var subjectlist;

//-----------------------------------------------//
// This function add a subject to subjects list. //
//-----------------------------------------------//
function AddSubject(subject, subsubject)
{
	var index;
	var subjectfound = false;
	var currentsubject;
	
	if (!subjectlist)
	{
		// Initialize subject Array;
		 subjectlist = new Array();
	}

	// Search the subject in subjects Array
	for (index = 0; (index < subjectlist.length) && !subjectfound; index++)
	{
		currentsubject = subjectlist[index];
		
		// Test if subject and current subject are defined
		if (subject && currentsubject)
		{
			if (subject.code == currentsubject.code)
			{
				subjectfound = true;
			}
		}
	}

	if (!subjectfound)
	{
		// Add the subject to subjects Array
		subjectlist.push(subject);
		currentsubject = subject;
	}

	// Add the subsubject to the subject
	AddSubSubject(currentsubject, subsubject);
}

//---------------------------------------------//
// This function add a subsubject to a subject //
//---------------------------------------------//
function AddSubSubject(subject, subsubject)
{
	// Test if subject and subsubject are defined
	if (subject && subsubject)
	{
		if (!subject.subsubjects)
		{
			subject.subsubjects = new Array();
		}
		
		// Add subsubject
		subject.subsubjects.push(subsubject);
	}
}

//---------------------------------------------//
// This function fills a select with subjects. //
//---------------------------------------------//
function FillSubject(optionselect,optionhidden)
{
	var index;
	var optionitem;
	var subjectselect = GetElement("mail_subject_list", 0);
	//alert(subjectselect);
	var subjecthidden = GetElement("mail_subject", 0);
	//alert(subjecthidden);
	
	if (subjectselect && subjectlist)
	{
		// Reset subjects
		ResetSelect(subjectselect);
		
		for (index = 0; index < subjectlist.length; index++)
		{
			// Add option item
			optionitem = document.createElement("OPTION");
			optionitem.text = subjectlist[index].text;
			optionitem.value = subjectlist[index].code;
			subjectselect.options[subjectselect.options.length] = optionitem;
			
			if (subjecthidden)
			{
				// Select the current value
				optionitem.selected = (optionitem.value == subjecthidden.value);
			}
		}
		
		if (optionselect)
		{
			var mail_subject_list = GetElement("mail_subject_list", 0);
			
			mail_subject_list_index = mail_subject_list.selectedIndex;
			mail_subject_list.selectedIndex = optionselect;
		}
		
		if (optionhidden)
		{
			var mail_subject_list = GetElement("mail_subject_list", 0);
			MM_showHideLayers("mail_subject_list","","hide");
		}
	}
}

//------------------------------------------------//
// This function fills a select with subsubjects. //
//------------------------------------------------//
function FillSubSubject()
{
	var optionitem;
	var index;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subsubjectselect = GetElement("mail_subsubject_list", 0);
	var subsubjecthidden = GetElement("mail_subsubject", 0);

	if (subjectselect && subsubjectselect)
	{
		// Reset subsubjects					
		ResetSelect(subsubjectselect);
		
		currentsubject = subjectlist[subjectselect.options.selectedIndex - 1];
		
		if (currentsubject && currentsubject.subsubjects)
		{		
			for (index = 0; index < currentsubject.subsubjects.length; index++)
			{
				// Add option item
				optionitem = document.createElement("OPTION");
				optionitem.text = currentsubject.subsubjects[index].text;
				optionitem.value = currentsubject.subsubjects[index].code;
				subsubjectselect.options[subsubjectselect.options.length] = optionitem;
				
				if (subsubjecthidden)
				{
					// Select the current value
					optionitem.selected = (optionitem.value == subsubjecthidden.value);
				}
			}
		}
	}
}

//------------------------------------------------//
// This function resets a select.                 //
//------------------------------------------------//
function ResetSelect(selectlist)
{
	// Remove options
	while (selectlist.options.length != 1)
	{
		selectlist.options[1] = null;
	}
}

//------------------------------------------------//
// This function gets a value from a list using   //
// its index and return the default value if      //
// index is out of range or list is null.         //
//------------------------------------------------//
function GetListValue(list, index, defaultvalue)
{
	if (list && list[index])
	{
		return list[index];
	}
	else
	{
		return defaultvalue;
	}
}	

//------------------------------------------------//
// This function sets mailto and subject values   //
// to hidden fields. It also rebuild subsubjects  //
// list.                                          //
//------------------------------------------------//
function OnSubjectListChange()
{
	var subjectselect = GetElement("mail_subject_list", 0);
	var subject = GetListValue(subjectlist, subjectselect.selectedIndex - 1, null);
	
	if (subject)
	{
		SaveElement('mail_subject', subject.text);
		SaveElement('mail_subject_code', subject.code);
		SaveElement('mail_to', subject.emailto);
	}
	else
	{
		SaveElement('mail_subject', '');
		SaveElement('mail_subject_code', '');
		SaveElement('mail_to', '');
	}
	
	if(GetElement("mail_subsubject_list", 0))
	{
		FillSubSubject();
	}
}

//------------------------------------------------//
// This function sets mailto and subject values   //
// to hidden fields. It also rebuild subsubjects  //
// list.                                          //
//------------------------------------------------//
function OnSubSubjectListChange()
{
	var subjectselect;
	var subsubjectselect;
	var subject;
	var subsubject;
	
	subjectselect = GetElement("mail_subject_list", 0);
	subsubjectselect = GetElement("mail_subsubject_list", 0);
	subject = GetListValue(subjectlist, subjectselect.selectedIndex - 1, null);
	
	if (subject)
	{
		subsubject = GetListValue(subject.subsubjects, subsubjectselect.selectedIndex - 1, null);
		
		if (subsubject)
		{
			SaveElement('mail_subsubject', subsubject.text);
			SaveElement('mail_subsubject_code', subsubject.code);
		}
		else
		{
			SaveElement('mail_subsubject', '');
			SaveElement('mail_subsubject_code', '');
		}
	}
}

