
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 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 hasMortgage()
{
  if($('has_mortgage_yes').checked)
  {
    $('tr_mortgage_repayment').show();
  }
  else
  {
    $('tr_mortgage_repayment').hide();
  }
}

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; },this);
  return match;
}

sfHover = function() {
  if(document.getElementById("nav")==null) return;
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	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);

function resetDOBField(element)
{
  if(element.value == 'day' || element.value == 'month' || element.value == 'year')
  {
    element.value = '';
  }
}

function cyclingSickPay()
{
  var show = $('sick-pay-yes').checked;
  $$('.cycling-sick-pay').each(function(element)
  {
    if(show)
    {
      element.show();
    }
    else
    {
      element.hide();
    }
  });
}
