
//Inclure ce fichier pour ajouter tout la sickness tel que AJAX et autre...

var xmlHttp = null;

//Array des langues du site
var strLg = [];
strLg[0] = "fr";
strLg[1] = "en";

//Pour le message d'erreur si le browser est invalid
var strErrXmlHttpObject = [];
strErrXmlHttpObject["fr"] = "Votre navigateur ne supporte pas ce site.<br/>\nNous vous recommandons d'installer la dernière version de <a href=\"http://www.mozilla.com/firefox/\">Mozilla Firefox</a> ou <a href=\"http://www.microsoft.com/windows/products/winfamily/ie/default.mspx\">Internet Explorer</a><br/>";
strErrXmlHttpObject["en"] = "Your browser does not support this web site.<br/>\nWe recommend that you install the latest version of <a href=\"http://www.mozilla.com/firefox/\">Mozilla Firefox</a> or <a href=\"http://www.microsoft.com/windows/products/winfamily/ie/default.mspx\">Internet Explorer</a><br/>";


//Fonction pour trouver la langue utilisé
// return : Retourne la langue
function stpi_chkLang()
{
	var l = "fr";
	
	var strHref = window.location.href;
	
	if ( strHref.indexOf("l=") > -1 )
	{
		var l = strHref.substr(strHref.indexOf("l=") + 2, 2);
	}
	
	return l;
} 


//Fonction pour créer l'objet nécéssaire pour AJAX
// return : Retourne l'objet XmlHttp
function stpi_XmlHttpObject()
{
	xmlHttp = null;

	try
  	{
  		//Pour Firefox, Opera 8.0+, Safari
  		xmlHttp = new XMLHttpRequest();
  	}
	catch (e)
  	{
  		//Pour la stupidit�e de Internet Explorer
  		try
    	{
    		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    	}
		catch (e)
    	{
    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    	}
 	}

	return xmlHttp;
}


//Fonction qui affiche le prix d'un sous-item, les type d'attribut et les attributs
// nnbItemID : Le numéro d'identification d'un Item
// return : retourn rien si il y a une erreur 
function stpi_affPrixAttribut(nnbItemID)
{
	if (nnbItemID == "")
	{
		document.getElementById("stpi_affPrixAttribut" + nnbItemID).innerHTML = "";
  		return;
  	}
  	
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_affPrixAttribut" + nnbItemID).innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	} 
	
	var strParam = "sid=" + Math.random();
	
	strParam = strParam + "&nbItemID=" + encodeURIComponent(nnbItemID);
	
	var i = 1;	
	while (true)
	{
		try
		{
			strParam = strParam + "&nbAttributID" + i + "=" + encodeURIComponent(document.getElementById("nbAttributID" + nnbItemID + i).value);			
		}
		catch(e)
		{
			break;
		}
		i++;
	}
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			document.getElementById("stpi_affPrixAttribut" + nnbItemID).innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("POST", "itemsousitemprixaff.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", strParam.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strParam);
}


//Fonction qui ajoute des sous-item au cart
// nnbSousItemID : Le numéro d'identification d'un sous-item
// nnbQuantity : La quantity du sous-item a ajouter
// nnbSpanID : Le numéro d'identification du span qui va recevoir la réponse
// return : retourn rien si il y a une erreur 
function stpi_AddToCart(nnbSousItemID, nnbQuantity, nnbSpanID)
{
	if (nnbSousItemID == "")
	{
		return;
  	}
  	
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		return;
	} 
	
	document.getElementById("buttoncart" + nnbSpanID).disabled = true;

	var strUrl = "cartadd.php?nbSousItemID=" + nnbSousItemID + "&nbQuantity=" + nnbQuantity + "&sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			document.getElementById("stpi_AddToCart" + nnbSpanID).innerHTML = xmlHttp.responseText;
  			document.getElementById("buttoncart" + nnbSpanID).disabled = false;
  			stpi_AffCart();
		}
	}
	
	xmlHttp.open("GET", strUrl, true);
	
	xmlHttp.send(null);
}


//Fonction qui affiche le statut du cart
// return : retourn rien si il y a une erreur 
function stpi_AffCart()
{
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		return;
	} 

	var strUrl = "cartaff.php?sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			document.getElementById("stpi_affCart").innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("GET", strUrl, true);
	
	xmlHttp.send(null);
}


//Fonction qui met à jour le cart
// narrSousItemID : Les numéros d'identification des sous-item
// return : retourn rien si il y a une erreur 
function stpi_EditCart(narrSousItemID)
{
	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_message").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	}
	
	var strParam = "sid=" + Math.random();
		
	for (i in narrSousItemID)
	{
		if (narrSousItemID[i] != 0)
		{
			strParam = strParam + "&nbSousItem" + narrSousItemID[i] + "=" + encodeURIComponent(document.getElementById("nbQuantity" + narrSousItemID[i]).value);
		}
	}
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			if (xmlHttp.responseText.indexOf("redirect") != -1)
  			{
  				var strUrlRedirect = "./checkout1.php?l=" + stpi_chkLang();
  				window.location = strUrlRedirect;
  			}
  			else
  			{
  				document.getElementById("stpi_message").innerHTML = xmlHttp.responseText;
  			}
		}
	}
	
	xmlHttp.open("POST", "cartedit.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", strParam.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strParam);
}


//Fonction qui enleve une item du cart
// nnbSousItemID : Le numéro d'identification d'un sous-item
// return : retourn rien si il y a une erreur 
function stpi_DelCart(nnbSousItemID)
{
  	if (nnbSousItemID == "")
	{
		return;
  	}
  	
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_message").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	}

	var strUrl = "cartdel.php?nbSousItemID=" + nnbSousItemID + "&sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			if (xmlHttp.responseText.indexOf("redirect") != -1)
  			{
  				var strUrlRedirect = "./checkout1.php?l=" + stpi_chkLang();
  				window.location = strUrlRedirect;
  			}
  			else
  			{
  				document.getElementById("stpi_message").innerHTML = xmlHttp.responseText;
  			}
		}
	}
	
	xmlHttp.open("GET", strUrl, true);
	
	xmlHttp.send(null);
}


//Fonction qui affiche les province en liens avec un pays
// nstrCountryID : Le numéro d'identification d'un pays
// nnbTypeAdresseID : Le numéro d'identification du type d'adresse
// return : retourn rien si il y a une erreur 
function stpi_affProvince(nstrCountryID, nnbTypeAdresseID)
{
	if (nstrCountryID.length == 0)
	{ 
		document.getElementById("stpi_affProvince" + nnbTypeAdresseID).innerHTML = "";
  		return;
  	}
  	
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_affProvince" + nnbTypeAdresseID).innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	} 

	var strUrl = "checkoutprovinceaff.php?l=" + stpi_chkLang() + "&strCountryID=" + nstrCountryID + "&nbTypeAdresseID=" + nnbTypeAdresseID + "&sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			document.getElementById("stpi_affProvince" + nnbTypeAdresseID).innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("GET", strUrl, true);
	
	xmlHttp.send(null);
}


//Fonction qui affiche le formulaire pour l'adresse de shipping 
// return : retourn rien si il y a une erreur 
function stpi_affCheckoutShippingAddress()
{
	if (document.getElementById("boolShippingAddresse").checked)
	{
		xmlHttp = stpi_XmlHttpObject();
	
		//Si le browser est invalide afficher l'erreur
		if (xmlHttp == null)
	  	{
	  		document.getElementById("stpi_affCheckoutShippingAddress").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
	  		return;
		} 
	
		var strUrl = "checkoutshippingaddressaff.php?l=" + stpi_chkLang() + "&sid=" + Math.random();
		
		xmlHttp.onreadystatechange=function()
		{
			if (xmlHttp.readyState == 4)
	  		{
	  			document.getElementById("stpi_affCheckoutShippingAddress").innerHTML = xmlHttp.responseText;
			}
		}
		
		xmlHttp.open("GET", strUrl, true);
		
		xmlHttp.send(null);
	}
	else
	{
		document.getElementById("stpi_affCheckoutShippingAddress").innerHTML = "";
	}
}


//Fonction qui affiche les methodes de paiement
// nnbMethodPayeID : Le numéro d'identification de la methode de paiement
// return : retourn rien si il y a une erreur 
function stpi_affCheckoutPaymentMethod(nnbMethodPayeID)
{
	if (nnbMethodPayeID.length == 0)
	{ 
		document.getElementById("stpi_affCheckoutPaymentMethod").innerHTML = "";
  		return;
  	}
  	
  	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_affCheckoutPaymentMethod").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	} 

	var strUrl = "checkoutmethodpayeaff.php?l=" + stpi_chkLang() + "&nbMethodPayeID=" + nnbMethodPayeID + "&sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			document.getElementById("stpi_affCheckoutPaymentMethod").innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("GET", strUrl, true);
	
	xmlHttp.send(null);
}


//Fonction qui met l'information du formulaire de commande dans la session
// return : retourn rien si il y a une erreur 
function stpi_OrderToSession()
{
	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_OrderToSession").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	}
	
	document.getElementById("stpi_OrderToSessionButton").style.visibility = "hidden";
	document.getElementById("stpi_OrderToSession").innerHTML = "<img alt=\"loading\" src=\"./images/loading.gif\" />";
	
	var strParam = "strPrenom1=" + encodeURIComponent(document.getElementById("strPrenom1").value);
	strParam = strParam + "&strNom1" + "=" + encodeURIComponent(document.getElementById("strNom1").value);
	strParam = strParam + "&strCie1" + "=" + encodeURIComponent(document.getElementById("strCie1").value);
	strParam = strParam + "&strAdresse1" + "=" + encodeURIComponent(document.getElementById("strAdresse1").value);
	strParam = strParam + "&strVille1" + "=" + encodeURIComponent(document.getElementById("strVille1").value);
	strParam = strParam + "&strCountryID1" + "=" + encodeURIComponent(document.getElementById("strCountryID1").value);
	try
	{
		strParam = strParam + "&strProvinceID1=" + encodeURIComponent(document.getElementById("strProvinceID1").value);
	}
	catch(e)
	{
		strParam = strParam + "&strProvinceID1=0";
	}
	strParam = strParam + "&strCodePostal1" + "=" + encodeURIComponent(document.getElementById("strCodePostal1").value);
	strParam = strParam + "&strAreaCode" + "=" + encodeURIComponent(document.getElementById("strAreaCode").value);
	strParam = strParam + "&strTel1" + "=" + encodeURIComponent(document.getElementById("strTel1").value);
	strParam = strParam + "&strTel2" + "=" + encodeURIComponent(document.getElementById("strTel2").value);
	strParam = strParam + "&strCourriel" + "=" + encodeURIComponent(document.getElementById("strCourriel").value);
	
	if (document.getElementById("boolShippingAddresse").checked)
	{
		strParam = strParam + "&boolShippingAddresse=1";
		strParam = strParam + "&strPrenom2=" + encodeURIComponent(document.getElementById("strPrenom2").value);
		strParam = strParam + "&strNom2" + "=" + encodeURIComponent(document.getElementById("strNom2").value);
		strParam = strParam + "&strCie2" + "=" + encodeURIComponent(document.getElementById("strCie2").value);
		strParam = strParam + "&strAdresse2" + "=" + encodeURIComponent(document.getElementById("strAdresse2").value);
		strParam = strParam + "&strVille2" + "=" + encodeURIComponent(document.getElementById("strVille2").value);
		strParam = strParam + "&strCountryID2" + "=" + encodeURIComponent(document.getElementById("strCountryID2").value);
		try
		{
			strParam = strParam + "&strProvinceID2=" + encodeURIComponent(document.getElementById("strProvinceID2").value);
		}
		catch(e)
		{
			strParam = strParam + "&strProvinceID2=0";
		}
		strParam = strParam + "&strCodePostal2" + "=" + encodeURIComponent(document.getElementById("strCodePostal2").value);
	}
	else
	{
		strParam = strParam + "&boolShippingAddresse=0";
	}
	
	strParam = strParam + "&nbMethodPayeID=" + encodeURIComponent(document.getElementById("nbMethodPayeID").value);

	try
	{
		strParam = strParam + "&strCardNom=" + encodeURIComponent(document.getElementById("strCardNom").value);
		strParam = strParam + "&strCardNum=" + encodeURIComponent(document.getElementById("strCardNum").value);
		strParam = strParam + "&dtCardDateExpir1=" + encodeURIComponent(document.getElementById("dtCardDateExpir1").value);
		strParam = strParam + "&dtCardDateExpir2=" + encodeURIComponent(document.getElementById("dtCardDateExpir2").value);
		strParam = strParam + "&strCardCodeSecur=" + encodeURIComponent(document.getElementById("strCardCodeSecur").value);
	}
	catch(e)
	{
	}
	
	strParam = strParam + "&sid=" + Math.random();
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			if (xmlHttp.responseText.indexOf("redirect") != -1)
  			{
  				var strUrlRedirect = "./checkout3.php?l=" + stpi_chkLang();
  				window.location = strUrlRedirect;
  			}
  			else
  			{
  				document.getElementById("stpi_OrderToSession").innerHTML = xmlHttp.responseText;
  				document.getElementById("stpi_OrderToSessionButton").style.visibility = "visible";
  			}
		}
	}
	
	xmlHttp.open("POST", "ordersave.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", strParam.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strParam);
}


//Fonction qui sauvegarde une commande dans la base de donnée
// return : retourn rien si il y a une erreur 
function stpi_AddOrder()
{
	xmlHttp = stpi_XmlHttpObject();
	
	//Si le browser est invalide afficher l'erreur
	if (xmlHttp == null)
  	{
  		document.getElementById("stpi_AddOrder").innerHTML = strErrXmlHttpObject[stpi_chkLang()];
  		return;
	}
	
	document.getElementById("buttonorder").style.visibility = "hidden";
	document.getElementById("stpi_AddOrder").innerHTML = "<img alt=\"loading\" src=\"./images/loading.gif\" />";
	
	if (document.getElementById("boolAgreement").checked)
	{
		var strParam = "boolAgreement=1";
	}
	else
	{
		var strParam = "boolAgreement=0";
	}		
	strParam = strParam + "&strOrderMessage" + "=" + encodeURIComponent(document.getElementById("strOrderMessage").value);
	strParam = strParam + "&sid=" + Math.random();	
		
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState == 4)
  		{
  			if (xmlHttp.responseText.indexOf("redirect") != -1)
  			{
  				var strUrlRedirect = "./checkout4.php?l=" + stpi_chkLang();
  				window.location = strUrlRedirect;
  			}
  			else
  			{
  				document.getElementById("stpi_AddOrder").innerHTML = xmlHttp.responseText;
  				document.getElementById("buttonorder").style.visibility = "visible";
  			}
		}
	}
	
	xmlHttp.open("POST", "orderadd.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", strParam.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strParam);
}