/**
 * @encoding UTF-8
 */

/**
 * 
 * @param {Object} myElementId
 * @param {Object} myOptionValue
 */
function autoSelect(myElementId, myOptionValue) {
  myElement = document.getElementById(myElementId);
  myOptions = myElement.options;
  n = myOptions.length;
  for (i = 0; i < n; i++) {
    if (myOptions[i].value == myOptionValue) {
      myElement.selectedIndex = i;
      break;
    }
  }
}

/**
 * 
 */
function unFrame() {
  if (window!=top) { top.location=window.location; }
}

/**
 * 
 */
function doTheCookieStuff() {
  sp = getCookie('selected_panel');
  if (sp != null) {
    p.show(sp, document.getElementById(sp + '_cursor'));
  }
}

/**
 * 
 * @param {Object} ElementID
 * @param {Object} Path
 */
function setLoadingDiv(ElementID, Path, Message) 
{
	if(arguments.length <= 1 || arguments[1].length < 1)
	{
		Path = '../..';
	}
	if(arguments.length <= 2 || arguments[2].length < 1)
	{
		Message = '';
	}
	$(ElementID).innerHTML = '<img src="' + Path + '/shop/images/ajax_loading.gif" /> '+ Message;
}

/**
 * 
 * @param {Object} ElementID
 */
function unsetLoadingDiv(ElementID) 
{
  $(ElementID).innerHTML = '';
}

/**
 * 
 * @param {Object} ProductID
 * @param {Object} quantityToAdd
 * @param {Object} maxQuantity
 */
function setQuantity(inputID, quantityToAdd)
{
	if (J('#' + inputID).val() == "")
	{
		J('#' + inputID).val(0);
	}
	
	var currentQuantity = parseInt(J('#' + inputID).val());
	var futureQuantity  = currentQuantity + parseInt(quantityToAdd);
	
	if (futureQuantity < 0)
	{
		alert('Vous ne pouvez pas disposer d\'un produit en quantité négative !');
	}
	else
	{
		J('#' + inputID).val(futureQuantity);
	}
	
	J('#myShoppingCart').submit();
	
	return true;
	
	var name = "quantity"; 
	var id = inputID.substring(name.length, inputID.length);
	
	if (id != "")
	{
		calculTotalPanier(id);
	}
}

function setQuantityGiftList(inputID, quantityToAdd)
{
	if (J('#quantity' + inputID).val() == "")
	{
		J('#quantity' + inputID).val(0);
	}
	
	var currentQuantity = parseInt(J('#quantity' + inputID).val());
	var futureQuantity  = currentQuantity + parseInt(quantityToAdd);
	
	if (futureQuantity < 0)
	{
		alert('Vous ne pouvez pas disposer d\'un produit en quantité négative !');
	}
	else
	{
		J('#quantity' + inputID).val(futureQuantity);
	}
	
	J.ajax({
			type: 'post',
			async: false,
			url: '../../../shop/ajax/updateGiftListQuantity.php',
			data: 'giftListID=' + J('#giftListID').val() +
				  '&productID=' + J('#productID' + inputID).val() +
				  '&quantity=' + futureQuantity
		}).responseText;
}

function calculTotalPanier(id)
{
	var nbArticle = parseInt(J('#nbArticle').val());
	var shippingFees = parseFloat(J('#shippingFees').val());
	var processingFees = parseFloat(J('#processingFees').val());
	var tauxTVA = parseFloat(J('#tauxTVA').val());
	var priceHT = 0;
	var priceTTC = 0;
	var weight = 0;
	var totalProductHT = 0;
	var totalProductTTC = 0;
	var totalMarchandiseHT = 0;
	var totalMarchandiseTTC = 0;
	var totalHT = 0;
	var totalTVA = 0;
	var totalTTC = 0;
	var totalWeight = 0;
	var quantite = 0;
	
	J('#load-image').css('display', 'block');
	
	tauxTVA = 1 + tauxTVA / 100;
	
	for (var i = 1; i <= nbArticle; i++)
	{
		priceHT  = parseFloat(J('#priceHT' + i).val());
		priceTTC = parseFloat(J('#priceTTC' + i).val());
		quantite = parseInt(J('#quantity' + i).val());
		weight	 = parseInt(J('#weight' + i).val());
		
		// Total produit
		totalProductHT  = parseFloat(priceHT * quantite);
		totalProductTTC = parseFloat(priceTTC * quantite);
		totalWeight		= parseInt(weight * quantite);
		totalProductHT  = totalProductHT.toFixed(2);
		totalProductTTC = totalProductTTC.toFixed(2);
		
		totalHT			= parseFloat(totalHT) + parseFloat(totalProductHT);
		totalTTC		= parseFloat(totalTTC) + parseFloat(totalProductTTC);
		totalHT			= totalHT.toFixed(2);
		totalTTC		= totalTTC.toFixed(2);
		
		// Affichage
		if (totalProductHT > 0)
		{
			J('#totalHT' + i).html(totalProductHT);
			J('#totalTTC' + i).html(totalProductTTC);
		}
		else
		{
			J('#totalHT' + i).html('0.00');
			J('#totalTTC' + i).html('0.00');
		}
	}
	
	if (id != 0) // Ajax MAJ du panier
	{
		J.ajax({
			type: 'post',
			url: '../../../shop/ajax/updateShoppingCart.php',
			data: 'productID=' + J('#productID' + id).val() +
				  '&quantity=' + J('#quantity' + id).val()
		});
		
		shippingFees = J.ajax({
				type: 'post',
				async: false,
				url: '../../../shop/ajax/updateShipping.php',
				data: 'totalHT=' + totalHT +
					  '&totalTTC=' + totalTTC +
					  '&totalWeight=' + totalWeight
			}).responseText;
	}
	
	// recalcul des frais de port en Ajax
	shippingFees = parseFloat(shippingFees);
	J('#shippingFees').val(shippingFees.toFixed(2));
	J('#fraisPort').html(shippingFees.toFixed(2));
	
	// Poids
	J('#totalWeight').html(totalWeight / 1000);
	
	// Voucher
	if (J('#voucher').val() != null)
	{
		var voucher = J('#voucher').val();
	}
	else
	{
		var voucher = 0;
	}
	
	totalTTC -= parseInt(voucher);
	
	if (J('#pmv_value').val() != null)
	{
		var pmv = J('#pmv_value').val();
	}
	else
	{
		var pmv = 0;
	}
	
	// Ajout des frais de port
	totalMarchandiseHT = totalHT;
	totalMarchandiseTTC = parseFloat(totalTTC);
	
	totalTTC -= parseInt(pmv);
	
	J('#totalPostRemise').html(totalTTC.toFixed(2));
	
	if (shippingFees > 0)
	{
		totalTTC = parseFloat(totalMarchandiseTTC - pmv) + parseFloat(shippingFees);
	}
	
	totalHT = parseFloat(totalTTC) / tauxTVA;
	
	// Parsage en float
	totalMarchandiseHT	= parseFloat(totalMarchandiseHT);
	totalMarchandiseTTC	= parseFloat(totalMarchandiseTTC);
	totalHT				= parseFloat(totalHT);	
	totalTTC			= parseFloat(totalTTC);
	totalTVA			= parseFloat(totalTTC - totalHT);
	
	if (processingFees > 0)
	{
		totalTTC = parseFloat(totalTTC) + parseFloat(processingFees);
	}
	
	// Affichage
	J('#totalMarchandiseHT').html(totalMarchandiseHT.toFixed(2));
	J('#totalMarchandiseTTC').html(totalMarchandiseTTC.toFixed(2));
	J('#totalHT').html(totalHT.toFixed(2));
	J('#totalTVA').html(totalTVA.toFixed(2));
	J('#totalTTC').html(totalTTC.toFixed(2));
	
	J('#load-image').css('display', 'none');
}

/**
 * 
 * @param {Object} id
 */
function openClose(id)
{
	var o = document.getElementById(id);
	if(o)
	{
		if(o.style.display == 'none')
		{
			o.style.display = 'block';
		}
		else
		{
			o.style.display = 'none';
		}
	}
}

function openClose2(id)
{
	var o = document.getElementById(id);
	if(o)
	{
		if(o.style.display == 'block')
		{
			o.style.display = 'none';
		}
		else
		{
			o.style.display = 'none';
		}
	}
}


function showBubble(e){
	if (document.all) {
		e = event;
	}
	var obj  = document.getElementById('bubble');
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if (navigator.userAgent.toLowerCase().indexOf('safari')>=0) {
		st = 0; 
	}
	var leftPos = e.clientX - 190;
	if (leftPos<0) {
		leftPos = 0;
	}
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
}	
	
function hideBubble() {
	document.getElementById('bubble').style.display = 'none';
}


function attrSelect(Main, Sub)
{		
	new Ajax.Request('../../../ajax/attrSelect.php', {
		method: 'post', 
		parameters: 'main=' + Main  +'&sub='+ Sub,
		onComplete: function(t) {
			eval(t.responseText);
		},
		evalScripts: true
	});
}
	
function addAttr(selectName, Ovalue, Otext, Otitle, Oclass, Oselection)
{
	var oOption = document.createElement("option");
	oOption.text= Otext;
	oOption.title = Otitle;
	oOption.value = Ovalue;
	oOption.className = Oclass;
	oOption.label = Otext;
	if(Oselection == 1)
	{
		oOption.selected = true;
	}
	$(selectName).appendChild(oOption);	
}

function setAttrStock(Stock)
{
	$("stock_level").src = "/images/icons/stock_vert_level_"+ Stock +".gif";
}


