var script_location = "http://travel.wonders1.googlepages.com/worldfirst-ajax.php";


var Worldfirst = {
  'key': 'YW1iZXJncmVlbjpNYXJxdWlzZVA3OTM=',
  'rate': 0.0,
  'from_value' : 0.0,
  'from_name' : '',
  'to_value' : 0.0,
  'to_name' : '',
  'amount_to_convert': 0.0,
  'total_amount': 0.0
};

// Define namespaced functions
Worldfirst.roundNumber = function(num, dec) {
  return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
};

Worldfirst.loading = function() {
  jQuery('#result_hook').html('');
  var output = ['<p class="wf-message">'];
    output.push('Loading.......');
  output.push('</p>');
  jQuery('#result_hook').append(output.join(''));
};

Worldfirst.append_result = function() {
  jQuery('#result_hook').html('');
  var output = ['<p class="wf-message">'];
    output.push('<strong>Conversion Rate: ' + Worldfirst.rate + '</strong><br /><br />');
    output.push('<strong>You will recieve: ' + Worldfirst.total_amount + ' ' + Worldfirst.to_name +'</strong>');
  output.push('</p>');
  jQuery('#result_hook').append(output.join(''));
};

Worldfirst.append_error = function(error_type) {
  jQuery('#result_hook').html('');
  switch(error_type)
  {
    case 0:
      var output = ['<p class="wf-error">'];
        output.push('<strong>Error:</strong><br />');
        output.push('Worldfirst do currently not provide this conversion.  Please try another combination.');
      output.push('</p>');
    break;
    case 1:
      var output = ['<p class="wf-error">'];
        output.push('<strong>Error:</strong><br />');
        output.push('The currency amount must be a number greater than zero.');
      output.push('</p>');
    break;
    case 2:
      var output = ['<p class="wf-error">'];
        output.push('<strong>Error:</strong><br />');
        output.push('Currently unable to get a quote for this currency conversion request.  Please try again.');
      output.push('</p>');
    break;
  }; 
  jQuery('#result_hook').append(output.join(''));
};

jQuery(document).ready(function() {
  jQuery('#worldfirst_currency').submit(function() {
    // Do the loading screen
    Worldfirst.loading();
    
    // Get the values from the DOM
    Worldfirst.from_value = jQuery('#currency_from').val();
    Worldfirst.from_name = jQuery('#currency_from :selected').text();
    Worldfirst.to_value = jQuery('#currency_to').val();
    Worldfirst.to_name = jQuery('#currency_to :selected').text();
    Worldfirst.amount_to_convert = parseFloat(jQuery('#amount_to_convert').val());
    
    // Now we check that the amount entered is valid
    if (Worldfirst.amount_to_convert > 0.0 && typeof Worldfirst.amount_to_convert == 'number') {
    
      // Do a GET request to fetch the data
      jQuery.get(script_location,
      {
        'worldfirst_get_rate': 'true',
        'from': Worldfirst.from_value,
        'to': Worldfirst.to_value,
        'key': Worldfirst.key,
      }, function(data, status) {
        if (typeof data.error == 'undefined') {
          if (data.output == "NOQUOTE") {
            Worldfirst.append_error(2);
          } else {
            Worldfirst.rate = data.RATE;
            Worldfirst.total_amount = Worldfirst.roundNumber((Worldfirst.amount_to_convert * Worldfirst.rate), 2);
            Worldfirst.append_result();
          }
        } else {
          Worldfirst.append_error(0);
        }  
      }, 'json');
    } else {
      Worldfirst.append_error(1);
    }
    return false;
  });
});
