function hideContainers()
{
	// logs("hideContainers");
	
	// hide all ellement with "product-preview-content" class
	var column = $id("column2");
	if(column != null)
	{
		var elements = $tagsClasses("div",["product-preview-content"],column);
		hideElements(elements);
	}
	
	// hide all ellement with "product-preview-ajax" class
	var column = $id("column2");
	if(column != null)
	{
		var elements = $tagsClasses("div",["product-preview-ajax"],column);
		hideElements(elements);
	}
	
}

function setModelSelectedIndex(id)
{
	for(var i = 0 ; i < $id('model').options.length;i++)
	{
		if(parseFloat($id('model').options[i].value) == id)
		{
			$id('model').selectedIndex = i;
			break;
		}
	}
}

function displayModels(oXML /*XML*/,params) 
{
	var container = $id('model-select-container');
	if(container != null)
	{
		container.innerHTML = oXML.responseText;
	}

	if(isset(params.ID))
	{
		setModelSelectedIndex(params.ID)
	}
}

function eraseProductBorder()
{
	var elements = $tagsClasses("div",["product-item"]);
	for (var i=0; i < elements.length; i++) {
		elements[i].style.border = "1px solid #FFF";
	};
	
}

function showMooblyContent(oXML/*XML*/,params/*object*/)
{
	var bulContent = $id('content-bull')
	if(bulContent != null)
	{
		if(oXML.responseText != '')
		{
			bulContent.innerHTML = oXML.responseText;
			
			if($id('moobly-man') != null)
				show('moobly-man');
			
			if($id('moobly') != null)
			{
				show('moobly');
				startDelay();			
			}
		}
	}
}

/*
function replaceSomeCaracter(string,froms,tos)
{
	if(!isset(string)) return;
	if(froms.length != tos.length) return;
	
	for(var i = 0 ; i< froms.length; i++)
	{
		var search = '/' + froms[i] + '/g';
		string = string.replace(search,tos[i]);		
	}
	return string;
}
*/

function showProduct(oXML/*XML*/,params/*object*/)
{
	if(isset(params.ID))
	{
		var id = params.ID;
		
		hideContainers();
		
		var product = $id('product-' + id);
		if(product != null)
		{
			if($id('normal-content-column2-media') != null)
				hide('normal-content-column2-media');
			
			show('product-' + id);
			product.innerHTML = oXML.responseText;		
			
			if($id('preview-flash-' + id) != null)
			{
				if($id('preview-data-' + id) != null)
				{
					var data = $inner('preview-data-' + id);
					data = data.replace(/&amp;/g,'%26');

					var url = $inner('preview-url-' + id);
					url = url.replace(/&amp;/g,'%26');
					
					if($id('ancestor1') != null)
						url += '%26ancestor1=' + $value('ancestor1');

					if($id('ancestor2') != null)
						url += '%26ancestor2=' + $value('ancestor2');
						
					if($id('price') != null)						
						url += '%26price=' + $value('price');
						
					if($id('brand') != null)							
						url += '%26brand=' + $value('brand');
					
					url = "<url>" + url + "</url>";
					
					var so = new SWFObject("preview.swf", "preview-product-" + id, "148px", "300px", "9", "#F");
					so.addParam("quality", "best");
					so.addParam("FlashVars", "xmldata=" + data + "&url=" + url);
					so.addParam("scale", "noscale");
					so.addParam("wmode", "opaque");
					so.write("preview-flash-" + id);
				}
			}
		}
	}
}

function setMotor(id)
{
//	logs("setMotor : " + id);
	
	switch(id)
	{
		case '247': // GSM
			show('formuleTypes');
			show('familiesMobile');			
		break;
		
		case '248': // FIXE
			hide('formuleTypes');
			hide('familiesMobile');			
		break;
	}
	
	var options = $inner("options-" + id);
	var optionsArray = eval(options);
	replaceSelectOptions('brand',optionsArray);

	var options = $inner("models-" + id);
	var optionsArray = eval(options);
	replaceSelectOptions('model',optionsArray);

	updateLiveSearch();
}

function replaceSelectOptions(selectID,optionsArray)
{
	var select = $id(selectID);
	var selectOptions = select.options;
	
	for (var i = selectOptions.length; --i >= 0;) 
	{
		select.remove(selectOptions[i]);
	};
	
	for (var i = 0 ;i < optionsArray.length; i++ ) 
	{
		var newOption = document.createElement('option');		
		newOption.text = optionsArray[i].label;
		newOption.value = optionsArray[i].value;
		try
	    {
			select.add(newOption,null); // standards compliant
	    }
	  	catch(ex)
	    {
			select.add(newOption);// IE only
	    }
	};
	
}

/////////////////////////////////////////////////////////// SEARCH

function addCategories(xmlLoader,classArray)
{
	var elements = $tagsClasses("input",classArray);
	for (var i=0; i < elements.length; i++) 
	{
		if(elements[i].checked)
		{
			var el = elements[i];
			xmlLoader.addParam(el.id,el.value);		
		}
	};
}

function setBrands(brand)
{
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchProductWithBrandID.php");	
	xmlLoader.addCallBack(this,"displayModels",{ID:initSearchProductID});
	xmlLoader.addParam("brand",brand);
	xmlLoader.addParam("type",$value('type'));
	
	if($id('ancestor1') != null)
		xmlLoader.addParam("ancestor1",$value('ancestor1'));		

	if($id('ancestor2') != null)
		xmlLoader.addParam("ancestor2",$value('ancestor2'));	
	
	xmlLoader.load();
	
	delete xmlLoader;

	if(!initSearchProductPage)
	{
		updateLiveSearch();
	}
	
	initSearchProductPage = false;		
}

function initSelect(id)
{
	$id(id).selectedIndex = 0;
}

function updateLiveSearch()
{
	// logs("updateLiveSearch");
	
	var xmlLoader = new XMLLoader();
	xmlLoader.setURL("searchLiveProduct.php");	
	xmlLoader.addCallBack(this,"onUpdateLiveSearch");
	
	if($id('brand') != null)
		xmlLoader.addParam("brand",$value('brand'));
		
	if($id('price') != null)	
		xmlLoader.addParam("price",$value('price'));

	if($id('ancestor1') != null)		
		xmlLoader.addParam("ancestor1",$value('ancestor1'));
		
	if($id('ancestor2') != null)		
		xmlLoader.addParam("ancestor2",$value('ancestor2'));

	if($id('type') != null)	
		xmlLoader.addParam("type",$value('type'));	
	
	if($id('familymobile') != null)			
		if($value('familymobile') != 'none')			
			xmlLoader.addParam("familymobile",$value('familymobile'));	

	if($id('familypc') != null)			
		if($value('familypc') != 'none')			
			xmlLoader.addParam("familypc",$value('familypc'));	

	if($id('fix') != null && $id('fix').checked)
	{
		addCategories(xmlLoader,["categories-search-mode"]);
	}
	else
	{
		if($id('post') != null)
			if($id('post').checked)
				xmlLoader.addParam("formuletype",$value('post'));	

		if($id('pre') != null)
			if($id('pre').checked)
				xmlLoader.addParam("formuletype",$value('pre'));	
				if($id('type') != null)	
					if($value('type') == 'Formulation')
						xmlLoader.addParam("mobile",'247');	
				addCategories(xmlLoader,["categories-search"]);
	}
	
	xmlLoader.load();
	
	delete xmlLoader;
}

function onUpdateLiveSearch(oXML/*XML*/)
{
// 	logs("onUpdateLiveSearch");
			
	var liveSearchContent = $id('live-search-content-column-media');
	if(liveSearchContent != null)
	{
		if($id('normal-content-column1-media') != null)
			hide('normal-content-column1-media');
			
		liveSearchContent.innerHTML = oXML.responseText;
	}
}


/////////////////////////////////////////////////////////// MOBILE


function setMobile(mobileID /*string*/,url /*string*/) 
{
	if(isset(url))
	{
		if(mobileID != 'none')
			this.location = url + "&ID=" + mobileID;
	}
	else
	{
		// set all border of "product-item" to white
		eraseProductBorder();
		
		// load the mobile
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchProductID.php");
		xmlLoader.addCallBack(this,"showProduct",{ID:mobileID});
		xmlLoader.addParam("ID",mobileID);
		xmlLoader.addParam("type",'Mobile');		
		
		if($id('ancestor1') != null)		
			xmlLoader.addParam("ancestor1",$value('ancestor1'));
		if($id('ancestor2') != null)			
			xmlLoader.addParam("ancestor2",$value('ancestor2'));		

		xmlLoader.load();

		delete xmlLoader;
		
		// load mobile moobly content
		
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchMooblyContentProduct.php");
		xmlLoader.addCallBack(this,"showMooblyContent",{ID:mobileID});
		xmlLoader.addParam("ID",mobileID);

		xmlLoader.load();

		delete xmlLoader;

		if($id('moobly-man') != null)
			hide('moobly-man');
		
		if($id('moobly') != null)
		{
			hide('moobly');
			stopDelay();			
		}
	}
}

/*
function setMobileBrands(brand) 
{
	// logs("setMobileBrands : " + initSearchMobilePage);
	
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchProductWithBrandID.php");	
	xmlLoader.addCallBack(this,"displayModels",{ID:initSearchProductID});
	xmlLoader.addParam("brand",brand);
	xmlLoader.addParam("type",'Mobile');
	
	xmlLoader.load();
	
	delete xmlLoader;

	if(!initSearchProductPage)
		updateMobileLiveSearch();

	initSearchProductPage = false;
}

function updateMobileLiveSearch()
{
	// logs("updateLiveSearch");
	
	var xmlLoader = new XMLLoader();
	xmlLoader.setURL("searchLiveProduct.php");	
	xmlLoader.addCallBack(this,"onUpdateMobileLiveSearch");
	xmlLoader.addParam("brand",$value('brand'));
	xmlLoader.addParam("price",$value('price'));
	xmlLoader.addParam("ancestor2",$value('ancestor2'));

	addCategories(xmlLoader);
	
	xmlLoader.load();
	
	delete xmlLoader;
}

function onUpdateMobileLiveSearch(oXML)
{
 	// logs("onUpdateLiveSearch");
			
	var liveSearchContent = $id('live-search-content-column-media');
	if(liveSearchContent != null)
	{
		if($id('normal-content-column-media') != null)
			hide('normal-content-column-media');
			
		liveSearchContent.innerHTML = oXML.responseText;
	}
	
}
*/

//////////////////////////////////////////////////////////////////// LAPTOP


function setLaptop(laptopID,url)
{
	// logs("setLapTop : " + laptopID + ' - ' + url);
	
	if(isset(url))
	{
		if(laptopID != 'none')
			this.location = url + "&ID=" + laptopID;
	}
	else
	{
		// set all border of "product-item" to white
		eraseProductBorder();
	
		// load the laptop
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchProductID.php");
		xmlLoader.addCallBack(this,"showProduct",{ID:laptopID});
		xmlLoader.addParam("ID",laptopID);
		xmlLoader.addParam("type",'Laptop');		

		if($id('ancestor1') != null)		
			xmlLoader.addParam("ancestor1",$value('ancestor1'));	
		
		xmlLoader.load();

		delete xmlLoader;
		
		
		// load laptop moobly content
		
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchMooblyContentProduct.php");		
		xmlLoader.addCallBack(this,"showMooblyContent",{ID:laptopID});
		xmlLoader.addParam("ID",laptopID);

		xmlLoader.load();

		delete xmlLoader;
				
		if($id('moobly-man') != null)
			hide('moobly-man');
	
		if($id('moobly') != null)
		{
			hide('moobly');
			stopDelay();			
		}
	}	
}

/*
function setLaptopBrands(brand)
{
	// logs("setLaptopBrands : " + initSearchLaptopPage);
	
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchProductWithBrandID.php");		
	xmlLoader.addCallBack(this,"displayModels",{ID:initSearchProductID});
	xmlLoader.addParam("brand",brand);
	xmlLoader.addParam("type",'Laptop');
		
	xmlLoader.load();
	
	delete xmlLoader;

	if(!initSearchLaptopPage)
		updateLaptopLiveSearch();

	initSearchLaptopPage = false;
}

function updateLaptopLiveSearch()
{
//	logs("updateLaptopLiveSearch");
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchLiveProduct.php");		
	
	xmlLoader.addCallBack(this,"onUpdateLaptopLiveSearch");
	xmlLoader.addParam("brand",$value('brand'));
	xmlLoader.addParam("price",$value('price'));
	xmlLoader.addParam("ancestor2",$value('ancestor2'));
	
	xmlLoader.load();
	
	delete xmlLoader;
}

function onUpdateLaptopLiveSearch(oXML)
{
//	logs("onUpdateLaptopLiveSearch");
	
	var liveSearchContent = $id('live-search-content-column-media');
	if(liveSearchContent != null)
	{
		if($id('normal-content-column-media') != null)
			hide('normal-content-column-media');
			
		liveSearchContent.innerHTML = oXML.responseText;
	}
	
}
*/


//////////////////////////////////////////////////////////////////// ACCESSORY


function setAccessory(accessoryID,url)
{
	// logs("setAccessory : " + accessoryID + " - " + url);	
	
	if(isset(url))
	{
		if(accessoryID != 'none')
			this.location = url + "&ID=" + accessoryID;
	}
	else
	{
		// set all border of "product-item" to white
		eraseProductBorder();
	
		// load the accessory
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchProductID.php");
		xmlLoader.addCallBack(this,"showProduct",{ID:accessoryID});
		xmlLoader.addParam("ID",accessoryID);
		xmlLoader.addParam("type",'Accessory');		

		if($id('ancestor1') != null)		
			xmlLoader.addParam("ancestor1",$value('ancestor1'));	
		
		xmlLoader.load();

		delete xmlLoader;
		
		// load accessories moobly content
		
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchMooblyContentProduct.php");		
		xmlLoader.addCallBack(this,"showMooblyContent",{ID:accessoryID});
		xmlLoader.addParam("ID",accessoryID);

		xmlLoader.load();

		delete xmlLoader;
				
		if($id('moobly-man') != null)
			hide('moobly-man');
	
		if($id('moobly') != null)
		{
			hide('moobly');
			stopDelay();			
		}
	}	
	
}

/*
function setAccessoryBrands(brand)
{
//	logs("setAccessoryBrands : " + brand);
	
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchProductWithBrandID.php");		
	xmlLoader.addCallBack(this,"displayModels",{ID:initSearchProductID});
	xmlLoader.addParam("brand",brand);
	xmlLoader.addParam("type",'Accessory');
			
	xmlLoader.load();
	
	delete xmlLoader;

	if(!initSearchAccessoryPage)
		updateAccessoryLiveSearch();

	initSearchAccessoryPage = false;
}

function updateAccessoryLiveSearch()
{
	// logs("updateAccessoryLiveSearch");	
	
	var xmlLoader = new XMLLoader();
	
	xmlLoader.setURL("searchLiveProduct.php");	
	xmlLoader.addCallBack(this,"onUpdateAccessoryLiveSearch");
	xmlLoader.addParam("brand",$value('brand'));
	xmlLoader.addParam("price",$value('price'));
	xmlLoader.addParam("ancestor2",$value('ancestor2'));

	xmlLoader.load();
	
	delete xmlLoader;
}

function onUpdateAccessoryLiveSearch(oXML)
{
	// logs("onUpdateAccessoryLiveSearch");
	
	var liveSearchContent = $id('live-search-content-column-media');
	if(liveSearchContent != null)
	{
		if($id('normal-content-column-media') != null)
			hide('normal-content-column-media');
			
		liveSearchContent.innerHTML = oXML.responseText;
	}

}
*/


//////////////////////////////////////////////////////////////////// FORMULATION


function setFormulation(formulationID,url)
{
	 // logs("setFormulation : " + formulationID + " - " + url);	
	
	if(isset(url))
	{
		if(formulationID != 'none')
			this.location = url + "&ID=" + formulationID;
	}
	else
	{
		// set all border of "product-item" to white
		eraseProductBorder();
	
		// load the accessory
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchProductID.php");
		xmlLoader.addCallBack(this,"showProduct",{ID:formulationID});
		xmlLoader.addParam("ID",formulationID);
		xmlLoader.addParam("type",'Formulation');	
		
		if($id('ancestor1') != null)		
			xmlLoader.addParam("ancestor1",$value('ancestor1'));	
		
		xmlLoader.load();

		delete xmlLoader;
		
		// load accessories moobly content
/*		
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchMooblyContentProduct.php");		
		xmlLoader.addCallBack(this,"showMooblyContent",{ID:formulationID});
		xmlLoader.addParam("ID",formulationID);

		xmlLoader.load();

		delete xmlLoader;
*/				
		if($id('moobly-man') != null)
			hide('moobly-man');
	
		if($id('moobly') != null)
		{
			hide('moobly');
			stopDelay();			
		}
	}	
	
}

//////////////////////////////////////////////////////////////////// FORMULATION


function setPromotion(promotionID,url,ancestor2)
{
	 // logs("setPromotion : " + promotionID + " - " + url);	
	
	if(isset(url))
	{
		if(promotionID != 'none')
			this.location = url + "&ID=" + promotionID;
	}
	else
	{
		// set all border of "product-item" to white
		eraseProductBorder();
	
		// load the promotion
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchProductID.php");
		xmlLoader.addCallBack(this,"showProduct",{ID:promotionID});
		
		xmlLoader.addParam("ID",promotionID);
		xmlLoader.addParam("type",'Promotion');		
		
		if($id('ancestor1') != null)		
			xmlLoader.addParam("ancestor1",$value('ancestor1'));
			
		if(isset(ancestor2))		
			xmlLoader.addParam("ancestor2",ancestor2);

			
		xmlLoader.load();

		delete xmlLoader;
		
		// load accessories moobly content
/*		
		var xmlLoader = new XMLLoader();
		xmlLoader.setURL("searchMooblyContentProduct.php");		
		xmlLoader.addCallBack(this,"showMooblyContent",{ID:formulationID});
		xmlLoader.addParam("ID",formulationID);

		xmlLoader.load();

		delete xmlLoader;
*/				
				
		if($id('moobly-man') != null)
			hide('moobly-man');
	
		if($id('moobly') != null)
		{
			hide('moobly');
			stopDelay();			
		}
	}	
	
}