// quote2.js

function on_load()
{
  // If a style selected but no colour, force the colour selector
  var x = readCookie('stylecolour');
  if (x != null){
    var y = x.indexOf("~");
    if (x != "" && y == -1){
      productsPopup("colours", "select_colours", $('this_style').value);
    }
  }
}

// Add a door/drawer front to quote - if sizes are OK
function addDoorLine()
{
  // do all validation server-side
  var height = $('doorHeightAdd').value;
  var width = $('doorWidthAdd').value;
  var qty = $('doorQtyAdd').value;

  if (height == "" || width == "" || qty == ""){
    return false;

  }else{
    ajaxCall($F('this_script') +"Ajax.php~_addDoorLine~~"+ height +"~"+ width +"~"+ qty);
    return true;
  }
}

// Show a selection of products to choose from
function productsPopup(anObject, boxID, doorStyle)
{
  if (anObject == "colours" && doorStyle == "aluminium"){
    return; // no colour for aluminium

  }else{
    var aGroup = "";
    if(anObject == "colours"){
      aGroup = "colours";
  
    }else{
      aGroup = $(anObject).value;

      if ($(anObject).id != "colour_select"){
        $(anObject).value = "";

      }else{
        $(anObject).checked = false;
      }
    }
  
    if (aGroup != ""){
      apsolvaShow(boxID +"_box", "block");

      ajaxCall($F('this_script') +"Ajax.php~_getProducts~~"+ aGroup +"~"+ boxID +"~"+ doorStyle);
    }
  }
}

// Close one of the selection boxes
function closeSelectBox(anObject)
{
  // Put box back to "loading" state, otherwise you get previous images showing up briefly
  var x = $(anObject).id;
  x = x.substring(0, x.length - 4);
  $(x).innerHTML = "<div id=\"select_waiting\">Loading ...<br /><img src=\"/images/loading.gif\" alt=\"Waiting ...\" title=\"Waiting ...\" /></div>";

  apsolvaHide(anObject);
}

// Select an item - add it to the quote ...
function itemSelected(anObject, aGroup)
{
  var anID = $(anObject).id.substring(3);

  if (aGroup > 3){
    // Get all the details and add to the quote
    ajaxCall($F('this_script') +"Ajax.php~_addToQuote~~"+ anID +"~0~0~");

  }else{
    if (aGroup == "colours"){
      // A colour
      var x = readCookie('stylecolour');
      var colourCode = anID.substring(4);
      var y = x.indexOf("~");
      if (y == -1){ // Just a style, so add the colour
        x = x +"~"+ colourCode;

      }else{
        // A door style AND colour, replace the colour
        x = x.substring(0, (y + 1)) + colourCode;
      }
      createCookie("stylecolour", x, 31);

      // Change the colour on the quote (if it exists)
      ajaxCall($F('this_script') +"Ajax.php~_amendStyleColour~~~"+ colourCode);

    }else{
      // Just a door style
      createCookie('stylecolour', anID, 31);  // Start afresh!

      // Change the style on the quote (if it exists)
      ajaxCall($F('this_script') +"Ajax.php~_amendStyleColour~~"+ anID +"~");
    }
  }
  
  // Close the relevant selection box
  var parentArr = $(anObject).ancestors();
  apsolvaHide($(parentArr[1]).id);
}

// Show or Hide the drill details
function showHideDrill(anObject, type, height, width)
{
  var anID = $(anObject).id.substring(8);

  if ($(anObject).checked){
    apsolvaShow("drl_det_"+ anID, "block");

    changedDrillPos(anObject, type, height, width, 'init');

  }else{
    apsolvaHide("drl_det_"+ anID);
  }
}

function showDrill(id) {
  elem = document.getElementById(id).checked = true;
}

// Changed the drill option
function changedDrillPos(anObject, type, height, width, init)
{
  var newPos = $(anObject).value;
  var quoteID = $(anObject).id.substring(8);

  if (newPos == "p" || newPos == ""){  // Going to None/Price only
    $$(".hle_pos_"+ quoteID).each(function (divObjects){
      $(divObjects).disable();
    });

  }else{
    $$(".hle_pos_"+ quoteID).each(function (divObjects){
      $(divObjects).enable();   // Going to a position - do this incase was disabled
    });
  }

  if (init == "" && (newPos == "p" || newPos == "")){
    // Price only or none - just save blank details
    saveDrill(quoteID, height, width);
    return;
  }

  // Calculate defaults or read in existing settings
  ajaxCall($F('this_script') +"Ajax.php~_getSetHoles~~"+ quoteID +"~"+ newPos +"~"+ type +"~"+ height +"~"+ width +"~"+ init);
  return;
}

// Save the drilling details
function saveDrill(quoteID, height, width)
{
  var drillPos = $('drl_pos_'+ quoteID).value;

  if ($('top_pos_'+ quoteID) == null){
    var topDef = 0;

  }else{
  	var topDef = $('top_pos_'+ quoteID).value;
  }

  if ($('top2_pos_'+ quoteID) != null){
  	var top2Def = $('top2_pos_'+ quoteID).value;

  }else{
    var top2Def = 0;
  }
  if ($('mid_pos_'+ quoteID) != null){
  	var midDef = $('mid_pos_'+ quoteID).value;

  }else{
    var midDef = 0;
  }
  if ($('bot2_pos_'+ quoteID) != null){
  	var bot2Def = $('bot2_pos_'+ quoteID).value;

  }else{
    var bot2Def = 0;
  }

  if ($('bot_pos_'+ quoteID) == null){
  	var botDef = 0;

  }else{
  	var botDef = $('bot_pos_'+ quoteID).value;
  }

  ajaxCall($F('this_script') +"Ajax.php~_saveDrill~~"+ quoteID +"~"+ drillPos +"~"+ topDef +"~"+ top2Def +"~"+ midDef +"~"+ bot2Def +"~"+ botDef +"~"+ height +"~"+ width);
}

// Save the new option
function saveOptions(anObject)
{
  var option = $(anObject).value;
  var quoteID = $(anObject).id.substring(4);

  ajaxCall($F('this_script') +"Ajax.php~_saveOptions~~"+ quoteID +"~"+ option);
}

// Delete a line from the quote
function deleteLine(anObject)
{
  var x = confirm("Are you sure you want to remove this line from your quote?")
  if (x){
    var anID = $(anObject).id.substring(4);

    ajaxCall($F('this_script') +"Ajax.php~_deleteLine~~"+ anID);

    return true;

  }else{
    return false;
  }
}

// User has changed a quantity, so prompt for an update
function changedQty(anObject)
{
  var anID = $(anObject).id.substring(4);

  $('tot_'+ anID).innerHTML = "<img onclick=\"updateTotals(this);\" id=\"upd_"+ anID +"\" class=\"update_img\" src=\"/images/accept.png\" alt=\"Update totals\" title=\"Update totals\" />";
}

// Update the totals
function updateTotals(anObject)
{
  var anID = $(anObject).id.substring(4);
  var qty = $("qty_"+ anID).value;

  if (qty == 0){
    deleteLine(anObject);
    return false;

  }else{
    if (!isPositiveInt(qty)){
      alert("Only quantities between 0 and 50 are valid.");
      return false;
    }
  }

  ajaxCall($F('this_script') +"Ajax.php~_updateTotals~~"+ anID +"~"+ qty);
}

// Clear the quote (after confirmation)
function clearQuote()
{
  if(confirm("Are you sure you want to clear you entire quote?")){
    ajaxCall($F('this_script') +"Ajax.php~_clearQuote~~");
    return true;

  }else{
    return false;
  }
}

// Check all the drilling boxes and print the quote
function printQuote() {
  /*
   * XXX
   * Unfold all drilling lines at a later date
  $$('.check_on_print').each(function(anObject) {
    //anObject.checked = true;
  });
  */

  window.print();  
}

