/* This notice must be untouched at all times.

Copyright (c) 2008 Albrecht Stoecklein. All rights reserved.

This program is made available in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License
at http://www.gnu.org/copyleft/gpl.html for more details.
*/

var RawCRCFloorTimberFrame;
var RawCRCFloorSteelFrame;
var RawCRCFloorConcreteFrame;
var RawCRCFloorLining;
var RawCRCFloorAirgap;
var RawCRCRoofTimberFrame;
var RawCRCRoofSteelFrame;
var RawCRCRoofLining;
var RawCRCRoofAirgap;
var RawCRCWallTimberFrame;
var RawCRCWallSteelFrame;
var RawCRCWallLining;
var RawCRCWallAirgap;
var RawCRCStrapping;
var RawCRCSolidTimber;
var RawCRCSolidMasonry;
var RawCRCICF;
var RawCRCRoofing;
var RawCRCCladding;
var RawCRCAirBarrier;
var RawCRCVeneer;
var RawCRCFlooring;
var RawCRCThermalBreak;
var RawCRCFlooring;
var RawCRCConstruction;

var XMLConstr;
var MyConstrXML = new Array();
var FloorTFOptionString;
var FloorSFOptionString;
var FloorCFOptionString;
var FloorLinOptionString;
var FloorAirgapOptionString;
var RoofTFOptionString;
var RoofSFOptionString;
var RoofLinOptionString;
var RoofAirgapOptionString;
var WallSFOptionString;
var WallTSOptionString;
var WallLinOptionString;
var WallAirgapOptionString;
var SolTOptionString;
var SolMasOptionString;
var RoofOptionString;
var CladOptionString;
var AirBarOptionString;
var VenrOptionString;
var FloOptionString;
var ThermBOptionString;
var SusFloorString;
var SlabFloorString;
var FloorSteelFrString;
var RoofSteelFrString;
var CurrentId;

function newWindow(file,window) {
    var MyWindow=open(file,window,'xscrollbars=yes xwidth=800');
    if (MyWindow.opener == null) MyWindow.opener = self;
}

function LoadRawXMLs() { // Load all XML files into memory
  RawCRCFloorTimberFrame = getFile('CRCFloorTimberFrame.xml');
  RawCRCFloorSteelFrame = getFile('CRCFloorSteelFrame.xml');
  RawCRCFloorConcreteFrame = getFile('CRCFloorConcreteFrame.xml');
  RawCRCFloorLining = getFile('CRCFloorLining.xml');
  RawCRCFloorAirgap = getFile('CRCFloorAirgap.xml');
  RawCRCRoofTimberFrame = getFile('CRCRoofTimberFrame.xml');
  RawCRCRoofSteelFrame = getFile('CRCRoofSteelFrame.xml');
  RawCRCRoofLining = getFile('CRCRoofLining.xml');
  RawCRCRoofAirgap = getFile('CRCRoofAirgap.xml');
  RawCRCWallTimberFrame = getFile('CRCWallTimberFrame.xml');
  RawCRCWallSteelFrame = getFile('CRCWallSteelFrame.xml');
  RawCRCWallLining = getFile('CRCWallLining.xml');
  RawCRCWallAirgap = getFile('CRCWallAirgap.xml');
  RawCRCStrapping = getFile('CRCStrapping.xml');
  RawCRCSolidTimber = getFile('CRCSolidTimber.xml');
  RawCRCSolidMasonry = getFile('CRCSolidMasonry.xml');
  RawCRCICF = getFile('CRCICF.xml');
  RawCRCRoofing = getFile('CRCRoofing.xml');
  RawCRCVeneer = getFile('CRCVeneer.xml');
  RawCRCVeneerCladding = RawCRCVeneer.replace(/<Veneer/g, "<Cladding");
  RawCRCVeneerCladding = RawCRCVeneerCladding.replace(/<\/Veneer/g, "</Cladding");
  RawCRCCladding = getFile('CRCCladding.xml') + RawCRCVeneerCladding;
  RawCRCAirBarrier = getFile('CRCAirBarrier.xml');
//  RawCRCCladding = getFile('CRCCladding.xml');
  RawCRCFlooring = getFile('CRCFlooring.xml');
  RawCRCThermalBreak = getFile('CRCThermalBreak.xml');
  RawCRCFlooring = getFile('CRCFlooring.xml');
  RawCRCConstruction = getFile('CRCConstruction.xml');
}

function getFile(filename) { //load one XML file 
  oxmlhttp = null;
  try
    { oxmlhttp = new XMLHttpRequest();
      oxmlhttp.overrideMimeType("text/xml");
    }
  catch(e)
    { try
        { oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch(e)
        { return null;
        }
    }
  if(!oxmlhttp) return null;
  try
    { oxmlhttp.open("GET",filename,false);
      oxmlhttp.send(null);
    }
  catch(e)
    { return null;
    }
  return oxmlhttp.responseText;
}

function CreateConstructions(k) { // Call all HTML Option code creators and call Construction HTML Option code creator 
  onConstrDownloadDone(RawCRCConstruction, k);
}

function ProcessXMLFile(m, k, MainTag, DataTag) { //create html option code from XMLs
 	XML = m;
  OptionString = '';
  ThisArray = XML.split("<" + MainTag + " name=\"");
  for (i=1; i<(ThisArray.length); i++) {
    ThisArrayName=ThisArray[i].split("\"")[0];
    ThisArrayData=ThisArray[i].split("<" + DataTag + ">")[1];
    ThisArrayData=ThisArrayData.split("</" + DataTag + ">")[0];
    OptionString = OptionString + '<Option value=\'' + ThisArrayData + '\'>' + ThisArrayName;
  }
  OptionString = '<select id=\"idOpt' + MainTag + '_' + k + '\" onchange=\"ParseXML(\'' + k + '\')\">' + OptionString + '</select>';
//alert(OptionString);
  return OptionString;
}


function SubRHTML(k) {
  ThisSubRHTML = '<table style=\'text-align:left; display:inline-table\'>';
  ThisSubRHTML += '<tr><td>Suspended floor area [m&sup2;]:</td><td><INPUT class=InputValue id=\'idTxtASusFloor_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisSubRHTML += '<tr><td>Perimeter length [m]:</td><td><INPUT class=InputValue id=\'idTxtLPerimSusFloor_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisSubRHTML += '<tr><td>Perimeter height [m]:</td><td><INPUT class=\'InputValue\' id=\'idTxtHPerimSusFloor_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisSubRHTML += '<tr><td>Perimeter type:</td><td><select id=\'idPerimType_' + k + '\' onchange=\'ParseXML("' + k + '")\'><option value=\'0\' selected>Exposed floor (pole house)</option><option value=\'0.057\'>Continuous perimeter wall (exposed)</option><option value=\'0.1\'>Continuous perimeter wall (sheltered)</option></select></td></tr></table>';
  return ThisSubRHTML;
}

function SlabRHTML(k) {
  ThisSlabRHTML = '<table style=\'text-align:left; display:inline-table\'>';
  ThisSlabRHTML += '<tr><td>Slab floor area [m&sup2;]:</td><td><INPUT class=InputValue id=\'idTxtASlabFloor_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisSlabRHTML += '<tr><td>Perimeter length [m]:</td><td><INPUT class=InputValue id=\'idTxtLPerimSlabFloor_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisSlabRHTML += '<tr><td>External wall thickness [mm]:</td><td><INPUT class=InputValue id=\'idTxtExtWallT_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'><IMG id=\'idHintExtWallT\'></td></tr>';
  ThisSlabRHTML += '<tr><td>Soil conductivity [W/m ºC]:</td><td><INPUT class=InputValue id=\'idTxtSoilCond_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\' value=\'1.2\'><IMG id=\'idHintSoilCond\'></td></tr>';
  ThisSlabRHTML += '<tr><td>Underslab insulation:</td><td><select id=\'idUnderSlabInsType_' + k + '\' onchange=\'ParseXML("' + k + '")\'><option value=\'none\' selected>none</option><option value=\'perim\'>Perimeter (1.2m)</option><option value=\'total\'>Total slab</option></select></td><td>Insulation0Product: </td><td>Insulation0R</td><td><IMG id=\'idHintUnderSlabInsulation\'></td></tr>';
  ThisSlabRHTML += '<tr><td>Piles Footings:</td><td>Number:&nbsp;<INPUT id=\'TxtPileNo_' + k + '\' style=\'width:40px\' onkeyup=\'ParseXML("' + k + '")\'></td><td colspan=2>Penetration Diameter:&nbsp;<INPUT id=\'TxtPileDiam_' + k + '\' style=\'width:40px\' onkeyup=\'ParseXML("' + k + '")\'>mm</td></tr>';
  ThisSlabRHTML += '<tr><td>Slab edge insulation:</td><td><select id=\'idSlabEdgeInsType_' + k + '\' onchange=\'ParseXML("' + k + '")\'><option value=\'none\' selected>none</option><option value=\'internal\'>Internal</option><option value=\'external\'>External</option></select></td><td>Insulation1Product: </td><td>Insulation1R</td><td><IMG id=\'idHintSlabEdgeInsulation\'></td></tr>';
  ThisSlabRHTML += '</td></tr></table>';
  return ThisSlabRHTML;
}

function DownlightHTML(k) {
  ThisDownlightHTML = '<table style=\'text-align:left; display:inline-table\'>';
  ThisDownlightHTML += '<tr><td>Ceiling area [m&sup2;]:<INPUT class=InputValue id=\'idTxtADownlightCeiling_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td><td>&nbsp;</td>';
  ThisDownlightHTML += '<td>Number of downlights:<INPUT class=InputValue id=\'idTxtDownlightCount_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td><td>&nbsp;</td>';
  ThisDownlightHTML += '<td>Clearance from lamp holder side [m]:<INPUT class=InputValue id=\'idTxtDownlightClearance_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'><IMG id=\'idHintDownLs\'></td></tr>';
  return ThisDownlightHTML;
}

function CustomSteelFrameRHTML(k) {
  ThisCustomSteelFrameRHTML = '<table style=\'text-align:left; display:inline-table\'>';
  ThisCustomSteelFrameRHTML += '<tr><td>Steel gauge [mm]:</td><td><INPUT class=InputValue id=\'idTxtSteelGauge_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisCustomSteelFrameRHTML += '<tr><td>Flange width [mm]:</td><td><INPUT class=InputValue id=\'idTxtFlange_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'></td></tr>';
  ThisCustomSteelFrameRHTML += '<tr><td>Insulation thickness [mm]:</td><td><INPUT class=InputValue id=\'idTxtInsulationT_' + k + '\' onkeyup=\'ParseXML("' + k + '")\' onfocus=\'ParseXML("' + k + '")\'><IMG id=\'idHintInsulationT\'></td></tr>';
  ThisCustomSteelFrameRHTML += '</td></tr></table>';
  return ThisCustomSteelFrameRHTML;
}

function onConstrDownloadDone(m, k) {
 	XMLConstr = m;
  FillConstrListIHTML(k);
}

function FillConstrListIHTML(k) { //Create the construction option HTML and calls the first default construction calculation 
  var ConstrNames = new Array();
  var OptionString = "";
  ConstrNames = XMLConstr.split("<Construction name=\"");
  OptionString = '<Option value=\"1\">';
  for (i=1; i<(ConstrNames.length); i++) {
    if(XMLExtract(ConstrNames[i], "ElementType") == ElementType) {
      ConstrNames[i]=ConstrNames[i].split("\"")[0];
      OptionString = OptionString + '<Option value=' + eval(i+1) + '>' + ConstrNames[i];};
    }
  OptionString = '<select id=\"OptConstr_' + k + '\" onchange=\"PickConstr(\'' + k + '\');\">' + OptionString + '</select>';
  document.getElementById("idOptConstr_" + k).innerHTML = OptionString;
	PickConstr(k);
};

function PickConstr(k){ // display the construction structure table
  DOMOptConstr = document.getElementById("OptConstr_" + k);  
  if (DOMOptConstr.value > 1) { // a construction has been selected
		MyConstrXML[k] = XMLConstr.split("<Construction")[DOMOptConstr.value-1];
	  MyConstrXML[k] = "<Construction" + XMLConstr.split("<Construction")[DOMOptConstr.value-1];
		ElementType = MyConstrXML[k].split("</ElementType>")[0];
		ElementType = ElementType.split("<ElementType>")[1];
    DisplayStructure(k); // display the structure
    document.getElementById("idResult_" + k).style.display = '';
	}	else { // no construction is selected
    MyConstrXML[k] = "";
		//ElementType = "Floor";
    //document.getElementById("idResult_" + k).style.display = 'none';
    document.getElementById("idTxtConstrR_" + k).value = 0;
    document.getElementById("RTable_" + k).innerHTML = '';
	}
}

function DisplayStructure(k) { // replace the XML for the structure with HTML tags and insert the option HTML elements & calls the parsing function
var String;
  String = MyConstrXML[k].split("</Details>")[0];
  String = String.split("<Details>")[1];
  String = RemoveLineBreaks(String);
  String = Replace(String, 'SusFloor', '<div id=\'idSusFloor_' + k + '\' style=\'display:inline\'>Insulation value of the subfloor space<br>' + SubRHTML(k) + '</div>');
  String = Replace(String, 'SlabFloor', '<div id=\'idSlabFloor_' + k + '\' style=\'display:inline\'>Insulation value of the slabfloor<br>' + SlabRHTML(k) + '</div>');
  String = Replace(String, 'Downlight', '<div id=\'idDownlight_' + k + '\' style=\'display:inline\'>Recessed downlights<br>' + DownlightHTML(k) + '</div>');
  String = Replace(String, 'CustomSFrame', '<div id=\'idCustomSteelFrame_' + k + '\' style=\'display:inline\'>Frame<br>' + CustomSteelFrameRHTML(k) + '</div>');
  for(i=0; i<5; i++) {
    String = Replace(String, 'Insulation' + i + 'Product', '<div id=\'idInsulation' + i + 'Product_' + k + '\' style=\'display:inline\'>Insulation</div>');
  }
//  String = Replace(String, 'InsulationAFProduct', '<div id=\'idInsulationAFProduct_' + k + '\' style=\'display:inline\'>Optional additional insulation layer covering all framing and any insulation between the framing</div>');
  String = Replace(String, '<Layer', '<tr><td class=\'tdLayer\' >');// Space in tdLayer\"_>' is important!!!
  String = Replace(String, '</Layer>', '</td></tr>');
  String = Replace(String, '<Process', '<hr><tr><td class=\'tdLayer\' >');// Space in tdLayer\"_>' is important!!!
  String = Replace(String, '</Process>', '</td></tr>');
  String = Replace(String, '<Composite>', '<br><table id=\'composite\' style=\'width:100%\'><tr style=\'width:100%\'>');
  String = Replace(String, '</Composite>', '</tr></table>');
  String = Replace(String, '<Component>', '<td style=\'width:50%\'><table id=\'component\' style=\'width:100%\'><tr><td style=\'text-align:center; width:100%\'>');
  String = Replace(String, '</Component>', '</td></tr></table></td>');
  String = ReplaceProp(String, 'R', 'SolidTimberR', '<div id=\'idSolidTimberR_' + k + '\' style=\'display:inline\'>SolidTimberR</div>');
  String = ReplaceProp(String, 'R', 'SolidMasonryR', '<div id=\'idSolidMasonryR_' + k + '\' style=\'display:inline\'>SolidMasonryR</div>');
  String = ReplaceProp(String, 'R', 'ICFR', '<div id=\'idICFR_' + k + '\' style=\'display:inline\'>ICFR</div>');
  String = ReplaceProp(String, 'R', 'RoofingR', '<div id=\'idRoofingR_' + k + '\' style=\'display:inline\'>RoofingR</div>');
  String = ReplaceProp(String, 'R', 'CladdingR', '<div id=\'idCladdingR_' + k + '\' style=\'display:inline\'>CladdingR</div>');
  String = ReplaceProp(String, 'R', 'AirBarrierR', '<div id=\'idAirBarrierR_' + k + '\' style=\'display:inline\'>AirBarrierR</div>');
  String = ReplaceProp(String, 'R', 'VeneerR', '<div id=\'idVeneerR_' + k + '\' style=\'display:inline\'>VeneerR</div>');
  String = ReplaceProp(String, 'R', 'FloorLiningR', '<div id=\'idFloorLiningR_' + k + '\' style=\'display:inline\'>FloorLiningR</div>');
  String = ReplaceProp(String, 'R', 'WallLiningR', '<div id=\'idWallLiningR_' + k + '\' style=\'display:inline\'>WallLiningR</div>');
  String = ReplaceProp(String, 'R', 'RoofLiningR', '<div id=\'idRoofLiningR_' + k + '\' style=\'display:inline\'>RoofLiningR</div>');
  String = ReplaceProp(String, 'R', 'FloorAirgapR', '<div id=\'idFloorAirgapR_' + k + '\' style=\'display:inline\'>FloorAirgapR</div>');
  String = ReplaceProp(String, 'R', 'WallAirgapR', '<div id=\'idWallAirgapR_' + k + '\' style=\'display:inline\'>WallAirgapR</div>');
  String = ReplaceProp(String, 'R', 'RoofAirgapR', '<div id=\'idRoofAirgapR_' + k + '\' style=\'display:inline\'>RoofAirgapR</div>');
  String = ReplaceProp(String, 'R', 'FlooringR', '<div id=\'idFlooringR_' + k + '\' style=\'display:inline\'>FlooringR</div>');
  String = ReplaceProp(String, 'R', 'ThermalBreakR', '<div id=\'idThermalBreakR_' + k + '\' style=\'display:inline\'>ThermalBreakR</div>');
  String = ReplaceProp(String, 'R', 'StrappingR', '<div id=\'idStrappingR_' + k + '\' style=\'display:inline\'>StrappingR</div>');
  String = ReplaceProp(String, 'R', 'FloorFrameR', '<div id=\'idFloorFrameR_' + k + '\' style=\'display:inline\'>FloorFrameR</div>');
  String = ReplaceProp(String, 'R', 'WallFrameR', '<div id=\'idWallFrameR_' + k + '\' style=\'display:inline\'>WallFrameR</div>');
  String = ReplaceProp(String, 'R', 'RoofFrameR', '<div id=\'idRoofFrameR_' + k + '\' style=\'display:inline\'>RoofFrameR</div>');
  String = ReplaceProp(String, 'R', 'SubR', '<div id=\'idSubR_' + k + '\' style=\'display:inline\'>SubR</div>');
  String = ReplaceProp(String, 'R', 'SlabR', '<div id=\'idSlabR_' + k + '\' style=\'display:inline\'>SlabR</div>');
  String = ReplaceProp(String, 'R', 'CustomSFR', '<div id=\'idCustomSFR_' + k + '\' style=\'display:inline\'>CustomSFR</div>');
//  String = ReplaceProp(String, 'R', 'InsulationAFR', '<input id=\'TxtInsulationAFR_' + k + '\' style=\'width:50px\' onkeyup=\' document.getElementById("idInsulationAFProduct_' + k + '").innerHTML = "Optional additional insulation layer covering all framing and any insulation between the framing"; ParseXML("' + k + '")\' onfocus=\'CurrentId=this.id; ParseXML("' + k + '")\'>');
  String = ReplaceProp(String, 'A', 'StrappingA', 'Strapping Area:&nbsp;<div id=\'idStrappingA_' + k + '\' style=\'display:inline\'>StrappingA</div>');
  String = ReplaceProp(String, 'A', 'FloorFrameA', 'Frame Area:&nbsp;<div id=\'idFloorFrameA_' + k + '\' style=\'display:inline\'>FloorFrameA</div>');
  String = ReplaceProp(String, 'A', 'WallFrameA', 'Frame Area:&nbsp;<div id=\'idWallFrameA_' + k + '\' style=\'display:inline\'>WallFrameA</div>');
  String = ReplaceProp(String, 'A', 'RoofFrameA', 'Frame Area:&nbsp;<div id=\'idRoofFrameA_' + k + '\' style=\'display:inline\'>RoofFrameA</div>');
  String = ReplaceProp(String, 'A', 'StrappingCavA', 'Cavity Area:&nbsp;<div id=\'idStrappingCavA_' + k + '\' style=\'display:inline\'>StrappingCavA</div>');
  String = ReplaceProp(String, 'A', 'FloorCavA', 'Cavity Area:&nbsp;<div id=\'idFloorCavA_' + k + '\' style=\'display:inline\'>FloorCavA</div>');
  String = ReplaceProp(String, 'A', 'WallCavA', 'Cavity Area:&nbsp;<div id=\'idWallCavA_' + k + '\' style=\'display:inline\'>WallCavA</div>');
  String = ReplaceProp(String, 'A', 'RoofCavA', 'Cavity Area:&nbsp;<div id=\'idRoofCavA_' + k + '\' style=\'display:inline\'>RoofCavA</div>');
// the next four commands are now included in the ReplaceProp function
/*  String = Replace(String, '<R>', '<br><font class=\'FontR\' style=\'font-style:italic\' >&nbsp R-value: ');
  String = Replace(String, '</R>', '&nbsp </font>');
  String = Replace(String, '<A>', '<font class=\'FontArea\' style=\'font-style:italic; text-align:center\' >');
  String = Replace(String, '</A>', '%&nbsp </font>'); */
  String = Replace(String, '<Hint>', '<IMG id=\'idHint');
  String = Replace(String, '</Hint>', '\' > ');
  String = Replace(String, 'name="', '');
  String = Replace(String, '">', '');
  for(i=0; i<5; i++) {
    String = Replace(String, 'Insulation' + i + 'R', '<input id=\'TxtInsulation' + i + 'R_' + k + '\' style=\'width:50px\' onkeyup=\'document.getElementById("idInsulation' + i + 'Product_' + k + '").innerHTML = "Insulation"; ParseXML("' + k + '")\' onfocus=\'CurrentId=this.id; ParseXML("' + k + '")\'><input id=\'TxtDownLightDownrating' + i + 'R_' + k + '\' style=\'display:none\'');
  }
  String = Replace(String, 'SolidTimberSelect', 'Solid&nbsp;Timber ' + ProcessXMLFile(RawCRCSolidTimber, k, 'SolidTimber', 'R'));
  String = Replace(String, 'SolidMasonrySelect', 'Solid&nbsp;Wall ' + ProcessXMLFile(RawCRCSolidMasonry, k, 'SolidMasonry', 'R'));
  String = Replace(String, 'ICFSelect', 'Insulated&nbsp;Concrete&nbsp;Form ' + ProcessXMLFile(RawCRCICF, k, 'ICF', 'R'));
  String = Replace(String, 'RoofingSelect', 'Roofing ' + ProcessXMLFile(RawCRCRoofing, k, 'Roofing', 'R'));
  String = Replace(String, 'CladdingSelect', 'Cladding ' + ProcessXMLFile(RawCRCCladding, k, 'Cladding', 'R'));
  String = Replace(String, 'AirBarrierSelect', 'Air Barrier ' + ProcessXMLFile(RawCRCAirBarrier, k, 'AirBarrier', 'R'));
  String = Replace(String, 'VeneerSelect', 'Veneer ' + ProcessXMLFile(RawCRCVeneer, k, 'Veneer', 'R'));
  String = Replace(String, 'FloorLiningSelect', 'Floor&nbsp;Lining ' + ProcessXMLFile(RawCRCFloorLining, k, 'FloorLining', 'R'));
  String = Replace(String, 'RoofLiningSelect', 'Ceiling&nbsp;Lining' + ProcessXMLFile(RawCRCRoofLining, k, 'RoofLining', 'R'));
  String = Replace(String, 'WallLiningSelect', 'Wall&nbsp;Lining ' + ProcessXMLFile(RawCRCWallLining, k, 'WallLining', 'R'));
  String = Replace(String, 'FloorAirgapSelect', 'still&nbsp;Airgap&nbsp;between Flooring/Insulation&nbsp;and&nbsp;Lining' + ProcessXMLFile(RawCRCFloorAirgap, k, 'FloorAirgap', 'R'));
  String = Replace(String, 'WallAirgapSelect', 'still&nbsp;Airgap ' + ProcessXMLFile(RawCRCWallAirgap, k, 'WallAirgap', 'R'));
  String = Replace(String, 'RoofAirgapSelect', 'still&nbsp;Airgap ' + ProcessXMLFile(RawCRCRoofAirgap, k, 'RoofAirgap', 'R'));
  String = Replace(String, 'FlooringSelect', 'Flooring ' + ProcessXMLFile(RawCRCFlooring, k, 'Flooring', 'R'));
  String = Replace(String, 'ThermalBreakSelect', 'Thermal&nbsp;Break ' + ProcessXMLFile(RawCRCThermalBreak, k, 'ThermalBreak', 'R'));
  String = Replace(String, 'FloorTimberFrame&amp;Cavity', 'Timber&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCFloorTimberFrame, k, 'FloorFrame', 'Details'));
  String = Replace(String, 'FloorSteelFrame&amp;Cavity', 'Steel&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCFloorSteelFrame, k, 'FloorFrame', 'Details'));
  String = Replace(String, 'FloorConcreteFrame&amp;Cavity', 'Concrete&nbsp;Joists&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCFloorConcreteFrame, k, 'FloorFrame', 'Details'));
  String = Replace(String, 'RoofTimberFrame&amp;Cavity', 'Timber&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCRoofTimberFrame, k, 'RoofFrame', 'Details'));
  String = Replace(String, 'RoofSteelFrame&amp;Cavity', 'Steel&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCRoofSteelFrame, k, 'RoofFrame', 'Details'));
  String = Replace(String, 'WallTimberFrame&amp;Cavity', 'Timber&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCWallTimberFrame, k, 'WallFrame', 'Details'));
  String = Replace(String, 'WallSteelFrame&amp;Cavity', 'Steel&nbsp;Frame&nbsp;&amp;&nbsp;Cavity ' + ProcessXMLFile(RawCRCWallSteelFrame, k, 'WallFrame', 'Details'));
  String = Replace(String, 'Strapping&amp;Cavity', 'Strapping ' + ProcessXMLFile(RawCRCStrapping, k, 'Strapping', 'Details'));
  String = '<table class=\"TableConstr\">' + String + '</table><p>'
  document.getElementById("RTable_" + k).innerHTML = String;
  FillHints(); 
  ParseXML(k); 
}

function SaveConstructions(ThisElementType) {
    for (k=1; k<ConstructionCount[ThisElementType]+1; k++) {
      opener.document.getElementById('id' + ThisElementType + 'ConstrXML' + k).value = MyConstrXML[ThisElementType + k];
      opener.document.getElementById('id' + ThisElementType + 'Constr' + k).value = document.getElementById('RTable_' + ThisElementType + k).innerHTML;
    }
    var element;
    var ControlStr = "";
    for (var i=0; i<document.FrmConstruction.elements.length; i++) {
      element = document.FrmConstruction.elements[i];
      switch (element.type) {
        case 'text':
          ThisValue = element.value;
          break;
        case 'select-one':
          ThisValue = element.selectedIndex;
          break;
      }
      ControlStr += "<Control><Id>" + element.id + "<Id><Type>" + element.type + "<Type><Value>" + ThisValue + "<Value>";  
      //alert(element.id + ': ' + ThisValue + '  ' + element.type);
    }
    opener.document.getElementById('id' + ThisElementType + 'ConstrControls').value = ControlStr;
}

function LoadConstructions(ThisElementType, Fixed) {
  for (k=1; k<ConstructionCount[ThisElementType]+1; k++) {
    MyConstrXML[ThisElementType + k] = opener.document.getElementById('id' + ThisElementType + 'ConstrXML' + k).value;
    if(MyConstrXML[ThisElementType + k] != "") {
      document.getElementById('idTblConstr' + ThisElementType).style.pageBreakBefore = "always";
      var Temp = MyConstrXML[ThisElementType + k].split('<ThisElementType>');
      Temp = Temp[0].split(ThisElementType + ': ');  
      Temp = Temp[1].split('">');
      RetrievedConstruction = Temp[0]; 
      document.getElementById('SavedDetail_' + ThisElementType + k).style.display = RowDisplay; 
      document.getElementById('idRetrievedName_' + ThisElementType + k).innerHTML = RetrievedConstruction;
      ThisConstrStr = ReplaceAll(opener.document.getElementById('id' + ThisElementType + 'Constr' + k).value, 'Â', '');
      ThisConstrStr = ReplaceAll(ThisConstrStr, 'ï¿½', '®');
      document.getElementById('RTable_' + ThisElementType + k).innerHTML = ThisConstrStr;
    } else {
      document.getElementById('SavedDetail_' + ThisElementType + k).style.display = 'none'; 
      document.getElementById('idRetrievedName_' + ThisElementType + k).innerHTML = "";
      document.getElementById('RTable_' + ThisElementType + k).innerHTML = "";
    }      
  }
  var Controls = opener.document.getElementById('id' + ThisElementType + 'ConstrControls').value.split('<Control>');
  for (var ki=1; ki<Controls.length; ki++) {
    ThisId = Controls[ki].split("<Id>")[1];
    if(ThisId.split('_')[0] != 'OptConstr') {
      ThisType = Controls[ki].split("<Type>")[1];
      ThisValue = Controls[ki].split("<Value>")[1];
//      alert('Id: ' + ThisId + ' Type: ' + ThisType+ ' Value: ' + ThisValue);
      document.getElementById(ThisId).disabled = Fixed;
      document.getElementById(ThisId).style.backgroundColor = 'white';
      document.getElementById(ThisId).style.color = 'black';
      switch (ThisType) {
        case 'text':
          document.getElementById(ThisId).value = ThisValue;
          break;
        case 'select-one':
          document.getElementById(ThisId).selectedIndex = ThisValue;
          break;
      }
    }
  }
  for (k=1; k<ConstructionCount[ThisElementType]+1; k++) {
    ParseXML(ThisElementType + k);
  }
}  

function FillHints() {
  var myImages = document.getElementsByTagName('img');
  for (var i=1; i<myImages.length; i++) {
    if (myImages[i].id.substring(0,6) == "idHint") {
//    alert(myImages[i].id);
      if(document.getElementById("Rep" + myImages[i].id)) {
        myImages[i].onmouseover = document.getElementById("Rep" + myImages[i].id).onmouseover;
        myImages[i].onmouseout = document.getElementById("Rep" + myImages[i].id).onmouseout;
        myImages[i].src = './Images/ismall.gif';
        myImages[i].height = '12';
        myImages[i].border = '0';      
      } else {
        myImages[i].style.display = 'none';
      }
    }
  }  
}

