//Javascript code for Water Conflict Chronology Timeline - Table Only
//created by Matthew Heberger, August 2009

function highlighttablerows(){

  $("tr:even").addClass("striped");
  $("td").css("border", "none");
    
  $("td").hover(function() {
            $(this).parents('tr').addClass('highlight');
          }, function() {
            $(this).parents('tr').removeClass('highlight');
      });
}


function onLoad(){
  loadNewData();
}


function loadNewData(){
  var params = {}
  params.region=document.getElementById('region').value;
  params.conftype = document.getElementById('conftype').value;
  params.epoch = document.getElementById('epoch').value;
  
  var jstr = JSON.stringify(params);
  var myURL = '../../php/table.php'
  var myRequest = new ajaxObject(myURL, processData);

  myRequest.update('jstr=' + jstr,'GET');
  showImage(true);
}

function processData(responseText, responseStatus) {
  showImage(false);
  if (responseStatus == 200) {
    var dict = JSON.parse(responseText);
    print(dict.txt);
    fillInfo(dict);
    highlighttablerows();
    }
  else {
    alert(responseStatus + ' -- Error Processing Request');
    }
}

function print(str){
  document.getElementById("txt").innerHTML = str;
}

function ListProperties(){
  o = eventSource;
  for(att in o){
    print(o[att]);
  }
}


function fillInfo(dict){
  var num =dict.num;
  var begin =dict.begin;
  var end = dict.end;
  
  document.getElementById("num").innerHTML = num;
  if (num>1){
    var a = " entries from " + formatBC(begin);  
    document.getElementById("begin").innerHTML = a;
    
    var a = " to " + formatBC(end);
    document.getElementById("end").innerHTML = a;
    
  }else if (num==1){
    var a = " entry in " + formatBC(begin);  
    document.getElementById("begin").innerHTML = a;
    document.getElementById("end").innerHTML = "";

  }else {
    document.getElementById("begin").innerHTML = "";
    document.getElementById("end").innerHTML = "entries";
  }
}

function formatBC(yr){
  if (yr>=0){
    return yr;
  }else{
    return -yr + " BC";
  }
}

//This function shows or hides the AJAX load image
function showImage(bShow){
  
  if (bShow){
    document.getElementById("ajaximg").style.display = 'block';}
  else{
    document.getElementById("ajaximg").style.display = 'none';}
}