

/**********************************************************/
/*** SmartASP product database ****************************/
/**********************************************************/
var categoryById 	 = new Object();
var productById  	 = new Object();
var productsByCategoryId = new Object();
var categoryList 	 = new Array();
var productList	 	 = new Array();	

/** Represents a Category (une gamme) of products */
function Category(ref, name, aUrl) {
	this.ref 	= ref ;
	this.name 	= name ;
	this.url 	= aUrl ;	
	
	categoryById[ref] = this;
	categoryList.push(this);	
}

/** Represents a product */
function Product(ref, categoryRef, name, aUrl) {
	this.ref 	= ref ;
	this.categoryRef = categoryRef ;
	this.name 	= name ;
	this.url 	= aUrl ;
	
	productById[ref] = this;
	productList.push(this);	
	
	var categoryProducts = productsByCategoryId[this.categoryRef];	
	if(categoryProducts == null) {
		categoryProducts = new Array();
		productsByCategoryId[this.categoryRef] = categoryProducts;
	}
	categoryProducts.push(this);
}

/** Return this Category's Product list */
Category.prototype.getProducts = function() {
	return productsByCategoryId[this.ref];
}

/** Return this Product's Category */
Product.prototype.getCategory = function() {
	return categoryById[this.categoryRef];
}

/** Displays the Gamme select in "Recherche produits" */ 
function displayCategorySelect() {
	var str = '<select name="category" onChange="categorySelected(this);">\n';
	str += '<option> -- sélectionner une gamme -- </option>\n';
	for(var i=0; i<categoryList.length; ++i) {
		var cCategory = categoryList[i];
		str += '<option value="';
		str += cCategory.ref ;
		str += '">';
		str += cCategory.name ;
		str += '</option>\n' ;
	}
	str += "</select>";
	document.write(str);	
}

/** Displays the Produit select in "Recherche produits" */
function displayProductSelect() {
	var str = '<select name="product">\n';
	str += '<option> -- sélectionner un produit -- </option>\n';
	for(var i=0; i<productList.length; ++i) {
		var cItem = productList[i];
		str += '<option value="';
		str += cItem.ref ;
		str += '">';
		str += cItem.name ;
		str += '</option>\n' ;
	}
	str += "</select>";
	document.write(str);	
}

/** Gamme select onchange handler. Restrict product select. */
function categorySelected(categorySelect) {
	var form = categorySelect.form;	
	var productSelect = form.product;	
	var categoryRef = categorySelect.options[categorySelect.selectedIndex].value;	
	var category = categoryById[categoryRef];
	
	empty(productSelect);
	addOption(productSelect, '', ' -- sélectionner un produit -- ');
	if(category != null) {
		populateSelect(productSelect, category.getProducts());
	} else {
		populateSelect(productSelect, productList);
	}
}

/** Used to navigate to the gamme or product page when "recherche produit" form is submitted */
function productFormSumit(productForm) {
	var targetUrl = null ;
	var productSelect = productForm.product;
	var productRef = productSelect.options[productSelect.selectedIndex].value;
	var product = productById[productRef];
	if(product == null) {
		var categorySelect = productForm.category;
		var categoryRef = categorySelect.options[categorySelect.selectedIndex].value;
		var category = categoryById[categoryRef];
		if(category != null) {
			targetUrl = category.url;	
		}
	} else {
		targetUrl = product.url;
	}
	
	if(targetUrl != null) {
		//productForm.action = targetUrl ;
		document.location.href = targetUrl ;
		return false;
	} else {
		alert("Veuillez sélectionner une gamme ou/et un produit.");
		return false;
	}
}

function populateSelect(aSelect, items) {
	if(items != null) {
		for(var i=0; i<items.length; ++i) {
			addOption(aSelect, items[i].ref, items[i].name);	
		}
	}
}

function empty(select) {
	var i = select.options.length;	
	while(i-- > 0) {
		if(select.options.remove) {
			select.options.remove(0);
		} else {
			select.options[0] = null;
		}
	}
}

function addOption(select, optionValue, optionText) {
	var opt = document.createElement("OPTION");
	opt.text = optionText;
	opt.value = optionValue;
	select.options.add(opt);		
}


////////////////////////////////
// Tests
/* 
new Category('c1', 'Google', 'http://www.google.com');
new Category('c2', 'Yahoo', 'http://www.yahoo.com');
new Category('c3', 'Lycos', 'http://www.lycos.com');

new Product('c1_p1', 'c1', 'Groupes', 'http://groups.google.fr/grphp?hl=fr&tab=wg&q=');
new Product('c1_p2', 'c1', 'Annuaire', 'http://www.google.fr/dirhp?hl=fr&tab=gd&q=');

new Product('c2_p1', 'c2', 'Horoscope', 'http://astrology.yahoo.com/astrology/');
new Product('c2_p2', 'c2', 'Shopping', 'http://shopping.yahoo.com/redirect/yahoo_fr.html');

new Product('c3_p1', 'c3', 'Yellow Pages', 'http://yellowpages.superpages.com/yp.basic.jsp?STYPE=S&SRC=lycos8');
*/

////////////////////////////////
//// Product catalog definition


  
   new Category("ADAPT", "Adapt", "/fre/produits/produits-alimentation-cheval/adapt");
   

   
   
       new Product("Horsy", "ADAPT", "Horsy", '/fre/produits/produits-alimentation-cheval/adapt/horsy.html');
        

   
       new Product("Horse-Prim", "ADAPT", "Horse-Prim", '/fre/produits/produits-alimentation-cheval/adapt/horse-prim.html');
        

   
       new Product("Fleky", "ADAPT", "Fleky", '/fre/produits/produits-alimentation-cheval/adapt/fleky.html');
        

   
       new Product("Horse-Run", "ADAPT", "Horse-Run", '/fre/produits/produits-alimentation-cheval/adapt/horse-run.html');
        

   
       new Product("HIPPO_63", "ADAPT", "Hippo-63", '/fre/produits/produits-alimentation-cheval/adapt/hippo-63.html');
        

   
       new Product("Tradition", "ADAPT", "Tradition", '/fre/produits/produits-alimentation-cheval/adapt/tradition.html');
        

   
       new Product("HORSE_RACES", "ADAPT", "Horse-races", '/fre/produits/produits-alimentation-cheval/adapt/horse-races.html');
        

   
       new Product("PANACHE_COMPETITION", "ADAPT", "Panache-Competition", '/fre/produits/produits-alimentation-cheval/adapt/panache-competition.html');
        

   
       new Product("Haras", "ADAPT", "Haras", '/fre/produits/produits-alimentation-cheval/adapt/haras.html');
        

   
       new Product("HORSE_STUD", "ADAPT", "Horse-Stud", '/fre/produits/produits-alimentation-cheval/adapt/horse-stud.html');
        

   
       new Product("HORSE_MIX", "ADAPT", "Horse-mix", '/fre/produits/produits-alimentation-cheval/adapt/horse-mix.html');
        

   


  
   new Category("ADJUST", "Adjust", "/fre/produits/produits-alimentation-cheval/adjust");
   

   
   
       new Product("Loisir/leisure", "ADJUST", "Loisir/Leisure", '/fre/produits/produits-alimentation-cheval/adjust/loisir.html');
        

   
       new Product("Sport/compétition", "ADJUST", "Sport Compétition", '/fre/produits/produits-alimentation-cheval/adjust/sport-competition.html');
        

   
       new Product("Endurance", "ADJUST", "Endurance", '/fre/produits/produits-alimentation-cheval/adjust/endurance.html');
        

   
       new Product("Courses/races", "ADJUST", "Courses/Races", '/fre/produits/produits-alimentation-cheval/adjust/courses.html');
        

   
       new Product("Junior", "ADJUST", "Junior", '/fre/produits/produits-alimentation-cheval/adjust/junior.html');
        

   
       new Product("Elevage/breeding", "ADJUST", "Elevage/Breeding", '/fre/produits/produits-alimentation-cheval/adjust/elevage.html');
        

   
       new Product("Mature", "ADJUST", "Mature", '/fre/produits/produits-alimentation-cheval/adjust/mature.html');
        

   


