//------------------------------------------------------------------------------------------------//
// 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;
	}
	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 GetElement(ElementName, formName)
{
	return document.forms[formName].elements[GetElementName(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);
	}
	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);
			SaveElement('mail_to', subsubject.emailto);
		}
		else
		{
			SaveElement('mail_subsubject', '');
			SaveElement('mail_subsubject_code', '');
			SaveElement('mail_to', '');
		}
	}
}

//------------------------------------------------//
// Forum functions								  //
//------------------------------------------------//
	function afficheEtoiles(nb)
	{
		for(i=0;i<nb && nb<=5;i++)
			document.write("<img src=\"/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
		}
		
/************************************/
/*	Used to Display the link		*/
/************************************/
function ConditionalDisplay(url,value, descrition, parametre, type)
{
	
	
	if (value != null && value != "")
	{
		switch (type)
		{
			case "img" :	TextToDisplay = '<img src="'+ url + value +'" alt="'+descrition;
							if (parametre != null  && parametre != "")
									TextToDisplay = TextToDisplay + '" ' + parametre +' />';
							else
									TextToDisplay = TextToDisplay +'" />';
							break;
			case "href" :	TextToDisplay = '<a href="'+ url + value +'">'+descrition+'</a>';
							break;
			default :
							break;
			
		}
		//alert(TextToDisplay)
		document.write(TextToDisplay);
	}
}

function GoPreAddToBasket(rtlid, RetailerFile)
{
	var frm = GetMainFormName();
	if (frm!=null)
	{
		var sku = GetElementName("varlist", frm);
		if (sku!=null)
		{
			var selindex = document.forms[frm].elements[sku].selectedIndex;
			var ProductId= document.forms[frm].elements[sku].options[selindex].value;
			ProductId=ProductId.slice(0,ProductId.indexOf("#"));			
			// ProductId = ProductId.slice(ProductId.lastIndexOf("#")+1);
			var qtyEl = GetElementName("qtylist", frm);
			
			if(qtyEl!=null)
			{
				var qty = document.forms[frm].elements[qtyEl].value;
			
				if(GoAddToBasket!=null && GoAddToBasket!='undefined' && qty!=null && qty!='undefined' && ProductId!=null && ProductId!='undefined')
				{
					if (qty > 0)
					{
						// window.open (RetailerFile + '?pid=' + ProductId + '&qty=' + qty + '&rtlid=' + rtlid, "_blank", "top=0,left=5000,height=100,width=100");
						// Call the retailer addtobasket action
						GoAddToBasket(document.forms[frm],ProductId, qty);
					}
				}
			}
		}
	}
	void(0);
}

function GoPreAddToBasketForCollection(nameprefix, ContainerNumber, rtlid, RetailerFile)
{
      var frm= GetMainFormName();
      if (frm!=null)
      {
					var sku = GetElName("varlist",ContainerNumber);
					var selindex = document.forms[frm].elements[sku].selectedIndex;
					var ProductId= document.forms[frm].elements[sku].options[selindex].value;
					var qtyEl = GetElName("qtylist",ContainerNumber);
					ProductId=ProductId.slice(0,ProductId.indexOf("#"));			
					// ProductId = ProductId.slice(ProductId.lastIndexOf("#")+1);
                  //alert('sku: '+sku+'--index: '+ selindex+ '--Id: '+ProductId+'--qtyEl: '+qtyEl);
                  if(qtyEl!=null)
                  {
                        var qty = document.forms[frm].elements[qtyEl].value;
                        // alert('qty: '+qty+'--ProductId: '+ProductId);
                        if(GoAddToBasket!=null && GoAddToBasket!='undefined' && qty!=null && qty!='undefined' && ProductId!=null && ProductId!='undefined')
                        {
                             if (qty > 0)
                             {
                                   // window.open (RetailerFile + '?pid=' + ProductId + '&qty=' + qty + '&rtlid=' + rtlid, "_blank", "top=0,left=5000,height=100,width=100");
                                   // Call the retailer addtobasket action
                                   GoAddToBasket(document.forms[frm],ProductId, qty);
                             }
                        }
                  }
      }
}

function loadprice(){
	var frm = GetMainFormName();
	if (frm!=null){
		var sku = GetElementName("varlist", frm);
		if (sku!=null){
			var first = document.forms[frm].elements[sku].options[0].value;
			selectprice(first);
		}
	}
}