// Frame size calculation helper routines
// V300 23-09-04 No javascript code changes - V7 version
// V209 13-02-04 (only framesize.js changed - typo in glass price calc fixed) 
// V208 03-02-04 (only framesize.js changed) 
//  amended to skip passing Length info if FramePrice is zero.
//  and Allow for using a hidden field to force Glass Price to be calculated.
//  also use JavaScript variables minwidth minheight (default 100)
// V207 original shipped script

var minheight = 100;  // V208 added
var minwidth = 100;  // V208 added

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 topounds(nPounds)
  {
  var nCurrency, nWholeCurrency, nFraction, sFraction;
// REMOVED 02-03-05 FrameCreate Update 19  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 errormsg(msg){
  noerror = false;
  alert(msg);
}

function setprice(prodref)
  {
  var params = formfield('params_' + prodref).value.split(',');
  var frameprice = params[0];
  var glassprice = params[1];
  var mountprice = params[2];
  var hardboardprice = params[3];
  var foamcoreprice = params[4];
  var cutoutprice = params[5];
  var cuttingcharge = params[6]; 
  var nonreflect = params[7]; 
  noerror = true;
  var info = '';
  var width = formfield('wdth_' + prodref).value - 0;
  if ( isNaN(width) || (width < minwidth) ) 
   {
   width = minwidth;
   errormsg('Bad width - minimum is ' + minheight + 'mm');
   }
  var rwidth = Math.ceil(width - 0);
  formfield('wdth_' + prodref).value = rwidth;

  var height = formfield('hght_' + prodref).value - 0;
  if ( isNaN(height) || (height < minheight) )
   {
   height = minheight;
   errormsg('Bad height - minimum is ' + minheight + 'mm');
   }

  var rheight = Math.ceil(height);
  formfield('hght_' + prodref).value = rheight;
  var length = (rwidth * 2 + rheight * 2) / 1000;
  var rlength = topounds(length);
  var area = rwidth * rheight / 1000000;
  if ( (area != 0) && (area < 0.01) ) area = 0.01;          // minimum .01 sq m
  var rarea = topounds(area);
  info = 'Width: ' + rwidth + ' mm|Height: ' + rheight + ' mm';
  formfield('len_' + prodref).value = rlength;
  var rtotal = 0;

  // the cutting charge
  if ( cuttingcharge ) 
    {
    rtotal += cuttingcharge - 0;
    formfield('cct_' + prodref).value = cuttingcharge;
    info += '|Cutting Charge: £' + cuttingcharge;
    }

  // the frame length price
  // V208 amended to skip if zero cost
  if ( (frameprice - 0) > 0 )
    {
    var lprice = topounds(frameprice * rlength);
    rtotal += lprice - 0;
    info += '|Length: ' + rlength + ' m @ £' + frameprice + ' - £' + lprice;
    formfield('frt_' + prodref).value = lprice;
    }

  // the glass price (V208 patch on 1 line below)
  if ( formfield('gab_' + prodref).checked || (formfield('gab_' + prodref).value == "SHOW") ) 
    {
    formfield('ar1_' + prodref).value = rarea;
    var gprice = topounds(glassprice * rarea);
    formfield('gat_' + prodref).value = gprice;
    rtotal += gprice - 0;
    info += '|Glass: ' + rarea + ' sq m @ £' + glassprice + ' - £' + gprice;
    }
  else
    {
    formfield('ar1_' + prodref).value = '';
    formfield('gat_' + prodref).value = '';
    }

  // the non-reflective price
  if ( formfield('non_' + prodref).checked && formfield('gab_' + prodref).checked ) 
    {
    formfield('ar3_' + prodref).value = rarea;
    var noncost = topounds(glassprice * nonreflect)
    var nonprice = topounds(noncost * rarea);
    formfield('not_' + prodref).value = nonprice;
    rtotal += nonprice - 0;
    info += '|Non Reflective: ' + rarea + ' sq m @ £' + noncost + ' - £' + nonprice;
    }
  else
    {
    formfield('ar3_' + prodref).value = '';
    formfield('not_' + prodref).value = '';
    }

  // hardboard
  if ( formfield('hab_' + prodref).checked ) 
    {
    formfield('ar4_' + prodref).value = rarea;
    var hprice = topounds(hardboardprice * rarea);
    formfield('hat_' + prodref).value = hprice;
    rtotal += hprice - 0;
    info += '|Hardboard: ' + rarea + ' sq m @ £' + hardboardprice + ' - £' + hprice;
    }
  else
    {
    formfield('ar4_' + prodref).value = '';
    formfield('hat_' + prodref).value = '';
    }

  // foamcore
  if ( formfield('foc_' + prodref).checked ) 
    {
    formfield('ar5_' + prodref).value = rarea;
    var fprice = topounds(foamcoreprice * rarea);
    formfield('fot_' + prodref).value = fprice;
    rtotal += fprice - 0;
    info += '|Foamcore: ' + rarea + ' sq m @ £' + foamcoreprice + ' - £' + fprice;
    }
  else
    {
    formfield('ar5_' + prodref).value = '';
    formfield('fot_' + prodref).value = '';
    }

  // mountboard
  if ( formfield('mob_' + prodref).checked ) 
    {
    formfield('ar2_' + prodref).value = rarea;
    var mprice = topounds(mountprice * rarea);
    formfield('mot_' + prodref).value = mprice;
    rtotal += mprice - 0;
    info += '|Mountboard: ' + rarea + ' sq m @ £' + mountprice + ' - £' + mprice;
    }
  else
    {
    formfield('ar2_' + prodref).value = '';
    formfield('mot_' + prodref).value = '';
    }

  // cutout
  if ( formfield('cuo_' + prodref).checked && formfield('mob_' + prodref).checked ) 
    {

    // check for valid width
    var cutwidth = formfield('cuw_' + prodref).value;
    if ( isNaN(cutwidth) || (cutwidth < 0) ) errormsg('Bad cutout width') 
      else if ( cutwidth >= width ) errormsg('Cutout too wide')
        else if ( cutwidth < 10 ) errormsg('Cutout width too small - minimum is 10mm');

    // check for valid height
    var cutheight = formfield('cuh_' + prodref).value;
    if ( isNaN(cutheight) || (cutheight < 0) ) errormsg('Bad cutout height') 
      else if ( cutheight >= height ) errormsg('Cutout too high')
        else if ( cutheight < 10 )  errormsg('Cutout height too small - minimum is 10mm');

    formfield('cut_' + prodref).value = cutoutprice;
    rtotal += cutoutprice - 0;
    info += '|Cutout: W ' + cutwidth + 'mm x H ' + cutheight + 'mm - £' + cutoutprice;
    }
  else
    {
    formfield('cut_' + prodref).value = '';
    }

  formfield('tot_' + prodref).value = '£' + topounds(rtotal);
  formfield('inf_' + prodref).value = info;
  return noerror;
}

function handleEnter (field, event) {   // disable enter key for our size fields
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) return false; 
		return true;
}  
