// Zone population function                                    cm_zone_fg.js
// Created 06/24/10
// 06/24/10 ram - copied cm_zone.js due to upsell products being displayed 
//                differently than the other sites

// *********************************************************************** //
// *** MAKE SURE TO MODIFY cm_zone.js ALSO WHEN MODIFYING THIS PROGRAM *** //
// *********************************************************************** //

   function PPZ1_zp(a_product_ids,
                    zone,
                    symbolic,
                    target_id,
                    category,
                    rec_attributes,
                    target_attributes,
                    target_header_txt) {
      //alert("recs " + rec_attributes);              
      var prodArr = Array();
      //var html = zone + "_zp: No recommendations returned"; *** default message ***
      var html = "";
      if (symbolic !== '_NR_')
      {
         // special case. Over ride of the header text passed from the configuration file
         target_header_txt = "You Might Also Like...";
         var div_recs = '' /*'<div class="v4_io_example_recs"\>'*/ ;
         var div_recs = '<div style="color:000000;font-family:Verdana,Arial,sans-serif;font-size:11px;">';
         var n_recs = 4; //show only 4 items as upsells //a_product_ids.length;
         var div_title = '<div class="io_recs_title_fg">' + target_header_txt + '<\/div>';
         var div_table = '<div class="io_recs_table">';
         var image_table = '<TABLE CELLSPACING="2" CELLPADDING="0" Align="center" style="bordercolor:white;"><TR>';
         var lines = [];
         var width = 560 / n_recs;
         // 0 product description
         // 1 category
         // 2 price
         // 3 not defined
         // 4 not defined
         // Recommendations
         
         for (var ii=0; ii < n_recs && ii < a_product_ids.length;  ii++)
         {
            
            var pid = rec_attributes[ii].shift();
            
            var str2=rec_attributes[ii][2];
            var str3=rec_attributes[ii][3];
            var rec_prod_id = a_product_ids[ii];
            var item_link = rec_attributes[ii][3];
            var item_description = rec_attributes[ii][0];
            var image_description = rec_attributes[ii][2];

            // - not passed to example
            var selected_href = rec_attributes[ii][2];
            // - not passed to example
            var price = rec_attributes[ii][1] ? '$' + rec_attributes[ii][1] : '';
            // - not passed to example
            var n_stars = 2 + Math.floor(Math.random() * 3);
            // - not passed to example
            //var category = rec_attributes[ii][1];
            // - not passed to example
            var image_url = rec_attributes[ii][2];
         
            var myKey = ".jpg";
            var myStringVar = image_description;
            var myMatch = myStringVar.search(myKey);

            if (myMatch != -1)
            {
               prodArr.push( pid );
               // - not passed to example
               var alt_description = image_description || rec_prod_id;
               lines.push('<TD valign="top" class="borderedRollover" width=' + width + '><div class="bordered_Rollover"><TABLE ALIGN="center" WIDTH=' + width + '>');
               //lines.push('<TR><TD ALIGN="center"><A HREF="' + selected_href + '"><IMG SRC="' + image_url + '?wid=128" + ALT="' + alt_description + '" + WIDTH=128 + height=128 + style="border: medium none; cursor: pointer; padding-bottom: 5px; padding-top: 5px;" /><\/TD><\/TR>');
               lines.push('<TR><TD ALIGN="center"><A HREF="' + item_link + '&zone=' + zone + '"><IMG SRC="/' + image_url + '?wid=128" ALT="' + alt_description + '" style="border: medium none; cursor: pointer; padding-bottom: 5px; padding-top: 5px;" /><\/TD><\/TR>');
               lines.push('<TR><TD ALIGN="center"><A STYLE="text-decoration:none" HREF="' + item_link + '&zone=' + zone + '"><strong>Item: ' + pid + '</strong><\/A><\/TD><\/TR>');
               lines.push('<TR><TD ALIGN="center"><A STYLE="text-decoration:none" HREF="' + item_link + '&zone=' + zone + '">' + item_description + '<\/A><\/TD><\/TR>');
               lines.push('<TR ><TD><TABLE BORDER="0" CELL ACING="0" CELLPADDING="0" WIDTH=100%><TR><TD ALIGN="center"><A STYLE="text-decoration:none" HREF="' + item_link + '&zone=' + zone + '"><span style="font-weight:bold;" id="price_' + pid + '">' + price + '</span><\/A><\/TD><\/TR><\/TABLE><\/TD><\/TR>');
               lines.push('<\/TABLE><\/div><\/TD>'); //&zone=' + zone + '
            }
            else 
            {
               n_recs++;
            }
 
         }
         // make a text string
         //html = div_title + div_table + image_table + lines.join("\n") + '<\/TR>' + image_trailer + '<\/TABLE><\/div>';
         html = div_title + div_recs + div_recs + div_table + image_table + lines.join("\n") + '<\/TR\><\/TABLE\><\/div\><\/div\>';
      }
      document.getElementById('io_zone').innerHTML= html;
      loadPrices(prodArr);
   }

   
   /***********************************************************
   *                                                          *
   * BEGIN NEW AJAX CODE                                      *
   *                                                          *
   ***********************************************************/

   var pricewait = false;
   function loadPrices(parr)
   {
      var pstr = "";
      for(var x = 0;x < parr.length; x++)
      {
         if(pstr != "")pstr = pstr + ",";
         pstr = pstr + parr[x];
      }
      getPriceRange_xml(pstr)
   }
   
   var xmlHttpReqAjaxObj;
   if (window.XMLHttpRequest)
   { 
      xmlHttpReqAjaxObj = new XMLHttpRequest(); 
   }
   else if (window.ActiveXObject)
   { 
      xmlHttpReqAjaxObj = new ActiveXObject("Microsoft.XMLHTTP"); 
   }
   
   function getPriceRange_xml(pids)
   {
   
      xmlHttpReqAjaxObj.open("POST", 'ajax/getProductMinMax.html', true);
      xmlHttpReqAjaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlHttpReqAjaxObj.onreadystatechange = ajaxstatechangeHandle;
      xmlHttpReqAjaxObj.send("pids=" + pids);
   
   }

   function ajaxstatechangeHandle()
   {
      
      var respText;
      var ajax_parser;
      var ajax_xmlDoc;
      var w;
      if(xmlHttpReqAjaxObj.readyState == 4)
      {
      
         respText = xmlHttpReqAjaxObj.responseText;
         LoadXMLString(respText);
       
      }
   }
   
   var ajaxparser;
   var ajaxxmlDoc;
   function LoadXMLString(txt)
   {
      
      if (window.DOMParser)
      {
         ajaxparser=new DOMParser();
         ajaxxmlDoc=ajaxparser.parseFromString(txt,"text/xml");
      }
      else
      {
         ajaxxmlDoc=new ActiveXObject("Microsoft.XMLDOM");
         ajaxxmlDoc.async="false";
         ajaxxmlDoc.loadXML(txt);
      }

      var doc=ajaxxmlDoc.documentElement;
      var dlen = doc.childNodes.length;
      for(var i= 0; i < dlen; i++)
      {
      
         var maxline = "";
         var pobj = doc.childNodes[i];
         var patt = pobj.attributes;
         var max = patt.getNamedItem("max").nodeValue
         var min = patt.getNamedItem("min").nodeValue
         var pid = patt.getNamedItem("id").nodeValue
         
         if(max != min) maxline = min + " - " + max;
         else maxline = min;
         
         var pdivid = "price_" + pid;
         var pdiv = document.getElementById(pdivid);
         pdiv.innerHTML = "";
         
         var tWasDiv = document.createElement("span");
         tWasDiv.innerHTML = maxline;
         pdiv.appendChild(tWasDiv);
      
      }
      
   }

   /***********************************************************
   *                                                          *
   * END NEW AJAX CODE                                        *
   *                                                          *
   ***********************************************************/


