function openNewPhoneSelect () {
  slideDivOpen('select_phone_new'); 
  slideDivClosed('select_phone_transfer');
}
function openTransferPhoneSelect () {
  
  slideDivOpen('select_phone_transfer'); 
  slideDivClosed('select_phone_new');
}
function showDetails(detailType, url, width, height) {
  if(detailType == "plan_suite_desc"){
    if(document.formShowPlan.suite_name.selectedIndex > 0){
      url += "#" + escape(replChar(document.formShowPlan.suite_name[document.formShowPlan.suiteword_name.selectedIndex].value, " ", "+"))
    }
  }
  openWind(url, width, height);
}

function openWind(url, width, height) {
  var WinName = "" + width + "x" + height
  var remote = eval("window.open(url, WinName, 'scrollbars=yes,width=" + width + ",height=" + height + "')");
  remote.focus();
}
function slideDivOpen (divName) {
  if($(divName).visible() == false) {
    Effect.toggle(divName,'blind',{duration:0.2});
  }
}
function slideDivClosed (divName) {
  if($(divName).visible() == true) {
    Effect.toggle(divName,'blind',{duration:0.2});
  }
}

function submitLOA(type){
  msg = "";
  
  // Checkboxes
  msg = "";
  if (!$('loa_agree_to_local').checked) {
    msg += "Check Box #1 must be checked\n";
  }
  if (!$('loa_agree_to_toll').checked) {
    msg += "Check Box #2 must be checked\n";
  }
  if (!$('loa_agree_to_long_distance').checked) {
    msg += "Check Box #3 must be checked\n";
  }
  
  // First and Last name fields
  if((remove($('loa_sig_first_name').value, " ")).length < 1) {
    msg += "First Name field is not valid\n";
  }
  if((remove($('loa_sig_last_name').value, " ")).length < 1) {
    msg += "Last Name field is not valid\n";
  }
  
  // Birthdate
  if ($('loa_dob_2i').selectedIndex == 0) {
    msg += "Birth Month field is required\n";
  }
  if ($('loa_dob_3i').selectedIndex == 0) {
    msg += "Birth Day field is required\n";
  }
  if ($('loa_dob_1i').selectedIndex == 0) {
    msg += "Birth Year field is required\n";
  }
  
  if(msg == ""){
    if(type == "print") {
      window.print();
    }
    return true;
  }else{
    if(type == "print") {
      alert("Although you opted not to submit the Letter of Authorization online we ask that you please complete the form BEFORE printing in order to esure that your form is legible and accurate. The information you print will NOT be submitted during the signup process and you will need to sign and mail/fax the form to us.\n\n" + msg);
    } else {
      alert("Before continuing, please address the following errors:\n\n" + msg);
    }
    return false;
  }
}
function setUpPhones () {
  if($('transfer_phone_radio') != null) {
    $$('.phoneselect').each(function(divnode) {
      divnode.hide();
    });
    if($('transfer_phone_radio').checked) { openTransferPhoneSelect();}
    if($('new_phone_radio').checked) { openNewPhoneSelect();}
    $('transfer_phone_desc').onclick = function() { $('transfer_phone_radio').checked = 1; openTransferPhoneSelect();};
    $('new_phone_desc').onclick = function() { $('new_phone_radio').checked = 1; openNewPhoneSelect();};
    $('new_phone_radio').onclick = function(){ openNewPhoneSelect() };
    $('transfer_phone_radio').onclick = function(){ openTransferPhoneSelect() };
  }
}

function setUpDirectory() {
  if($('directory_list_or_publish') != null) {
    directoryToggleFields();
    $('directory_list_or_publish').onchange = directoryToggleFields;
  }
}
function directoryToggleFields () {
  if($F('directory_list_or_publish') == 'none') {
    $('directory_first_name').value = "";
    $('directory_first_name').disable();
    $('directory_last_name').value = "";
    $('directory_last_name').disable();
  } else {
    $('directory_first_name').enable();
    $('directory_last_name').enable();
  }
}
function openSpin() {
  if($('spinner') == null) {
    $('plan_wizard').insert({before: '<div id="spinner" class="spinner" style="display:none;"><img src="/images/spinner.gif"><p>Loading</p></div>'});
  }
  $('plan_wizard').hide()
  $('spinner').show()
}
function closeSpin() {
  $('spinner').hide()
  $('plan_wizard').show()
}

/* Removes chars from str.
   Returns cleaned str.
   AF*/
function remove(str, chars) {
  var newStr = "";
  for (var i = 0; i < str.length; i++)
    if (chars.indexOf(str.charAt(i)) == -1) newStr += str.charAt(i);
  return newStr;
} // remove()


function getPlanWizard(url, fn) {
  new Ajax.Request(url, {
		asynchronous: true,
		evalScripts: true,
    method: 'get',
    onLoading: openSpin,
    onComplete: function() {
      closeSpin();
      if (fn) { fn();}
      }
  });
}

/* Checks the length of the field to ensure it's not more than the specified length.
 * If it's great than len, then it moves the cursor to the next field
 */
function handleFieldKey(obj, len, next) {
  if(obj.value.length >= len) {
    currentFieldCount = 0;
    next.activate();
  }
}

// Reloads a select box with new data
// 'options' must be in the format passed to options_for_select, that is:
//   [[label1, value1], ...]
// If you pass in a currently selected value, it will mark that item as selected.
function reload_select(select_name, new_options, currently_selected) {
    // First clear the old options
    $(select_name).options.length = 0;
    // Now add the new entries
    new_options.each(function(name, index) {
        defSelected = false;
        selected    = (name[1] == currently_selected);
        new_option  = new Option(name[0], name[1], defSelected, selected);
        $(select_name).options[index] = new_option;
    });
		$(select_name).enable();
}

// Run on-load for the virtual number selection page to add event listeners to the drop downs.
function vnumDropDowns() {
	$('virtual_number_country_code', 
		'virtual_number_state_code',
		'virtual_number_npa',
		'virtual_number_did_type_id').each(function(box) {
		if(box) {
			box.observe('change', submitVnum)
		}
	})
}

// Clears the dropdowns below the one that is currently below the one selected.
// Fall-through between cases is intentional
function clear_dropdowns(el) {
	function set_loading(load_el) {
		$(load_el).options.length=0;
		$(load_el).options[0] = new Option("Loading", "", false, true);
		$(load_el).disable();
	}
	$('available_phone_numbers').innerHTML="";
	switch(el.id){
		case "virtual_number_country_code":
			set_loading('virtual_number_state_code');
			$('virtual_number_npa').options.length=0;
			$('virtual_number_did_type_id').options.length=0;
			break;
		case "virtual_number_state_code":
			set_loading('virtual_number_npa');
			$('virtual_number_did_type_id').options.length=0;
			break;
		case "virtual_number_npa":
			set_loading('virtual_number_did_type_id');
			break;
		case "virtual_number_did_type_id":
			$('available_phone_numbers').innerHTML="<p>Loading Available Phone Numbers...</p>";
	}	
}

// Handles both the next button and the drop-down on-change event in the virtual number selection page
function submitVnum(event) {
	if(event != null) {
		ddown = Event.element(event);
		clear_dropdowns(ddown);
	}
	new Ajax.Request('/virtual_number_selection/virtual_number', {
		asynchronous:true,
		evalScripts:true,
		parameters:Form.serialize($('virtual_number_form'))});
	return false;
}
