/*******************************************************************************
 * 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()
{
	var aGET = $('formPackage').serialize().split('&');
	if(aGET[0] != "")
	{
		// oprSim
		tab_optSim = aGET[1].split('=');
	//	alert("0 : " + aGET[0] + " 1 : " + aGET[1] + " 2 : " + aGET[2] + " 3 : " + aGET[3] + " 4 : " + aGET[4] + " 5 : " + aGET[5]);
		if (tab_optSim[0] == "opt_sim")
		{
			var iOptSim = tab_optSim[1];
		}
		else
		{
			alert("Choisissez votre SIM.");
			return false;
		}
		
		// id_art sim light
		tab_simLight = aGET[2].split('=');
	//	alert(" tab_simLight " + tab_simLight);
		var iIdArtSimLight = tab_simLight[1];
		
		// RECHARGE et Rich
		if (aGET.length > 3)
		{
			tab_refill = aGET[3].split('=');
			if(tab_refill[0] != "id_art_simrich"){
				var iIdArtRefill = tab_refill[1];
			}else{
		       		tab_simRich = aGET[3].split('=');
				var iIdArtSimRich = tab_simRich[1];

			}
		}
		else
		{
			var iIdArtRefill = "";
		}
		
		// id_art de l'offre selectionnée
		var iIdArtOffre = null;
		if (iOptSim == 1 || iOptSim == 2)
		{
			if(iIdArtSimLight == 'non'){
				iIdArtOffre = 790;
			}else{
				iIdArtOffre = iIdArtSimLight;
			}
		}
		else if (iOptSim == 3)
		{
			iIdArtOffre = iIdArtSimRich;
		}
		else if(iOptSim == 0 || iOptSim == 790) // correspond au Liberty Sim
		{
			iIdArtOffre = 790; //iOptSim;
		}

		// AVEC OU SANS MOBILE
		var offreSeule = 0;
		if (iOptSim == 2 || iOptSim == 0 || iOptSim == 3 || iOptSim == 790) // 45 -> Liberty SIM
		{
			offreSeule = 1;
		}

		// gestion de la liberty SIM
		if(aGET[6] ^ aGET[7]) // opérateur ou exclusif
		{
			//erreur, si le montant est renseigné (6), alors l'ID_LBS (7) doit l'etre aussi
			alert("Erreur interne, nos équipe s'occupent de vous... Veuillez réessayer plus tard. Merci");
			return false;
		}
		
		if(aGET[6] || aGET[7])
		{
			var tab_simLiberty = aGET[6].split('=');
			var fLibertySimTarif = tab_simLiberty[1];
			var tab_idLimLiberty = aGET[7].split('=');
			var iIdLibertySim = tab_idLimLiberty[1];
		}
		var iIdArtPack = "";
		var iIdArtMobile = "";
		var sOrigine = "prepaid";
		var sTypePackage = "";
		
		

		checkCart(iIdArtPack,iIdArtMobile,iIdArtOffre,iIdArtRefill,offreSeule,sOrigine,sTypePackage,fLibertySimTarif, iIdLibertySim);
	}
	else
	{
		alert("Choisissez votre SIM.");
		return false;
	}
}


/*******************************************************************************
 * 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);

			}
		}
	);
}
