//=====================================================================||
//               NOP Design JavaScript Shopping Cart                   ||
//                                                                     ||
// For more information on SmartSystems, or how NOPDesign can help you ||
// Please visit us on the WWW at http://www.nopdesign.com              ||
//                                                                     ||
// Javascript portions of this shopping cart software are available as ||
// freeware from NOP Design under the GPL.  You must keep this comment ||
// unchanged in your code.  For more information contact               ||
// Scott@NopDesign.com                                                 ||
//                                                                     ||
// JavaScript Shop Module, V.4.1.0                                     ||
//=====================================================================||

//---------------------------------------------------------------------||
// FUNCTION:    CKquantity                                             ||
// PARAMETERS:  Quantity to                                            ||
// RETURNS:     Quantity as a number, and possible alert               ||
// PURPOSE:     Make sure quantity is represented as a number          ||
//---------------------------------------------------------------------||
function CKquantity(checkString) {
   strNewQuantity = "";

   for ( i = 0; i < checkString.length; i++ ) {
      ch = checkString.substring(i, i+1);
      if ( (ch >= "0" && ch <= "9") || (ch == '.') )
         strNewQuantity += ch;
   }

   if ( strNewQuantity.length < 1 )
      strNewQuantity = "1";

   return(strNewQuantity);
}


//---------------------------------------------------------------------||
// FUNCTION:    AddToCart                                              ||
// PARAMETERS:  Form Object                                            ||
// RETURNS:     Cookie to user's browser, with prompt                  ||
// PURPOSE:     Adds a product to the user's shopping cart             ||
//---------------------------------------------------------------------||
function AddToCart(thisForm) {
   iNumberOrdered = 0;
   iNumberOrdered = GetCookie("NumberOrdered");
   iNumberOrdered++;

   if ( iNumberOrdered > 12 )
      alert("Oups!  Votre panier est plein!  Veuillez passer &agrave; la caisse...");
   else {
      if ( thisForm.ID_NUM == null )
         strID_NUM    = "";
      else
         strID_NUM    = thisForm.ID_NUM.value;
      
      if (( thisForm.QUANTITY_adu == null ) || ( thisForm.QUANTITY.value == '' ))
         strQUANTITY  = "0";
      else
         strQUANTITY  = thisForm.QUANTITY.value;
      
      if ( thisForm.PRICE == null )
         strPRICE     = "0.00";
      else
         strPRICE     = thisForm.PRICE.value;
      
      if ( thisForm.TITLE == null )
         strTITLE     = "";
      else
         strTITLE     = thisForm.TITLE.value;
      
      if ( thisForm.DETAILS == null )
         strDETAILS     = "";
      else
         strDETAILS     = thisForm.DETAILS.value;
      
      dbUpdatedOrder = strID_NUM    + "|" + 
                       strQUANTITY  + "|" +
                       strPRICE     + "|" +
                       strTITLE     + "|" +
                       strDETAILS;

			if(strQUANTITY > 0) {
	      strNewOrder = "Order." + iNumberOrdered;
	      SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
	      SetCookie("NumberOrdered", iNumberOrdered, null, "/");
	    } else {
	    	iNumberOrdered--;
	    }

      //notice = strQUANTITY + " " + strNAME + " added to your shopping cart.";
      //alert(notice);
      //alert(location.href);

      location.href = location.href;
   }
}

function PutInCart(strID_NUM, strQUANTITY, strPRICE, strTITLE, strDETAILS) {
   iNumberOrdered = 0;
   iNumberOrdered = GetCookie("NumberOrdered");
   iNumberOrdered++;

   if ( iNumberOrdered > 12 )
      alert("Oups!  Votre panier est plein!  Veuillez passer &agrave; la caisse...");
   else {
      dbUpdatedOrder = strID_NUM    + "|" + 
                       strQUANTITY  + "|" +
                       strPRICE     + "|" +
                       strTITLE     + "|" +
                       strDETAILS;

      strNewOrder = "Order." + iNumberOrdered;
      SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
      SetCookie("NumberOrdered", iNumberOrdered, null, "/");
      //notice = strQUANTITY + " item added to your shopping cart.";
      //alert(notice);
      
      //currentURL = location.href;
      //poundPos   = currentURL.indexOf("#", 0);
			//if(poundPos > 0) {
			//	currentURL = currentURL.substring(0, poundPos);
			//}
      //location.href=currentURL + "#" + strID_NUM;

      location.href = location.href;
   }
}



//---------------------------------------------------------------------||
// FUNCTION:    getCookieVal                                           ||
// PARAMETERS:  offset                                                 ||
// RETURNS:     URL unescaped Cookie Value                             ||
// PURPOSE:     Get a specific value from a cookie                     ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}


//---------------------------------------------------------------------||
// FUNCTION:    FixCookieDate                                          ||
// PARAMETERS:  date                                                   ||
// RETURNS:     date                                                   ||
// PURPOSE:     Fixes cookie date, stores back in date                 ||
//---------------------------------------------------------------------||
function FixCookieDate (date) {
   var base = new Date(0);
   var skew = base.getTime();

   date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------||
// FUNCTION:    GetCookie                                              ||
// PARAMETERS:  Name                                                   ||
// RETURNS:     Value in Cookie                                        ||
// PURPOSE:     Retrieves cookie from users browser                    ||
//---------------------------------------------------------------------||
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}


//---------------------------------------------------------------------||
// FUNCTION:    SetCookie                                              ||
// PARAMETERS:  name, value, expiration date, path, domain, security   ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Stores a cookie in the users browser                   ||
//---------------------------------------------------------------------||
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}


//---------------------------------------------------------------------||
// FUNCTION:    DeleteCookie                                           ||
// PARAMETERS:  Cookie name, path, domain                              ||
// RETURNS:     null                                                   ||
// PURPOSE:     Removes a cookie from users browser.                   ||
//---------------------------------------------------------------------||
function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    MoneyFormat                                            ||
// PARAMETERS:  Number to be formatted                                 ||
// RETURNS:     Formatted Number                                       ||
// PURPOSE:     Reformats Dollar Amount to #.## format                 ||
//---------------------------------------------------------------------||
function moneyFormat(input) {
   var dollars = Math.floor(input);
   var tmp = new String(input);

   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
      if ( tmp.charAt(decimalAt)=="." )
         break;
   }

   var cents  = "" + Math.round(input * 100);
   cents = cents.substring(cents.length-2, cents.length)
           dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

   if ( cents == "0" )
      cents = "00";

   return(dollars + "," + cents);
}


//---------------------------------------------------------------------||
// FUNCTION:    RemoveFromCart                                         ||
// PARAMETERS:  Order Number to Remove                                 ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Removes an item from a users shopping cart             ||
//---------------------------------------------------------------------||
function RemoveFromCart(RemOrder) {
   if ( confirm("Appuyez sur 'OK' pour retirer cet article de votre panier.") ) {
      NumberOrdered = GetCookie("NumberOrdered");
      for ( i=RemOrder; i < NumberOrdered; i++ ) {
         NewOrder1 = "Order." + (i+1);
         NewOrder2 = "Order." + (i);
         database = GetCookie(NewOrder1);
         SetCookie (NewOrder2, database, null, "/");
      }
      NewOrder = "Order." + NumberOrdered;
      SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");
      DeleteCookie(NewOrder, "/");
      location.href=location.href;
   }
}

function RemoveAllFromCart () {
	NumberOrdered = GetCookie("NumberOrdered");
	if ( NumberOrdered > 0 ) {
		for ( i=1; i < NumberOrdered; i++ ) {
			NewOrder = "Order." + (i);
			DeleteCookie(NewOrder, "/");
		}
    SetCookie ("NumberOrdered", 0, null, "/");
    location.href=location.href;
	}
}

function SetPaiement ( Mode ) {
  SetCookie ("ModePaiement", Mode, null, "/");
  location.href=location.href;
}

function SetLivraison ( Mode ) {
  SetCookie ("ModeLivraison", Mode, null, "/");
  location.href=location.href;
}

function SetVis ( Mode ) {
  SetCookie ("Vis", Mode, null, "/");
  location.href=location.href;
}



//---------------------------------------------------------------------||
// FUNCTION:    GetFromCart                                            ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product Table Written to Document                      ||
// PURPOSE:     Draws current cart product table on HTML page          ||
//---------------------------------------------------------------------||
function GetCart( fShipping ) {
   if( fShipping )
      WriteCart( true, fShipping );
   else
      WriteCart( true, 0 );
}
function GetForm( fShipping ) {
   if( fShipping )
      WriteCart( true, fShipping, 1 );
   else
      WriteCart( true, 0, 1 );
}
function GetPaiement( iStep ) {
    WritePaiement( iStep );
}


//---------------------------------------------------------------------||
// FUNCTION:    WriteToForm                                            ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product hidden fields Written to Document              ||
// PURPOSE:     Draws current cart product hidden fields on HTML form  ||
//              if bDisplay == true, shows cart output as HTML table   ||
//---------------------------------------------------------------------||
function WriteCart( bDisplay, fShipping, isForm ) {
	iNumberOrdered = 0;
	fTotal         = 0;
	fQty           = 0;
	strTotal       = "";
	strShipping    = "";
	strOutput      = "";
	strOutputForm  = "";
	isBonus        = 0;
	
	iModePaiement  = GetCookie("ModePaiement");
  if (( iModePaiement == null ) || ( iModePaiement == "" )) 
  	iModePaiement = "credit";
  SetCookie ("ModePaiement", iModePaiement, null, "/");

	iVis = GetCookie("Vis");
  if (( iVis == null ) || ( iVis == "" )) 
  	iVis = "non";
  SetCookie ("Vis", iVis, null, "/");

	iNumberOrdered = GetCookie("NumberOrdered");
	if(iNumberOrdered > 0) {

		if (! isForm ) {
			strOutput += "<div id=\"panier\">";
			strOutput += "<form>";
		}
		
		if (! isForm ) {
			if ( iVis == "non" ) {
				strOutput += "<div class=\"twig\"><a href=\"javascript:SetVis('oui')\"><img src=\"../res/panier/vis.0.png\" width=\"7\" height=\"7\" border=\"0\" alt=\" \"></a></div>";
			} else {
				strOutput += "<div class=\"twig\"><a href=\"javascript:SetVis('non')\"><img src=\"../res/panier/vis.1.png\" width=\"7\" height=\"7\" border=\"0\" alt=\" \"></a></div>";
			}

			strOutput += "<div class=\"lien-caisse\"><a href=\"../panier/caisse.html\"><b>Passer &agrave; la caisse</b></a></div>";

			if ( iVis == "non" ) {
				strOutput += "<h2><a href=\"javascript:SetVis('oui')\">contenu de votre panier</a>";
			} else {
				strOutput += "<h2><a href=\"javascript:SetVis('non')\">contenu de votre panier</a>";
			}
		} else {
			strOutput += "<h2>contenu de votre panier";
		}
		strOutput += ": " + iNumberOrdered + " article"
		if(iNumberOrdered > 1) 
			strOutput += "s";
		strOutput += "</h2>";
	
		if ( ( isForm ) || ( ( ! isForm) && ( iVis != "non" ) ) )
			strOutput += "<table>";

		for ( i = 1; i <= iNumberOrdered; i++ ) {
			NewOrder = "Order." + i;
			database = "";
			database = GetCookie(NewOrder);
	
			Token0 = database.indexOf("|", 0);
			Token1 = database.indexOf("|", Token0+1);
			Token2 = database.indexOf("|", Token1+1);
			Token3 = database.indexOf("|", Token2+1);
			Token4 = database.indexOf("|", Token3+1);

			fields = new Array;
			fields[0] = database.substring( 0, Token0 );
			fields[1] = database.substring( Token0+1, Token1 );
			fields[2] = database.substring( Token1+1, Token2 );
			fields[3] = database.substring( Token2+1, Token3 );
			fields[4] = database.substring( Token3+1, database.length );
			
			fTotal     += parseInt(fields[1]) * parseFloat(fields[2]);
			fQty       += parseInt(fields[1]);
			
			if ( ( isForm ) || ( ( ! isForm) && ( iVis != "non" ) ) ) {
	
				strOutput += "  <tr>" +
										 "    <td><b>" + fields[3] + "</b> (" + fields[4] + ")</td>";
	
				strOutput += "    <td>" + moneyFormat(fields[2]) + "$</td>";
	
				strOutput += "    <td><a href=\"javascript:RemoveFromCart("+i+")\">effacer</a></td>" +
										 "  </tr>";
	
				strOutputForm += "<input type=\"hidden\" name=\"TITLE__" + i + "\" value=\"" + fields[3] + "\">";
				strOutputForm += "<input type=\"hidden\" name=\"ID_____" + i + "\" value=\"" + fields[0] + "\">";
				strOutputForm += "<input type=\"hidden\" name=\"QTY____" + i + "\" value=\"" + fields[1] + "\">";
				strOutputForm += "<input type=\"hidden\" name=\"PRI____" + i + "\" value=\"" + moneyFormat(fields[2]) + "\">";
			}
	
		}
	
		if ( ( isForm ) || ( ( ! isForm) && ( iVis != "non" ) ) )
			strOutput += "</table>";

//		if ( ( isForm ) || ( ( ! isForm) && ( iVis != "non" ) ) ) {
//			strOutput += "<p>S&eacute;lectionner un mode de paiement:";
//	
//			if(iModePaiement == "credit")
//				strOutput += "<input onClick=\"SetPaiement('credit')\" type=\"radio\" name=\"paiement\" value=\"credit\" checked>" +
//										 "<a href=\"javascript:SetPaiement('credit')\"><b>carte de cr&eacute;dit</b></a> " +
//										 "<input onClick=\"SetPaiement('cheque')\" type=\"radio\" name=\"paiement\" value=\"cheque\">" +
//										 "<a href=\"javascript:SetPaiement('cheque')\">ch&egrave;que</a>"
//			else
//				strOutput += "<input onClick=\"SetPaiement('credit')\" type=\"radio\" name=\"paiement\" value=\"credit\">" +
//										 "<a href=\"javascript:SetPaiement('credit')\">carte de cr&eacute;dit</a> " +
//										 "<input onClick=\"SetPaiement('cheque')\" type=\"radio\" name=\"paiement\" value=\"cheque\" checked>" +
//										 "<a href=\"javascript:SetPaiement('cheque')\"><b>ch&egrave;que</b></a>";
//			strOutput += "</p>";
//		}
		
		//if(iModePaiement == "credit")
		//	fTotal   += fQty * 2.50;
    
	  strTotal    = moneyFormat(fTotal);
		
		strOutput += "<p>Grand total: <b>" + strTotal + "$</b> (port et taxes comprises)</p>";

	  strOutputForm += "<input type=\"hidden\" name=\"TOTAL_______\" value=\"$" + moneyFormat(fTotal) + "\">";

		if (! isForm ) {
			strOutput += "</form>";
			strOutput += "</div>";
		}

	  document.write(strOutput);
	  if ( isForm )
	  	document.write(strOutputForm);
	  document.close();

	} else {
		if ( isForm )
			location.href = "../";
	}
}


function WritePaiement( iStep ) {
	
	iModePaiement  = GetCookie("ModePaiement");
  if (( iModePaiement == null ) || ( iModePaiement == "" )) 
  	iModePaiement = "credit";

	strOutput = "";

	strOutput += "<h2>paiement</h2>";
	
	if ( iModePaiement == "credit" ) {
	
		if ( iStep == 1 ) {
	
			strOutput += "<p>S&eacute;lectionner une carte de cr&eacute;dit:<br />" +
									 "<input type=\"radio\" name=\"carte___\" value=\"VISA____\" checked> " +
									 "<b>VISA</b> " +
									 "<input type=\"radio\" name=\"carte___\" value=\"MASTER__\"> " +
									 "<b>MasterCard</b> </td>" +
									 "<input type=\"radio\" name=\"carte___\" value=\"AMEX____\"> " +
									 "<b>American Express</b> </td>" +
									 "<input type=\"radio\" name=\"carte___\" value=\"DINERS__\"> " +
									 "<b>Diner's Club</b> </td>" +
									 "<br />" +
									 "(J'accepte que mon compte soit factur&eacute; en $ canadiens.)" +
									 "</p>";
	
			strOutput += "<h3>premi&egrave;re &eacute;tape</b></h3>";
	
			strOutput += "<table class=\"form\">" +
									 "  <tr>" +
									 "    <td class=\"q\">nom sur la carte</td>" +
									 "    <td class=\"a\"><input type=\"text\" name=\"realname\" size=\"40\" class=\"widget\" onBlur=\"SetCookie('Realname', this.value, null, '/');\"></td>" +
									 "  </tr>" +
									 "</table>";
	
			strOutput += "<p>Ne transcrivez que les 8 <b>premiers</b> chiffres (<b>un</b> par case) de votre carte de cr&eacute;dit.</p>";
	
			strOutput += "<table class=\"form\">" +
									 "  <tr>" +
									 "    <td class=\"q\">num&eacute;ro de la carte</td>" +
									 "    <td class=\"a\"><b>" +
									 "      <input type=\"text\" size=\"1\" name=\"Va1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vb1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vc1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vd1\" class=\"widget\"> -" +
									 "      <input type=\"text\" size=\"1\" name=\"Ve_1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vf_1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vg_1\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vh_1\" class=\"widget\"><b> -" +
									 "      X X X X - X X X X" +
									 "    </b></td>" +
									 "  </tr>";
	
			strOutput += "  <tr>" +
									 "    <td class=\"q\">date d'&eacute;ch&eacute;ance (ex.: 07/99)</td>" +
									 "    <td class=\"a\"><b>" +
									 "      X X / X X" +
									 "    </b></td>" +
									 "  </tr>" +
									 "</table>";
	
			strOutput += "<p>Note: Le nom du marchand sur votre relev&eacute; de carte de cr&eacute;dit sera <b>DIFFUSION i M&eacute;DIA</b>.</p>" +
									 "<p class=\"submit\"><input type=\"submit\" value=\"transmettre cette 1e &eacute;tape\" class=\"widget\"></p>";
	
		} else { // iStep == 2
		
			strOutput += "<h3>deuxi&egrave;me &eacute;tape</h3>";
	
			strOutput += "<table class=\"form\">" +
									 "  <tr>" +
									 "    <td class=\"q\">nom sur la carte</td>" +
			    			   "    <td class=\"a\">" +
			    			   "      <input type=\"text\" name=\"realname\" size=\"40\" class=\"widget\" value=\"" + GetCookie("Realname") + "\">" +
			    			   "    </td>" +
									 "  </tr>" +
									 "</table>";
	
			strOutput += "<p>" +
									 "  Inscrivez seulement les <b>derniers</b> chiffres (du 9e au 12e, ou au 16e)" +
									 "  de votre num&eacute;ro de carte de cr&eacute;dit ainsi que la "+
									 "  date d'&eacute;ch&eacute;ance sur ce 2e formulaire -- <b>un chiffre</b> par case." +
									 "</p>";
	
			strOutput += "<table class=\"form\">" +
									 "  <tr>" +
									 "    <td class=\"q\">num&eacute;ro de la carte</td>" +
									 "    <td class=\"a\"><b>" +
									 "      X X X X - X X X X -" +
									 "      <input type=\"text\" size=\"1\" name=\"Vi2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vj2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vk2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vl2\" class=\"widget\"> -" +
									 "      <input type=\"text\" size=\"1\" name=\"Vm_2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vn_2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vo_2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Vp_2\" class=\"widget\">" +
									 "    </b></td>" +
									 "  </tr>";
	
			strOutput += "  <tr>" +
									 "    <td class=\"q\">date d'&eacute;ch&eacute;ance (ex.: 07/99)</td>" +
									 "    <td class=\"a\"><b>" +
									 "      <input type=\"text\" size=\"1\" name=\"Ew2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Ex2\" class=\"widget\"> / " +
									 "      <input type=\"text\" size=\"1\" name=\"Ey2\" class=\"widget\"><input type=\"text\" size=\"1\" name=\"Ez2\" class=\"widget\">" +
									 "    </b></td>" +
									 "  </tr>";
	
			strOutput += "  <tr>" +
									 "    <td class=\"q\">courriel</td>" +
									 "    <td class=\"a\">" +
			    			   "      <input type=\"text\" name=\"email\" size=\"40\" class=\"widget\" value=\"" + GetCookie("Email") + "\">" +
									 "    </td>" +
									 "  </tr>";
	
			strOutput += "  <tr>" +
									 "    <td class=\"q\">commentaires</td>" +
									 "    <td class=\"a\"><textarea name=\"commenta\" rows=\"5\" cols=\"40\" wrap=\"virtual\" class=\"widget\"></textarea></td>" +
									 "  </tr>" +
									 "</table>";
	
			strOutput += "<p>Note: Le nom du marchand sur votre relev&eacute; de carte de cr&eacute;dit sera <b>DIFFUSION i M&eacute;DIA</b>.</p>" +
									 "<p class=\"submit\"><input type=\"submit\" value=\"transmettre cette 2e &eacute;tape\" class=\"widget\"></p>";
	
		}

	} else { // iModePaiement == "cheque"

		strOutput += "<p>&Agrave; remplir...</p>";

	}

	document.write(strOutput);
	document.close();
}

//=====================================================================||
//               END NOP Design SmartPost Shopping Cart                ||
//                              V.4.0.0                                ||
//=====================================================================||

	function setDevise(Mode) {
		SetCookie ("devise", Mode, null, "/");
		location.href=location.href;
	}

