// quantitablesupport rev 204d (perl mod's rev 204).

// 18-01-07 	   Update-1. Allow for quotes in row / column names. Changed: qtytablesupport.js, ActinicOrder.pm.

// 09-01-07 	   Customisation for Lloys Attree and Smith
//		   Also added code to prevent form being submitted if Enter key used.

// 15-09-05 (204d) Fix to allow Duplicates to use matrix.
//                 Files changed quantitytablesupport.js, QtyCartTable_ProductLine.html, QtyProductPageTable_ProductLine.html 

// 10-02-04 (204b) Optionally show running total on product page.   
//		  (only quantitablesupport.js and quantitablesupport.css changed)

// 04-05-03 (204) All scripts ignore negative entries.

// 03-05-03 (003) Now restores previous table values on error bounce pages.  
//		  Required changes to ActiniOrder.pm (001), ShoppingCart.pl (001), CartManager.pl (001), Product Templates
//                Negative input values now set to zero.

// 30-04-03 (002) Added optional checkxy function that checks for a minimum quantity before submitting.

var showtotals = true;			// whether to show a running total on the product page

function formfield(fname){		// look through all forms 'till one containing field "fname"
  var df = document.forms;		// simplified version - only return object fname
  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].name == (fname) ) 
        {
        return df[j].elements[l];
        }
      }
    }
  alert('Cannot find field ' + fname);  // shouldn't happen...
  return false;
}

function setrowcol(cols, rows, col, row, prodref, val){
  val = parseInt(val);
  if ( isNaN(val) ) val = 0;
  if ( val < 0 ) val = 0;
  var oldval = formfield('XY_' + prodref).value;
  var vals = oldval.split(',');
  vals[(cols * (col - 1)) + ( row - 1) + cols + rows + 4] = val;
  formfield('XY_' + prodref).value = vals.join(',');
  if ( showtotals )
    {
    var qtybase = (vals[0] - 0) + (vals[1] - 0) + 4;
    var qtyend = qtybase + (vals[0] * vals[1]);
    var qtytot = 0;
    for ( var I = qtybase; I < qtyend; I++ ) qtytot += (vals[I] - 0);
    formfield('rtot_' + prodref).value = qtytot;
    }
}


function qtytable(colvar,rowvar,prodref,initvalue,qtyavail){	// lloyd stock mod. extra qtyavail parameter
  // write a quantity / size matrix
  var availlist = qtyavail.split(',');				// lloyd stock mod
  prodref = prodref.replace(/.*"Q_(.*)" SIZE=.*/, "$1");	// 204d allow for duplicates
  var cols = colvar.split(',');
  var rows = rowvar.split(',');
  var colname = cols[0];
  var rowname = rows[0];
  var colcount = cols.length - 1;
  var rowcount = rows.length - 1;
  var rtot = 0;
  // setup col count, row count, col desc & sizes, row desc & sizes
  if ( initvalue == 'new' )
    {
    var initvalue = (cols.length - 1) + ',' + (rows.length - 1) + ',' + colvar  + ',' + rowvar + ',';
      for ( var I=1; I <= rowcount; I++ )
      {
      for ( var J=1; J <= colcount; J++ ) initvalue += "0,";
      }
    initvalue = initvalue.slice(0,-1);
    }
  initvalue = initvalue.replace(/"/g, '&#34;');				// allow for quotes in Row and Col names
  document.write('<input type=hidden name="XY_' + prodref + '" value="' + initvalue + '">');
  var sep = ((rowname != '' ) && (colname != '')) ? '/' : '';		// lloyd separator mod
  document.write('<table class="xyptable"><tr><td class="xyptopleft">' + rowname + sep + colname + '</td>');	// lloyd separator mod

  for ( var I=1; I <= colcount; I++ ) document.write('<td class="xypcoltitle">' + cols[I] + '</td>');
  document.write('<td>&nbsp;</td></tr>');	// lloyd stock mod	
// initvalue --> 4,3,Size,S,M,L,XL,Colour,Red,Green,Blue,0,0,0,0,0,0,0,0,0,0,0,0
  var oldvals = initvalue.split(',');
  var qtybase = colcount + rowcount + 4;
  for ( var I=1; I <= rowcount; I++ )
    {
    document.write('<tr><td class="xyprowtitle">' + rows[I] + '</td>');
    for ( var J=1; J <= colcount; J++ )
      {
      var val = oldvals[qtybase++];
      rtot = rtot + (val - 0);
      if ( val == 0 ) val = '';
      document.write('<td class="xypinputcell">'
		   + '<input type=text class="xypinputbox" value="' + val + '"'
                   + ' onchange="setrowcol(' + colcount + ',' + rowcount + ',' + I + ',' + J + ',\'' + prodref + '\',this.value)"'
                   + ' onKeypress="return ((event.which ? event.which : event.keyCode) != 13)"'		// disable enter key
                   + '></td>');
      }
    var thisstock = availlist[I - 1] ? availlist[I - 1] : '&nbsp;';	// lloyd stock mod
    document.write('<td> (Availability: ' + thisstock + ')</td>');			// lloyd stock mod
    document.write('</tr>');
    }

  if ( showtotals )
    {
    document.write('<tr><td class="xyptotaltitle">Total</td>');
    document.write('<td class="xyprtotalcell" colspan="' + (colcount + 1) + '">');	// lloyd stock mod
    document.write('<input class="xyprtotal" type=text name="rtot_' + prodref + '" readonly value="' + rtot + '"'
                 + ' onKeypress="return ((event.which ? event.which : event.keyCode) != 13)"'		// disable enter key 
                 + '></td></tr>');
    }

  document.write('</table>');
}

function qtyinit(colvar,rowvar,prodref,initvalue){
  // write a quantity / size matrix
  prodref = prodref.replace(/.*"Q_(.*)" VALUE=.*/, "$1");	// 204d allow for duplicates
  var cols = colvar.split(',');
  var rows = rowvar.split(',');
  var colname = cols[0];
  var rowname = rows[0];
  var colcount = cols.length - 1;
  var rowcount = rows.length - 1;
  if ( initvalue == 'new' )
    {
    var initvalue = (cols.length - 1) + ',' + (rows.length - 1) + ',' + colvar  + ',' + rowvar + ',';
      for ( var I=1; I <= rowcount; I++ )
      {
      for ( var J=1; J <= colcount; J++ ) initvalue += "0,";
      }
    initvalue = initvalue.slice(0,-1);
    }
  initvalue = initvalue.replace(/"/g, '&#34;');				// allow for quotes in Row and Col names
  document.write('<input type=hidden name="XY_' + prodref + '" value="' + initvalue + '">');
}

function checkxy(prodref, minval){
  var oldval = formfield('XY_' + prodref).value;
  var vals = oldval.split(',');
  var qtybase = (vals[0] - 0) + (vals[1] - 0) + 4;
  var qtyend = qtybase + (vals[0] * vals[1]);
  var qtytot = 0;
  for ( var I = qtybase; I < qtyend; I++ ) qtytot += (vals[I] - 0);
  if ( qtytot >= minval ) return true;
  alert ('Minimum quantity is ' + minval);
  return false;
}
