/******************************************************************************
* filloFunctions.js
*******************************************************************************

*******************************************************************************
*                                                                             *
* Copyright 2006							      *
*                                                                             *
******************************************************************************/
function ValideFormSearchBarProducts(change,currentObj){
	if(change != "pages"){
		document.getElementById('pageCours').selectedIndex=0;
	}
	if(change == "nbparpage"){
		lst_nbShowedProductsInPage = document.getElementsByName('nbShowedProductsInPage');
		for (i=0;i < lst_nbShowedProductsInPage.length;i++)
		{
		  lst_nbShowedProductsInPage[i].value = currentObj.value
		}
	}
	if(change == "pages"){
		lst_pageCours = document.getElementsByName('pageCours');
		for (i=0;i < lst_pageCours.length;i++)
		{
		  lst_pageCours[i].value = currentObj.value
		}
	}
	document.getElementById('formSearcBar').submit();

}

function changePageSearchBarProducts(page){
	document.getElementById('pageCours').selectedIndex=page;
	document.getElementById('formSearcBar').submit();

}

function gshpChangeQuantityFillon(position)
{
	var form = document.getElementById("form_quantityForm_" + position);
	var input = document.getElementById("ctrl_quantity_" + position);
	var input_stock = document.getElementById("ctrl_stock_" + position);
	if ((form == null) || (input == null)) {
		return false;
	}
	var quantity = parseInt(input.value, 10);
	var stock = parseInt(input_stock.value, 10);
	if (isNaN(quantity) || quantity<0) {
		alert(objThesaurus.translate("gshpInvalidNewQuantity"));
		return false;
	}
	else if(quantity > stock){
		alert('Vous ne pouvez pas commander autant de ce produit, il n\'en reste que '+stock);
        return false;
	}
	form.submit();
	return false;
}

function gshpChangeQuantityFillonMaqPrint(position)
{
	var form = document.getElementById("form_quantityForm_" + position);
	var input = document.getElementById("ctrl_quantity_" + position);
	var quantity = parseInt(input.value, 10);
	
	
	var ctrlPriceValidationMq = document.getElementById("ctrlPriceValidationMq_" + position);
	if (isNaN(quantity)) {
		alert(objThesaurus.translate("gshpInvalidNewQuantity"));
		return false;
	}
	
	var prix_MaqPrint = document.getElementById("prix_MaqPrint" + position).value;

	var paliers = prix_MaqPrint.split(';');
	var lib_prix = "";
	var currentPrice = 0;
	for(i=0;i<paliers.length;i++){
		//alert(paliers[i]);
		var palier_prix = paliers[i].split('#');
		//alert("prix : "+palier_prix[1]);
		var palierhb = palier_prix[0].split('-');
		if(i==0){
			quantityMin = palierhb[0];
		}
		//alert(palierhb[0]);
		//alert(palierhb[1]);
		if(quantity >= palierhb[0] && (palierhb[1] == "" || quantity <= palierhb[1])) {
			currentPrice = palier_prix[1]/1.196;
		}
		if(palierhb.length <2 && currentPrice == 0){//Dernier palier
			currentPrice = palier_prix[1]/1.196;
		}

		
	}
	//alert('currentPrice  : '+currentPrice );
	if(quantity == "" || parseInt(quantity) < parseInt(quantityMin)){
		alert("Vous devez saisir une quantité d'au moins "+quantityMin);
		return false;
	}

	ctrlPriceValidationMq.value = currentPrice;
	//On envoie la modification de la qté à MaqPrint
	
	
	form.submit();
	return false;
}
