var emailToBishopsBaseText = "";
var dioceseChangeCount = 0; // this is just so the diocese box doesn't blink when the page loads

var handleSendToLocalBishopCheckbox = function() {
// if the sendtolocalbishop box is checked, show the select local bishop panel; otherwise, hide it

  if (document.getElementById('sendtolocalbishop').checked) {
     document.getElementById('localbishopselectionbox').style.display = 'block'; 
  }
  else {
     document.getElementById('localbishopselectionbox').style.display = 'none';
  }

  // make sure the local bishop's name is not being displayed in the list of postcard recipients
  handleDioceseBoxChange();   

}


var handleDioceseBoxChange = function() {
  var dioceseBox = document.getElementById('bishops_list');


    // set the "To:" box to the default values
    document.getElementById('emailToBishopsSlot').innerHTML = emailToBishopsBaseText;

    // get the email address and name of the currently selected bishop
    localBishopEmail = dioceseBox.value;
	if (dioceseBox.selectedIndex >= 0) {
    	localBishopInfo = dioceseBox.options[dioceseBox.selectedIndex].innerHTML;
	} else {
		localBishopInfo = ""
	}
    localBishopNameLeft = localBishopInfo.indexOf('--') + 3;
    localBishopNameLength = (localBishopInfo.indexOf('(') - 1) - localBishopNameLeft;
    localBishopName = localBishopInfo.substr(localBishopNameLeft, localBishopNameLength);
    localBishopCity = localBishopInfo.substr(0, localBishopInfo.indexOf('--'));

    // if the localBishopEmail is a web address, show the "submit through web" notification panel
    // (otherwise, update the "To:" box)
    if (localBishopEmail.indexOf('http://') != -1) {
      document.getElementById('submitThroughWebNotificationPanel-name1').innerHTML = localBishopName;
      document.getElementById('submitThroughWebNotificationPanel-name2').innerHTML = localBishopName;
      document.getElementById('submitThroughWebNotificationPanel-city').innerHTML = localBishopCity;
      document.getElementById('submitThroughWebNotificationPanel-address').innerHTML = localBishopEmail;
      document.getElementById('submitThroughWebNotificationPanel-address').href = localBishopEmail;
      document.getElementById('submitThroughWebNotificationPanel').style.display = 'block';
    }
    else {
      // hide the "submit through web" notification panel
      document.getElementById('submitThroughWebNotificationPanel').style.display = 'none';

      if ((emailToBishopsBaseText.indexOf(localBishopEmail) == -1) && (document.getElementById('sendtolocalbishop').checked)) {
        // changed from: if ((localBishopEmail == 'mailbox@dioceseoflacrosse.com') || (localBishopEmail == 'srkeen@diobpt.org')) {
        // parse out the name of the bishop from the Diocese List text, and write a string that looks like an email address

        emailToLocalBishopSlot = document.getElementById('email_to_local_bishop_slot');

        // line changed to use innerHTML instead of value - document.getElementById('emailToBishopsSlot').value =
        //    localBishopName + " <" + localBishopEmail + ">, " + emailToBishopsBaseText;

        document.getElementById('emailToBishopsSlot').innerHTML =
            '"' + localBishopName + '" &lt;' + localBishopEmail + '&gt;,<br /> ' + emailToBishopsBaseText;
      }
    }
  };


var changeDioceseList = function(state) {
  var stateBishops = bishopEmails[state];
  var bishopsBox = document.getElementById('bishops_list');

  // clear bishops from list
  for (var i=bishopsBox.options.length-1; i >= 0; i--) {
    bishopsBox.options[i] = null;
  }

  if (typeof(stateBishops) == "undefined") {
	bishopsBox.parentNode.style.display = 'none';
  }
  else {
	  stateBishops = stateBishops.sort();
	  // add new bishops from master list
	  for (var i=0; i < stateBishops.length; i++) {
	    // add the bishop to the Diocese List
	    bishopsBox.options[i] = new Option(stateBishops[i][0], stateBishops[i][1]);
	    bishopsBox.disabled=false;
	  }
	  bishopsBox.parentNode.style.display = 'inline';

	  // blink the Diocese List area, to bring attention to the updated list
      document.getElementById('bishops_list_container').className = 'highlighted';
      setTimeout("document.getElementById('bishops_list_container').className = ''", 750);
  }
}


var addOnLoadHandler = function(f) {
  var prev=window.onload;
  window.onload=function(){ if(prev)prev(); f(); }
}


// when the document loads, we need to take care of a couple things:
//  - populate the state list
//  - link up the state list to the diocese list (and the State box on the form)
addOnLoadHandler(function() {
  var statesBox = document.getElementById('state_list');
  var dioceseBox = document.getElementById('bishops_list');
  var sendToLocalBishopCheckbox = document.getElementById('sendtolocalbishop');


  // make an array of state names
  var i = 0;
  statesList = []  
  for (b in bishopEmails) {
    if (typeof(bishopEmails[b]) == "object"){ statesList[i++] = b}
  }
  statesList.sort()

  // now add the states to the state box
  i = 1;
  for (s in statesList) { statesBox[i++] = new Option(statesList[s]) }

  // get the saved value for the selected state, if any
  statePlaceholder = document.forms.postcard.bishop_state;
  if (statePlaceholder.value != '') {
    statesBox.value = statePlaceholder.value;
  }
  statesBox.name = statePlaceholder.name;
  statePlaceholder.removeAttribute('name');

  // add onchange handler to bishops box
  dioceseBox.onchange = handleDioceseBoxChange;

  // add onchange handler to send to local bishop checkbox
  // can't access onchange handler through DOM -- unaesthetically added to the HTML instead
   sendToLocalBishopCheckbox.onchange = handleSendToLocalBishopCheckbox;

  // add onchange handler to states box
  statesBox.onchange = function() {
    state = this.options[this.selectedIndex].innerHTML;
	if (state != "Select State") {
    	document.forms.postcard.state.value=state;
	}
    	changeDioceseList(state);
    	handleDioceseBoxChange();
  };


  // update the list of Dioceses
  statesBox.onchange();

  // select the saved Diocese, if any
  diocesePlaceholder = document.forms.postcard.bishopID;
  if (diocesePlaceholder.value != '') {
    dioceseBox.value = diocesePlaceholder.value;
  }
  dioceseBox.name = diocesePlaceholder.name;
  diocesePlaceholder.removeAttribute('name');

})


