/*******************************************************************************************
 *  Script to create the price tables shown on the prices page and the availability page.
 *  NOTE: This script uses the DOM in XML files and the HTML pages. Not all browsers will
 *  read the DOM in the same way. XML files and HTML files have been adapted to render correctly
 *  in all tested browsers.
 ********************************************************************************************/
function loadPrices() {
  var priceDoc;
  // Use the standard DOM level 2 technique, if it is supported.
  if (document.implementation && document.implementation.createDocument) {
      // Create a new Document object
      priceDoc = document.implementation.createDocument("", "", null);
      // Specify what should happen when it finishes loading
      priceDoc.onload = function(  ) { getPrices(priceDoc); }
  }
  // Otherwise, use Microsoft's proprietary API for Internet Explorer
  else if (window.ActiveXObject) {
      priceDoc = new ActiveXObject("Microsoft.XMLDOM");   // Create doc
      priceDoc.onreadystatechange = function(  ) {              // Specify onload
          if (priceDoc.readyState == 4) getPrices(priceDoc);
      }
  }
  priceDoc.load("scripts/prices.xml"); // Start loading!
}

function getPrices(xmlDoc){
    var xmlPriceYear = xmlDoc.getElementsByTagName("priceyear");
    var docPriceTable = document.getElementById("priceTable");
    var yearTag = dataTag = 0;
    var priceTblRowCount = 1;


    // For each of the years in the XML file
    for (yearTag=0; yearTag<xmlPriceYear.length; yearTag++) {

        // Get the price year from the XML document
        var prYearVal = xmlPriceYear[yearTag].getAttribute("id");
        var prYearSlot = xmlPriceYear[yearTag].getAttribute("priceyearval");

        var dateRange = xmlPriceYear[yearTag].getElementsByTagName("daterange");

        if (dateRange != null) {
            priceTblRowCount = priceTblRowCount + 2;
            //Create the year header in the price table
            var prYearHead = docPriceTable.childNodes[1].childNodes[priceTblRowCount].childNodes[1];
            var prYearBold = document.createElement("b");
            prYearHead.className = "emph";
            prYearHead.insertBefore(prYearBold, prYearHead.firstChild);
            prYearHead.appendChild(document.createTextNode(xmlPriceYear[yearTag].getAttribute("id")));

            for (dataTag = 0; dataTag<dateRange.length; dataTag++) {
                // Increment the row counter to the next row.
                // NOTE: the increment is two because the DOM has comments to ensure cross browser compatibility.
                priceTblRowCount = priceTblRowCount + 2;
                // Get the table row from the HTML document and the cell positions in that row.
                var prTableRow = docPriceTable.childNodes[1].childNodes[priceTblRowCount];
                var prTblRowDate = prTableRow.childNodes[1];
                var prTblRowPrice = prTableRow.childNodes[3];

                // Get the start and end dates and the price for the current row.
                var startDate = dateRange[dataTag].childNodes[1].childNodes[1].childNodes[0].nodeValue;
                var endDate = dateRange[dataTag].childNodes[3].childNodes[1].childNodes[0].nodeValue;
                var newPrice = dateRange[dataTag].childNodes[5].childNodes[0].nodeValue;

                var prRowDateData = startDate + " - " + endDate; // Concatenate the start and end dates for the first cell in the row.
                prTblRowDate.appendChild(document.createTextNode(prRowDateData)); // Populate the first cell in the row.
                prTblRowPrice.appendChild(document.createTextNode(newPrice)); // Populate the second cell in the row.

            } // End of inner For dataTag loop
        } //End of If dateRange != null
    } // End of For yearTag loop
}