function build_data_url(url) {
  url = new String(url)
  $$('.nav select').each(function(el) {
    url = url.replace(new RegExp("_" + el.id + "_id_"), el.value)
  });
  return url;
}

function reload_chart_xml(url) {
  getChartFromId('chart').setDataURL(url);
}

function reload_analysis_data(url) {
  new Ajax.Request(url, {method: 'get'})
}

jQuery(document).ready(function() {
  jQuery("table tr td #check_all").click(function() {
    var status = this.checked;
    jQuery(this).parent().parent().parent().find("input:checkbox").each(function() {
      if (jQuery(this).attr("id") != 'check_all')
        this.checked = status;
    });
  });
  
  jQuery(".set_check_all").click(function() {
    var status = this.checked;
    
    if (this.id == "criteria_check_all")
    {
      jQuery("input[name^='resource_ids']").each(function() {
        this.checked = status;
      });
    }
    else if (this.id == "skill_check_all")
    {
      jQuery("input[name^='skill_ids']").each(function() {
        this.checked = status;
      });
    }
    
    jQuery(this).parent().parent().next().find("input:checkbox").each(function() {
      if (jQuery(this).attr("id") != 'set_check_all')
      {
        this.checked = status;
        setSelectShowHideForm(this.name);
      }
    });
  });
  
  
  jQuery("p span.field-with-errors").each(function(el) {
    jQuery(this).parent().css('background', '#a4d94b');
  });

  jQuery("#feature_search_input").focus();

  if (jQuery.browser.msie) {
    jQuery(".topleft, .topright, .bottomleft, .bottomright").hide();
  };  
});

/*
  The following three functions are used to filter out
  complete/incomplete/etc. features from the Scoring page.
*/
jQuery(document).ready(function() {
  jQuery("#hide_complete").click(function(el) {
    
    if (this.checked)
    {
      jQuery("#show_only_complete").attr("checked", false);
      jQuery("#show_only_incomplete").attr("checked", false);
    }
    
    do_search(jQuery("#feature_search_input").val());      
  });
});

jQuery(document).ready(function() {
  jQuery("#show_only_complete").click(function(el) {
    
    if (this.checked)
    {
      jQuery("#hide_complete").attr("checked", false);
      jQuery("#show_only_incomplete").attr("checked", false);
    }

    do_search(jQuery("#feature_search_input").val());
  });
});

jQuery(document).ready(function() {
  jQuery("#show_only_incomplete").click(function(el) {
    
    if (this.checked)
    {
      jQuery("#hide_complete").attr("checked", false);
      jQuery("#show_only_complete").attr("checked", false);
    }
    
    do_search(jQuery("#feature_search_input").val());
  });
});

jQuery(document).ready(function() {
  jQuery("#feature_group_selector").change(function(el) {
    do_search(jQuery("#feature_search_input").val());
  });
});

function indexOfFrom(char, start, string)
{
  for (var i = start; i < string.length; i++)
  {
    if (string.charAt(i) == char)
      return i;
  }
  return -1;
}

function subsequence(needle, haystack)
{
  needle = needle.toLowerCase();
  haystack = haystack.toLowerCase();
  var i = 0, j = 0;
  
  for (i = 0; i < needle.length; i++)
  {
    j = indexOfFrom(needle.charAt(i), j, haystack);
    if (j < 0)
      return false;
  }
  return true;
}

var last_called = 0;
jQuery(document).ready(function() {
  jQuery("#feature_search_input").keyup(function(el) {
    var text = this.value;
    last_called = (new Date()).getTime();
    setTimeout(function() { do_search(text) }, 260);
  });
});

function do_search(text)
{
  if ((new Date()).getTime() - last_called < 250)
    return;
  
  text = text.toLowerCase();
  
  var mode = 0;
  if (jQuery("#hide_complete").attr("checked"))
    mode = 1;
  else if (jQuery("#show_only_complete").attr("checked"))
    mode = 2;
  else if (jQuery("#show_only_incomplete").attr("checked"))
    mode = 3;
  
  var feature_group_id = jQuery("#feature_group_selector").val();
  
  jQuery("li.planning_item, tr.planning_item, div.planning_item").each(function() {
    
    var other_boolean = true;
    if (mode == 1)
    {
      other_boolean = !jQuery(this).hasClass("completed_item");
    }
    else if (mode == 2)
    {
      other_boolean = jQuery(this).hasClass("completed_item");
    }
    else if (mode == 3)
    {
      other_boolean = jQuery(this).hasClass("incomplete_item");
    }
    
    if (feature_group_id != null && feature_group_id != "all")
    {
      other_boolean = other_boolean && jQuery(this).find("span.pig_id").text() == feature_group_id
    }
    
    var main_boolean = false;
    var regex = /^\".*\"$/;
    
    if (text.match(regex))
      main_boolean = this.getAttribute("alt").toLowerCase().match(text.substr(1,text.length-2));
    else
      main_boolean = subsequence(text, this.getAttribute("alt"));
    
    if (main_boolean && other_boolean)
      jQuery(this).show();
    else
      jQuery(this).hide();
  });
}
