/*

EXFO Parts Configurator
September 2009

Author:
Jeremy Sisson
me@jeremysisson.com
http://www.jeremysisson.com

Created for:
Jigsaw Advertising Inc.
http://www.jigsawadvertising.com

*/
var controllerpath = '/configurator/ajax_configurator.php';
var setup = '';
var currentpartslist
var currentconfiguration = {
  'headnumber' : '',
  'cablelength' : '',
  'printernumber' : '',
  'companyname' : ''
};


function initialize_configurator()
{
  // Reset the select box
  $('systemSetup').options[0].selected = true;

  // Assign some behaviours

  // setup change
  $('systemSetup').observe('change', function() { return changeSetup(); });

  // get parts list click
  $('getPartsList').observe('click', function() { return getPartsList(); });

  // save and restart click
  $('saverestart').observe('click', function() { return saveConfiguration(); });

//  $('submitforquote').observe('click', function() { return submitForQuote(); });


  // Load the saved configurations
  listConfigurations();

  return true;
}


// Load the configurations from the user's session (on the server)
function listConfigurations()
{
  var parameters = {
    'method' : 'listConfigurations'
  };

  // Submit the ajax
  new Ajax.Request(
    controllerpath,
    {
      'method' : 'get',
      'parameters' : parameters,
      'onSuccess' : listConfigurations_success,
      'onFailure' : function() { alert('server error'); }
    }
  );

  return true;
}

function listConfigurations_success(transport)
{
  // show the results
  var results = transport.responseText;

  if (results == 'error') {
    return true;
  }

  if (results == '') {
    new Effect.BlindUp('saved_configurations', { 'duration' : 0.5 });
    $('configurationlist_results').innerHTML = '';

    return true;
  }

  var configurationlist = eval(results);

  // Delete the children of saved_configurations
  $('configurationlist_results').childElements().each(function(item) {
    item.remove();
  }.bind(this));

  configurationlist.each(function(item) {
    newrow = document.createElement('tr');

    cell1 = document.createElement('td');
    newrow.appendChild(cell1);
    cell1.innerHTML = item['setup'];

    cell2 = document.createElement('td');
    newrow.appendChild(cell2);
    cell2.innerHTML = item['description'];

    cell3 = document.createElement('td');
    cell3.className = 'center';
    newrow.appendChild(cell3);

    var text = '<a href="/generatepdf.php?id='+item['id']+'" class="spnMiniPDF">Generate PDF</a>';
    text = text + '<a href="submitforquote.php?id='+item['id']+'" class="spnMiniQuote">Submit for Quote</a>';
    text = text + '<a href="javascript:void(0);" id="deleteconfiguration'+item['id']+'" class="spnMiniClose">Delete Configuration</a>';
    cell3.innerHTML = text;

    $('configurationlist_results').appendChild(newrow);
  }.bind(this));

  configurationlist.each(function(item) {
    $('deleteconfiguration'+item['id']).observe('click', function() { deleteConfiguration(item['id']); });
  });

  $('saved_configurations').style.display = 'block';

  return true;
}






function deleteConfiguration(configurationid)
{
  var parameters = {
    'method' : 'deleteConfiguration',
    'id' : configurationid
  };

  // Submit the ajax
  new Ajax.Request(
    controllerpath,
    {
      'method' : 'get',
      'parameters' : parameters,
      'onSuccess' : deleteConfiguration_success,
      'onFailure' : function() { alert('server error'); }
    }
  );

  return true;
}

function deleteConfiguration_success()
{
  listConfigurations();

  return true;
}




// Save the configurations to the user's session (on the server)
function saveConfiguration()
{
  var parameters = {
    'method' : 'saveConfiguration'
  };

  // Submit the ajax
  new Ajax.Request(
    controllerpath,
    {
      'method' : 'get',
      'parameters' : parameters,
      'onSuccess' : saveConfiguration_success,
      'onFailure' : function() { alert('server error'); }
    }
  );

  return true;
}

function saveConfiguration_success()
{
  // Reset the first select box
  $('configurator').reset();
  listConfigurations();
  showStep(1);

  return true;
}



function submitForQuote()
{
//alert('test1');
//  $('configurator').submit();
//alert('test2');
  return true;
}


// Send the parts list off to the server to be calculated
function getPartsList()
{
  // Is config_headnumber blank?
  if ($F('config_headnumber').blank()) {
    alert('You must enter a value for "# of Heads"');
    return false;
  }

  // Is config_headnumber a number?
  if (isNaN($F('config_headnumber'))) {
    alert('You must enter a number for "# of Heads"');
    return false;
  }

  // Has config_cablelength been selected?
  if ($F('config_cablelength').blank()) {
    alert('You must select a value for "Cable Length"');
    return false;
  }

  // Is config_printernumber blank?
  if ($F('config_printernumber').blank()) {
    alert('You must enter a value for "# of Printers"');
    return false;
  }

  // Is config_printernumber a number?
  if (isNaN($F('config_printernumber'))) {
    alert('You must enter a number for "# of Printers"');
    return false;
  }

  currentconfiguration['setup'] = setup;
  currentconfiguration['headnumber'] = $F('config_headnumber');
  currentconfiguration['cablelength'] = $F('config_cablelength');
  currentconfiguration['printernumber'] = $F('config_printernumber');

  // Can we show a spinny thing?
  $('step2_throbber').style.display = 'inline';
  $('getPartsList').style.display = 'none';

  var parameters = {
    'method' : 'getPartsList',
    'configuration' : Object.toJSON(currentconfiguration)
  };

  // Submit the ajax
  new Ajax.Request(
    controllerpath,
    {
      'method' : 'get',
      'parameters' : parameters,
      'onSuccess' : getPartsList_success,
      'onFailure' : getPartsList_failure
    }
  );
  return true;
}

function getPartsList_success(transport)
{
  // show the results
  var results = transport.responseText;

  if (results == 'error') {
    return true;
  }

  var partslist = eval(results);

  // Delete the children of partslist_results
  $('partslist_results').childElements().each(function(item) {
    item.remove();
  }.bind(this));

  partslist.each(function(item) {
    newrow = document.createElement('tr');

    cell1 = document.createElement('td');
    newrow.appendChild(cell1);
    cell1.innerHTML = item['partnumber'];

    cell2 = document.createElement('td');
    newrow.appendChild(cell2);
    cell2.innerHTML = item['description'];

    cell3 = document.createElement('td');
    cell3.className = 'center';
    newrow.appendChild(cell3);
    cell3.innerHTML = item['quantity'];

    $('partslist_results').appendChild(newrow);
  }.bind(this));

  $('step2_throbber').style.display = 'none';
  $('getPartsList').style.display = 'block';

  showStep(3);

  return true;
}

function getPartsList_failure()
{
  alert('server error');
  $('step2_throbber').style.display = 'none';
  $('getPartsList').style.display = 'block';

  return true;
}





function saveAndRestart()
{
  saveConfiguration();
  return true;
}






function showStep(stepnumber)
{

  if (stepnumber == 2) {
    // Show step 2
    Effect.BlindDown('step2', { 'duration' : 0.5 });
    $('spnProgress_step1').removeClassName('Active');
    $('spnProgress_step1').addClassName('Done');
    $('spnProgress_step2').addClassName('Active');
  }
  else if (stepnumber == 3) {
    // Show step 3
    Effect.BlindDown('step3', { 'duration' : 0.5 });
    $('spnProgress_step2').removeClassName('Active');
    $('spnProgress_step2').addClassName('Done');
    $('spnProgress_step3').addClassName('Active');
  }
  else {
    // Hide steps 2, 3, 4
    Effect.BlindUp('step3', { 'duration' : 0.5 });
    Effect.BlindUp('step2', { 'duration' : 0.5 });

    $('spnProgress_step1').addClassName('Active');

    $('spnProgress_step2').removeClassName('Active');
    $('spnProgress_step2').removeClassName('Done');

    $('spnProgress_step3').removeClassName('Active');
    $('spnProgress_step3').removeClassName('Done');
  }

  return true;
}


function changeSetup()
{
  setup = $F('systemSetup');
  $('configurationTitle').innerHTML = setup+' Based Configuration Options';
  $('configurator').reset();

  if (setup == '') {
    // Show step 1
    showStep(1);
  }
  else {
    // Show step 2
    showStep(2);
  }

  return true;
}


