//src=listbox
//num=indexname of composant (ex: "" = main page, "2" popup page)

/**
 * Dynamic behavior of elements of product page in function of the selected variant
 * num = 2 in case of call from the popup "size - quantity"
 * hideButtonAddToCart : used for countries - the add to cart button is not clickable after	page load if there are several variants
 * numberOfvariants : given only in countries - else it ias calculated from combo box
 * index : index of selected variant - same thing, it is given only in countries
 */
function displayPrice(src,num, hideButtonAddToCart, numberOfvariants, index) {

	if (numberOfvariants==undefined || index==undefined) {
		//get index 
		index = 0; 
		numberOfvariants = 1;
	    try {
	       if (src.length!=undefined) numberOfvariants = src.length;
	    } catch (e) {
	       numberOfvariants=1;
	    }
	    
	    if (numberOfvariants==undefined) {
	    	numberOfvariants = 1;
	    }
	    
		if(numberOfvariants == 1){
			index = 1;
		} else {
			index = src.selectedIndex;
		}
	}

	// flags to determine if we have to displays some elements on the page
	var displayPrice = true;
	var displayAvailability = false;
	var displayNonAvailability = false;
	var displayPartiallyAvailable = false;
	var displayItemButton = false;	
	var displayPromoFromThru = false;	
	var displayFromPrice = false;	
	var displayFromPricePromo = false;
	var selectedIndex = index;
	
	// there is one single variant or one variant has been selected on the list
	if(numberOfvariants == 1 || index != 0){
	
		// one variant : no choose of the variant - set the index to 1 to display values.
		if (numberOfvariants == 1) {
			index = 1;
		}
		
		displayPrice = true;
		displayFromPrice = false;	
		displayFromPricePromo = false;
		// availability
		maxDelay = MAXDELAY[index];
		if (maxDelay != 0 && maxDelay != undefined) {
			displayAvailability = true;
			displayItemButton = true;
		}
		else {
			displayNonAvailability = true;
			displayItemButton = false;
		}
		hideButtonAddToCart = false;
	}
	// several variants and no one has been selected : 
	// displays price if all variants prices are equals
	// displays availability if all variants availabilities are equals
	else {
        // set the index to 1 to display values from 1st variant
        // the code below will determine if we have to display the data or not
        index = INDEXMINPRICE;
        displayFromPrice = !ALLPRICESEQUALS;
		displayFromPricePromo = !ALLPROMOEQUALS;
        
        displayPrice = true;
        maxDelay = MAXDELAY[0];
        if (num==2) {
            displayItemButton = false;
        } else {    
            displayItemButton = true;
        }
        
        // model is not available
        if (modelAvailability == "U") {
       		displayNonAvailability = true;
        	displayItemButton = false;
        }
        
        else {
        	if (num=="") displayItemButton=true;
        	// model available
        	if (modelAvailability == "A") {
        		displayAvailability = true;
        	}
        	// model partially available
        	else {
        		displayPartiallyAvailable = true;
        	}
        }
   	}
	
	// Undefined taille
	if (document.getElementById('TsMemoTaille2') != undefined && document.getElementById('TsMemoTaille2').value == '...') {
		displayItemButton = false;
	}
	
	// -----------
	// init values
	// -----------
			
	// displays the product price
	if (displayPrice) {
		if (PROMOPRICES[index]) {
			// PROMO
			var oldPrice = PRICES[index];
			var newPrice = PROMOPRICES[index];
			var promoPercentage = PROMOPERCENTAGE[index];			
			var promoFromDate = PROMOFROMDATES[index];
			var promoThruDate = PROMOTHRUDATES[index];
			var deee = DEEE[index];
						
			//set values
			document.getElementById('old_price'+num).innerHTML = oldPrice;
			document.getElementById('new_price'+num).innerHTML = newPrice;
			document.getElementById('promo_percentage'+num).innerHTML = promoPercentage;
			if(deee != undefined){
				document.getElementById('deee_promo'+num).innerHTML = deee;
			}

			//set display
			document.getElementById('produit_prix'+num).style.display = "none";
			document.getElementById('produit_promo'+num).style.display = "block";
			if (promoThruDate != "noEndDate"){
				document.getElementById('promo_from_date'+num).innerHTML = promoFromDate;
				document.getElementById('promo_thru_date'+num).innerHTML = promoThruDate;
				document.getElementById('price_delay'+num).style.display = "block";
			} else {
				//hide the display of the start date/end Date
				document.getElementById('price_delay'+num).style.display = "none";
			}
		}
		else {
			// NOT PROMO			
			var displayPrice = PRICES[index];
			var deee = DEEE[index];
			
			//set values
			if(displayPrice != undefined){
				document.getElementById('price'+num).innerHTML = displayPrice;
			}
			if(deee != undefined){
				document.getElementById('deee_prix'+num).innerHTML = deee; 
			}
			
			//set display
			document.getElementById('produit_promo'+num).style.display = "none";
			document.getElementById('produit_prix'+num).style.display = "block";
		}
		
		// display the high store price
		var highStorePrice = STOREPRICES[index];
		if(highStorePrice != undefined){
			var storePriceElement = document.getElementById('price_magasin');
			if (storePriceElement != undefined){
				storePriceElement.innerHTML = highStorePrice;
			}
		}
		
		if (document.getElementById('price_from'+num)) {
			if (displayFromPrice == false && displayFromPricePromo == false){
				document.getElementById('price_from'+num).style.display = "none";
			} else {
				document.getElementById('price_from'+num).style.display = "block";
			}
		}
		// display the price per unit (15EUR / Kg...)
		if (PERUNITPRICES[index]) {
			if( document.getElementById('unit_price') ) document.getElementById('unit_price').style.display = "block";
			if( document.getElementById('unit_price_promo') ) document.getElementById('unit_price_promo').style.display = "block";
			if( document.getElementById('unit_price') ) document.getElementById('unit_price').innerHTML = PERUNITPRICES[index];
			if( document.getElementById('unit_price_promo') ) document.getElementById('unit_price_promo').innerHTML = PERUNITPRICES[index];
		}
		else {
			if( document.getElementById('unit_price') ) document.getElementById('unit_price').style.display = "none";
			if( document.getElementById('unit_price_promo') ) document.getElementById('unit_price_promo').style.display = "none";
		}
	}
	else {
		//set values
		document.getElementById('price'+num).innerHTML = "";
		//set display
		document.getElementById('produit_promo'+num).style.display = "none";
		document.getElementById('produit_prix'+num).style.display = "block";
		document.getElementById('deee'+num).style.display = "none";
		if (document.getElementById('price_from'+num)) {
			document.getElementById('price_from'+num).style.display = "none";
		}
	}

	// ----------------------------------
	// displays or not availability label
	// ----------------------------------
	
	// displays the product availability
	if (displayAvailability) {
		if (document.getElementById('itemAvailable'+num)) {
			document.getElementById('itemAvailable'+num).style.display='block';
		}
		var maxDelaySpan = document.getElementById('livraison_content'+num);
		maxDelaySpan.innerHTML=maxDelay;
		document.getElementById('infosLivraison'+num).style.display='block';
		if (selectedIndex == 0 && numberOfvariants > 1) {
			setClass("livraison no-selection",document.getElementById('infosLivraison'+num));
		}
		else {
			setClass("livraison",document.getElementById('infosLivraison'+num));
		}
	}
	else {
		if (document.getElementById('itemAvailable'+num)) {
			document.getElementById('itemAvailable'+num).style.display='none';
		}
	}

	// displays the product non-availability
	if (displayNonAvailability) {
		if (document.getElementById('dispo-main')) {
			document.getElementById('dispo-main').style.display='block';
		}
		if (document.getElementById('itemNotAvailable'+num)) {
			document.getElementById('itemNotAvailable'+num).style.display='block';
		}
		document.getElementById('infosLivraison'+num).style.display='block';
		setClass("livraison-nondispo",document.getElementById('infosLivraison'+num));
	}
	else {
		if (document.getElementById('itemNotAvailable'+num)) {
			document.getElementById('itemNotAvailable'+num).style.display='none';
		}
	}
	
	// displays the product Partially Available
	if (displayPartiallyAvailable) {
		if (document.getElementById('itemPartiallyAvailable'+num)) {
			document.getElementById('itemPartiallyAvailable'+num).style.display='block';
		}
		document.getElementById('infosLivraison'+num).style.display='block';
		setClass("livraison-nondispo",document.getElementById('infosLivraison'+num));
	}
	else {
		if (document.getElementById('itemPartiallyAvailable'+num)) {
			document.getElementById('itemPartiallyAvailable'+num).style.display='none';
		}
	}	
	
	if (!displayAvailability && !displayNonAvailability ){
		document.getElementById('infosLivraison'+num).style.display='none';
	}
			
	// ------------------------------------------------
	// display or not the add item button (add to cart)
	// ------------------------------------------------	
	var addToCartDiv = document.getElementById("addItemButton"+num);
	
	if (addToCartDiv) {
		
		if (displayItemButton && ISSELLABLE!=false && ONLINEBUYABLE!=false && !hideButtonAddToCart) {
		    setClass("ajoutPanier", addToCartDiv);
		    var addToCartLink = addToCartDiv.getElementsByTagName('A')[0];
		    if (!_notAddInCartTips) {
		      
		      try {
		          document.getElementById("tooltip-addcart-text"+num).innerHTML=_messageAddToCart;
		      } catch(e){
		          addToCartLink.title = _messageAddToCart;
		      };
		    } else {
		      setClass("ajoutPanier inactive", addToCartDiv);
		    }
		}
		else {
			setClass("ajoutPanier inactive", addToCartDiv);
			
	        var addToCartLink = addToCartDiv.getElementsByTagName('A')[0];
	        if (!_notAddInCartTips) {
	           try {
	               document.getElementById("tooltip-addcart-text"+num).innerHTML=_messageChooseSize;
	           } catch(e){
	               addToCartLink.title = _messageChooseSize;
	           };
	        }
		}
	}


}
				

//Modify quantity add value(pQuantity) in textbox
//ex : ModifyQuantity(pId, -1) to decrement the value
//Value min in the textbox = 0
function ModifyTheQuantity(pId, pQuantity) {
	if (document.getElementById(pId).value == parseInt(document.getElementById(pId).value)){
		var _qty=parseInt(document.getElementById(pId).value)+pQuantity;
		if (_qty<0) {
			document.getElementById(pId).value = 0;
			
		} else {
			document.getElementById(pId).value = _qty;
		}
	}
}

//Modify quantity set value(pQuantity) in textbox
//ex : ModifySetQuantity(pId, 18) to set the value
//Value min in the textbox = 0
function ModifySetQuantity(pId, pQuantity) {
	if (pQuantity<0) {
		document.getElementById(pId).value = 0;
	} else {
		document.getElementById(pId).value = pQuantity;
	}
}

// validate the list quantity in the listbox
// if ok -> submit
function validateListQtyProductModel() {
	if (document.getElementById('popupquantity').value == parseInt(document.getElementById('popupquantity').value)){
		if ((document.getElementById('TsMemoTaille2').value != '...') && (parseInt(document.getElementById('popupquantity').value) > 0)){
			var _name_input_quantity= '_' + document.getElementById('TsMemoTaille2').value + '_quantity';
			
			//in product page: more 1 same id then get all by name 
			var liste = document.getElementsByName(_name_input_quantity);
			for(i=0;i<liste.length;i++){
				liste[i].value=parseInt(document.getElementById('popupquantity').value);
			}
			//$HidePopup(); 
			document.popupaddform.submit();
		}
	}
}

function multiClass(eltId) {
	arrLinkId = new Array('onglet-0','onglet-1','onglet-2','onglet-3');
	intNbLinkElt = new Number(arrLinkId.length);
	arrClassLink = new Array('selected','normal');
	strContent = new String()
	for (i=0; i<intNbLinkElt; i++) {
		strContent = "bloc-"+arrLinkId[i];
		if ( arrLinkId[i] == eltId ) {
			document.getElementById(arrLinkId[i]).className = arrClassLink[0];
			document.getElementById(strContent).className = 'tableau-details-contenu on';
		} else {
			if (document.getElementById(strContent)!=null){
			document.getElementById(arrLinkId[i]).className = arrClassLink[1];
			document.getElementById(strContent).className = 'tableau-details-contenu off';
			}
		}
	}
}


// Redefinition of functions since this script is called on page load
function $n(o) {
    return (typeof o=='string') ? o=document.getElementById(o) : o;
}
function setVisible(elt)
{
  if( !$n(elt) ) {return false};
  
  $n(elt).style.display = 'block';
  $n(elt).style.visibility = 'visible';
}
function setInvisible(elt)
{
  if(!$n(elt)) {return false};
  
  $n(elt).style.display = 'none';
  $n(elt).style.visibility = 'hidden';
}
function setClass(className, element)
{
    var elt = $n(element);
    if(!elt) {return false;}
    elt.className = className;
}

function displayPriceLine(src,num) {
	document.getElementById('availableList'+num).selectedIndex = src.selectedIndex;
	document.getElementById('available'+num).innerHTML = document.getElementById('availableList'+num).options[src.selectedIndex].text;
	
	//document.getElementById('priceList'+num).selectedIndex = src.selectedIndex;
	//document.getElementById('price'+num).innerHTML = document.getElementById('priceList'+num).value;
	displayAddItemButtonPopup();
}

function displayAddItemButtonPopup() {
	nbLine = document.getElementById('nbProductLine').value;
	
	var showButton = true; 
	
	for (i=1; i<=nbLine; i++) {
		if (document.getElementById('availableList'+i).value != "A") {
			showButton = false;
		}
	}
	if (showButton == true ) {
		setVisible("addItemButtonPopup");
	} else {
		setInvisible("addItemButtonPopup");
	}	
}