/**
 * @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(ProductID, quantityToAdd, price) {
	var currentQuantity = parseInt(document.getElementById(ProductID).value);
	var futureQuantity  = currentQuantity + parseInt(quantityToAdd);
	if (futureQuantity < 0) {
		alert('Vous ne pouvez pas disposer d\'un produit en quantité négative !');
	} else {
		document.getElementById(ProductID).value = futureQuantity;
	}
	calculTotal();
}

function toggleQtyEffect(i, qty) {
	J('.pdp-'+ i).css('color', '#333333');
	J('.pdp-'+ i).css('font-weight', 'normal');
	J('#pdp-'+ i+'-'+ qty).css('color', 'blue');
	J('#pdp-'+ i+'-'+ qty).css('font-weight', 'bold');
}

function calculTotal(){
	nbAttr = document.getElementById('nbattr').value;
	var total = 0;
	var normalTotal = 0;
	var saveTotal = 0;
	var price = 0;
	var quantite = 0;
	var thetotal = 0;
	for (i=1; i<nbAttr; i++) {
		price = parseFloat(document.getElementById('price'+i).value);
		normalPrice =  parseFloat(document.getElementById('normalPrice'+i).value);
		quantite =  parseInt(document.getElementById('Quantity'+i).value);
		
		var qtyPrice = 0;
		currentQtyValid = 0;
		for(qty in tarifProduct[i]) {
			if(quantite >= qty) {
				qtyPrice = tarifProduct[i][qty];
				currentQtyValid = qty;
			}
			
		}
		if(currentQtyValid > 0) {
			toggleQtyEffect(i, currentQtyValid);
		}
		if(qtyPrice > 0 && qtyPrice != price) {
			price = qtyPrice;
			document.getElementById('price'+i).value = qtyPrice.toFixed(2);
			//document.getElementById('spanPrice'+i).innerHTML = qtyPrice.toFixed(2);
			var pourcent = Math.round((normalPrice -  price)/ (normalPrice / 100));	 
			
			if (pourcent > 0)
			{
				document.getElementById('tdPourcent'+i).innerHTML = '-'+ pourcent +'%';
				document.getElementById('stikePrice'+i).innerHTML = normalPrice.toFixed(2) +' &euro;';	
				document.getElementById('tdPourcent'+i).className = 'reduc';	
			}	
			else
			{
				if (document.getElementById('tdPourcent'+i))
				{
					document.getElementById('tdPourcent'+i).innerHTML = '';
					document.getElementById('stikePrice'+i).innerHTML = '';
					document.getElementById('tdPourcent'+i).className = '';
				}
			}
		}
		

		normalTotal = parseFloat(normalTotal+(normalPrice*quantite));
		total = parseFloat(total+(price*quantite));
		thetotal = total.toFixed(2);

	}
	saveTotal = parseFloat(normalTotal - total);
	saveTotal = saveTotal.toFixed(2);
	
	document.getElementById('total').innerHTML = thetotal+' &euro; HT';
	if(saveTotal > 0) {
		document.getElementById('total_remise').innerHTML = saveTotal +' &euro; HT';
		document.getElementById('span_total_remise').style.display = "inline";
	}
	else {
		document.getElementById('total_remise').innerHTML = "";
		document.getElementById('span_total_remise').style.display = "none";
	}
}

/**
 * 
 * @param {Object} ProductID
 * @param {Object} quantityToAdd
 * @param {Object} maxQuantity
 */
function setQuantityAcc(ProductID, quantityToAdd, price) {
	var currentQuantity = parseInt(document.getElementById(ProductID).value);
	var futureQuantity  = currentQuantity + parseInt(quantityToAdd);
	if (futureQuantity < 0) {
		alert('Vous ne pouvez pas disposer d\'un produit en quantité négative !');
	} else {
		document.getElementById(ProductID).value = futureQuantity;
	}
	calculTotalAcc();
}

function calculTotalAcc(){
	nbAttr = document.getElementById('nbattr2').value;
	var total = 0;
	var normalTotal = 0;
	var saveTotal = 0;
	var price = 0;
	var quantite = 0;
	var thetotal = 0;
	
	for (i=1; i<nbAttr; i++) {
		price = parseFloat(document.getElementById('price'+i).value);
		normalPrice =  parseFloat(document.getElementById('normalPrice'+i).value);
		quantite =  parseInt(document.getElementById('Quantity'+i).value);
		
		var qtyPrice = 0;
		currentQtyValid = 0;
		for(qty in tarifProduct[i]) {
			if(quantite >= qty) {
				qtyPrice = tarifProduct[i][qty];
				currentQtyValid = qty;
			}
			
		}
		if(currentQtyValid > 0) {
			toggleQtyEffect(i, currentQtyValid);
		}
	
		if(qtyPrice > 0 && qtyPrice != price) {
			price = qtyPrice;
			document.getElementById('price'+i).value = qtyPrice.toFixed(2);
			//document.getElementById('spanPrice'+i).innerHTML = qtyPrice.toFixed(2);
			var pourcent = Math.round((normalPrice -  price)/ (normalPrice / 100));	 
			
			if(pourcent > 0) {
				document.getElementById('tdPourcent'+i).innerHTML = '-'+ pourcent +'%';
				document.getElementById('tdPourcent'+i).className = 'reduc';
				
			}	
			else {
				document.getElementById('tdPourcent'+i).innerHTML = '';
				document.getElementById('tdPourcent'+i).className = '';
			}
		}
		

		normalTotal = parseFloat(normalTotal+(normalPrice*quantite));
		total = parseFloat(total+(price*quantite));
		thetotal = total.toFixed(2);

	}
	saveTotal = normalTotal - total;
	
	document.getElementById('totalAcc').innerHTML = thetotal+' &euro; HT';
	if(saveTotal > 0)
	{
		document.getElementById('total_remiseAcc').innerHTML = saveTotal.toFixed(2) +' &euro; HT';
		document.getElementById('span_total_remiseAcc').style.display = "inline";
	}
	else
	{
		document.getElementById('total_remiseAcc').innerHTML = "";
		document.getElementById('span_total_remiseAcc').style.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";
}

