/*******************************************************************************
 * MOBILE
 */
function cartAddMobile(objMobile)
{
	var iIdArtMobile = "";

	if (isNaN(objMobile))
	{
		// objMobile est un bouton -> On recupere la value du bouton
		iIdArtMobile = objMobile.getAttribute('rel');
	}
	else
	{
		// objMobile est un id_article
		iIdArtMobile = objMobile;
	}

	var iIdArtPack = "";
	var iIdArtOffre = "";
	var iIdArtRefill = "";
	var offreSeule = "";
	var sOrigine = "";
	var sTypePackage = "mobile";
	
	checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine, sTypePackage);
}

document.observe('dom:loaded', function(){
	$$('a.select').each(function(el){
		Event.observe(el, 'click', function(ev){
		Event.stop(ev);
		cartAddMobile(el);
		});
	});
});

document.observe('dom:loaded', function(){
	$$('a.selectProduct').each(function(el){
		Event.observe(el, 'click', function(ev){
		Event.stop(ev);
		cartAddMobile(el);
		});
	});
});

/*******************************************************************************
 * PACK
 */
function cartAddPack(iIdArtPack, sOrigine)
{
	var iIdArtMobile = "";
	var iIdArtOffre = "";
	var iIdArtRefill = "";
	var offreSeule = "";
		
	if (sOrigine == "")
	{
		sOrigine = "postpaid";
	}

	checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine);
}

/*******************************************************************************
 * OFFRE
 */
function cartAddOffre()
{
	var aGET = $('formPackage').serialize().split('&');
	
	// Si Page prépayé, goto cartAddOffre_prepaid()
	if(aGET[0] == "offre_prepaye=1")
	{
		cartAddOffre_prepaid();
	}
	else
	{
		cartAddOffre_postpaid();
	}
}

/**
 * OFFRE POSTPAID
 */
function cartAddOffre_postpaid()
{
	var aGET = $('formPackage').serialize().split('&');
	
	if(aGET[0] != "")
	{
		//récup OFFRE
		tab_offre = aGET[0].split('=');
		if (tab_offre[0] == "offre")
		{
			var iIdArtOffre = tab_offre[1];
		}
		else
		{
			alert("Choisissez votre temps de communication.");
			return false;
		}
		//récuperation type de forfait paradyse
		tab_type = aGET[1].split('=');
		if(tab_type[0] != ""){
			if (tab_type[0] == "type_forfait")
			{
				var sTypePackage = tab_type[1];
			}
			else if(tab_type[0] == "offre_prepaye")
			{
				tab_type_pp = aGET[4].split('=');
				if(tab_type_pp[0] != ""){
					if (tab_type_pp[0] == "type_forfait")
					{
						var sTypePackage = tab_type_pp[1];
					}
				}
			}else
			{
				var sTypePackage = "";
			}
		}
			
		//AVEC OU SANS MOBILE
		var offreSeule = 0;
		
		if (aGET[1] == "avecTel=0" || aGET[2] == "avecTel=0")
		{
			offreSeule = 1;
		}

		var iIdArtRefill = "";
		var sOrigine = "postpaid";

		var iIdArtPack = "";
		var iIdArtMobile = "";
		var iIdArtRefill = "";
		
		checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage);
	}
	else
	{
		alert("Choisissez votre temps de communication.");
		return false;
	}

}

/**
 * OFFRE PREPAID
 */
function cartAddOffre_prepaid(avecMobile)
{

    var iIdArtPack = "";
    var iIdArtMobile = "";
    var iIdArtOffre = "59927";
    var iIdArtRefill = "";
    var sOrigine = "prepaid";
    var sTypePackage = "";
    var offreSeule = 0;
    var fLibertySimTarif = "";
    var iIdLibertySim = "";
    if(avecMobile == '1')
    {
        offreSeule = 1;
    }
    checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage,fLibertySimTarif, iIdLibertySim);
}


/*******************************************************************************
 * PANIER
 */

// vérification des données et affichage d'un message si besoin
function checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage, fLibertySimTarif, iIdLibertySim)
{
	var sText = "";
	
	new Ajax.Request('/ajax/checkCart.ajax.php?'
									+'iIdArtPack='+iIdArtPack
					  				+'&iIdArtMobile='+iIdArtMobile
									+'&iIdArtOffre='+iIdArtOffre
									+'&iIdArtRefill='+iIdArtRefill
									+'&iOffreSeule='+offreSeule
									+'&origine='+sOrigine
									+'&type_forfait='+sTypePackage
									+'&iIdLibertySim='+iIdLibertySim,
		{
			asynchronous:true,
			method: 'get',
			onSuccess: function(t)
			{
				sText = t.responseText;
				var iConfirm = 1;

				if (sText != "" && sText != "\n")
				{
					iConfirm = 0;
					var aText = sText.split('\n');
					for (i = 0; i < aText.length; i++)
					{
						var aSousText = aText[i].split('|');
						var type = aSousText[0];
						var msg  = aSousText[1];

						if (type != "" && msg != "")
						{
							if (type == "alert")
							{
								alert(msg);
							}
							else if (type == "confirm")
							{
								if (confirm(msg))
								{
									iConfirm = 1;
								}
								else
								{
									iConfirm = 0;
								}
							}
						}
					}
				}

				if (iConfirm != 1)
				{
					return false;
				}
				else
				{
					addCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage, fLibertySimTarif, iIdLibertySim);
					return true;
				}
			}
		}
	);


}

//Si L'utilisateur a confirmé vouloir modifier son panier
function addCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage, fLibertySimTarif, iIdLibertySim)
{
	new Ajax.Request('/ajax/addCart.ajax.php?'
								+'iIdArtPack='+iIdArtPack
								+'&iIdArtMobile='+iIdArtMobile
								+'&iIdArtOffre='+iIdArtOffre
								+'&iIdArtRefill='+iIdArtRefill
								+'&iOffreSeule='+offreSeule
								+'&origine='+sOrigine
								+'&type_forfait='+sTypePackage
								+'&fLibertySimTarif='+fLibertySimTarif
								+'&iIdLibertySim='+iIdLibertySim,
		{
			asynchronous:true,
			method: 'get',
			onSuccess: function(t)
			{
				window.location.replace(t.responseText);

			}
		}
	);
}


//Si L'utilisateur a demandé à vider sonpanier
function emptyCart()
{
	new Ajax.Request('/ajax/addCart.ajax.php?sAction=empty',
		{
			asynchronous:true,
			method: 'get',
			onSuccess: function(t)
			{
				window.location.replace(t.responseText);

			}
		}
	);
}

