function test() {
  return true;
}

var price_sheet_options = {};

loadXMLDoc("price_quote_system/price_sheet.xml", test, true);

addLoadEvent(grab_form_values);
addLoadEvent(submit_form);

function grab_form_values() {
  var selects = document.forms[0].getElementsByTagName("select");
  for (var i = 0; i < selects.length; i++) {
    selects[i].onchange = function() {
      this.disabled = true;
      switch(this.name) {
      case "media":
        price_sheet_options.media = this.value;
        qty_load();
        break;
      case "quantity":
        price_sheet_options.media_costs = this.value.split(",");
        price_sheet_options.media_costs[0] = parseFloat(price_sheet_options.media_costs[0]);
        price_sheet_options.media_costs[1] = parseInt(price_sheet_options.media_costs[1]);
        price_sheet_options.media_costs[2] = parseInt(price_sheet_options.media_costs[2]);
        case_load();
        break;
      case "casing":
        price_sheet_options.casing = this.value.split(",");
        price_sheet_options.casing[0] = parseFloat(price_sheet_options.casing[0]);
        price_sheet_options.casing[2] = parseInt(price_sheet_options.casing[2]);
        insert_load();
        break;
      case "insert":
        price_sheet_options.inserts = this.value.split(",");
        price_sheet_options.inserts[0] = parseFloat(price_sheet_options.inserts[0]);
        price_sheet_options.inserts[2] = parseInt(price_sheet_options.inserts[2]);
        price_sheet_options.inserts[3] = parseInt(price_sheet_options.inserts[3]);
        price_sheet_options.inserts[4] = parseFloat(price_sheet_options.inserts[4]);
        disc_label_load();
        break;
      case "disc_label":
        price_sheet_options.disc_label = this.value.split(",");
        price_sheet_options.disc_label[0] = parseFloat(price_sheet_options.disc_label[0]);
        break;
      }
    }
  }
}

// Used to load the quantity field.
function qty_load() {
  var media = resultXML.documentElement.childNodes[0];
  var media_selected = media.getElementsByTagName(price_sheet_options.media)[0];
  var quants = media_selected.getElementsByTagName("quantity");
  var qty_select = document.forms[0].getElementsByTagName("select")[1];
  for (var i = 0; i < quants.length; i++) {
    var the_text = quants[i].lastChild.nodeValue;
    var qty_opts = document.createElement("option");
    qty_opts.setAttribute("value", quants[i].getAttribute("unit_cost") + "," + the_text + "," + i);
    var qty = document.createTextNode(the_text);
    qty_opts.appendChild(qty);
    qty_select.appendChild(qty_opts);
  }
}

function case_load() {
  var casing = resultXML.documentElement.childNodes[1];
  var cases = casing.getElementsByTagName("case");
  var opt_casing = document.forms[0].getElementsByTagName("select")[2];
  for (var i = 0; i < cases.length; i++) {
    var available = cases[i].getElementsByTagName("availability")[0].lastChild.nodeValue;
    if (available.search(price_sheet_options.media) !== -1) {
      var the_text = cases[i].getAttribute("name");
      var case_opts = document.createElement("option");
      case_opts.setAttribute("value", cases[i].getAttribute("unit_cost") + "," + the_text + "," + i);
      var qty = document.createTextNode(the_text);
      case_opts.appendChild(qty);
      opt_casing.appendChild(case_opts);
    }
  }
}

function insert_load() {
  var cases = resultXML.documentElement.childNodes[1].childNodes[price_sheet_options.casing[2]];
  var inserts = cases.getElementsByTagName("insert");
  var opt_inserts = document.forms[0].getElementsByTagName("select")[3];
  for (var i = 0; i < inserts.length; i++) {
    var options = inserts[i].getElementsByTagName('option');
    var text_pt1 = inserts[i].getAttribute("name");
    for (var j = 0; j < options.length; j++) {
      var text_pt2 = options[j].getElementsByTagName("description")[0].lastChild.nodeValue;
      var the_text = (text_pt2 !== "none") ? text_pt1 + " (" + text_pt2 + ")" : text_pt1;
      var setup_proofs = parseFloat(options[j].getAttribute("setup_proofs"));
      var unit_cost = parseFloat(options[j].getElementsByTagName("quantity")[price_sheet_options.media_costs[2]].getAttribute("unit_cost"));
      var new_option = document.createElement("option");
      new_option.setAttribute("value", unit_cost + "," + the_text + "," + i + "," + j + "," + setup_proofs);
      var new_textNode = document.createTextNode(the_text);
      new_option.appendChild(new_textNode);
      opt_inserts.appendChild(new_option);
    }
  }
}

function disc_label_load() {
  var disc_labels = resultXML.documentElement.childNodes[2].childNodes;
  var opt_disc_labels = document.forms[0].getElementsByTagName("select")[4];
  for (var i = 0; i < disc_labels.length; i++) {
    var the_text = disc_labels[i].getAttribute("name");
    var unit_cost = disc_labels[i].getAttribute("unit_cost");
    var new_element = document.createElement("option");
    new_element.setAttribute("value", unit_cost + "," + the_text);
    var new_text = document.createTextNode(the_text);
    new_element.appendChild(new_text);
    opt_disc_labels.appendChild(new_element);
  }
}

function display_price() {
  var media_cost  = Math.round(price_sheet_options.media_costs[0] * price_sheet_options.media_costs[1]);
  var case_cost   = Math.round(price_sheet_options.casing[0] * price_sheet_options.media_costs[1]);
  var insert_cost = Math.round(price_sheet_options.inserts[0] * price_sheet_options.media_costs[1]);
  var total       = media_cost + case_cost + insert_cost;
  alert("Quantity requested: " + price_sheet_options.media_costs[1] + "\n" +
        price_sheet_options.media + " duplication cost: $" + media_cost + "\n" +
        price_sheet_options.casing[1] + " case: $" + case_cost + "\n" +
        price_sheet_options.inserts[1] + " insert: $" + insert_cost + "\n" +
        "Insert Setup/Proofs: $" + price_sheet_options.inserts[4] + "\n" +
        price_sheet_options.disc_label[1] + " setup: $" + price_sheet_options.disc_label[0] + "\n" +
        "Total: $" + total);
}

function submit_form() {
  document.forms[0].onsubmit = function() {
    var honkey = 0;
    var form_errors = {};
    form_errors.stop = false;
    form_errors.phrases = [];
    var selects = document.forms[0].getElementsByTagName("select");
    for (var i = 0; i < selects.length; i++) {
      if (selects[i].value == "none") {
        if (form_errors.stop !== true) {
          form_errors.stop = true;
          form_errors.phrases[honkey] = "** Finish configuring your project";
          honkey++;
        }
      }
    }
    var text_boxes = document.forms[0].getElementsByTagName("input");
    for (var j = 0; j < text_boxes.length; j++) {
      if (text_boxes[j].type == "text") {
        if (!(text_boxes[j].value.length > 0) && j != 1) {
          if (form_errors.stop !== true) {
            form_errors.stop = true;
          }
          form_errors.phrases[honkey] = "** Fill in your contact information";
        }
      }
    }
    
    if (form_errors.stop) {
      form_errors.report = "We need just a little more information first...\n\n";
      for (var h = 0; h < form_errors.phrases.length; h++) {
        form_errors.report += form_errors.phrases[h] + "\n";
      }
      form_errors.report += "\nThanks!";
      alert(form_errors.report);
      return false;
    } else {
      var selects = document.forms[0].getElementsByTagName("select");
      for (var meow = 0; meow < selects.length; meow++) {
        selects[meow].disabled = false;
      }
    }
  }
}