/**
   Updates a dropdown based on the selection of another
   @var source DOMElement Element which has been selected
   @var targetID string ID of the element to be updated
   @var url string URL which we should be calling to (with ajax_id passed)
**/

function ajaxChangeSelect(source, targetID, url, imgID)
{
   document.getElementById(imgID).style.display = 'inline';
   document.getElementById(targetID).disabled = true;
   $.get(url, {source_value: source.value, source_id: source.id}, function(x)
   {
      var xmlResponse = x.documentElement;
      var newOptions = xmlResponse.getElementsByTagName('option');
      updateSelect(targetID, xmlResponse, imgID);
   });
}

/**
   Updates the options of a dropdown with new options
   @var targetID string ID of the target dropdown
   @var newOptions HTMLCollection Results from query, structured in <option label="" value=""/> format
**/
function updateSelect(targetID, newOptions, imgID)
{
   var target = document.getElementById(targetID);
   target.options.length = 0;
   $(newOptions).find('option').each(function(){
      newOpt = new Option(this.getAttribute('label'), this.getAttribute('value'));
      target.options[target.options.length] = newOpt;
   }
   );
   document.getElementById(targetID).disabled = false;
   document.getElementById(imgID).style.display = 'none';
}

/**
   Enables fields based on a target ID
   @var targetID string ID of the target dropdown
   @var myID string ID of the even triggering element
   @var maxField number of fields to loop through for activation/reset
**/
function enableLocationSelect(targetID,myID,maxField)
{
   var targetID = 'id' + targetID;
   var loops = myID.substr(10);
   var i = j = 1;

   // Activate the target widget, plus any parents
   for(i;i <= loops;i++)
   {
      var targetElement = document.getElementById(targetID+i);

      targetElement.style.display='block';
   }

   // Reset all of the images via swapping classes
   for(j;j <= maxField;j++)
   {
      var bgElement = document.getElementById('idsearchBy'+j);

      bgElement.className='javascriptLink';
   }

   // If not the last field, disable all of the subsequent fields
   if (loops < maxField)
   {
      var k = parseInt(loops)+1;

      for(k;k <= maxField;k++)
      {
         var targetElement = document.getElementById(targetID+(k));

         targetElement.style.display='none';
      }
   }

   // Set a hidden field to our search type ID, unfortunately the build order doesn't
   // match up nicely with their actual search types in the database, so we have to switch it up.
   switch(parseInt(loops))
   {
      case 1 :  // Country
         searchTypeID = 2;
         break;
      case 2 :  // State
         searchTypeID = 3;
         break;
      case 3 :  // City
         searchTypeID = 1;
         break;
      case 4 :  // Zip
         searchTypeID = 4;
         break;
      default :
         searchTypeID = 2;
         break;
   }

   document.getElementById('idsearchTypeId').value = searchTypeID;

   // Highlight our selected field via swapping classes
   document.getElementById(myID).className='javascriptLinkSel';
}

/**
   Formats a number as a currency type number
   Might be needing some more parameters as implementation increases
**/
function formatCurrency(n)
{
    n = Number(n);
    n = n.toFixed(3);
    n = n * 100;
    n = Math.round(n);
    n = n / 100;
    n = n.toFixed(2);
    return n;
}

/**
   Formats a number as a currency type number
   Might be needing some more parameters as implementation increases
**/
function formatNumber(n)
{
    var n = Number(n);
    return n.toFixed(0);
}
