/****************************************************************************
 * This file contains code that:
 * 1) Loads the country list from XML into HTML table
 * 2) Functions that handle filtering the table based on input box input
 * 3) Function for striping odd rows
 * 4) Functions that display either all countries or all legal countries
 ****************************************************************************/

//Global variables
//Filter input box
var filterBox;
//Array of table rows
var countryRows;

$(document).ready(function() {
  //Initialize the filterBox global var
  filterBox = document.getElementById('country_filter');
  //Clear filterbox at initialization (For Firefox)
  filterBox.value = '';
  
  //Initialize country table DOM object
  var countryTbl = document.getElementById('country_table');
  countryRows = countryTbl.rows;

  //Create country table jQuery object
  var countryTable = $('#country_table');

  //Used for initial row striping
  var countryCounter = 0;
  
  $.ajax( {
    url: '/EUROPE_NOKIA_COM_3/Get_Support/Integrated_FM_Transmitter/xml/fm_transmitter_countries.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 10000,
    error: function() {
    // alert("Error encountered!");
    },
    
    
    success: function(xml) {
        //Clear filterbox at initialization (for IE)
        filterBox.value = '';
        $(xml).find('country').each(function() {
          //Extract country name and country status for each country
          countryName = $(this).find('name').text();
          legalStatus = $(this).find('status').text();
         
          //Color the legal status text
          var legalClass = 'legalOrange';
          if (legalStatus == "Yes") {
            legalClass = 'legalGreen';
          } else if (legalStatus == "No") {
            legalClass = 'legalRed';
          }
          
          //Creat table row object
          var tableRow = $('<tr></tr>');
          
          //Initial row striping
          if (countryCounter%2 == 0) {
            tableRow.addClass("odd");
          }
          
          //Initially hide countries without legal status
          if (legalStatus != "Yes") {
            tableRow.css({display: "none"});
          } else {
            countryCounter++;
          }
          
          //Append the country name and legal status to the current row
          $("<td class='nameColumn'></td>").html(countryName).appendTo(tableRow);
          $("<td class='statusColumn'></td>").html(legalStatus).addClass(legalClass).appendTo(tableRow);
      
          //Append current row to the table
          tableRow.appendTo(countryTable);
        });
    }
  });
});

function stripeRows() {
  var rows = countryRows;
	var counter = 1;
	for(var i=1; i<rows.length; i++) {
		if(rows[i].style.display!='none') {
			if(counter%2==1) {
        rows[i].className = "odd";
			}
			else {
  			rows[i].className="";
      }
			counter++;
		}
	}
}

function showLegalCountries() {
  var rows = countryRows;
  for (var i=1; i<rows.length; i++) {
    var cells = rows[i].cells;
    if (cells[1].innerHTML == 'Yes') {
      rows[i].style.display='';
    } else {
      rows[i].style.display='none';
    }
  }
  stripeRows();
}

function showAllCountries() {
  var rows = countryRows;
  
  for (var i=1; i<rows.length; i++) {
    rows[i].style.display='';
  }
  stripeRows();
}

function filterCountries() {
//Hide/show countries matching the filter input
  var q = filterBox.value;
  q = q.toLowerCase();
  
  //Start by showing all countries
  if (q.length == 1) {
    showAllCountries();
  }
  
  var rows = countryRows;

  for (var i=1; i<rows.length; i++) {
    var cells = rows[i].cells;
    
    for (var j=0; j<cells.length-1; j++) {
      //var val = cells[j].innerHTML;
      var val = cells[j].firstChild.nodeValue;
      val = val.toLowerCase();

      if (val.indexOf(q) != -1){
        rows[i].style.display='';
        break;
      } else {
        rows[i].style.display='none';
      }
    }
  }
  
  //If the filter is empty, show only legal countries
  if (q == '') {
    showLegalCountries();
    return;
  }
  
  //Stripe rows otherwise 
  stripeRows();
}

function clickedShowAllCountries() {
  filterBox.value = '';
  showAllCountries();
}

function clickedShowLegalCountries() {
  filterBox.value = '';
  showLegalCountries();
}
