
var OSC_initial_structure = new Array();
var OSC_closed_elem = 0;
var OSC_closed_elem_struc = 0;
var OSC_display="";
var OSC_panier = new Array();
var OSC_articles = new Array();
var OSC_empty = "";
var OSC_allowed_div_elements = new Array("OSC_products_image","OSC_products_delete","OSC_products_name","OSC_products_model","OSC_products_options","OSC_products_quantity","OSC_nb_products","OSC_total_ht","OSC_total_ttc","OSC_products_price_ttc","OSC_products_price_ht","OSC_manufacturers","OSC_specials_price_ht","OSC_specials_price_ttc");

function OSC_reset_array(myarray)
{
for (i=0;i<myarray.length;i++) myarray.shift();
}

function OSC_in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: OSC_in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function OSC_find_array(myarray, value)
{
for (i=0;i<myarray.length;i++)
	{
	if (myarray[i]["nom"]==value) return i;
	}
return -1;
}

  
  function OSC_count_backstep(myelem)
  	{
	var start = myelem-1;
	var count = 0;
	if (OSC_initial_structure[myelem]["parent"]!=start)
	{
	count++;
	OSC_closed_elem++;
		while (OSC_initial_structure[start]["parent"]!=OSC_initial_structure[myelem]["parent"])
			{
			start = OSC_initial_structure[start]["parent"];
			count++;
			OSC_closed_elem++;
			}
	}
	return count;
	}
	
  function OSC_count_backstep1(myelem)
  	{
	var start = myelem-1;
	var count = 0;
	if (OSC_initial_structure[myelem]["parent"]!=start)
	{
	count++;
	OSC_closed_elem_struc++;
		while (OSC_initial_structure[start]["parent"]!=OSC_initial_structure[myelem]["parent"])
			{
			start = OSC_initial_structure[start]["parent"];
			count++;
			OSC_closed_elem_struc++;
			}
	}
	return count;
	}
	
function OSC_printQueryResults(query,limit,languages_id){
    //dojo.query returns a node list
    var nodes = dojo.query(query,limit);
	var result = new Array();

    for(var i = 0; i < nodes.length; i++){
	 result[i] = new Array();
	 result[i]["nom"] = nodes[i].id;
	 result[i]["class"] = nodes[i].className;
	 result[i]["title"] = nodes[i].title;
	 result[i]["html"] = nodes[i].innerHTML;
	 result[i]["prefixe"] = nodes[i].getAttribute("prefixe");
	 result[i]["suffixe"] = nodes[i].getAttribute("suffixe");
 	 result[i]["href"] = nodes[i].getAttribute("href");
 	 result[i]["width"] = nodes[i].getAttribute("width");
 	 result[i]["height"] = nodes[i].getAttribute("height");
	 result[i]["parent"] = OSC_find_array(result,nodes[i].parentNode.id);
	 if (nodes[i].id=="OSC_article") OSC_empty = nodes[i].getAttribute("empty");
    }
    
	OSC_initial_structure = result;
	OSC_getText("","languages_id="+languages_id);

  }
  
function OSC_products_loop(starting_count)
{
var OSC_is_article=true;
var to_stop = "";
for (kln = 0; kln < OSC_articles.length; kln++)
    {
	// we start with a new article
	OSC_display+=OSC_panier[kln]["OSC_article"];
	OSC_closed_elem_struc = 0;
	kk = starting_count;
	OSC_is_article=true;
	for (kk; kk<OSC_initial_structure.length;kk++)
		{
		to_close = OSC_count_backstep1(kk);
		if (OSC_initial_structure[kk]["nom"]!="OSC_article") {for (kkk=0;kkk<to_close;kkk++) OSC_display += "</div>\n";}
		if (OSC_initial_structure[kk]["nom"]=="OSC_article" && kln==0)
			{
			to_stop = to_close;
			}
		// if we've done with articles, break the loop to ignore next structural elements
		if ((kk-starting_count)==(OSC_closed_elem_struc-to_stop) && OSC_initial_structure[kk]["nom"]!="OSC_article")
			{
			OSC_is_article=false;
			break;
			}
		OSC_display += "<div id=\""+OSC_initial_structure[kk]["nom"]+"\" class=\""+OSC_initial_structure[kk]["class"]+"\">";
		if (OSC_initial_structure[kk]["prefixe"] && OSC_panier[kln][OSC_initial_structure[kk]["nom"]]!="\n") OSC_display += OSC_initial_structure[kk]["prefixe"];
		OSC_display += "\n";
		if (OSC_in_array(OSC_initial_structure[kk]["nom"],OSC_allowed_div_elements))
			{
			OSC_display += OSC_panier[kln][OSC_initial_structure[kk]["nom"]];
			}
		if (OSC_initial_structure[kk]["suffixe"] && OSC_panier[kln][OSC_initial_structure[kk]["nom"]]!="\n") OSC_display += OSC_initial_structure[kk]["suffixe"];
		}
	// end of an article
	if (OSC_is_article==false) OSC_display+=OSC_panier[kln]["OSC_article_end"];
	}
}

		function OSC_getText(form_id,param) { 
		  //The form data is sent to the given URL using a POST method, 
		  //rather than a GET by using the dojo.xhrPost function.
		  dojo.xhrPost({
		    url: "/linkeo/catalogue/shopping_cart_ajax.php?"+param,
            handleAs : "xml",
		    load: function(response, ioArgs){
		    //alert(response);
			try{

				var total_ht = "";
				var total_ttc = "";
				var nb_products = "";
				var nomNoeud = "panier";
				var options = new Array();
				OSC_reset_array(OSC_panier);
				OSC_reset_array(OSC_articles);
		
				
				var lNodes = response.getElementsByTagName(nomNoeud);
				
				for(iln = 0; iln < lNodes.length; iln++){
					total_ht += lNodes[iln].getAttribute("total_ht");
					total_ttc += lNodes[iln].getAttribute("total_ttc");
					nb_products = lNodes[iln].getAttribute("quantity");

					var CNodes = lNodes[iln].childNodes;
					for(jln = 0; jln < CNodes.length; jln++){
						OSC_articles[jln] = new Array();
						OSC_articles[jln]["id"] = CNodes[jln].getAttribute("id");
						OSC_articles[jln]["idPanier"] = CNodes[jln].getAttribute("idPanier");
						OSC_articles[jln]["nom"] = CNodes[jln].getAttribute("nom");
						OSC_articles[jln]["reference"] = CNodes[jln].getAttribute("reference");
						OSC_articles[jln]["fabricant"] = CNodes[jln].getAttribute("fabricant");
						OSC_articles[jln]["products_image"] = CNodes[jln].getAttribute("chemin_image");
						OSC_articles[jln]["prix_unitaire_ht"] = CNodes[jln].getAttribute("prix_unitaire_ht");
						OSC_articles[jln]["prix_unitaire_ttc"] = CNodes[jln].getAttribute("prix_unitaire_ttc");
						OSC_articles[jln]["prix_promotionnel_ht"] = CNodes[jln].getAttribute("prix_promotionnel_ht");
						OSC_articles[jln]["prix_promotionnel_ttc"] = CNodes[jln].getAttribute("prix_promotionnel_ttc");
						OSC_articles[jln]["quantite"] = CNodes[jln].getAttribute("quantite");
						
						var ONodes = CNodes[jln].childNodes;
						options[jln] = new Array();
						for(jjln = 0; jjln < ONodes.length; jjln++){
							options[jln][jjln] = new Array();
							options[jln][jjln]["id"] = ONodes[jjln].getAttribute("id");
							options[jln][jjln]["value"] = ONodes[jjln].getAttribute("value");
							options[jln][jjln]["nom"] = ONodes[jjln].getAttribute("nom");
							options[jln][jjln]["valeur"] = ONodes[jjln].getAttribute("valeur");
							options[jln][jjln]["prix"] = ONodes[jjln].getAttribute("prix");
							options[jln][jjln]["operateur"] = ONodes[jjln].getAttribute("operateur");
						}
						
					}

				}
				
				for (kln = 0; kln < OSC_articles.length; kln++){
					// action panier
					OSC_panier[kln] = new Array();
					OSC_panier[kln]["OSC_article"] = "<form id=\"modForm"+OSC_articles[kln]["idPanier"]+"\" onSubmit=\"return false;\">\n";
					// rebuild attributes hidden fields
					for (kkln = 0; kkln < options[kln].length; kkln++){
						OSC_panier[kln]["OSC_article"] += "<input name=\"id["+OSC_articles[kln]["idPanier"]+"]["+options[kln][kkln]["id"]+"]\" type=\"hidden\" value=\""+options[kln][kkln]["value"]+"\">\n";
					}
					// colonne image
					if (OSC_articles[kln]["products_image"]!="/linkeo/catalogue/images/" && OSC_find_array(OSC_initial_structure,"OSC_products_image")!=-1)						{
						OSC_panier[kln]["OSC_products_image"] = "<img src=\""+escape(OSC_articles[kln]["products_image"])+"\"";
						if (OSC_initial_structure[OSC_find_array(OSC_initial_structure,"OSC_products_image")]["width"]!=null) OSC_panier[kln]["OSC_products_image"] += " width=\""+OSC_initial_structure[OSC_find_array(OSC_initial_structure,"OSC_products_image")]["width"]+"\"";
						if (OSC_initial_structure[OSC_find_array(OSC_initial_structure,"OSC_products_image")]["height"]!=null) OSC_panier[kln]["OSC_products_image"] += " height=\""+OSC_initial_structure[OSC_find_array(OSC_initial_structure,"OSC_products_image")]["height"]+"\"";
						OSC_panier[kln]["OSC_products_image"] += ">\n";
						}
					else OSC_panier[kln]["OSC_products_image"]="\n";
					
					// colonne delete
					OSC_panier[kln]["OSC_products_delete"] = "<input type=\"checkbox\" name=\"cart_delete[]\" value=\""+OSC_articles[kln]["idPanier"]+"\" onClick=\"OSC_getText('modForm"+OSC_articles[kln]["idPanier"]+"','action=update_product');\">\n";

					// colonne prix ttc
					if (OSC_articles[kln]["prix_promotionnel_ttc"]!=OSC_articles[kln]["prix_unitaire_ttc"])					
					OSC_panier[kln]["OSC_products_price_ttc"] = "<div id=\"OSC_barre\">"+OSC_articles[kln]["prix_unitaire_ttc"]+"</div>\n";
					else OSC_panier[kln]["OSC_products_price_ttc"] = OSC_articles[kln]["prix_unitaire_ttc"]+"\n";
					
					// colonne prix ht
					if (OSC_articles[kln]["prix_promotionnel_ht"]!=OSC_articles[kln]["prix_unitaire_ht"])
					OSC_panier[kln]["OSC_products_price_ht"] = "<div id=\"OSC_barre\">"+OSC_articles[kln]["prix_unitaire_ht"]+"</div>\n";
					else OSC_panier[kln]["OSC_products_price_ht"] = OSC_articles[kln]["prix_unitaire_ht"]+"\n";

					// colonne prix promotionnel ht
					if (OSC_articles[kln]["prix_promotionnel_ht"]!=OSC_articles[kln]["prix_unitaire_ht"])
					OSC_panier[kln]["OSC_specials_price_ht"] = OSC_articles[kln]["prix_promotionnel_ht"]+"\n";
					else OSC_panier[kln]["OSC_specials_price_ht"]="\n";
					
					// colonne prix promotionnel ttc
					if (OSC_articles[kln]["prix_promotionnel_ttc"]!=OSC_articles[kln]["prix_unitaire_ttc"])
					OSC_panier[kln]["OSC_specials_price_ttc"] = OSC_articles[kln]["prix_promotionnel_ttc"]+"\n";
					else OSC_panier[kln]["OSC_specials_price_ttc"]="\n";

					// colonne products_name
					if (OSC_find_array(OSC_initial_structure,"OSC_products_name")!=-1)
					OSC_panier[kln]["OSC_products_name"] = "<a href=\""+OSC_initial_structure[OSC_find_array(OSC_initial_structure,"OSC_products_name")]["href"]+"?products_id="+OSC_articles[kln]["id"]+"\">"+OSC_articles[kln]["nom"]+"</a>\n";
					
					// colonne fabricant
					OSC_panier[kln]["OSC_manufacturers"] = OSC_articles[kln]["fabricant"]+"\n";
					
					OSC_panier[kln]["OSC_products_model"] = OSC_articles[kln]["reference"]+"\n";
					// affichage options de ce produit
					OSC_panier[kln]["OSC_products_options"]="";
					for (kkln = 0; kkln < options[kln].length; kkln++){
						OSC_panier[kln]["OSC_products_options"] += options[kln][kkln]["nom"]+"&nbsp;"+options[kln][kkln]["valeur"]+"&nbsp;"+options[kln][kkln]["operateur"]+options[kln][kkln]["prix"]+"<br />\n";
					}

					// colonne modification quantite
					OSC_panier[kln]["OSC_products_quantity"] = "<input name=\"cart_quantity[]\" value=\""+OSC_articles[kln]["quantite"]+"\" size=\"4\" onChange=\"OSC_getText('modForm"+OSC_articles[kln]["idPanier"]+"','action=update_product');\">\n";
					OSC_panier[kln]["OSC_products_quantity"] += "<input type=\"hidden\" name=\"products_id[]\" value=\""+OSC_articles[kln]["idPanier"]+"\">\n";

					
					// fin action panier
					OSC_panier[kln]["OSC_article_end"] = "</form>\n";
					//alert(OSC_panier[kln]["products_name"]);
					OSC_panier["OSC_total_ht"] = total_ht+"\n";
					OSC_panier["OSC_total_ttc"] = total_ttc+"\n";
					OSC_panier["OSC_nb_products"] = nb_products+"\n";

				}
			
			}
			catch(exc){
				//alert(exc);
			}
			
			OSC_display="\n";
			//***************
			OSC_closed_elem=0;
			
			for(kk=0;kk<OSC_initial_structure.length;kk++)
				{
				to_close = OSC_count_backstep(kk);
				for (kkk=0;kkk<to_close;kkk++) OSC_display += "</div>\n";
				if (kk==OSC_closed_elem && OSC_initial_structure[kk]["nom"]=="OSC_article")
					{
					starting_count=kk;
					OSC_products_loop(starting_count);
					}
				if (kk<OSC_initial_structure.length){
				
					OSC_display += "<div id=\""+OSC_initial_structure[kk]["nom"]+"\" class=\""+OSC_initial_structure[kk]["class"]+"\">";
					if (OSC_panier[OSC_initial_structure[kk]["nom"]]!="\n" && OSC_initial_structure[kk]["prefixe"]) OSC_display += OSC_initial_structure[kk]["prefixe"];
					OSC_display += "\n";
					if (OSC_in_array(OSC_initial_structure[kk]["nom"],OSC_allowed_div_elements) && OSC_articles.length>0) OSC_display += OSC_panier[OSC_initial_structure[kk]["nom"]];
					else 
						{
						if (OSC_initial_structure[kk]["html"].indexOf("<div")==-1) OSC_display+=OSC_initial_structure[kk]["html"];
						else
							{
							OSC_display+=OSC_initial_structure[kk]["html"].substr(0,OSC_initial_structure[kk]["html"].indexOf("<div"));
							}
						}
					if (OSC_panier[OSC_initial_structure[kk]["nom"]]!="\n" && OSC_initial_structure[kk]["suffixe"]) OSC_display += OSC_initial_structure[kk]["suffixe"];
					}
				}
			// add div enclosures except last one 
			for (unclosed_elem=0;unclosed_elem<OSC_initial_structure.length-OSC_closed_elem;unclosed_elem++) OSC_display += "</div>\n";
			//***************
			
			
			// final OSC_display in choosen div - check if empty
			if (OSC_articles.length>0) dojo.byId("OSC_panier").innerHTML = OSC_display;
			else dojo.byId("OSC_panier").innerHTML = "<div id=\"OSC_article\">"+OSC_empty+"</div>\n";
			
		     
		      
		      //Dojo recommends that you always return(response); to propagate 
		      //the response to other callback handlers. Otherwise, the error 
		      //callbacks may be called in the success case.
		      return response;
		    },
		    error: function(response, ioArgs){
		      dojo.byId("OSC_panier").innerHTML = 
		        "An error occurred, with response: " + response;
		      return response;
		    },
		    
		    //Setting the 'form' parameter to the ID of a form on the page
		    //submits that form to the specified URL
		    form:form_id
		  });
		}
