jQuery(document).ready(function() {
  // set the initial default text in the investment input box
  jQuery("#investment").val("By investment name");
  
  // change the text to black and clear the default text
  jQuery("#investment").focus(function() {
    jQuery(this).removeClass("default-input-text");
    jQuery(this).addClass("non-default-input-text");
    if(jQuery(this).val() == "By investment name")
    {
      jQuery(this).val("");
    }
  });
  
  // change the text back to grey and put the default back in if nothing has been entered
  jQuery("#investment").blur(function() {
    jQuery(this).removeClass("non-default-input-text");
    jQuery(this).addClass("default-input-text");
    if(jQuery(this).val() == "")
    {
      jQuery(this).val("By investment name");
    }
  });
  
  // add a submit handler that clears the value of the investment input box if not used
  jQuery("#fund-search-home").submit(function() {
    if(jQuery("#investment").val() == "By investment name" || jQuery("#investment").val() == "")
    {
      jQuery("#investment").attr('disabled', true);
    }
    
    return true;
  });
    
  // populate the select boxes when the page loads since Matrix can't do it
  var companyid = getUrlParam("companyid");
  var sectorid = getUrlParam("sectorid");
  
  if(companyid != "")
  {
    jQuery("#companyid").val(companyid);
  }
  
  if(sectorid != "")
  {
    jQuery("#sectorid").val(sectorid);
  }
  
  // if either select box is changed, reset the other
  jQuery("#companyid").change(function() {
    if(jQuery(this).parents(".wealth150").length == 0) {
      jQuery("#sectorid option[value='']").attr("selected", "selected");
    }
  });

  jQuery("#sectorid").change(function() {
    if(jQuery(this).parents(".wealth150").length == 0) {
      jQuery("#companyid option[value='']").attr("selected", "selected");
    }
  });
});


function getUrlParam(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
  {
    return "";
  }
  else
  {
    return results[1];
  }
}
