//------------------------------------------------------------------------------------------------//
// MAIN FUNCTIONS SECTION                                                                         //
//------------------------------------------------------------------------------------------------//
function GetMainFormName()
{
	var i;
	for (i=0;i<document.forms.length;i++)
	{
		// Framework v1.0.3705
		if (document.forms[i].name.indexOf("_ServerForm") != -1)
			return document.forms[i].name;
		// Framework v1.1.4322
		if (document.forms[i].name.indexOf("__aspnetForm") != -1)
			return document.forms[i].name;
		// Framework v2.0.50727
		if (document.forms[i].name.indexOf("aspnetForm") != -1)
		    return document.forms[i].name;	
	}
	return null;
}

function SaveElement(ElementObject,ElementValue)
{
	var mf = GetMainFormName();					
	if (mf==null) return;
	var element  = GetElement(ElementObject,mf);
	if (element!=null){ 
		element.value = ElementValue;
	}
}

function GetElementName(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].name.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].name.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].name;
		}	
	}
	return null;
}

function GetElementID(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].id.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].id.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].id;
		}	
	}
	return null;
}

function GetElement(ElementName, formName)
{
	return document.forms[formName].elements[GetElementID(ElementName,formName)];
}

//------------------------------------------------------------------------------------------------//
// 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()
{
	var index;
	var optionitem;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subjecthidden = GetElement("mail_subject", 0);
	
	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);
			}
		}
	}
}

//------------------------------------------------//
// 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 subsubject;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subject = GetListValue(subjectlist, subjectselect.selectedIndex - 1, null);    
	    
	if (subject)
	{
		// Get the first subsubject for current subject
		subsubject = subject.subsubjects[0];
		
		SaveElement('mail_subject', subject.text);
		SaveElement('mail_subject_code', subject.code);
		
		if (subsubject){
			SaveElement('mail_to', subsubject.emailto);
			SaveElement('mail_bcc', subsubject.emailbcc);
		}
	}
	else
	{
		SaveElement('mail_subject', '');
		SaveElement('mail_subject_code', '');
		SaveElement('mail_to', '');
		SaveElement('mail_bcc', '');
	}
	
	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);
			SaveElement('mail_to', subsubject.emailto);
			SaveElement('mail_bcc', subsubject.emailbcc);
		}
		else
		{
			SaveElement('mail_subsubject', '');
			SaveElement('mail_subsubject_code', '');
			SaveElement('mail_to', '');
			SaveElement('mail_bcc', '');
		}
	}
}

//------------------------------------------------//
// Forum functions								  //
//------------------------------------------------//
	function afficheEtoiles(nb)
	{
		for(i=0;i<nb && nb<=5;i++)
			document.write("<img src=\"/shuuemura/img/img_ref/common/forum/pict_etoile.gif\" border=\"0\">");
	}

	var OpenedDiv = "";
	
	function DisplayPost(divname)
	{
		if(OpenedDiv != "")
			document.getElementById(OpenedDiv).style.display="none";

		if(OpenedDiv != divname)
		{
			document.getElementById(divname).style.display="block";
			OpenedDiv = divname;
		}
		else
			OpenedDiv = "";
	}
	
	function forumsearch() 
	{
		var mf = GetMainFormName();
		if (mf==null) return;
		var sfield = document.forms[mf].searchfield.value;
		var criteria = document.forms[mf].selectsearch.selectedIndex;
		if (criteria=="0")
		{
			alert("no critaria");
			return;
		}
		if (sfield=="")
		{
			alert("no field");
			return;
		}
			else window.location ="search.aspx?searchid="+ criteria +"&search=" + sfield
	}

var newWindow;
function openPopup(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 2;
	var wint = (screen.height - popupHeight) / 2;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}

function openPopup2(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 6;
	var wint = (screen.height - popupHeight) / 6;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}

function openPopup3(url,popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl =0;
	var wint =0;
    var w=screen.width;
    var h=screen.height;
    if (popupWidth>0) { w =popupWidth;}
    if (popupHeight>0) { h =popupHeight;}
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" +w + ",height=" +h + "\""));
	newWindow.focus();
}
function openPopup4(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 8;
	var wint = (screen.height - popupHeight) / 8;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}
function openOpener1(url) {

	try {
	    window.opener.focus()
	    window.close();
	
	}
	catch (e) {//error is raised of opener is closed, then open a new window
	
	   var newWindow1;	
	   newWindow1=open(url, '', ("toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,top=" + 0 + ",left=" + 0 + ",width=" +screen.width + ",height=" +screen.height + "\""));
	   newWindow1.focus();
	   window.close();
	}
		
	
}

function openPressPopup(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 6;
	var wint = (screen.height - popupHeight) / 6;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}



function NavigateTo(_url) {
	if (_url!="") document.location.href= _url;
}

//---------------------------------------------------------//
// Validate zipcode: Zip should have 5 numeric characters  //
//---------------------------------------------------------//
function ValidateZip(zipTextbox)
{		
	//Check if zip code length is 5
	if(zipTextbox.value.length != 5)
	{	    
		return false;
	}				
	else			
	{
		//If zipcode length is 5, only allow 0-9 to be entered as the characters
		var checkOK = "0123456789";
		var allValid = true;
		for (var i=0; i < zipTextbox.value.length; i++) 
		{ 
			ch = zipTextbox.value.charAt(i);
			
			for (var j = 0;  j < checkOK.length;  j++)
			{							
				if (ch == checkOK.charAt(j))
					break;							
			}
			
			if (j == checkOK.length)
			{							
				allValid = false;
				break;
			}
		}
		if (!allValid) 
		{
			return false;
		}	
		else
		{
			return true;
		}						
	}
}
