/* note: the brackets '[]' seem to prevent referencing with prototype $("aofs[]") */ 

function aof_selectall() {
   col = $("aofs[]");
   len = col.length;

   for(i=0; i < len; i++) {
     x = col.options[i].value;
     y = col.options[i].innerHTML;
     col.options[i].value = x + '|^|' + y;      
     col.options[i].selected = true;      
   }
}

function moveUp(element) {
  for(i = 0; i < element.options.length; i++) {
    if(element.options[i].selected == true) {
      if(i != 0) {
        var temp = new Option(element.options[i-1].text,element.options[i-1].value);
        var temp2 = new Option(element.options[i].text,element.options[i].value);
        element.options[i-1] = temp2;
        element.options[i-1].selected = true;
        element.options[i] = temp;
      }
    }
  }
  prepareAof();
}

function moveDown(element) {
  for(i = (element.options.length - 1); i >= 0; i--) {
    if(element.options[i].selected == true) {
      if(i != (element.options.length - 1)) {
        var temp = new Option(element.options[i+1].text,element.options[i+1].value);
        var temp2 = new Option(element.options[i].text,element.options[i].value);
        element.options[i+1] = temp2;
        element.options[i+1].selected = true;
        element.options[i] = temp;
      }
    }
  }
  prepareAof();
}

function prepareAof() {
  //col = document.fm_aof["aofs[]"]; 

  col = $("aofs[]"); 
  len = col.length; 
 	 
	serial = "";
	 	 
  delimiter = ''; 
  for(i=0; i < len; i++) { 
    var order = i+1;
    if ( i > 0 ) { delimiter = ','; }
    serial = serial + delimiter + col.options[i].value + ":" + order + ":"; 
 	} 
 	
  $('aof_serialized').value = serial;
}

function xMod() {
  col = $("aofs[]");

  if( Element.getStyle('allaofs', 'display') != "none") {
    colfrm = 'dummyaofs';
  } else {
    colfrm = 'filteraofs';
  }
  
  sl = col.selectedIndex;
  if ( sl != -1 ) {
    for (var i=0; i < col.options.length;i++) {
      if (col.options[i].selected) {
    	id  = col.options[sl].value;
		lbl = col.options[sl].innerHTML.replace(/&amp;/, "&");
		if ( !optionExistsInSource(id, colfrm) ) {
	  	  $(colfrm).options[$(colfrm).options.length] = new Option(lbl, id);	
		}
      	col.options[sl]=null;
      	i--;
      }
    }
  } else {
    alert("Please select an Area of Focus first.");
    return false;
  }

  prepareAof();
  return false;
}

function optionExistsInSource(val, fld){
  for (var i=0; i < $(fld).options.length;i++) {
  	if ( $(fld).options[i].value == val ) {
  		return true;
  	}
  }
  return false
}

function addMod() {
  col = $("aofs[]");

  idx = col.length

  if( Element.getStyle('allaofs', 'display') != "none") {
    colfrm = 'dummyaofs';
  } else {
    colfrm = 'filteraofs';
  }

  if ($(colfrm).selectedIndex == -1) {
  	alert("Please select an Area of Focus first.");
    return false;
  }
  
  anyDupes = false;
  while ($(colfrm).selectedIndex != -1) {
	sl = $(colfrm).selectedIndex;
    thereYet = false;
    id  = $(colfrm).options[sl].value;
	lbl = $(colfrm).options[sl].innerHTML.replace(/&amp;/, "&");
	//check if this option has already been selected
	for(j=0; j < idx; j++) {
      if ( col.options[j] && col.options[j].value == id ) {
	    thereYet = true;
	    anyDupes = true;
      }
    }
    if ( col && !thereYet ) {
//      col.options[idx] = new Option(lbl, id);
      if ( col.selectedIndex == -1 ) {
      	col.options[idx] = new Option(lbl, id);
      } else {
      	insertOption(col, lbl, id);
      }
      idx++;
    }
    $(colfrm).options[sl].selected=false;
    $(colfrm).remove(sl);;
  }
  if ( anyDupes ) { alert('One or more Areas of Focus were already selected, so they were not added again'); }

  prepareAof();
  return false;
}

function insertOption(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      if (newCount == theSel.selectedIndex) {
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newCount++;
        newSelected = newCount;
      }
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function makeToggle(masterid) {
  theDiv = "details_" + masterid;
  var imgPath = '/images/info.gif';
  output = '<a onclick="Element.toggle(&quot;' + theDiv + '&quot;);"><img class="infoicon" src="' + imgPath + '" alt="Info" /></a>';
  return output;
}

function updateJSONAofDetails(request, json){
  var responses = json;
  if (!json) {
    var responses = eval('(' + request.responseText + ')');
  }

  expandAllAofs(responses);
}

function hideAllAofs(aofData) {
  var children = $A($("aof_container").childNodes);

  $('toggle_all_link').innerHTML = "<a href=# onclick='showAllAofs();'>Show all Definitions</a>";

  children.each(function(child) {

    if (child.className == "aof_listing" ) {
      var masterid = child.id.split('aof')[1];
      var detailsDivId = "details_" + masterid;
      Element.hide(detailsDivId);
    }
  });  

}

function showAllAofs(aofData) {
  var children = $A($("aof_container").childNodes);

  $('toggle_all_link').innerHTML = "<a href=# onclick='hideAllAofs();'>Hide all Definitions</a>";

  children.each(function(child) {
    if (child.className == "aof_listing" ) {
      var masterid = child.id.split('aof')[1];
      var detailsDivId = "details_" + masterid;
      Element.show(detailsDivId);
    }
  });  

}

function expandAllAofs(aofData) {

  var children = $A($("aof_container").childNodes);

  children.each(function(child) {

     if (child.className == "aof_listing" ) {
       var masterid = child.id.split('aof')[1];
       var detailsDivId = "details_" + masterid;
       var infoLinkDivId = "info_link_" + masterid;
       var aofDetails = "";
       aofDetails += "<p><b>Definition: </b>" + aofData[masterid]['definition'] + "</p>";
       aofDetails += "<p><b>Keywords: </b>" + aofData[masterid]['keywords'] + "</p>";
       $(detailsDivId).innerHTML = aofDetails;
       Element.show(detailsDivId);
       $(infoLinkDivId).innerHTML = makeToggle(masterid);
     }
  });  

  $('toggle_all_link').innerHTML = "<a href=# onclick='hideAllAofs();'>Hide all Definitions</a>";

}

function aofTestForEnter(e) {
  var keyCode = e.keyCode;
  if ( keyCode == 13 ) {
    $('filter_button').click();
    return false;
  } else {
    return true;
  }
}

function makeInfoOnClick(id, scriptName) {
  new Ajax.Updater('details_' + id, scriptName + '/aof/expand/aof_masterid/' + id + '/', {asynchronous:true, evalScripts:false, onComplete:function(request, json){Element.hide('indicator'), $('info_link_' + id).innerHTML = makeToggle(id)}, onLoading:function(request, json){Element.toggle('details_' + id), Element.show('indicator')}});
}
