// Zone population function                                    cm_zone.js
// Created 11/30/09
// 06/15/10 msc - fixed an index when coremetrics added a field at beginning of array

   function PPZ1_zp(a_product_ids,
                    zone,
                    symbolic,
                    target_id,
                    category,
                    rec_attributes,
                    target_attributes,
                    target_header_txt) {
      //alert("recs " + rec_attributes);              
      //var html = zone + "_zp: No recommendations returned";
      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 n_recs = 4; //show only 4 items as upsells //a_product_ids.length;
         var div_title = '<div class="io_recs_title">' + 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)
            {
               // - 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 + '">' + price + '<\/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_recs + div_title + div_table + image_table + lines.join("\n") + '<\/TR\><\/TABLE\><\/div\><\/div\>';
      }
      document.getElementById('io_zone').innerHTML= html;
   }

