// JavaScript Document
// TSA TOOL


//
// DISPLAY HIDDEN SUB ROWS WITHIN RESULTS
function fn_openrows(ids_array, td_id)
{
  tot = ids_array.length
  array_st = "";
  for(i=0;i<tot;i++)
  {
    row_id = "hidden_row_"+ids_array[i];
    if(document.getElementById(row_id))
    {
      document.getElementById(row_id).style.display = '';

      if(array_st != "")
      {
        array_st += "', '";
      }
      array_st += ids_array[i];
    }
  }
  array_st = "'"+array_st+"'";


  //
  // CHANGE TD TO CLOSE
  document.getElementById(td_id).innerHTML = '<a class="tsa_breakdown_close" href="#" onClick="fn_closerows(new Array('+array_st+'),\''+td_id+'\'); return false;" title="Close breakdown"></a>';
}

//
// DISPLAY HIDDEN SUB ROWS WITHIN RESULTS
function fn_closerows(ids_array, td_id)
{
  tot = ids_array.length
  array_st = "";
  for(i=0;i<tot;i++)
  {
    row_id = "hidden_row_"+ids_array[i];
    if(document.getElementById(row_id))
    {
      document.getElementById(row_id).style.display = 'none';

      if(array_st != "")
      {
        array_st += "', '";
      }
      array_st += ids_array[i];
    }
  }
  array_st = "'"+array_st+"'";

  //
  // CHANGE TD TO OPEN
  document.getElementById(td_id).innerHTML = '<a class="tsa_breakdown_open" href="#" onClick="fn_openrows(new Array('+array_st+'),\''+td_id+'\'); return false;" title="View breakdown"></a>';
}


//
// CALL TSA DISPLAY
function fn_display_results(path_to_root, lang)
{
  /*alert(path_to_root);
  */
  new Ajax.Updater('tsa_tool',path_to_root+'scripts/php/dm_TSA_display.php',
  {
    onComplete:function(request)
    {
      new Effect.Appear('tsa_tool');
      initialise_dhtml(); //
    },
    parameters:"path_to_root="+path_to_root+"&LANG="+lang,
    evalScripts:true,
    asynchronous:true
  }
  );

}

//
// VALIDATE TSA TOOL FORM
function fn_validate_tsa_tool(form, path_to_root, lang)
{

  list = document.forms['tsa_tool'].tsa01;
  tot_list = list.length;

  selected = 0;
  var ctry_list = ',';
  for (var idx = 0; idx < tot_list; idx ++)
  {
    if (list.options[idx].selected && list.options[idx].value != "")
    {
      selected = selected + 1;
      ctry_list += list.options[idx].value + ',';
    }
  }

  //
  // VALIDATE 01
  if(selected < 1)
  {
    alert("(01) Select a country / region");
    return (false);
  }
  else if(selected > 5)
  {
    alert("(01) Selections are limited to no more than five.");
    return (false);
  }

  //
  // VALIDATE 02
  t2data = "";
  for (var i=0; i < document.forms['tsa_tool'].tsa02.length; i++)
  {
    if (document.forms['tsa_tool'].tsa02[i].checked)
    {
      t2data = document.forms['tsa_tool'].tsa02[i].value;
    }

  }


  if(t2data == "")
  {
    alert("(02) Please select your data type.");
    return (false);
  }

  //
  // VALIDATE 03
  if(!document.forms['tsa_tool'].tsa03_from)
  {
    //
    // SINGLE DATE
    var date_from = '';
    var date_to = '';
    var date_date = document.forms['tsa_tool'].tsa03_date.value;
    date = document.forms['tsa_tool'].tsa03_date;
    if(date.value == "")
    {
      alert("(04) Please select your date.");
      return (false);
    }
  }
  else
  {
    //
    // DATE RANGE
    var date_from = document.forms['tsa_tool'].tsa03_from.value;
    var date_to = document.forms['tsa_tool'].tsa03_to.value;
    var date_date = '';

    date1 = document.forms['tsa_tool'].tsa03_from;
    date2 = document.forms['tsa_tool'].tsa03_to;
    if(date1.value == "")
    {
      alert("(04) Please select your 'from' date.");
      return (false);
    }
    if(date2.value == "")
    {
      alert("(04) Please select your 'to' date.");
      return (false);
    }
    if(date2.value < date1.value)
    {
      alert("(04) Please set the 'to' date after the 'from' date.");
      return (false);
    }

  }

  //alert(t2data);
  //
  // SUBMIT DATA

  new Ajax.Updater('tsa_tool',path_to_root+'scripts/php/dm_TSA_display.php',
  {
    onComplete:function(request)
    {
      new Effect.Appear('tsa_tool');
      initialise_dhtml(); //
    },

    parameters:"path_to_root="+path_to_root+"&LANG="+lang+"&tsa01="+ctry_list+"&tsa02="+t2data+"&tsa03_from="+date_from+"&tsa03_to="+date_to+"&tsa03_date="+date_date,
    evalScripts:true,
    asynchronous:true
  }
  );
  /**/


  /*alert('validate');*/
  return (false);
}

//
// 01
// CHECK FOR MULTIPLE SELECTIONS
var current_tsa_selections = "single";
function fn_tsa01_check(list, path_to_root, lang)
{
  selected = 0;
  tot_list = list.length;
  for (var idx = 0; idx < tot_list; idx ++)
  {
    if (list.options[idx].selected && list.options[idx].value != "")
    {
      selected = selected + 1;
    }
  }

  //
  // 5 MAX SELECTIONS
  if(selected > 5)
  {
    alert("(01) Selections are limited to no more than five.");
  }


  change = false;

  if(current_tsa_selections == "single" && selected < 2)
  {
    current_tsa_selections = "single";
    change = true;
  }
  else if(current_tsa_selections == "single" && selected > 1)
  {
    current_tsa_selections = "multi";
    change = true;
  }
  else if(current_tsa_selections == "multi" && selected < 2)
  {
    current_tsa_selections = "single";
    change = true;
  }

  //
  // 1 SELECTION :: OFFER DATE RANGE

  //
  // +1 SELECTION :: OFFER SINGLE DATE

  if(change == true)
  {
    new Ajax.Updater('tsa_date_input',path_to_root+'scripts/php/dm_TSA_date_range.php',
    {
      onComplete:function(request)
      {
        //Element.hide('active_rollover_details');
        new Effect.Appear('tsa_date_input');
        //new Effect.Appear('active_rollover_details');
        initialise_dhtml(); //
      },
      parameters:"selection_count="+selected+"&LANG="+lang,
      evalScripts:true,
      asynchronous:true
    }
    );
  }

}

// Added new functions for new TSA tool

//
// VALIDATE TSA TOOL FORM
function fn_validate_tsa_tool_v2(form, path_to_root, lang)
{
  // Empty vars used for palce holders
  var region_country = "";
  var concepts = "";
  var units = "";
  var func = "display_results";

  // Get all the Regions and Countries
  for (var i=0; i < document.tsa_tool.tsa01.length; i++)
  {
    if (document.tsa_tool.tsa01[i].checked)
    {
      region_country = region_country + document.tsa_tool.tsa01[i].value + ',';
    }
  }

  // Get all the concepts
  for (var i=0; i < document.tsa_tool.tsa02.length; i++)
  {
    if (document.tsa_tool.tsa02[i].checked)
    {
      concepts = concepts + document.tsa_tool.tsa02[i].value + ',';
    }
  }

  // Get all the units
  for (var i=0; i < document.tsa_tool.tsa03.length; i++)
  {
    if (document.tsa_tool.tsa03[i].checked)
    {
      units =  units + document.tsa_tool.tsa03[i].value + ',';
    }
  }

  // $('region_country').value = region_country.slice(0,-1);
  // $('concepts').value = concepts.slice(0,-1);
  // $('units').value = units.slice(0,-1);

  // Values
  ctry_list = region_country.slice(0,-1);
  t2data = concepts.slice(0,-1);
  t3data = units.slice(0,-1);

  // To and From dates
  date_from = document.tsa_tool.tsa04_from.value;
  date_to = document.tsa_tool.tsa04_2.value;

  // VALIDATE 01
  if(ctry_list == "")
  {
    alert("(01) Select a country / region");
    //$('tsa_step1').scrollTo();
    return (false);
  }

  // VALIDATE 02
  if(t2data == "")
  {
    alert("(02) Please select your Concept type");
    //$('tsa_step2').scrollTo();
    return (false);
  }

  // VALIDATE 03
  if(t3data == "")
  {
    alert("(03) Please select your Unit type");
    // $('tsa_step3').scrollTo();
    return (false);
  }

  // Hide the search term results section when displaying results
  $('search_terms').style.display = 'none';

  // Hide the form
  $('tsa_tool').style.display = 'none';

  // SUBMIT DATA
  new Ajax.Updater('tsa_tool',path_to_root+'scripts/php/dm_TSA_display_v2.php',
  {
    onComplete:function(request)
    {
      new Effect.Appear('tsa_tool');
      initialise_dhtml(); //
    },

    //parameters:"path_to_root="+path_to_root+"&LANG="+lang+"&tsa01="+ctry_list+"&tsa02="+t2data+"&tsa03_from="+date_from+"&tsa03_to="+date_to+"&tsa03_date="+date_date,
    parameters:"function="+func+"&path_to_root="+path_to_root+"&LANG="+lang+"&tsa01="+ctry_list+"&tsa02="+t2data+"&tsa03="+t3data+"&tsa04_from="+date_from+"&tsa04_to="+date_to,
    evalScripts:true,
    asynchronous:true
  }
  );

  /*alert('validate');*/
  return (false);
}


/*

TSA TOOL FUNCTION

- CHECKS WINDOWS FOR THIER CORRECT OPACITY SETTINGS
*/
var first_pass = false;
function fn_check_tsa_windows()
{
  //alert('check');
  var check1 = false;
  var check2 = false;
  var check3 = false;
  var check4 = false;

  // tsa01
  var tot = document.tsa_tool.tsa01.length;
  for (i = 0;  i < tot;  i++)
  {
    if (document.tsa_tool.tsa01[i].checked)
    {
      check1 = true;
      break;
    }
  }


  // CHECK IF tsa02 SHOULD BE 100%
  var tot = document.tsa_tool.tsa01.length;
  for (i = 0;  i < tot;  i++)
  {
    if (document.tsa_tool.tsa01[i].checked == true)
    {
      check2 = true;
      //check3 = true;
      break;
    }
  }

  var tot = document.tsa_tool.tsa02.length;
  var tsa2_checks_out = false;
  for (i = 0;  i < tot;  i++)
  {
    if (document.tsa_tool.tsa02[i].checked == true)
    {
      check2 = true;
      check3 = true;
      tsa2_checks_out = true;
      break;
    }

  }
  if(tsa2_checks_out == true)
  {

    // CHECK IF tsa03 SHOULD BE 100%
    var tot = document.tsa_tool.tsa03.length;
    for (i = 0;  i < tot;  i++)
    {
      if (document.tsa_tool.tsa03[i].checked == true)
      {
        //alert(i);
        check3 = true;
        check4 = true;
        break;
      }
    }
  }





  // CHANGE ALPHAS
  if(check1 == true)
  {
    // nothing
  }else
  {
    // change others
    check2 = false;
    check3 = false;
    check4 = false;
  }
  if(check2 == true)
  {
    fn_change_opacity('tsa_step2', 1);
  }else
  {
    check3 = false;
    check4 = false;
    fn_change_opacity('tsa_step2', 0.5);
  }
  if(check3 == true)
  {
    fn_change_opacity('tsa_step3', 1);
  }else
  {
    check4 = false;
    fn_change_opacity('tsa_step3', 0.5);
  }
  if(check4 == true)
  {
    fn_change_opacity('tsa_step4', 1);
  }else
  {
    fn_change_opacity('tsa_step4', 0.5);
  }

}




/*

TSA TOOL FUNCTION
- Changes opacity
*/
function fn_change_opacity(id, strength)
{
  var objToFade = document.getElementById(id);
  if(typeof objToFade.style.opacity == "string")
  {
    //objToFade.style.opacity = strength;

    if (strength == 0.5)
    {
      objToFade.addClassName('opacity50');
    }
    else
    {
      objToFade.removeClassName('opacity50');
    }
  }
  else
  {
    //document.getElementById(id).filters.alpha.opacity = strength;

    if (strength == 0.5)
    {
      $(id).addClassName('opacity50');
    }
    else
    {
      $(id).removeClassName('opacity50');
    }
  }
}



/*

TSA TOOL FUNCTION
- Selects and unselects nested checkboxes for the country options in tsa01

Requires:
ul - set as 'this' in the parent ul input
id - the id value of the li containg the parent input

*/
function fn_select_all(ul, id)
{
  var oUl = $(id);

  //alert(oUl.childNodes.length);// length of nodes
  //alert(oUl.childNodes[0].checked); // checks checked
  //alert(oUl.childNodes[0].nodeName); // checks nn

  if(ul == "span") // select all from span click
  {
    var input_parent = id+"_input";
    if($(input_parent).checked == true)
    {
      $(input_parent).checked = false;
      // update select all link
      var span_parent = id+"_span";
      $(span_parent).innerHTML = " (select all)";
    }
    else
    {
      $(input_parent).checked = true;
      // update select all link
      var span_parent = id+"_span";
      $(span_parent).innerHTML = " (de-select all)";
    }

    ul = $(input_parent);
  }/**/





  nodes_length = oUl.childNodes.length;
  for(n=0; n<nodes_length; n++)
  {
    // looking for the nested UL
    nn = oUl.childNodes[n].nodeName;
    if(nn == "UL" || nn == "ul")
    {
      li_tot = oUl.childNodes[n].childNodes.length;
      for(i=0; i<li_tot; i++)
      {
        // looking for li's within
        nnn = oUl.childNodes[n].childNodes[i].nodeName;
        if(nnn=='LI' || nnn=='li')
        {
          n_tot = oUl.childNodes[n].childNodes[i].childNodes.length;
          for(ni=0; ni<n_tot; ni++)
          {
            // looking for the input
            nin = oUl.childNodes[n].childNodes[i].childNodes[ni].nodeName;
            if(nin=='INPUT' || nnn=='input')
            {
              if(ul.checked == true) // CHECK ALL WITHIN
              {
                oUl.childNodes[n].childNodes[i].childNodes[ni].checked = true;
              }
              else if(ul.checked == false) // UNCHECK ALL WITHIN
              {
                oUl.childNodes[n].childNodes[i].childNodes[ni].checked = false;
              }
            }
          }
        }
      }
    }
  }

  fn_check_tsa01();
}



/*

TSA TOOL FUNCTION
- Opens and closes tipping arrows

Requires:
label - set as 'this' in the parent label tag
id - the id value of the li containg the parent input

*/
function fn_open_nested(label,id)
{
  //alert ('label'+label);
  //alert ('id'+id);
  var oUl = $(id);

  nodes_length = oUl.childNodes.length;
  for(n=0; n<nodes_length; n++)
  {
    // looking for the nested UL
    nn = oUl.childNodes[n].nodeName;
    if(nn == "UL" || nn == "ul")
    {
      //alert(oUl.childNodes[n].style.display);
      if(oUl.childNodes[n].style.display == 'none') // open
      {
        n_tot = oUl.childNodes[n].style.display = 'block';
        label.className = 'arrow_on';
        var action = "open";
      }
      else // close
      {
        n_tot = oUl.childNodes[n].style.display = 'none';
        label.className = 'arrow_off';
        var action = "close";
      }
    }
  }

  if(id != "COUNTRIES") // DO NOT TURN ON MULTISELECT FOR COUNTRIES
  {
    if(action == "open")
    {
      // turn on parent input
      var input_parent = id+"_input";
      $(input_parent).disabled = false;

      // turn on span link
      var input_span = id+"_span";
      $(input_span).style.display = 'inline';

    }else
    {
      // turn on parent input
      var input_parent = id+"_input";
      $(input_parent).disabled = true;

      // turn on span link
      var input_span = id+"_span";
      $(input_span).style.display = 'none';
    }
  }
}




/*

TSA TOOL FUNCTION
// FUNCTION TO KEEP TRACK OF HOW MANY COUNTRIES ARE SELECTED IN tsa01
//
// more than one country = only one concept

*/
var concepts_limit;
function fn_check_tsa01()
{
  var checkSelected = false;
  var checkCounter = 0;
  var tot = document.tsa_tool.tsa01.length;

  for (i = 0;  i < tot;  i++)
  {
    if (document.tsa_tool.tsa01[i].checked)
    {
      checkCounter = checkCounter + 1;
      checkSelected = true;
    }
  }

  if(checkCounter > 1)
  {
    concepts_limit = 'just one';

    // show message
    $('tsa_step2_message').style.display = 'block';

  }else
  {
    concepts_limit = 'more than one';

    // hide message
    $('tsa_step2_message').style.display = 'none';
  }
  //alert(checkCounter);
  //alert(concepts_limit);

  // check tsa02
  fn_check_tsa02();

}





/*

TSA TOOL FUNCTION
// FUNCTION TO KEEP TRACK OF HOW MANY CONCEPTS ARE SELECTED in tsa02
//
// more than one country = only one concept

*/
var unit_options = "";
function fn_check_tsa02()
{
  var checkCounter = 0;
  unit_options = "";
  var tot = document.tsa_tool.tsa02.length;
  for (i = 0;  i < tot;  i++)
  {
    if (document.tsa_tool.tsa02[i].checked)
    {
      checkCounter = checkCounter + 1;
      var this_unit_option = document.tsa_tool.tsa02[i].className;
      unit_options = unit_options + this_unit_option;
      //alert(this_unit_option);
    }
  }
  //alert(checkCounter+ " " +concepts_limit);//

  if(checkCounter > 1 && concepts_limit == 'just one')
  {
    //
    // clear selctions in tsa02 and alert user
    for (i = 0;  i < tot;  i++)
    {
      document.tsa_tool.tsa02[i].checked = false;
    }

    // announce
    //alert('Your concepts selections have been cleared because you have more than one country selected, please reselect a single concept or change your country selections.');
    alert('You may only select one measure as you have chosen more than one country. For multiple countries, please select one measure per search enquiry only.');


  }
  else
  {
    //
    // CHECK UNITS
    fn_check_tsa03();
  }

  fn_check_tsa_windows();
}







/*

TSA TOOL FUNCTION FOR tsa03
- check units selection
- hide and unhide unit options based on selections made on the concepts
- uses concept input class to identify what units are for which concepts

*/
function fn_check_tsa03()
{
  // set all to hidden/disabled
  var tot = document.tsa_tool.tsa03.length;
  if(unit_options == "")
  {
    // ENABLE ALL UNIT OPTIONS
    for (i = 0;  i < tot;  i++)
    {
      //document.tsa_tool.tsa03[i].checked=false;
      document.tsa_tool.tsa03[i].disabled=false;
      $("unit0"+(i+1)+"_label").style.color = "#333";
    }
    // hide message
    $('tsa_step3_message').style.display = 'none';
  }
  else
  {
    // DISABLE ALL READY FOR ENABLING SPECIFIC OPTIONS ONLY
    for (i = 0;  i < tot;  i++)
    {
      //document.tsa_tool.tsa03[i].checked=false;
      document.tsa_tool.tsa03[i].disabled=true;
      $("unit0"+(i+1)+"_label").style.color = "#999";
    }

    // enable specifics
    var l = unit_options.length;
    for(sp=0;sp<l;sp++)
    {
      //alert(unit_options.charAt(sp));
      $('unit0'+unit_options.charAt(sp)).disabled=false;
      $("unit0"+unit_options.charAt(sp)+"_label").style.color = "#333";
    }

    // check if any disabled
    // hide message
    $('tsa_step3_message').style.display = 'none';

    for (i = 0;  i < tot;  i++)
    {
      if(document.tsa_tool.tsa03[i].disabled==true)
      {
        // show message
        $('tsa_step3_message').style.display = 'block';

        // check to see if disabled is checked
        if(document.tsa_tool.tsa03[i].checked==true)
        {
          document.tsa_tool.tsa03[i].checked=false;
        }
        //break;
      }
    }


  }




  fn_check_tsa_windows();
}





/*

TSA TOOL FUNCTION FOR tsa04
- check date selections

*/
function fn_check_tsa04(to)
{
  var date_from = $('tsa04_1').selectedIndex;
  var date_to = $('tsa04_2').selectedIndex;
  //alert(date_from+" - "+date_to);

  // check if from is later than to!
  if(date_from >= date_to)
  {
    // was the 'to' date just changed?
    if(to == true)
    {
      $('tsa04_1').selectedIndex = date_to-1; // change the from date
    }
    else
    {
      $('tsa04_2').selectedIndex = date_from+1; // change the to date
    }
  }
}




/*

TSA TOOL FUNCTION
- sets up monitoring functions

*/
function fn_setup_tsa()
{

  // SETUP ON CHANGE FOR THE COUNTRY SELECTOR SECTION
  // ID = tsa01
  var tsa01,i;
  tsa01 = document.getElementById('tsa01').getElementsByTagName('INPUT');
  for(i in tsa01)
  {
    if(tsa01[i].value != "ignor")
    {
      //tsa01[i].setAttribute("onChange", "fn_check_tsa02();");
      tsa01[i].onchange = function()
      {
        fn_check_tsa01();
      }
    }
  }


  // SETUP ON CHANGE FOR THE CONCEPT SELECTOR SECTION
  // ID = tsa02
  var tsa02,i;
  tsa02 = document.getElementById('tsa02').getElementsByTagName('INPUT');
  for(i in tsa02)
  {
    //tsa02[i].setAttribute("onChange", "fn_check_tsa02();");
    tsa02[i].onchange = function()
    {
      //fn_check_tsa02();
      fn_check_tsa01();
    }

  }


  // SETUP ON CHANGE FOR THE CONCEPT SELECTOR SECTION
  // ID = tsa03
  var tsa03,i;
  tsa03 = document.getElementById('tsa03').getElementsByTagName('INPUT');
  for(i in tsa03)
  {
    //tsa02[i].setAttribute("onChange", "fn_check_tsa02();");
    tsa03[i].onchange = function()
    {
      fn_check_tsa_windows();
      //fn_check_tsa01();
    }/**/

  }

  fn_check_tsa01();

  fn_check_tsa_windows();

  fn_set_checkboxes();
}

// IE fix, onclick does not not work in IE
// When the page loads, set the check boxes to call respective functions when checked
function fn_set_checkboxes()
{
  $$('#tsa_step1 input').each(function(el){
    el.observe("click",function(){
      fn_check_tsa01();
    })
  });

  $$('#tsa_step2 input').each(function(el){
    el.observe("click",function(){
      fn_check_tsa01();
    })
  });

  $$('#tsa_step3 input').each(function(el){
    el.observe("click",function(){
      fn_check_tsa01();
    })
  });
}

// Send existing search term
function fn_display_search_term(form_name)
{
  var name = form_name;

  // Empty vars used for palce holders
  var tsa01 = name.tsa01.value;
  var tsa02 = name.tsa02.value;
  var tsa03 = name.tsa03.value;
  var tsa04_from = name.tsa04_from.value;
  var tsa04_to = name.tsa04_to.value;
  var path_to_root = name.path_to_root.value;
  var lang= name.lang.value;
  var func = "display_results";

  // Hide the search term results section when displaying results
  $('search_terms').style.display = 'none';

  // Hide the form
  $('tsa_tool').style.display = 'none';

  // SUBMIT DATA
  new Ajax.Updater('tsa_tool',path_to_root+'scripts/php/dm_TSA_display_v2.php',
  {
    onComplete:function(request)
    {
      new Effect.Appear('tsa_tool');
      initialise_dhtml(); //
    },
    parameters:"function="+func+"&path_to_root="+path_to_root+"&LANG="+lang+"&tsa01="+tsa01+"&tsa02="+tsa02+"&tsa03="+tsa03+"&tsa04_from="+tsa04_from+"&tsa04_to="+tsa04_to,
    evalScripts:true,
    asynchronous:true
  }
  );

  /*alert('validate');*/
  return (false);
}

// When the page is reloaded
// Set spans (when the page is reloaded the check boxes do not work on first click, needed to click twice, set the event)
function set_region_select_unselect(id)
{
  //var span_parent = id+"_span";
  //$(span_parent).innerHTML = " (de-select all)";

  // Set spans
  Event.observe($(id+'_span'),'span');
}

// Set Event.observe for country checkboxescheckboxes
function set_country_select_unselect(countries)
{
  //var span_parent = id+"_span";
  //$(span_parent).innerHTML = " (de-select all)";

  // Split string into array
  var set_country = countries.split(',');

  // Get just unique values
  var set_unique_country=new Array();

  label:for(var i=0; i<set_country.length;i++ )
  {
    for(var j=0; j<set_unique_country.length;j++ )
    {
      if(set_unique_country[j]==set_country[i])
      continue label;
    }
    set_unique_country[set_unique_country.length] = set_country[i];
  }

  // For each set event
  for(var i=0; i<set_unique_country.length;i++ )
  {
    Event.observe(set_unique_country[i]+'_span');
  }
}

// Display/Open relevant Country tipping arrows
function fn_display_country_tabs(countries)
{
  // Split string into array
  var country = countries.split(',');

  // Get just unique values
  var unique_country=new Array();
  label:for(var i=0; i<country.length;i++ )
  {
    for(var j=0; j<unique_country.length;j++ )
    {
      if(unique_country[j]==country[i])
      continue label;
    }
    unique_country[unique_country.length] = country[i];
  }

  // For each open relevant countries tipping arrow
  for(var i=0; i<unique_country.length;i++ )
  {
    fn_open_nested(this,unique_country[i]);
  }

}

// Function the handles the open/close of search results
var state;
function fn_open_close_search_results(click_action)
{
  var action = click_action;

  if (action == 'open' && state != 'open')
  {
    // disable link
    state = 'open';

    //Effect.SlideDown('computer_search_results');
    Effect.BlindDown('computer_search_results');
  }
  else if (action == 'close')
  {
    // Enable link
    state = 'close';

    //Effect.SlideUp('computer_search_results');
    Effect.BlindUp('computer_search_results');
  }

}

// Function to delete search history
function fn_delete_history(cookie,path_to_root)
{
  var cookie = cookie;
  var func = "delete_history";
  var path_to_root = path_to_root;

  // SUBMIT DATA
  if(confirm("Are you sure you wish to delete your search history?"))
  {
    new Ajax.Updater('tsa_tool',path_to_root+'scripts/php/dm_TSA_display_v2.php',
    {
      onComplete:function(request)
      {
        //new Effect.Appear('tsa_tool');
        //initialise_dhtml(); //
        window.location.reload();
      },
      parameters:"function="+func+"&cookie="+cookie+"&path_to_root="+path_to_root,
      evalScripts:true,
      asynchronous:true
    }
    );

    /*alert('validate');*/
    return (false);
  }
}