// Message Functions
function doInsert(anyObj) {
  var toField = anyObj[0];
  if (doInsertRedraw(toField)) {
    var indx = lastFormIndex(document);
    document.forms[indx].cardReload.click();
  }
  else {
    ClosePopup("BlueMtnNumFour");
    return false;
  }
}

function doInsertRedraw(recip) {
  // entry is an array of recipients with the
  // each index containing the recip. info separated
  // by a + sign. data is returned as email,nickname
  var indx = lastFormIndex(document);
  var hdnEmail = document.forms[indx].abEmailTo;
  var hdnName = document.forms[indx].abNameFirstTo;
  
  for (var i = 0; i < recip.length; i++) {
    var email = recip[i].split("+");
    var tEmail = email[0];
    var tName = email[1];
    if (hdnEmail.value.length > 0) {
      hdnEmail.value += "-$-" + tEmail;
      hdnName.value += "-$-" + tName;   
    }
    else {
      hdnEmail.value += tEmail;
      hdnName.value += tName;   
    }
  }
  if (recip.length == 0) 
    return false;
  return true;
}

function gotoNumFour(purl) {
  var plannerStr = "";

  // figure out what planner to goto based on purl
  if (purl == "selector") {
    plannerStr = "http://address.bluemountain.com/address/selector" +
                 "?CB=to:To&returnFields=email,nickname&ACT=Add+to+Card" +
                 "&OPENER=OTHER";
  }
  else if (purl == "planner") {
    plannerStr = "http://address.bluemountain.com/address";
  }
  else {
    plannerStr = "http://www.excite.com/";
  }

  // open it up
  openNumFour(plannerStr);
}

function openNumFour(purl) {
  if (FocusPopup("BlueMtnNumFour")) return;
  // zero out hidden fields
  var indx = lastFormIndex(document);
  document.forms[indx].abEmailTo.length = 0;
  document.forms[indx].abNameFirstTo.length = 0;

  newWin = window.open(purl,"BlueMtnAddBook",
               "height=400,width=520,hotkeys,resizable,scrollbars,dependent");
  AddPopup("BlueMtnNumFour",newWin);
  FocusPopup("BlueMtnNumFour");
}

function openContactLong(url,id,folder) {
  newLocation = url + '/address/edit/contact/long?origin=none&OPENER=SELF' +
                '&I=' + id +
                '&F=' + escape(folder);
  newWin = window.open(newLocation,"BlueMtnCntctLong",
                "height=400,width=680,hotkeys,resizable,scrollbars,dependent");
  AddPopup("BlueMtnCntctLong",newWin);
  FocusPopup("BlueMtnCntctLong");
}

function goSelf() {
  window.location = "#SELF1";
}

function openHelpPopup(id)
{
  var help_url;

  // select url
  switch (id) {
    case "sam":
       help_url = "/help/sampopup.html";
       break;
    case "pch":
       help_url = "/help/pchpopup.html";
       break;
    case "giftcert":
       help_url = "/help/giftcertpopup.html";
       break;
    case "gift":
       help_url = "/help/giftattachpopup.html";
       break;
    case "coupon":
       help_url = "/help/couponpopup.html";
       break;
    case "media":
       help_url = "/help/enclosurepopup.html";
       break;
    default:
       help_url = "/help/" + id;
       break;
  }
  
  // return if window is already open
  if (FocusPopup("BlueMtnHelpWin")) {
    var refIndex = popupNames["BlueMtnHelpWin"];
    var win = popupReferences[refIndex];
    win.location = help_url;
  }
  else {
    newWin = window.open(help_url,"BlueMtnHelpWin",
               "height=400,width=520,hotkeys,resizable,scrollbars,dependent");
    AddPopup("BlueMtnHelpWin",newWin);
 }
 FocusPopup("BlueMtnHelpWin");
}

function closeHelpPopup() 
{
  ClosePopup("BlueMtnHelpWin");
}

function gotoEditCard()
{
 var secureExpr = "https://secure";
 var myURL;
 var editURL = "";

 myURL = new String (document.URL);
 if (myURL.substring(0,secureExpr.length) == secureExpr)
 {
    var subURL = myURL.substring(secureExpr.length,myURL.length-1);
    var endPos = subURL.indexOf("/");
    if (endPos < 0)
    {
      // Shouldn't happend but doesn't hurt to bulletproof
      endPos = myURL.length;
    }
    else
    {
      endPos += secureExpr.length;
    }
    editURL = "http://www" + myURL.substring(secureExpr.length,endPos);
 }
 editURL += "/cgi-bin/edit";
 document.forms[0].action = editURL;
 document.forms[0].submit();
}

function gotoNext(purl)
{
  if (purl != "") {
      document.forms[0].action = purl;
  } else {
      document.forms[0].action = "/cgi-bin/BMApc1";
  }
  //document.forms[0].submit();
  return 1;
}

function openGenPopup(name,url,width,height)
{
  if (FocusPopup(name)) {
    var refIndex = popupNames[name];
    var win = popupReferences[refIndex];
    win.location = url;
  }
  else {
    var desc_str = "height=" + height + ",width=" + width +
                   ",hotkeys,resizable,scrollbars,dependent";
    newWin = window.open(url,name,desc_str);
    AddPopup(name,newWin);
 }
 FocusPopup(name);
}

// When the user selects a merchant through one of a card's 
// Gift Certificate or Gift Attachment drop-down lists, select
// the appropriate radio button.  If the user selects the '- Select
// a merchant -' option, select "No Gift Attachment, Thanks".
// Because the names of the radio buttons and some of the drop
// down lists contain hyphens, JavaScript will not function properly
// if you refer to them by name, so I had to make the function loop
// through all of the form fields and find the appropriate radio 
// buttons.
function ChangeGiftRadio(selectedElement) {
  // First of all, return if user's browser is WebTV.
  // convert all characters to lowercase to simplify testing 
  var agt      = navigator.userAgent.toLowerCase(); 
  var is_webtv = (agt.indexOf("webtv") != -1);
  if (is_webtv) {
    return true;
  }

  // Some cards, i.e., flash, have more than one form in them.
  // The card _should_ be the last one. 
  var form       = document.forms.length > 0 ? document.forms[document.forms.length-1] : document.forms[0];

  var opt        = 'OPT\-G';    // Find elements containing this (e.g., OPT-GC-GiftAttach)
  var opt_radio  = new Array(); // Keep gift radio buttons here
  var opt_select = new Array(); // Keep select lists here

  // Go through all elements in the form.  Make an array containing the  
  // locations of all select lists and radio buttons whose names match
  // OPT-G, EXCEPT for OPT-GC-Amount (dollar amount list for Gift Certs).
  // That one will mess everything up.  Since the select lists immediately
  // follow their respective radio buttons, keeping track of them through
  // array element values will work. 
  for (var i = 0; i < form.length; i++) {
    var element = form.elements[i]; // Current element

    // If name contains 'OPT-G', add to appropriate array
    if (element.name.match(opt)) {
      if (element.type == 'radio') {
        opt_radio[opt_radio.length] = i;
      }
      else if (element.type == 'select-one') {
        // Skip OPT-GC-Amount
        if (!element.name.match('Amount')) {
          opt_select[opt_select.length] = i;
        }
      }
    }
  }

  // Now that we know the location of the radio buttons and select lists,
  // we need to match the element that just changed to one of them and do
  // the appropriate thing.
  var name = selectedElement.name; // OPT-GC-MerchantProductId, etc. 
  var type = selectedElement.type; // select-one, radio

  // If a select list triggered this function, check the right radio button
  // and reset all other select lists
  if (type == 'select-one') {
    var selected_index = selectedElement.selectedIndex;
    if (selected_index > 0) {
      // Option was selected:
      // 1.  Change radio button to this gift option
      // 2.  Change other select list to selectedIndex = 0
      for (var i = 0; i < form.length; i++) {
        for (var j = 0; j < opt_select.length; j++) {
          if (name == form.elements[opt_select[j]].name) {
            // We found the list - select radio button
            form.elements[opt_radio[j]].checked = true;
          }
          else {
            // This is not our list - reset it
            form.elements[opt_select[j]].selectedIndex = 0;
          }
        }
      }
    }
    else {
      // No option selected:
      // 1.  Change radio button back to 'No Gift Attachment, Thanks'
      form.elements[opt_radio[opt_radio.length - 1]].checked = true;
    } 
  }
  else if (type == 'radio') {
    // The only radio button that will trigger this function is the 'No
    // Gift Attachment, Thanks' option.  Reset all select lists. 
    for (var i = 0; i < opt_select.length; i++) {
      form.elements[opt_select[i]].selectedIndex = 0;
    }
  }

  return true;
}

