
function show_hide(id, target) {
  current_desc = document.getElementById( id );

  if( current_desc.style.display == 'none' ) {
    current_desc.style.display = 'inline';
    target.innerHTML = 'hide';
  }
  else {
    current_desc.style.display = 'none';
    target.innerHTML = 'show';
  }
}



var stage;


function add_to_selection(stage){
  
if(
(document.getElementById('standard_size').checked == true &&
 document.getElementById('dimensions').value == 0) ||
 (document.getElementById('custom_size').checked == true)
&& 
(document.getElementById('height').value == ''
|| document.getElementById('width').value == ''
)
 )
return select_size();

if (stage ==  1)
return choose_finish();
else if (stage == 2){
return add_door();
} 
}

function add_door(){
if(check_sizes_in_range(true))
{
new Ajax.Updater('summary', '/ajax.php?action=add', { method: 'post', parameters: $('doors').serialize(true) });
alert('Succesfully added door / drawer front to your basket.');
}
return false;
}

function get_standard_sizes(){
if (document.getElementById('type_drawer').checked == true)
document.getElementById('drilled_no').checked = true;
new Ajax.Updater({ success: 'sizes' }, '/ajax.php?action=standard', { method: 'post', evalScripts:  false,parameters: $('doors').serialize(true) });
return true;
}

function add_panel(id)
{
  var height = document.getElementById("height_" + id).value;
  var width = document.getElementById("width_" + id).value;
  if (!height || !width)
  {
    alert("Please enter the height and width in mm");
    return false;
  }
  
  height = parseInt(height);
  width = parseInt(width);
  
  if (height < 100 || height > 2400)
  {
    alert("Panel height must be between 100mm and 2400mm");
  }
  else if (width < 100 || width > 1200)
  {
    alert("Panel width must be between 100mm and 1200mm");
  }
  else
  {
    return add_accessory(id);
  }
}

function add_accessory(id , type)
{
  if (typeof type == "undefined")
  {
    // Accessory, Panel
    new Ajax.Updater('summary', '/accessories.php?id='+id, { method: 'post', parameters: $('accessories').serialize(true) });
  }
  else
  {
    // Handle
    new Ajax.Updater('summary', '/accessories.php?id='+id+'&type='+type, { method: 'post', parameters: $('accessories').serialize(true) });
  }
  alert('Succesfully added accessory to your basket.');
  return false;
}

function empty_basket(){
new Ajax.Updater('summary', '/ajax.php', { method: 'get', parameters: { action: 'empty_basket'} });
alert('Your Basket is now empty');
return false;
}


function update_range()
{
//new Ajax.Updater('range_image', '/ajax_update_quote.php?stage=range_image'  , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('styles', '/ajax_update_quote.php?stage=style_select'  , { method: 'post', parameters: $('update_quote').serialize(true) });
return false;
}

function update_style()
{
new Ajax.Updater('style_image', '/ajax_update_quote.php?stage=style_image'  , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('finishes', '/ajax_update_quote.php?stage=finish_select' , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('style_info', '/ajax_update_quote.php?stage=style_info'  , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('cat_b_link', '/ajax_update_quote.php?stage=link'  , { method: 'post', parameters: $('update_quote').serialize(true) });
return false;
}

function update_finishes()
{
new Ajax.Updater('finish_image', '/ajax_update_quote.php?stage=finish_image'  , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('price', '/ajax_update_quote.php?stage=price'  , { method: 'post', parameters: $('update_quote').serialize(true) });
new Ajax.Updater('cat_b_link', '/ajax_update_quote.php?stage=link'  , { method: 'post', parameters: $('update_quote').serialize(true) });
return false;
}

function update_link()
{
new Ajax.Updater('cat_b_link', '/ajax_update_quote.php?stage=link'  , { method: 'post', parameters: $('update_quote').serialize(true) });
return false;
}

function submit_update()
{
if($('style').value == 0){
alert('Please select a style');
return false;
}
if($('finish').value == 0){
alert('Please select a finish');
return false;
}

return true;
}

function clear_original()
{
  new Ajax.Updater('original_price', '/ajax_update_quote.php?stage=clear' );
return false;
}

function check_price(){
if(document.getElementById('standard_size').checked == true &&
 document.getElementById('dimensions').value == 0  )
return select_size();
if(check_sizes_in_range(true))
{
new Ajax.Updater( 'price', '/ajax.php?action=check', { method: 'post', parameters: $('doors').serialize(true) });
//onChange="sync_prices();";
alert('Success: New Price Calculated for the selected Single Item\n\n To get an On Line Quotation you will need to Click on a \nColour and then enter sizes and quantites for each item.\n\n Watch your Discount increase as you add more items. \nYou can see what you have entered by clicking \'Check Basket\' \n button at any time.');
}
return false;
}

function update_live_price()
{
  if (
    (document.getElementById('standard_size').checked == true && document.getElementById('dimensions').value == 0)
    || !check_sizes_in_range(false)
  )
  {
    document.getElementById('live_price_container').style.display = "none";
  }
  else
  {
    document.getElementById('live_price').innerHTML = "";
    new Ajax.Updater('live_price', '/ajax.php?action=check&mode=live', { method: 'post', parameters: $('doors').serialize(true) });
    document.getElementById('live_price_container').style.display = "block";
  }
}

function sync_prices(){
  document.getElementById('price_two').innerHTML =  document.getElementById('price').innerHTML;
}

function select_size(){
alert('Please select a size');
return false;
}

function check_sizes_in_range(show_message)
{
  
  if (document.getElementById('custom_size').checked == true){
    
    var curr_height = parseInt(document.getElementById('height').value);
    var curr_width  = parseInt(document.getElementById('width').value);
    
    // Blank
    if (!curr_height || !curr_width)
    {
      if (show_message)
        alert('Please enter the height and width, or select a standard size');
      return false;
    }
    
    // Maximum
    var max_height  = parseInt(document.getElementById('max_height').value);
    var max_width   = parseInt(document.getElementById('max_width' ).value);
    
    if (curr_height > max_height || curr_width > max_width) {
      if (show_message)
        alert('Maximum height is ' + max_height + 'mm, maximum width is ' + max_width + 'mm');
      return false;
    }
    
    // Minimum
    if ( curr_height < 100 || curr_width < 100) {
      if (show_message)
        alert('Minimum height and width is 100mm');
      return false;
    }
  }
  
  return true;
  
}

function choose_finish(){
alert('You must select a finish before you can add to basket');
return false;
}


function chooseFinish(colour)
{
document.doors.idColour.value = colour;
document.getElementById("doors").submit();
}

function standardSizeChanged()
{
  var dropdown = document.getElementById('dimensions');
  if (dropdown.options[dropdown.selectedIndex].value == "custom")
  {
    dropdown.selectedIndex = 0;
    document.getElementById('custom_size').checked = true;
    document.getElementById('height').focus();
  }
  else
  {
    document.getElementById('standard_size').checked = true;
  }
  standardCustomChanged();
}

function standardCustomChanged()
{
  var customCheckbox = document.getElementById('custom_size');
  if (customCheckbox)
  {
    if (customCheckbox.checked)
    {
      var custom = true;
    }
    else
    {
      var custom = false;
    }
    var standard = !custom;
    document.getElementById('standard_fields').style.display = (standard ? "block" : "none");
    document.getElementById('custom_fields').style.display = (custom ? "block" : "none");
  }
}

document.observe('dom:loaded', standardCustomChanged);

var oldHeight = 0;
var oldWidth = 0;
function customSizeChanged()
{
  document.getElementById('custom_size').checked=true;
  standardCustomChanged();
  
  var newHeight = document.getElementById('height').value;
  var newWidth = document.getElementById('width').value;
  
  // Only update price if it actually changed, to prevent flickering
  if (oldHeight != newHeight || oldWidth != newWidth)
  {
    update_live_price();
    oldHeight = newHeight;
    oldWidth = newWidth;
  }
}

