$(document).ready(function(){
	//	Attach externals
	$('.external').attr('target', '_blank');	
	
	startList('languagelist');
	    
  $('div#header div#language ul li').hover(function() {
    $(this).addClass('over');
    }, function() {
    $(this).removeClass('over');
  });
  
//controls sieves and screeners table filtering
//if the table exists we use it, else we define it as an empty string
  var table = $('table#sieves_and_screeners') || 'empty';
  var anchors = [];
  //if the table exists...
  if(typeof(table) == 'object'){
  		table.find('th a').each(function(){
  			// we bind a click event to the table headers
			$(this).bind('click', function(){
				table.filterTableContents(this);
				return false;
			});
		// we keep an array of table header anchors to be used when the url is checked
		anchors.push($(this).attr('id'));	
		});
	// grab the url
	var path = window.location.href;
	// check to see if an anchor exists
	if(path.match(/#(.*)/)){
		// if the anchor resides within the available anchors...
		if(anchors.indexOf(RegExp.$1) != -1){
			var anchor = '#' + RegExp.$1;
			table.filterTableContents(anchor);
		}
	}
  }
});

$.fn.filterTableContents = function(anchor){
// get the number of headers
var headers = $(this).find('th a').length;
// get the parent of the anchor passed in
var parent = $(anchor).parent('th');
// get the table headers cell index
index = parent.parent('tr').children().index(parent);
// a variable to indicate that a column has been removed
var removed = 0;
// loop through the headers and compare to the selected index. if the index is the same as that clicked then do nothing, else remove it.
for(var i = 1; i <= headers; i++){
	if(i != index){
		/*
		var column = (i + 1) - removed;
		$('tr td:nth-child(' + column + '), tr th:nth-child(' + column + ')', $(this)).remove();
		removed = 1;
		*/
	}
	// once the columns have been removed, go through the rows and perform removal of those that do not have the ticked class under the remaining column
	if(i == headers){
		var count = 0;
		$(this).find('tbody tr').each(function(){
			var cell = $(this).children('td').get(index);
			if(typeof(cell) == 'object'){
				if($(cell).attr('class') != 'tick'){
					$(this).remove();
				}else{
					count++;
					var className = ((count % 2) == 1 ? 'strip' : '');
					$(this).attr('class', className);
				}
			}
		});
	}
}
}

function contactCountryChanged()
{
	var CountrySelect = document.getElementById('NewContactForm_row5_country');
	var index = CountrySelect.selectedIndex;
	if(CountrySelect.options[index].value == 'United States - RFI')
	{
		var CanadaStateRow = document.getElementById('StateCanadaContactRow'); //get the canada state row
		var StateRow = document.getElementById('StateContactRow'); //get the us state row
		var CityRow = document.getElementById('CityContactRow'); //get the city row
		var CountyRow = document.getElementById('CountyContactRow'); //get the county row
		
		//hide canada, show us
		CanadaStateRow.style.display = 'none';
		CountyRow.style.display = 'none';
		StateRow.style.display = 'block';
		CityRow.style.display = 'block';
	}
	else if(CountrySelect.options[index].value == 'Canada - RFI')
	{
		var CanadaStateRow = document.getElementById('StateCanadaContactRow'); //get the canada state row
		var StateRow = document.getElementById('StateContactRow'); //get the us state row
		var CityRow = document.getElementById('CityContactRow'); //get the city row
		var CountyRow = document.getElementById('CountyContactRow'); //get the county row
		
		//hide us, show canada
		StateRow.style.display = 'none';
		CountyRow.style.display = 'none';
		CanadaStateRow.style.display = 'block';
		CityRow.style.display = 'block';
	}
	else if(CountrySelect.options[index].value == 'United Kingdom - RFL')
	{
		var CanadaStateRow = document.getElementById('StateCanadaContactRow'); //get the canada state row
		var StateRow = document.getElementById('StateContactRow'); //get the us state row
		var CityRow = document.getElementById('CityContactRow'); //get the city row
		var CountyRow = document.getElementById('CountyContactRow'); //get the county row
		
		//hide us, show canada
		StateRow.style.display = 'none';
		CanadaStateRow.style.display = 'none';
		CityRow.style.display = 'none';
		CountyRow.style.display = 'block';
	}
	else
	{
		var CanadaStateRow = document.getElementById('StateCanadaContactRow'); //get the canada state row
		var StateRow = document.getElementById('StateContactRow'); //get the us state row
		var CityRow = document.getElementById('CityContactRow'); //get the city row
		var CountyRow = document.getElementById('CountyContactRow'); //get the county row
		
		//hide them all
		StateRow.style.display = 'none';
		CanadaStateRow.style.display = 'none';
		CityRow.style.display = 'none';
		CountyRow.style.display = 'none';
	}
}

function startList(nav) {
  if (document.all&&document.getElementById(nav)) {
    navRoot = document.getElementById(nav);
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
          this.style.zIndex=200;
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
          this.style.zIndex=0;
        }
      }
    }
  }
}

function goto(select){
var index = select.selectedIndex;
if(select.options[index].value != "0"){
  location = select.options[index].value;
}
}
