function loadRecentAddrsArray(fieldName)
{
      // before doing any work, we will first see if the address is already in the list.
      var locSelect = document.getElementById(fieldName);
      locSelect.options.length = 0;
          // repopulate the optionlist.
      locSelect.options[0] = new Option (" Recently Used...", "");
      if (recentAddrsArr.length > 0)
      {
          for (var j = 0; j < recentAddrsArr.length; j++)
          {
              splitArr = recentAddrsArr[j].split("*");
              var newOpt;
              if (splitArr[0] == "")
              {
                  if (splitArr[1].length > 20)
                  {
                      newOpt = new Option(splitArr[1].substring(0, 19), recentAddrsArr[j]);
                  }
                  else
                  {
                      newOpt = new Option(splitArr[1], recentAddrsArr[j]);
                  }
              }
              else
              {
                  if (splitArr[0].length > 20)
                  {
                      newOpt = new Option(splitArr[0].substring(0, 19), recentAddrsArr[j]);
                  }
                  else
                  {
                     newOpt = new Option(splitArr[0], recentAddrsArr[j]);
                  }
              }
              locSelect.options[j+1] = newOpt;
          }
      }
}

function clearFields(fieldName)
{
        eval("document.getElementById('" + fieldName + "line1').value = \"\"");
        eval("document.getElementById('" + fieldName + "line2').value = \"\"");
}

function populateFields(e, fieldName)
{
    var ndx = e.selectedIndex;
    e.selectedIndex = 0;
    if(ndx<0)
    {
        clearFields(fieldName);
    }
    else
    {
       var splitArray = e.options[ndx].value.split("*");
       populateFieldsWithEntry(splitArray[0], splitArray[1], splitArray[2], fieldName);
    }
}

function populateFieldsWithEntry(line1, line2, country, fieldName)
{
       eval("document.getElementById('" + fieldName + "line1').value = line1");
       eval("document.getElementById('" + fieldName + "line2').value = line2");
       if ( eval("document.getElementById('" + fieldName + "country')")
            != null)
       {
            var len = eval("document.getElementById('" + fieldName + "country').options.length");

            for (var i = 0; i < len;i++)
            {
                if (eval("document.getElementById('" + fieldName + "country').options[i].value") == country)
                {
                eval("document.getElementById('" + fieldName + "country').selectedIndex = i");
                }
            }
       } 
}

function doMapSelectRecentCallbackHandler(e, fieldName)
{
    populateFields(e, fieldName);
}

function loadRecentAddressEntries(cookieEnabled)
{
    
    if (cookieEnabled == "false")
    {
        return;
    }
    var allcookies = document.cookie;
    // find the index of the relevant cooie

    var pos = allcookies.indexOf("rcntaddr"+"=");
    if (pos != -1)
    {
        start = pos + 9;
        var end = allcookies.indexOf(";", start);
        if (end == -1)
        {
          end = allcookies.length;
        }

       // get the value for the cookie
       var value = allcookies.substring(start, end);
       // split it into address arrays
       value = unescape(value);
       var addrArr = value.split("~");
       for (var i = 0; i < addrArr.length; i++)
       {

           var unescaped  = unescape(addrArr[i]);
           // cereate an option and add it to the recent location select
           // there is a slight concurrency issue, so make sure that the
            // option isn't already in the location list...
           if (!isAlreadyInRecentAddr(unescaped) != -1)
           {
               recentAddrsArr[recentAddrsArr.length] = unescaped;
           }
       }
   }
}

function isAlreadyInRecentAddr(value)
{
    for (var i =0; i < recentAddrsArr.length;i++)
    {
         if (recentAddrsArr[i] == value)
             return i;
    }
    return -1;
}

function addRecentAddressEntry(line1, line2, country)
{
    // before doing any work, we will first see if the address is already in the list.
    if (line1 == "" && line2=="")
        return;
    var finalLine1 = line1;
    if (line1 != "")
    {
        var splitArr = line1.split("-");
        if (splitArr.length > 1)
        {
            var secSplitArr = splitArr[1].split(" ");
            if (parseInt(splitArr[0]) != 0 &&
                parseInt(secSplitArr[0]) != 0)
            {
                finalLine1 = splitArr[1].substring(1, (splitArr[1].length));
            }
        }
    }
    var line2Split = line2.split(",");
    if (line2Split[1].charAt(0) != " ")
    {
        line2 = line2Split[0] + "," + " " + line2Split[1];
    }

    var value = finalLine1 + "*" + line2 + "*" + country + "*";
    var index = -1;
    if ((index = isAlreadyInRecentAddr(value)) != -1)
    {
        // if we've already saved the location, we need to grab that location and put it at the top
        // of the select option list.
        var topOpt = recentAddrsArr[index];
        optArr = [topOpt];
        // copy everything before that index into the temp array
        for (var i = 0; i < index; i++)
        {
            optArr[optArr.length] = recentAddrsArr[i];
        }
        // copy everything after that index into the temp array
        for (i=index+1; i < recentAddrsArr.length;i++)
        {
            optArr[optArr.length] = recentAddrsArr[i];
        }
    }
    else
    {
        // make an option array
        optArr = [value];
        // copy the current option array into the temp option array
        for (var i = 0; i < recentAddrsArr.length; i++)
        {
            optArr[optArr.length] = recentAddrsArr[i];
        }
    }
    recentAddrsArr = optArr;
        // if there are more than 10 addresses, ensure the lenth is 10 (1 is the recent locations)
    if (recentAddrsArr.length > 10)
    {
        recentAddrsArr.length = 10;
    }
    // now lets cookie the browser
    value = "";

    // assemble an address string
    for (var n = 0; n < recentAddrsArr.length;n++)
    {
        if (n == (recentAddrsArr.length - 1))
            value += recentAddrsArr[n];
        else
            value += recentAddrsArr[n] + "~";
    }
    var cookie = "rcntaddr" +"="+ escape(value);
    // set the expiration date
    var date = new Date();
    date.setFullYear(date.getFullYear() + 1);
    cookie +=" ; path=/ ";
    cookie +="; expires=" + date.toGMTString() +" ;";
    // set the cookie
    document.cookie = cookie;
}

