// HEY@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function auto_complete_on_select(element, selectedElement)
{
  var entityParts = selectedElement.id.split('::');
  var entityType  = entityParts[0];
  var entityId    = entityParts[1];
  var per_id 	  = entityParts[2];
  var office      = entityParts[3];
  //messy, but seems to be neccessary
  //comes in as Name (uid)Office
  var parts = element.value.split(')');
  var display_name = parts[0] + ')';

  document.getElementById(entityType).value = per_id;
  document.getElementById(entityType+'_name').innerHTML = display_name +
       "<a href=\"#\" onclick=\"remove_cla_person('"+entityType+"');\">[x]</a>";
  if(office != '') {
    document.getElementById(entityType+'_name').innerHTML += '<br /><span class="office">' + office + '</span>'	
  }
  element.value = '';
}

function multiple_auto_complete_on_select(element, selectedElement)
{
  var entityParts = selectedElement.id.split('::');
  var entityType  = entityParts[0];
  var entityId    = entityParts[1];
  var per_id 	  = entityParts[2];

  //messy but seems neccessary
  //comes in as Name (uid)Office
  var parts = element.value.split(')');
  var display_name = parts[0] + ')';

//  alert(entityType + '/' + entityId + '/' + per_id)
  options = document.getElementById(entityType).innerHTML;
  options = options += '<option value="'+ per_id +'">'+display_name+'</option>';
  document.getElementById(entityType).innerHTML = options;
  element.value = '';
}
function add_on_submit_to_form_for_multiple_auto_complete(field)
{
	var form = document.getElementsByTagName("form")[0];
	form.setAttribute("onsubmit", "select_all_for_multiple_auto_complete('"+field+"');");
}

function select_all_for_multiple_auto_complete(field)
{
	select = document.getElementById(field);
	for(var i = 0; i < select.length; i++) {
		select.options[i].selected = "selected";
	}
}

function delete_from_multiple_select(field) {
	select = document.getElementById(field);
	//$A() is a prototype helper that turns the passed collection into an array
	opts = $A(select.options);
	
	for(var j = 0; j < opts.length; j++) {
		if(opts[j].selected) {
			select.remove(j);
			
			//need to rebuild the options array, because the all down in index after a remove
			opts = $A(select.options);
			
			//go back one to compensate for the index shift (could start back at zero, but needless (and inefficient))
			j = j-1; 
		}
	}
}

function show_advanced_search_div(id, anchor) {
  Effect.BlindDown($(id));
  anchor.innerHTML = 'Basic search';
  anchor.setAttribute('onclick', "hide_advanced_search_div('"+ id +"', this)");
}

function hide_advanced_search_div(id, anchor) {
  Effect.BlindUp($(id));
  anchor.innerHTML = 'Advanced search';
  anchor.setAttribute('onclick', "show_advanced_search_div('"+ id +"', this)");
}

function show_div(id, anchor) {
  Effect.BlindDown($(id));
  anchor.innerHTML = '<img src="/images/btnMinus.png" />';
  anchor.setAttribute('onclick', "hide_div('"+ id +"', this)");
}
function hide_div(id, anchor) {
  Effect.BlindUp($(id));
  anchor.innerHTML = '<img src="/images/btnPlus.png" />';
  anchor.setAttribute('onclick', "show_div('"+ id +"', this)");
}

function remove_cla_person(entityType)
{
	document.getElementById(entityType).value = '';
	document.getElementById(entityType+"_name").innerHTML = '';
}

/************************************************************
   Search Term Highlighting Functions 
   Written By: Matt Vermaak
   Purpose: These Functions Are Used to Add Highlighting to 
            Search Terms on the DCL show details and show 
            results pages
**************************************************************/
 function highlight(words_string, within, type)
 {
   // Make a better regexp
   words_string = words_string.replace(/\//g, "");
   words_string = words_string.replace(/</g, "");
   words_string = words_string.replace(/>/g, "");  
   temp = words_string.split(" ");
   if(type == "class")
   {
     if(data = document.getElementsByClassName(within))
       for(i = 0; i < data.length; i++)
         if(children = data[i].childNodes)
           for(j = 0; j < children.length-1; j++)
             if(data[i].childNodes[j].innerHTML) data[i].childNodes[j].innerHTML = highlightTextIgnoringHTML(data[i].childNodes[j].innerHTML, temp); 
   }   
   else if(type == "id")
   {
     if(data = document.getElementById(within))
       if(children = data.childNodes)
         for(j = 0; j < children.length-1; j++)
           if(data.childNodes[j].innerHTML) data.childNodes[j].innerHTML = highlightTextIgnoringHTML(data.childNodes[j].innerHTML, temp); 
   }   
  }

 function highlightTextIgnoringHTML(old_text, words_array)
 {
   plain_text_array = ignoreHTML(old_text);
   html_text_array = gatherHTML(old_text);
   new_text = "";   

   even_within = false;
   for(t = 0; t < words_array.length; t++)
     if(old_text.toLowerCase().indexOf(words_array[t].toLowerCase()) > 0)
        even_within = true;
   
   if(!even_within) return old_text;

   for(k = 0; k < plain_text_array.length; k++)
   {
     for(l = 0; l < words_array.length; l++)
     {
       if(plain_text_array[k].replace(/\s+/g, '').length == 0 || plain_text_array[k] == null) continue;
       // TODO Remove the hardcoded mod out. (REM you mod out by the number of highlight colors.
       plain_text_array[k] = wrap_occur(plain_text_array[k], words_array[l], "<span class='highlight" + (l % 2)+ "'>", "</span>");
     }     

     if(html_text_array.length > 0 && html_text_array[k]) new_text += plain_text_array[k] + html_text_array[k];
     else new_text += plain_text_array[k];
   }
   return new_text; 
 }

 function ignoreHTML(from_string)
 {
   text_inside = from_string.split(/<.*?>/);
   return text_inside;
 }
 
 function gatherHTML(from_string)
 {
   html_inside = new Array(count_occur(from_string, "<"));
   html_index = 0;
   while(from_string.indexOf("<") != -1 && from_string.indexOf(">") != -1)
   {
     open_html_marker = from_string.indexOf("<");
     close_html_marker = from_string.indexOf(">");

     html_inside[html_index] = '<' + from_string.substring(open_html_marker+1, close_html_marker) + '>';
     html_index++;

     from_string = from_string.substring(0, open_html_marker-1) + from_string.substring(close_html_marker+1, from_string.length);  
   }
   return html_inside; 
 }

 function count_occur(from_string, character_to_count)
 {
   if(character_to_count == "") return 0;
   count = 0;
   cur_position = from_string.indexOf(character_to_count);
   while( cur_position != -1 )
   {
     count++;
     cur_position = from_string.indexOf(character_to_count, cur_position + character_to_count.length);
   }
   return count;
 }

 function wrap_occur(old_string, string_to_replace, replacement_preface, replacement_suffix)
 {
   if(count_occur(old_string.toLowerCase(), string_to_replace.toLowerCase()) == 0) return old_string;
  

   new_string = "";
   old_position = 0;
   cur_position = old_string.toLowerCase().indexOf(string_to_replace.toLowerCase());
   while( cur_position != -1)
   {
     replacement_string = replacement_preface + old_string.substr(cur_position, string_to_replace.length) + replacement_suffix;
     new_string += old_string.substring(old_position, cur_position) + old_string.substr(cur_position, string_to_replace.length).replace(new RegExp(string_to_replace, "i"), replacement_string); 
     old_position = cur_position + string_to_replace.length; 
     cur_position = old_string.toLowerCase().indexOf(string_to_replace.toLowerCase(), cur_position + string_to_replace.length);
   }
   if(old_position < old_string.length) new_string += old_string.substring(old_position, old_string.length);
   return new_string; 
  }
  
  function colorHighlights()
  {
    for(highlight_index = 0; highlight_index < 2; highlight_index++)
    {
      possible_colors = new Array("#FFFFA0", "#FFB6C1");
      
      attr = "backgroundColor";
      value = possible_colors[highlight_index % 2]; 
      theClass = ".highlight" + (highlight_index % 2);
     
      var cssRules;
      if (document.all) cssRules = 'rules';
      else if (document.getElementById) cssRules = 'cssRules';
      
      for (var s = 0; s < document.styleSheets.length; s++)
        for (var r = 0; r < document.styleSheets[s][cssRules].length; r++) 
          if (document.styleSheets[s][cssRules][r].selectorText == theClass)
          {
            if(document.styleSheets[s][cssRules][r].style[attr] != "transparent") document.styleSheets[s][cssRules][r].style[attr] = "transparent";
            else document.styleSheets[s][cssRules][r].style[attr] = value;
          }
    }
  }

/* End of search Highlighting... */
