﻿function createExtensionCalculator(selHeight,addHeading) {
  try {
    if (!selHeight) selHeight="single";
    document.write('<form class="extcalc" name="frmExtensionCalc" onsubmit="return calculateExtension(this);"><table border="0" cellpadding="0" cellspacing="0">');
    if (addHeading) document.write('<tr><td colspan="2"><h4>Extension Calculator</h4></td></tr>');
    document.write('<tr><td>What type of extension?</td><td><select size="1" name="calcHeight" id="calcHeight" onchange="changeHeight();">');
      document.write(getOption(selHeight,'single','Single Storey'));
      document.write(getOption(selHeight,'two','Two Storey'));
      document.write(getOption(selHeight,'loft','Loft Conversion'));
    document.write('</select></td></tr>');
    document.write('<tr><td>How deep will the extension be?</td><td><input type="text" size="5" maxlength="5" name="calcDepth" style="width:20px;" onchange="clearResults();" /> <b>m</b></td></tr>');
    document.write('<tr><td>How wide will the extension be?</td><td><input type="text" size="5" maxlength="5" name="calcWidth" style="width:20px;" onchange="clearResults();" /> <b>m</b></td></tr>');
    document.write('<tr><td id="calcTypeLabel"');
    if (selHeight=='loft') document.write(' style="color:#ccc;"');
    document.write('>Will it have a flat or pitched roof?</td><td><span id="calcTypeNA" style="color:#ccc;');
    if (selHeight!='loft') document.write('display:none;');
    document.write('">N/A</span><select size="1" name="calcType" id="calcType" onchange="clearResults();"');
    if (selHeight=='loft') document.write('style="display:none;"');
    document.write('><option value="flat">Flat Roof</option><option value="pitched" selected="selected">Pitched Roof</option></select></td></tr>');
    document.write('<tr><td colspan="2" style="text-align:center;" id="calcResult"></td></tr>');
    document.write('<tr><td colspan="2" style="text-align:center;"><input type="submit" name="go" value="Calculate Cost" /></td></tr>');
    document.write('</table>');
    document.write('<p id="calcNote" style="display:none;"><small>Given that most extensions are of brick and block construction, it is assumed that the extension will also be constructed in that way. The following calculation is for illustrative purposes only and does not include any fees, assumes a flat area of land upon which the extension is to be built on and does not include any contingency costs for ground remediation work (should they be needed). Also, we have not included any building cost variations you may experience in your part of the country.<br />NB These figures do not include kitchen and bathroom costs.<br />Orchard Planning Solutions is not responsible for any disparity between the estimated cost and the actual cost paid by you.</small></p>');
    document.write('</form>');
  } catch(e) {}
}
function clearResults() {
  try {
    var elRes = document.getElementById("calcResult");
    var elNote = document.getElementById("calcNote");
    elRes.innerHTML=''; elNote.style.display='none';
  } catch(e) {}
  return true;
}
function calculateExtension(f) {
  try {
    var elRes = document.getElementById("calcResult");
    var elNote = document.getElementById("calcNote");
    elRes.innerHTML=''; elNote.style.display='none';
    var d = f.calcDepth.value.toString(); var w = f.calcWidth.value.toString();
    var t = f.calcType.value.toString(); var h = f.calcHeight.value.toString();
    if (!(IsNumeric(d)&&IsNumeric(w))) {
      alert('Please enter valid Depth and Width values');
      return false;
    }
    var total=d*w; var res='<em>';
    switch (h) {
      case 'single': 
        res+='Single Storey, ';if(t=='flat')total*=1100;else total*=1200;
        break;
      case 'two':
        res+='Two Storey, ';total*=2;if(t=="flat")total*=690;else total*=750;
        break;
      case 'loft':
        res+='Loft Conversion';total*=610;
    }
    if(h!='loft')if(t=="flat")res+='Flat Roof';else res+='Pitched Roof';
    res+='</em><br />'+d+'m deep x '+w+'m wide = <b>'+formatCurrency(total)+'</b>';
    elRes.innerHTML='<div class="whatthismeans">'+res+'</div>'; elNote.style.display='block';
  } catch(e) {alert('Please enter valid Depth and Width values');}
  return false;
}
function changeHeight() {
  try {
    clearResults();
    var elType = document.getElementById("calcType");
    var elTypeNA = document.getElementById("calcTypeNA");
    var elTypeLabel = document.getElementById("calcTypeLabel");
    var elHeight = document.getElementById("calcHeight");
    var h = elHeight.value.toString();
    if (h == 'loft') {
      elType.style.display='none';
      elTypeNA.style.display='block';
      elTypeLabel.style.color='#ccc';
    } else {
      elType.style.display='block';
      elTypeNA.style.display='none';
      elTypeLabel.style.color='';
    }
  } catch(e) {}
  return true;
}