function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
  elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
    vis = elem.style;
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function turnOn(oImage,id,type){
  oImage.style.cursor='pointer';
  tmp = oImage.name;
  end = tmp.match(/\d/);
  for(var i = end; i > 0 ; i--)
  {
    star = "star"+i+"_"+type+"_"+id;
    document.images[star].src="/images/star-on.png";
  }
}

function turnOff(id,type){
  for(var i = 5; i > 0; i--)
  {
    star = "star"+i+"_"+type+"_"+id;
    document.images[star].src="/images/star-off.png";
  }
}

function getRequest()
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
        {
          try
            {
              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
              alert("Your browser does not support AJAX!");
              return false;
            }
        }
  }

  return xmlHttp;
}

function changeTagView(showWhich)
{
  var tagCloud = document.getElementById('tagcloud');
  var tagList = document.getElementById('taglist');


  if (showWhich == 'cloud')
  {
    tagCloud.style.display = 'block';
    tagList.style.display = 'none';
  }
  else
  {
    tagCloud.style.display = 'none';
    tagList.style.display = 'block';
  }
}

function closeSuggestion()
{
  var tagSuggestion = document.getElementById('tag_suggestion');
  tagSuggestion.style.display = 'none';
}

function tagSuggest()
{
  var xmlHttp = getRequest();
  var str = document.getElementById('textTags').value;
  if (str.indexOf(',') != -1 && str.indexOf(' ') == (str.indexOf(',') + 1))
  {
    var newArray = str.split(", ");
    str = newArray[newArray.length - 1];
    tagList = document.getElementById('textTags').value;
  }
  
  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var tagSuggestion = document.getElementById('tag_suggestion');
      tagSuggestion.innerHTML = '<p style="text-align: right;width: 100%;">[ <a style="cursor: pointer;" onclick="closeSuggestion()">X</a> ]</p>';
      tagSuggestion.innerHTML += xmlHttp.responseText;

      tagSuggestion.style.display = 'block';
    }
  }
  xmlHttp.open("GET", "/knowledge/suggestTags.php?tag=" + str, true);
  xmlHttp.send(null);
}

function setTag(tag)
{
  var textBox = document.getElementById('textTags');
  var str = textBox.value;
  var textArray = str.split(", ");
  var tagList = '';
  var i = 0;

  if (str.indexOf(',') != -1 && str.indexOf(' ') == (str.indexOf(',') + 1))
  {
    for (i = 0; i < textArray.length - 1; i++)
    {
      tagList += textArray[i] + ', ';
    }

    tagList += tag + ', ';
  }
  else
  {
    tagList = tag + ', ';
  }

  textBox.value = tagList;
  textBox.focus();
  tagSuggest();
}

function showValue(value)
{
  var info = document.getElementById('bct-info');
  info.innerHTML = value;
}

function rateComment(user_id, comment_id, rating)
{
  var xmlHttp = getRequest();

  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var rating = xmlHttp.responseText;
      if (rating >= 1 && rating <= 5)
      {
        var stars = document.getElementById('rating'+comment_id);
        stars.innerHTML = '';
        var i, j, x;
        
        x = 1;
        for (i = 1; i <= rating; i++)
        {
          stars.innerHTML = stars.innerHTML + '<img src="/images/star-on.png" style="cursor: pointer;" onclick="rateComment('+user_id+','+comment_id+','+x+')" />';
          x++;
        }

        alert('star'+j+'_comment_'+comment_id);

        for (j = x; j <= 5; j++)
        {
          stars.innerHTML = stars.innerHTML + '<img src="/images/star-off.png" name="star'+j+'_comment_'+comment_id+'" onmouseover="turnOn(this, '+comment_id+', \'comment\')" onmouseout="turnOff('+comment_id+', \'comment\')" onclick="rateComment('+user_id+','+comment_id+','+x+')" />';
          x++;
        }

        alert('You have successfully rated this comment.');
      }

      if (rating == 6)
      {
        alert('You have already rated this comment.');
      }

      if (rating == 0)
      {
        alert('An error has occurred. Please try again later.');
      }
    }
  }
  xmlHttp.open("GET", "rate.php?user_id=" + user_id + "&comment_id=" + comment_id + "&rating=" + rating + "&type=comment", true);
  xmlHttp.send(null);
  }	

function rateArticle(user_id, article_id, rating)
{
  var xmlHttp = getRequest();

  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var rating = xmlHttp.responseText;
      if (rating >= 1 && rating <= 5)
      {
        var stars = document.getElementById('rating'+article_id);
        stars.innerHTML = '<strong style=\"color: #0071C7;\">Rating:</strong> ';
        var i, j, x;
        
        x = 1;
        for (i = 1; i <= rating; i++)
        {
          stars.innerHTML = stars.innerHTML + '<img src="/images/star-on.png" style="cursor: pointer;" onclick="rateArticle('+user_id+','+article_id+','+x+')" />';
          x++;
        }

        for (j = x; j <= 5; j++)
        {
          stars.innerHTML = stars.innerHTML + '<img src="/images/star-off.png" name="star'+j+'_article_'+article_id+'" onmouseover="turnOn(this, '+article_id+', \'article\')" onmouseout="turnOff('+article_id+', \'article\')" onclick="rateArticle('+user_id+','+article_id+','+x+')" />';
          x++;
        }

        alert('You have successfully rated this article.');
      }

      if (rating == 6)
      {
        alert('You have already rated this article.');
      }

      if (rating == 0)
      {
        alert('An error has occurred. Please try again later.');
      }
    }
  }

  xmlHttp.open("GET", "rate.php?user_id=" + user_id + "&article_id=" + article_id + "&rating=" + rating + "&type=article", true);
  xmlHttp.send(null);
}

function setImage(imageSelect) {
  var img0 = "../images/region_map.gif";
  var img1 = "../images/region_map_ne.gif";
  var img2 = "../images/region_map_mw.gif";
  var img3 = "../images/region_map_s.gif";
  var img4 = "../images/region_map_w.gif";
  theImageIndex = imageSelect.options[imageSelect.selectedIndex].value;
  document.region_img.src = eval("img" + theImageIndex);
}

function changeRights(m, f, u)
{
  var xmlHttp = getRequest();
  
  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var message = xmlHttp.responseText;
      message = message.replace(new RegExp( "\\n", "g" ), "");
      alert(message);
        
      var img = m + '_' + f + '_' + u;
      var cross = new Image();
      var tick = new Image();
      var invImage = document.getElementById(img);
        
      cross.src = '/images/cross.png';
      tick.src = '/images/tick.png';

      if (invImage.src == cross.src)
        invImage.src = tick.src
      else
        invImage.src = cross.src;
    }
  }
    
    xmlHttp.open("GET", "user_rights.php?m=" + m + "&f=" + f + "&u=" + u, true);
    xmlHttp.send(null);
}

function showFrontpage(id)
{
  var xmlHttp = getRequest();
  
  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var message = xmlHttp.responseText;
      message = message.replace(new RegExp( "\\n", "g" ), "");
      alert(message);
      
      var img = 'frontpage_' + id;
      var cross = new Image();
      var tick = new Image();
      var fpImage = document.getElementById(img);
      
      cross.src = '/images/cross.png';
      tick.src = '/images/tick.png';
      
      if (fpImage.src == cross.src)
        fpImage.src = tick.src;
      else
        fpImage.src = cross.src;
    }
  }
  
  xmlHttp.open("GET", "frontpage.php?id=" + id, true);
  xmlHttp.send(null);
}

function refreshCategoryList()
{
  var list = document.getElementById('study_category');
  var xmlHttp = getRequest();
  
  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var categoryOptions = xmlHttp.responseText;
      list.innerHTML = categoryOptions;
    }
  }
  
  xmlHttp.open("GET", "list.php?l=categories", true);
  xmlHttp.send(null);
}

function refreshQuestionList()
{
  var list = document.getElementById('study_questions');
  var xmlHttp = getRequest();
  
  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      var questionOptions = xmlHttp.responseText;
      list.innerHTML = questionOptions;
    }
  }
  
  xmlHttp.open("GET", "list.php?l=questions", true);
  xmlHttp.send(null);
}

function articlePreview()
{
  var title = document.getElementById('article_title').value;
  var update = document.getElementById('update_div_content');
  var content = document.getElementById('article_text').value;
  var months = new Array(12);
  months[0] = "January";
  months[1] = "February";
  months[2] = "March";
  months[3] = "April";
  months[4] = "May";
  months[5] = "June";
  months[6] = "July";
  months[7] = "August";
  months[8] = "September";
  months[9] = "October";
  months[10] = "November";
  months[11] = "December";
  var date = new Date();
  var month = months[date.getMonth()];
  var day = date.getDate();
  var year = date.getFullYear();
  var sfx = '';
  
  sfx = 'th';
  
  if ( day == 1 || day == 21 || day == 31 )
    sfx = 'st';
  
  if ( day == 2 || day == 22 )
    sfx = 'nd';
    
  if ( day == 3 || day == 23 )
    sfx = 'rd';
  
  
  var html = '';
  
  if (title.length > 85) title = title.substring(0, 85) + '...';
  html += '<h5>' + title + '</h5>';
  html += '<p><strong>' + month + ' ' + day + sfx + ', ' + year +'</strong></p>';
  html += '<p style="margin-top: 10px;">' + content.substring(0, 100) + '...</p>';
  
  update.innerHTML = html;
}

function changeIndicator(indicator)
{
  var i;
  var display;
  for (i = 1; i <= 11; i++)
  {
    display = 'none';
    if (i == indicator) display = 'block';
    if (document.getElementById('indicator' + i))
    {
      document.getElementById('indicator' + i).style.display = display;
    }
  }
}

function changeChart(indicator, year)
{
  var chart = document.getElementById('chart_ind' + indicator);
  var new_chart = new Image();
  new_chart.src = '/fmhscorecard/long_fmh_chart.php?y=' + year;
  
  chart.src = new_chart.src;
}

function add_graph(indi, y)
{
  var indicator = typeof(indi) == 'undefined' ? document.graph_form.graph_indicator.options[document.graph_form.graph_indicator.selectedIndex].value : indi;
  
  var year = typeof(y) == 'undefined' ? document.graph_form.graph_year.options[document.graph_form.graph_year.selectedIndex].value : y;
  
  var container = document.getElementById('graph_container');
  var counter_in = document.getElementById('counter');
  var counter = parseInt(counter_in.value);
  counter = counter + 1;
  var new_graph = document.createElement('div');
  var new_graph_id = 'graph_' + counter;
  new_graph.id = new_graph_id;
  new_graph.innerHTML = '<img src="long_fmh_graph.php?i=' + indicator + '&y=' + year + '" alt="graph" class="pointer" title="Click to close!" onclick="remove_graph(\'' + new_graph_id + '\');" />';
  counter_in.value = counter;
  container.appendChild(new_graph);
  
  document.graph_form.graph_indicator.options[indicator - 1].defaultSelected = 1;
  
  var form_container = document.getElementById('graph_form');
  form_container.style.display = 'block';
}

function remove_graph( graph_id )
{
  var container = document.getElementById('graph_container');
  var graph = document.getElementById(graph_id);
  var form = document.getElementById('graph_form');
  container.removeChild(graph);
  if (container.innerHTML == '') 
    form.style.display = 'none';
}

function show_compare_options(id)
{
  var compare_options = document.getElementById('compare_options');
  compare_options.style.display = 'block';
  
  var norm_dist = document.getElementById('norm_dist');
  norm_dist.href = 'norm_dist.php?i=' + id;
}

function toggle_trends( whichTrend )
{
  var d = document;
  
  var mammos = d.getElementById('mammos');
  var mammo_unit = d.getElementById('mammo_unit');
  var analog_facil = d.getElementById('analog_facil');
  var digital_facil = d.getElementById('digital_facil');
  var analog_unit = d.getElementById('analog_unit');
  var digital_unit = d.getElementById('digital_unit');
  var analog_percent = d.getElementById('analog_percent');
  var digital_percent = d.getElementById('digital_percent');
  
  if ( whichTrend == 'mammos' )
  {
    mammos.style.display = 'block';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'mammo_unit' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'block';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'analog_facil' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'block';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'digital_facil' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'block';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'analog_unit' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'block';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'digital_unit' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'block';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'analog_percent' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'block';
    digital_percent.style.display = 'none';
  }
  
  if ( whichTrend == 'digital_percent' )
  {
    mammos.style.display = 'none';
    mammo_unit.style.display = 'none';
    analog_facil.style.display = 'none';
    digital_facil.style.display = 'none';
    analog_unit.style.display = 'none';
    digital_unit.style.display = 'none';
    analog_percent.style.display = 'none';
    digital_percent.style.display = 'block';
  }
}

function toggleViolation( whichViolation )
{
  var d = document;
  var vio1 = d.getElementById('violation1');
  var vio2 = d.getElementById('violation2');
  var vio3 = d.getElementById('violation3');
  var noVio = d.getElementById('no_violation');
  
  if ( whichViolation == 1 )
  {
    vio1.style.display = 'block';
    vio2.style.display = 'none';
    vio3.style.display = 'none';
    noVio.style.display = 'none';
  }
  
  if ( whichViolation == 2 )
  {
    vio1.style.display = 'none';
    vio2.style.display = 'block';
    vio3.style.display = 'none';
    noVio.style.display = 'none';
  }
  
  if ( whichViolation == 3 )
  {
    vio1.style.display = 'none';
    vio2.style.display = 'none';
    vio3.style.display = 'block';
    noVio.style.display = 'none';
  }
  
  if ( whichViolation == 0 )
  {
    vio1.style.display = 'none';
    vio2.style.display = 'none';
    vio3.style.display = 'none';
    noVio.style.display = 'block';
  }
}