Event.observe(window,'load', function() {
  if($('request-live-help'))
  {
    $('request-live-help').observe('click', function(event)
    {
      event.stop();
      this.newWindow = window.open('/live-help', 'Lifebroker - Live Help', 'toolbar=no, location=no, directories=no, menubar=no, status=no, scrollbars=no, resizable=yes, replace=no, width=400, height=400');
      this.newWindow.focus();
      this.newWindow.opener=window;
    });
  }
});

var opened = null;

function clickInfo(id,box_id)
{
  if(opened != null)
  {
    closeInfo(opened);
  }
  opened = box_id;
  
  var box_element = $(box_id);
  var position = $(id).cumulativeOffset();
  position = position[1];
  
  var parent = $('content-wrap3');
  var right_div = $('right_editable');
  
  var top_minimum = right_div.cumulativeOffset();
  var top_minimum = top_minimum[1] + right_div.getHeight();
  
  var bottom_max = parent.cumulativeOffset();
  var bottom_max = parent.getHeight() + bottom_max[1];

  if(position + box_element.getHeight() > bottom_max)
  {
    position = bottom_max - box_element.getHeight();
  }
  else if(position < top_minimum)
  {
    position = top_minimum;
  }
  
  box_element.style.display = 'block';
  box_element.style.top = position + 'px';
  
  return false;
}

function closeInfo(box_id)
{
  $(box_id).style.display = 'none';
}

function chooseOther(el,id)
{
  if(el.value=='other')
  {
    el.selectedIndex = 1;
    $(id).enable();
    $(id).removeClassName('disabled');
    $(id).focus();
  }
  else
  {
    $(id).value = '';
    $(id).disable();
    $(id).addClassName('disabled');
  }
}

function showDIV(id)
{
  $(id).style.display = 'block';
}

function hideDIV(id)
{
  $(id).style.display = 'none';
}

function hoverOver(element)
{
  Element.extend(element);
  element.addClassName('hover');
}

function checkChild(element)
{
  var childinput = element.getElementsByTagName('input');
  if(childinput[0].checked == true)
  {
    childinput[0].checked = false;
  }
  else
  {
    childinput[0].checked = true;
  }
  
}

function toggleCheckbox(event)
{
  var element = Event.element(event);
  console.log(element);
  console.log('Test');
  Event.stop(event);
  //alert('hello!');
  $('')
}

function stopClick(event)
{
  Event.stop(event);
  console.log('stopping event');
  //alert('hello!');
}

function hoverOut(element)
{
  Element.extend(element);
  element.removeClassName('hover');
}

function callMeNow(button)
{
  if($('call-me-now')&&button)
  {
    //Set up alt right column
    var height = $('right-col').getHeight();
    $('right-col-alternative').style.height = height + 'px';
    $('right-col').style.display = 'none';
    $('right-col-alternative').style.display = 'block';
    
    //Look for button on the left column
    var button_on_left = false;
    $$('div#left-col .' + button.className).each(
      function (element) {
      button_on_left = true;
      }
    );
    
    //Only perform positioning if button on left column
    if(button_on_left)
    {
      //Position call-me-now button
      var left_x = $('left-col').cumulativeOffset();left_x = left_x[1];
      var button_x = button.cumulativeOffset();button_x = button_x[1];
      var box_height = $('call-me-now').getHeight();
      var box_margin = ((button_x - left_x)-box_height/2);
      if(box_margin + box_height > $('left-col').getHeight())
      {
        box_margin = $('left-col').getHeight() - box_height;
      }
      $('call-me-now').style.margin = box_margin + 'px 0 0 0';
    }
    
    $('call_name').focus();
    
    return false;
  }
  return true;
}

function prepareSurveyQuestionLogic()
{
  observe.each(function(pair){
  if($('element_e' + pair.value.condition_element))
  {
    pair.value.event($('element_e' + pair.value.condition_element).value);
    $('element_e' + pair.value.condition_element).observe('change', pair.value.event);
  }
  else if($('element_e' + pair.value.condition_element + '_yes'))
  {
    if($('element_e' + pair.value.condition_element +'_yes').checked)
      pair.value.event('yes');
    else if($('element_e' + pair.value.condition_element +'_no').checked)
      pair.value.event('no');
      
    $('element_e' + pair.value.condition_element + '_yes').observe('click', pair.value.event);
    $('element_e' + pair.value.condition_element + '_no').observe('click', pair.value.event);
  }
});

  //Add event to track page changes
  $$('.survey .element input, .survey .element textarea, .survey .element select').each(function(element)
  {
    element.observe('change', function(){changes=true});
  });
  
  //Add event to confirm navigation without saving
  $$('.survey #wrapper a').each(function(element)
  {
    element.observe('click', function(event){if(changes&&!confirm('Information on this page will be lost if you navigate away without submitting your information. Click OK if you want to proceed without saving.'))event.stop()});
  });
}

function surveyLogicCompare(value,reference)
{
  var match = false;
  var value = value.toLowerCase().replace(/\s/g,'');
  var reference = reference.split(',');
  reference.each(function(test) 
    {
      if(test==value) match = true;
      if(test.substr(0,1)=='!' && test.substr(1)!=value) match = true;
    },this);
  return match;
}

//Occupation Hinting
function startHint(element,parameters)
{
  element.addClassName('loading');
  return parameters;
}

function displayHint(element)
{
  element.removeClassName('loading');
}

//Optional field hide/display
function setupOptionalForm(event)
{
  $$('.btn-optional-quote').each(function(button)
  {
    button.observe('click',updateOptionalBoxes);
  });
}

//Optional field hide/display
function setupOptionalInsuranceForm()
{
  Event.observe('include_quote', 'click', function()
  {
    updateOptionalInsurance();
  });
  updateOptionalInsurance();
}

function updateOptionalInsurance()
{
  $$('.optional').each(function(option)
  {
    var element = $('include_quote');
    if(element.checked)
    {
      option.style.display = "block";
    }
    else
    {
      option.style.display = "none";
    }
  });
}

function updateOptionalBoxes(event)
{
  var visible = false;
  event.stop();
  button = event.element();
  if(button.hasClassName('on'))
  {
    button.removeClassName('on');
  }
  else
  {
    visible = true;
    button.addClassName('on');
  }
  $$('.optional').each(function(option)
  {
    if(visible)
    {
      option.style.visibility = "visible";
    }
    else
    {
      option.style.visibility = "hidden";
    }
  });
}

//Track quote wizard changes
function trackQuoteWizardChanges()
{
  //Add event to track page changes
  $$('#left-col form input, #left-col form textarea, #left-col form select').each(function(element)
  {
    element.observe('change', function(){changes=true});
  });
  
  //Add event to confirm navigation without saving
  $$('#wrapper a').each(function(element)
  {
    element.observe('click', function(event)
    {
      var anchor = event.element();
      if(changes && anchor.href!='' && anchor.href!='#' && anchor.href!=null && !(anchor.href==document.location || anchor.href==document.location+'#'))
      {
        if(!confirm('Information on this page will be lost if you navigate away without submitting your information. Click OK if you want to proceed without saving.'))
        {
          event.stop()
        }
      }
    });
  });
}

sfHover = function() {
	var sfEls = $$('#nav li, #share-wrap');
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);