// blindpricesupport.js
// Change Log
// Rev 103. Added option to hide widths if SHOWWIDTHS is 0 - same for SHOWDROPS.
//          Also 2 new routines getmaxwidth and getmaxdrop now available.
// Rev 102. Added Custom Properties for the text Width, Drop and Price. Added tablecolour.
// Rev 101. Added showwidths and showdrops. Based on Rev B.
// Rev 100. Abandoned attempt at highlighting selected item
// Rev 000. Initial Working version.
 
function formbyfieldid(fname){		// return the form that contains unique field "fname"
  var df = document.forms;
  var i = df.length - 1;
  for ( var j = 0; j <= i; j++ )
    {
    var k = df[j].length - 1; 
    for ( var l = 0; l <= k; l++ ) if ( df[j].elements[l].id == (fname) ) return df[j];
    }
  // shouldn't get here but...
  alert('Cannot find form element' + fname);
  return false;
}

function topounds(nPounds)
  {
  var nCurrency, nWholeCurrency, nFraction, sFraction;
  nPounds = nPounds + 0.001;
  nWholeCurrency = Math.floor(nPounds);                // no of pounds
  nFraction = Math.round((nPounds - nWholeCurrency) * 100);  // Number of Fraction (2 decimal places)
  if ( nFraction > 99 ) nFraction = 99;
  if (nFraction < 10)                                  // Needs to be two digits
    {
    sFraction = "0" + nFraction.toString();            // Pad with leading zero
    }
  else
    {                                                 // Already has two digits
    sFraction = nFraction.toString();                  // So just format it
    }
  return(nWholeCurrency.toString() + '.' + sFraction);
  }

function getnumdrops(product){
  for ( var I = 1; I < 20; I++ ) if ( ! product.drops[I] ) return I - 1;
  return 20;
}

function getwidths(product){
  return product.widths.replace(/\s+/g,"").split(',');
}

function getmaxwidth(product){
  var widths = getwidths(product);
  return widths[widths.length - 1];
}

function getdrops(product, index){
  return product.drops[index].replace(/\s+/g,"").split(',');
}

function getmaxdrop(product){
  var drops = getdrops(product, getnumdrops(product));
  return drops[0];
}

function getfeature1(product){
  var features = product.feature1.split(',');
  var fname = features.shift();
  var fvalues = fname + ',' + features.join().replace(/\s+/g,"");
  return fvalues.split(',');
}

function getfeature2(product){
  var features = product.feature2.split(',');
  var fname = features.shift();
  var fvalues = fname + ',' + features.join().replace(/\s+/g,"");
  return fvalues.split(',');
}

function getfeature3(product){
  var features = product.feature3.split(',');
  var fname = features.shift();
  var fvalues = fname + ',' + features.join().replace(/\s+/g,"");
  return fvalues.split(',');
}

function showerror(error){
  document.write('<br><font color=red><b>' + error + '</b></font>');
}

function checkarray(vector){
  errors = '';
  for ( var I = 0; I < vector.length; I++ )
    {
    var thisitem = vector[I] - 0
    if ( isNaN(thisitem) || (thisitem < 0) ) errors = errors + '<BR>&nbsp;bad number (' + vector[I] + ')';
    }
  return errors;
}

function checkdata(product){
  var minwidth = product.minwidth - 0;
  if ( isNaN(minwidth) || minwidth < 0) showerror('Bad MINWIDTH (' + product.minwidth + ')');
    else product.minwidth = minwidth; 
  var mindrop = product.mindrop - 0;
  if ( isNaN(mindrop) || mindrop < 0) showerror('Bad MINDROP (' + product.mindrop + ')');
    else product.mindrop = mindrop;
  if ( product.widths == '' ) showerror('Missing WIDTHS ' + product.widths);
  var widths = getwidths(product);
  var numwidths = widths.length;
  if ( checkarray(widths) ) showerror('Error in WIDTHS ' + checkarray(widths));
  if ( product.feature1 != '' ) 
    {
    var feature1 = getfeature1(product);
    var f1name = feature1.shift();
    var numfeature1 = feature1.length;
    if ( numfeature1 != numwidths ) showerror('Number WIDTHS (' + product.widths + ') and number FEATURE1 (' + feature1 + ') don\'t match');
    if ( checkarray(feature1) ) showerror('Error in FEATURE1 ' + checkarray(feature1));
    }
  if ( product.feature2 != '' ) 
    {
    var feature2 = getfeature2(product);
    var f2name = feature2.shift();
    var numfeature2 = feature2.length;
    if ( numfeature2 != numwidths ) showerror('Number WIDTHS (' + product.widths + ') and number FEATURE2 (' + feature2 + ') don\'t match');
    if ( checkarray(feature2) ) showerror('Error in FEATURE2 ' + checkarray(feature2));
    }
  if ( product.feature3 != '' ) 
    {
    var feature3 = getfeature3(product);
    var f3name = feature3.shift();
    var numfeature3 = feature3.length;
    if ( numfeature3 != numwidths ) showerror('Number WIDTHS (' + product.widths + ') and number FEATURE3 (' + feature3 + ') don\'t match');
    if ( checkarray(feature3) ) showerror('Error in FEATURE2 ' + checkarray(feature3));
    }
  var numdrops = getnumdrops(product);
  if ( numdrops == 0 )  showerror('No DROPS defined');
  var prevdrop = 0;
  for ( var I = 1 ; I <= numdrops; I++)
    {
    var prices = getdrops(product, I);
    if ( isNaN(prices[0]) || prices[0] < 0) showerror('Bad DROP' + I + ' drop (' + getdrops(product, I)[0] + ')');
    if ( (prices[0] - 0) <= prevdrop ) showerror('DROP' + I + ' ('  + prices[0] + ') out of sequence ' + ' - previous (' + prevdrop + ')');
    prevdrop = prices[0]; 
    if ( (prices.length - 1) != numwidths ) showerror('DROP' + I + ' prices (' + prices + ') does not match number of widths');
    for ( var J = 1; J < prices.length; J++) 
      {
      if ( prices[J] != '-' )
        {
        var thisprice = prices[J] - 0;
        if ( isNaN(thisprice) || thisprice < 0 ) showerror('DROP' + I + ' has bad price (' + prices[J] + ')');
        }
      }
    } 
}

function getitemprice(product,width,drop){
  product.info = '';
  width = width - 0;
  if ( isNaN(width) )
    {
    alert('Bad width ');
    return 0;
    }
    
  if ( width < product.minwidth )
    {
    alert('Minimum ' + product.widthtext + ' is ' + product.minwidth);
    return 0;
    }
  var widths = getwidths(product);
  var maxwidth = widths[widths.length - 1] - 0;
  if ( width > maxwidth )
    {
    alert('Maximum ' + product.widthtext + ' is ' + maxwidth);
    return 0;
    }
  drop = drop - 0;
  if ( isNaN(drop) )
    {
    alert('Bad drop');
    return 0;
    }
    
  if ( drop < product.mindrop )
    {
    alert('Minimum ' + product.droptext + ' is ' + product.mindrop);
    return 0;
    }
  var numdrops = getnumdrops(product);
  var maxdrop = getdrops(product, numdrops)[0];
  if ( drop > maxdrop )
    {
    alert('Maximum ' + product.droptext + ' is ' + maxdrop);
    return 0;
    }
  var J = 0;
  for ( var I = 0; I < widths.length; I++)
    {
    if ( width > (widths[I] - 0) )J = I + 1;
    }
  var K = 1;
  for ( var I = 1; I <= numdrops; I++)
    {
    if ( drop > (getdrops(product, I)[0] - 0) ) K = I + 1;
    }
  var thisprice = getdrops(product, K)[J + 1];
  if ( thisprice == '-' )
    {
    alert('Sorry - not available in this size');
    return 0;
    }
  product.info = 'W:' + width + ';X:' + (J + 1) + ';D:' + drop + ';Y:' + K + ';';
  return thisprice - 0;
}

function getfeature1price(product, width){
  var feature1 = getfeature1(product, width);
  feature1.shift();                            // lose the name
  var widths = getwidths(product);
  var J = 0;
  for ( var I = 0; I < widths.length; I++)
    {
    if ( width > (widths[I] - 0) )J = I + 1;
    }
  return feature1[J] - 0;  
}

function getfeature2price(product, width){
  var feature2 = getfeature2(product, width);
  feature2.shift();                            // lose the name
  var widths = getwidths(product);
  var J = 0;
  for ( var I = 0; I < widths.length; I++)
    {
    if ( width > (widths[I] - 0) )J = I + 1;
    }
  return feature2[J] - 0;  
}

function getfeature3price(product, width){
  var feature3 = getfeature3(product, width);
  feature3.shift();                            // lose the name
  var widths = getwidths(product);
  var J = 0;
  for ( var I = 0; I < widths.length; I++)
    {
    if ( width > (widths[I] - 0) )J = I + 1;
    }
  return feature3[J] - 0;  
}

function displayprices(product){
  var showwidths = product.showwidths - 0;
  var showdrops = product.showdrops - 0;
  if ( product.previewall && (location.href.indexOf('PreviewHTML') > -1) )	// if previewing and we want to show all
    {
    showwidths = 1000;
    showdrops = 1000;
    }
  if ( showwidths == 0 ) return;
  document.write('<table bgcolor="' + product.tablecolour 
               + '" cellspacing=5 cellpadding=0><tr align=right valign=top><td style="font-size:10px" align=left>' 
               + product.widthtext);
  if ( showdrops > 0 ) document.write('<br>' + product.droptext);
  document.write('</td>');
  var widths = getwidths(product);
  var numwidths = widths.length;
  if ( numwidths > showwidths )
    {
    windex = numwidths / (showwidths - 1);     // scale down to only showwidths items
    }
  else
    {
    showwidths = numwidths;			// show entire liste
    windex = 1;
    }
//  var windex = numwidths > showwidths ? numwidths / (showwidths - 1) : 1;
  var witem = 0;
  for ( var I = 1; I <= showwidths; I++ )
    {
    document.write('<td style="font-size:10px">' + widths[Math.floor(witem)] + '</td>');
    witem += windex;
    if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
    }
  document.write('<td>&nbsp;</td></tr>');
  var numdrops = getnumdrops(product);
  if ( numdrops > showdrops )
    {
    dindex = numdrops / (showdrops - 1);
    }
  else
    {
    showdrops = numdrops;
    dindex = 1;
    }
//  var dindex = numdrops > showdrops ? numdrops / (showdrops - 1) : 1;
  var ditem = 1;
  for ( I = 1; I <= showdrops; I++)
    {
    var thisdrop = getdrops(product, Math.floor(ditem));
    ditem += dindex;
    if ( ditem > ( numdrops ) ) ditem = numdrops;
    document.write('<tr align=right><td style="font-size:10px" align=left>' + thisdrop[0] + '</td>');   // the drop length
    var witem = 0;
    for ( var J = 1; J <= showwidths; J++) 
      {
      var pound = product.currency;
      var thisprice = thisdrop[Math.floor(witem) + 1];
      if ( isNaN(thisprice) ) pound = '';
      document.write('<td style="font-size:10px">' + pound + thisprice + '</td>');
      witem += windex;
      if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
      }
    document.write('<td>&nbsp;</td></tr>');
    }

  if ( product.feature1 + product.feature2 + product.feature3 )
    {
    document.write('<tr><td colspan="' + (widths + 2) + '" style="font-size:10px" align=center>' + product.fheading + '</td></tr>');
    }

  pound = product.currency;
  if ( product.feature1 )
    {
    var feature1 = getfeature1(product);
    var f1name = feature1.shift();
    document.write('<tr align=right><td style="font-size:10px" align=left>' + f1name + '</td>');
    var witem = 0;
    for ( var I = 1; I <= showwidths; I++ ) 
      {
      document.write('<td style="font-size:10px">' + pound + feature1[Math.floor(witem)] + '</td>');
      witem += windex;
      if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
      }
    document.write('<td><input type=checkbox id="FE1_' + product.prodref + '" '
        + 'onclick="showprice(thisprod_' + product.prodref + ')"></td>');    
    if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
    }

  if ( product.feature2 )
    {
    var feature2 = getfeature2(product);
    var f2name = feature2.shift();
    document.write('<tr align=right><td style="font-size:10px" align=left>' + f2name + '</td>');
    var witem = 0;
    for ( var I = 1; I <= showwidths; I++ ) 
      {
      document.write('<td style="font-size:10px">' + pound + feature2[Math.floor(witem)] + '</td>');
      witem += windex;
      if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
      }
    document.write('<td><input type=checkbox id="FE2_' + product.prodref + '" '
        + 'onclick="showprice(thisprod_' + product.prodref + ')"></td>');    
    document.write('</tr>');
    }

  if ( product.feature3 )
    {
    var feature3 = getfeature3(product);
    var f3name = feature3.shift();
    document.write('<tr align=right><td style="font-size:10px" align=left>' + f3name + '</td>');
    var witem = 0;
    for ( var I = 1; I <= showwidths; I++ ) 
      {
      document.write('<td style="font-size:10px">' + pound + feature3[Math.floor(witem)] + '</td>');
      witem += windex;
      if ( witem > ( numwidths - 1) ) witem = numwidths - 1;
      }
    document.write('<td><input type=checkbox id="FE3_' + product.prodref + '" '
        + 'onclick="showprice(thisprod_' + product.prodref + ')"></td>');    
    document.write('</tr>');
    }

    document.write('</table>');
}

function showprice(product){
  var width = document.getElementById('WDT_' + product.prodref).value;
  var drop = document.getElementById('DRP_' + product.prodref).value;
  var thisprice = getitemprice(product,width,drop);
  if ( product.feature1 && thisprice && document.getElementById('FE1_' + product.prodref).checked ) 
    {
    thisprice += getfeature1price(product, width);
    product.info += 'F1:' + getfeature1(product).shift() + ';';
    }
  else 
    {
    product.info += 'F1:;';
    }

  if ( product.feature2 && thisprice && document.getElementById('FE2_' + product.prodref).checked ) 
    {
    thisprice += getfeature2price(product, width);
    product.info += 'F2:' + getfeature2(product).shift() + ';';
    }
  else 
    {
    product.info += 'F2:;';
    }
  if ( product.feature3 && thisprice && document.getElementById('FE3_' + product.prodref).checked )
    {
    thisprice += getfeature3price(product, width);
    product.info += 'F3:' + getfeature3(product).shift() + ';';
    }
  else 
    {
    product.info += 'F3:;';
    }
  product.info += 'P:' + thisprice + ';';
  document.getElementById('UNT_' + product.prodref).value = product.currency + topounds(thisprice);
  return thisprice;
}

function oktosubmit(product){
 var thisprice = showprice(product) - 0;
 if ( thisprice == 0 ) return false;
 // alert(product.info);
 // parameters W:width;X:windex;D:drop;Y:dindex;F1:f1name/n;;F2:f2name/nF3:f3name/n;P:price;
 document.getElementById('inf_' + product.prodref).value = product.info;  // all our parameters
 return true;  
}