// ============================================================================
// ARJE_ajax.js - this javascript file provides the AJAX capabilites
//                for the IACA Annual Jurisdiction Report system
// ----------------------------------------------------------------------------
//            Copyright (c) 2008 Ose Micro Solutions, Inc.
// ----------------------------------------------------------------------------
// History
//  03/21/2008  tmo 01.00 Started tracking changes and inital release
// ============================================================================
/*
 * this function is used to test the submission to insure every thing has been selected.
 */
function submitForm()
{
  var myState = document.getElementById('stateCode').value != "" ? document.getElementById('stateCode').value : "AA";
  var myCountry = document.getElementById('countryCode').value != "" ? document.getElementById('countryCode').value : "USA";
  if (myState == "AA")
  {
    switch (myCountry)
    {
      case "USA":
          displayNotice("You must select a state before continuing");
          return false;
          break;
      case "CAN":
          displayNotice("You must select a province before continuing");
          return false;
          break;
      default: return true;
    }
  }
  return true;
}
/* 
 * this function is used to clear the notice area
 */
function displayNotice(msg)
{
  document.getElementById('noticeArea').innerHTML = msg;
}                                                                                                  //--- end of displayNotice()
/*
 * clears the notice area
 */
function clearNotice()
{
  document.getElementById('noticeArea').innerHTML = "";
}                                                                                                  //--- end of clearNotice()
/*
 * Updates the country list 
 */
function updateCountry()
{
  clearNotice();
  clearState();
  var myURL = "/iacareg/DataInterface.php?mode=AJREsel";
  new net.ContentLoader(myURL, parseCountry);
}                                                                                                  //--- end of updateCountry()
/*
 * Removes any state options clears the selection 
 */
function clearState()
{
  while (document.ARJSelect.stateCode.length)                                                      // Clear the previous values
      document.ARJSelect.stateCode.options[0] = null;
}                                                                                                  //--- end of clearState()
/*
 * Updates the state information and retrieves the proper data
 */
function updateState()
{
  clearNotice();
  var countryCode = document.getElementById('countryCode').value != "" ? document.getElementById('countryCode').value : inCountry;
  document.getElementById('lable1').innerHTML = countryCode == "USA" ? "State" : "Province";
  if (countryCode == "USA" || countryCode == "CAN")
  {
    document.getElementById('state').style.visibility = 'visible';
    var myURL = "/iacareg/DataInterface.php?mode=jdatasel&sel=" + countryCode;
    new net.ContentLoader(myURL, parseState);
  }
  else
  {
    document.getElementById('state').style.visibility = 'hidden';
    document.getElementById('stateCode').value = "";
  }
}                                                                                                  //--- end of updateState()
/*
 * Parses the state array into the state selection list
 */
function parseState()
{
  clearState();
  var sel = document.getElementById('stateCode').value != "" ? document.getElementById('stateCode').value : inState;
  var resp = this.req.responseText.split("|");                                                     // Split the response into an array
  for (var i = 0; i < resp.length; i += 2)
  {
    if (resp[i])
    {
      opt = document.createElement('OPTION');
      opt.text = resp[i + 1];
      opt.value = resp[i];
      if (opt.value == sel)
        opt.selected = true;
      document.ARJSelect.stateCode.options[document.ARJSelect.stateCode.length] = opt;
    }
  }
}                                                                                                  //--- end of parseState()
/*
 * Parses the Countyr area inot the option list
 */
function parseCountry()
{
  var sel = inCountry;
  var resp = this.req.responseText.split("|");                                                     // Split the response into an array
  while (document.ARJSelect.countryCode.length)                                                    // Clear the previous values
      document.ARJSelect.countryCode.options[0] = null;
  for (var i = 0; i < resp.length; i += 2)
  {
    if (resp[i])
    {
      opt = document.createElement('OPTION');
      opt.text = resp[i + 1];
      opt.value = resp[i];
      if (opt.value == sel)
        opt.selected = true;
      document.ARJSelect.countryCode.options[document.ARJSelect.countryCode.length] = opt;
    }
  }
}                                                                                                  //--- end of parseCountry()
/*
 * this function is called when the page is first loaded
 */
function LoadUp()
{
  updateCountry();
  updateState();
  document.ARJSelect.countryCode.onchange = updateState;
  document.ARJSelect.stateCode.onchange = clearNotice;
}                                                                                                  //--- end of LoadUp()