// strip spaces from any string and return it
function strip_spaces(instr) {
  var stub = instr.split(' ');
  var i = stub.length - 1;
  while (i > 0) {
    stub[i-1] += stub[i];
    i--;
  }
  return stub[0];
}

// create Product object and initialize from ProductData array of strings
function ProductInfo (proddat)
{
  this.name = (typeof proddat[0] == 'undefined') ? '' : proddat[0];
  this.catnum = (typeof proddat[1] == 'undefined') ? '' : proddat[1];
  this.colorways = (typeof proddat[2] == 'undefined') ? '' : proddat[2];
  this.material = (typeof proddat[3] == 'undefined') ? '' : proddat[3];
  this.width = (typeof proddat[4] == 'undefined') ? '' : proddat[4];
  this.repeat = (typeof proddat[5] == 'undefined') ? '' : proddat[5];
  this.dblrubs = (typeof proddat[6] == 'undefined') ? '' : proddat[6];
  this.standards = (typeof proddat[7] == 'undefined') ? '' : proddat[7];
  this.actsym = (typeof proddat[8] == 'undefined') ? '' : proddat[8];
  this.colorsshown = (typeof proddat[9] == 'undefined') ? '' : proddat[9];

  this.coordname = new Array(2);
  this.coordclr = new Array(2);
  this.coordprodix = new Array(2);

  this.coordname[0] = (typeof proddat[10] == 'undefined') ? '' : proddat[10];
  this.coordclr[0] = (typeof proddat[11] == 'undefined') ? '' : proddat[11];
  this.coordname[1] = (typeof proddat[12] == 'undefined') ? '' : proddat[12];
  this.coordclr[1] = (typeof proddat[13] == 'undefined') ? '' : proddat[13];
  this.coordprodix[0] = 
  this.coordprodix[1] = -1;

  this.pagetag = strip_spaces(this.name).toLowerCase();
  this.mainix = -1;
  this.newprodflag = false;
}

// initialize Product array of product info objects
var Product = new Array(ProductData.length);
var ProductMainIndex = new Array(ProductMain.length);

function initProductArrays() {
  var i;
  var j;

  for (i = 0; i < ProductData.length; i++)
  {
    Product[i] = new ProductInfo(ProductData[i]);
  }

  // initialize Product array's coordinate product indices
  for (i = 0; i < ProductData.length; i++) {
    for (j = 0; j < Product[i].coordprodix.length; j++) {
      var prodselix;
      var coord_name = Product[i].coordname[j].toLowerCase();

      // find and store the product index for the coordinate
      for (prodselix = Product.length - 1; prodselix >= 0; prodselix--) {
         if (coord_name == Product[prodselix].name.toLowerCase())
           break;
      } 
      Product[i].coordprodix[j] = prodselix;
    }
  }

  ProductData = []; // kill the product data init array

  // initialize ProductMain's index array.
  for (j = 0; j < ProductMain.length; j++)
  {
    ProductMainIndex[j] = -1;
    for (i = 0; i < Product.length; i++)
    {
      if (Product[i].name == ProductMain[j])
      {
        ProductMainIndex[j] = i;
        Product[i].mainix = j;
        break;
      }
    }
  }

  // check ProductNew for new products and set Product.newprodflag accordingly
  for (j = 0; j < ProductNew.length; j++)
  {
    for (i = 0; i < Product.length; i++)
    {
      if (Product[i].name == ProductNew[j])
      {
        Product[i].newprodflag = true;
        break;
      }
    }
  }
}

var prodfillsrc =  'graphics/yoma_textiles_prodfill.jpg';
initProductArrays();

