var m_names2 = new Array("test", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var regionName = new Array('Asia Pacific', 'Europe, Middle East & Africa', 'Latin America', 'North America');
var regionXmlName = new Array('apbank', 'emeabank', 'latambank', 'nabank');
var regionIcgName = new Array('icgap', 'icgemea', 'icglatam', 'icgna');
var regionCode = new Array('APAC', 'EMEA', 'LATAM', 'NAM');

var d = new Date();
var thisYear = d.getFullYear();

function rowStyle(num){
	
	if(num % 2) {
		return 'even';
	} else {
		return 'odd';
	}
}

function tabs(className, ulName) {
	if (className == '') {
		className = '.newsList'
	}
	
	if ((ulName == '') || (ulName == undefined)) {
		ulName = '#tabheader';
	}
	
	$(ulName + ' a').click(function () {
		$(ulName + ' a').removeClass('selected');
		$(this).addClass('selected');

		// Assign value of the link target
		var thisTarget = $(this).attr('rel');

		$(className).hide();

		$(thisTarget).show();
		this.blur();
		return false;
	});
}

function countryImage(region, pagetype) {
	
	if (pagetype != 'market') {
		// load tabs
		var tabHTML = '<li><a href="#" id="#content0" rel="#content0" class="selected"><span>Country and Territory Information</span></a></li>'+
		'<li><a href="#" id="#content1" rel="#content1"><span>All Countries and Territories</span></a></li>';
		
		$('#tabheader').empty();
		$('#tabheader').append(tabHTML);
		
		tabs('.countryContent');
	} else {
		loadRegSecName(region);
	}
	
	var imgHTML = '';
	
	switch(regionName[region]) {
		case regionName[0]:
			imgHTML += '<img src="images/sec/ml_apac1.jpg" width="694px" height="140px" alt="' + regionName[0] + '. Market Listing" />';
			
			var siteTitleHTML = '<img src="images/txt/apac_lg.gif" alt="' + regionName[0] + '" />';
		break;
		case regionName[1]:
			imgHTML += '<img src="images/sec/ml_emea1.jpg" width="694px" height="140px" alt="' + regionName[1] + '. Market Listing" />'+
			'<img src="images/sec/ml_emea2.jpg" width="694px" height="140px" alt="' + regionName[1] + '. Market Listing" />'+
			'<img src="images/sec/ml_emea3.jpg" width="694px" height="140px" alt="' + regionName[1] + '. Market Listing" />';
			
			var siteTitleHTML = '<img src="images/txt/emea_lg.gif" alt="' + regionName[1] + '" />';
		break;
		case regionName[2]:
			imgHTML += '<img src="images/sec/ml_latam1.jpg" width="694px" height="140px" alt="' + regionName[2] + '. Market Listing" />';
			
			var siteTitleHTML = '<img src="images/txt/latam_lg.gif" alt="' + regionName[2] + '" />';
		break;
		case regionName[3]:
			imgHTML += '<img src="images/sec/ml_nam1.jpg" width="694px" height="140px" alt="' + regionName[3] + '. Market Listing" />' +
			'<img src="images/sec/ml_nam2.jpg" width="694px" height="140px" alt="' +
			regionName[3] +
			'. Market Listing" />' +
			'<img src="images/sec/ml_nam3.jpg" width="694px" height="140px" alt="' +
			regionName[3] +
			'. Market Listing" />';
			
			var siteTitleHTML = '<img src="images/txt/nam_lg.gif" alt="' + regionName[3] + '" />';
		break;
	}
	
	$('#mainImage').empty();
	$('#mainImage').append(imgHTML);
	
	$('#mainImage')
	.cycle({
		fx:     'fade',
		speed:  'slow',
		timeout: 10000
	});
	
	$('#SiteTitle').empty();
	$('#SiteTitle').append(siteTitleHTML);
	
	if (pagetype != 'market') {
		loadCountryList(region);
	}
}

function loadCountryList(regNum){
	var subNavHTML = '';
	
	for (var y = 0; y < 4; y++) {
		subNavHTML += '<a href="#" rel="#list' + y + '"';
		
		if (y == regNum) {
			subNavHTML += ' class="selected"';
		}
		
		subNavHTML += '>' + regionName[y] + '</a>';
		
		if (y != 3) {
			subNavHTML += '|';
		}
	}
	
	$('.subTabLinks').empty();
	$('.subTabLinks').append(subNavHTML);
	
	$.ajax({
		type: "GET",
		url: 'xml/country_info.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			
			for (var i = 0; i < 4; i++) {
				var ncounter = 0;
				var modCounter = 1;
				var regionNameHD = '';
				
				var HTML = '';
				var modValue = '';

				HTML += '<table cellpadding="0" border="0" cellspacing="1" width="99%">';
				
				$(xml).find('Entry').each(function(){
					if ($(this).find('region').text() == regionCode[i]) {
						if(regionNameHD != $(this).find('section').text()) {
							HTML += '<tr><td height="15">&nbsp;</td></tr><tr>' +
								'<td class="row2" colspan="3"><b>' + $(this).find('section').text() + '</b></td>' +
							'</tr>';
							regionNameHD = $(this).find('section').text();
							modCounter = 1;
						}
						modValue = modCounter % 3;
						if (modValue == 1) {
							HTML += '<tr class="' + rowStyle(ncounter) + '">';
						}
						HTML += '<td width="33%"><a href="' + $(this).find('code').text() + '.htm">' + $(this).find('country').text() + '</a></td>';
						modCounter++;
						
						if (modValue == 0) {
							HTML += '</tr>';
							ncounter++;
						}
					}
				});
				HTML += '</tr></table>';
				
				$('#list' + i).empty();
				$('#list' + i).append(HTML);
				
				HTML = '';
				
				$('#list' + regNum).css('display', 'block');
			}
		}
	});
	tabs('.countryList', '.subTabLinks');
}

function loadRegSecName(regNum) {
	$.ajax({
		type: "GET",
		url: 'xml/country_info.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			// create tabs
			var tabsCt = 0;
			var tabHTML = '';
			var regionNameHD = '';
			var sectionName = new Array();
			
			$(xml).find('Entry').each(function(){
				if ($(this).find('region').text() == regionCode[regNum]) {
					if(regionNameHD != $(this).find('section').text()) {
						if (tabsCt == 0) {
							tabHTML += '<li><a href="#" id="#content' + tabsCt + '" rel="#content' + tabsCt + '" class="selected"><span>' + $(this).find('section').text() + '</span></a></li>';
						} else {
							tabHTML += '<li><a href="#" id="#content' + tabsCt + '" rel="#content' + tabsCt + '"><span>' + $(this).find('section').text() + '</span></a></li>';
						}
						regionNameHD = $(this).find('section').text();
						sectionName[tabsCt] = $(this).find('section').text();
						tabsCt++;
					}
				}
			});
			
			$('#tabheader').empty();
			$('#tabheader').append(tabHTML);
			
			var HTML = '';
			contentCt = 0;
			regionNameHD = '';
			
			for (var i = 0; i < sectionName.length; i++) {
				HTML += '<ul class="country">';
					$(xml).find('Entry').each(function(){
						if ($(this).find('region').text() == regionCode[regNum]) {
							if (sectionName[i] == $(this).find('section').text()) {
								HTML += '<li><a href="' + $(this).find('code').text() + '.htm">' + $(this).find('country').text() + '</a></li>';
							}
						}
					});
				HTML += '</ul>';
				//alert(HTML);
				$('#content' + i).empty();
				$('#content' + i).append(HTML);
				
				HTML = '';
			}
			tabs('.countryContent');
		}
	});
}

function loadLatamAwards() {
	$.ajax({
		type: "GET",
		url: 'xml/latam_award.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			
			var HTML = '';
			awardCt = 1;
			
			HTML += '<table cellpadding="0" cellspacing="1" width="100%" border="0"  class="tablesorter" id="myTable">'+
			'<thead><tr>'+
				'<th class="hd2" width="70px">Month of Award</th>'+
				'<th class="hd2" width="70px">Year of Award</th>'+				
				'<th class="hd2">Award Title</th>'+
				'<th class="hd2">Award Sponsor / Publication</th>'+
				'<th class="hd2">Award Category</th>'+
				'<th class="hd2">Business / Country</th>'+
			'</tr></thead><tbody>';
			$(xml).find('award').each(function(){
				HTML += '<tr class="' + rowStyle(awardCt) + '">'+
					'<td>' + $(this).find('month').text() + '</td>'+
					'<td>' + $(this).find('year').text() + '</td>'+					
					'<td>' + $(this).find('name').text() + '</td>'+
					'<td>' + $(this).find('pub_org').text() + '</td>'+
					'<td>' + $(this).find('nomination').text() + '</td>'+
					'<td>' + $(this).find('business').text() + '</td>'+
				'</tr>';
				awardCt++;
			});
			HTML += '</tbody></table>';
			
			$('#content1').empty();
			$('#content1').append(HTML);
			
			$(document).ready(function()
			{        
				 $("#myTable").tablesorter({widgets: ['zebra']});
} ); 
		}
	});
}

function loadEmeaAwards(year,container) { 

var xmlPath = 'xml/emea_awards' + year + '.xml';
	$.ajax({
		type: "GET",
		url: xmlPath,
		cache: false,
		dataType: "xml",
		success: function(xml){
			
			var HTML = '';
			awardCt = 1;
			
			HTML += '<table cellpadding="0" cellspacing="1" width="100%" border="0"  class="tablesorter" id="myTable' + year + '">'+
			'<thead><tr>'+
				'<th class="hd2">Award Title</th>'+
				'<th class="hd2" width="100px">Date of Award</th>'+
				'<th class="hd2" width="170px">Award Sponsor / Publication</th>'+
				'<th class="hd2" width="170px">Business / Country</th>'+
			'</tr></thead><tbody>';
			$(xml).find('award').each(function(){
				HTML += '<tr class="' + rowStyle(awardCt) + '">'+
					'<td>' + $(this).find('name').text() + '</td>'+
					'<td>' + $(this).find('date').text() + '</td>'+
					'<td>' + $(this).find('pub_org').text() + '</td>'+
					'<td>' + $(this).find('business').text() + '</td>'+
				'</tr>';
				awardCt++;
			});
			HTML += '</tbody></table>';
			
			$(container).empty();
			$(container).append(HTML);
			
			$(document).ready(function()
			
			{        
				var myTable = '#myTable' + year;
				 $(myTable).tablesorter({widgets: ['zebra']});
} ); 
		}
	});
}


function loadApAwards() {
	$.ajax({
		type: "GET",
		url: 'xml/ap_award.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			
			var HTML = '';
			awardCt = 1;
			
			HTML += '<table cellpadding="0" cellspacing="1" width="100%" border="0"  class="tablesorter" id="myTable">'+
			'<thead><tr>'+
				'<th class="hd2">Award Name</th>'+
				'<th class="hd2" width="50px">Year</th>'+
				'<th class="hd2" width="170px">Publication/Organization</th>'+
				'<th class="hd2" width="70px">Country</th>'+
			'</tr></thead><tbody>';
			$(xml).find('award').each(function(){
				HTML += '<tr class="' + rowStyle(awardCt) + '">'+
					'<td><b>' + $(this).find('name').text() + '</b><ul class="plain">';
					
					var nominations = ($(this).find('nominations').text()).split(';');

					for(i=0; i< nominations.length; i++){
						HTML += '<li>' + nominations[i] + '</li>';
					}
				HTML += '</ul></td>'+
					'<td>' + $(this).find('year').text() + '</td>'+
					'<td>' + $(this).find('pub_org').text() + '</td>'+
					'<td>' + $(this).find('business').text() + '</td>'+
				'</tr>';
				awardCt++;
			});
			HTML += '</tbody></table>';
			
			$('#content1').empty();
			$('#content1').append(HTML);
			
			$(document).ready(function()
			{        
				 $("#myTable").tablesorter({widgets: ['zebra']});
} ); 
		}
	});
}

function getResults(query) {
	
if(query == 'not found'){		
	document.getElementById('noresult').style.display='inline';
} else {
	//document.getElementById('noresult').style.display='none';
	$.ajax({
		type: "GET",
		url: 'xml/country_info.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			var tabsCt = 0;
			var tabHTML = '';
			var regionNameHD = '';
			var sectionName = new Array();
			
			$(xml).find('Entry').each(function(){
				if (((($(this).find('section').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1) || ((($(this).find('country').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1)){
					if(regionNameHD != $(this).find('section').text()) {
						if (tabsCt == 0) {
							tabHTML += '<li><a href="#" id="#content' + tabsCt + '" rel="#content' + tabsCt + '" class="selected"><span>';
								if ($(this).find('section').text() == 'Asia Pacific'){
									tabHTML +='ASPAC</span></a></li>';
								} else if ($(this).find('section').text() == 'Central America / Caribbean'){
								tabHTML +='C. America</span></a></li>';
								} else if ($(this).find('section').text() == 'North America'){
								tabHTML +='N. America</span></a></li>';
								} else if ($(this).find('section').text() == 'South America'){
								tabHTML +='S. America</span></a></li>';								
								} else if ($(this).find('section').text() == 'Middle East'){
								tabHTML +='M. East</span></a></li>';	
							} else {
								tabHTML += $(this).find('section').text() + '</span></a></li>';
							}
						} else {
							tabHTML += '<li><a href="#" id="#content' + tabsCt + '" rel="#content' + tabsCt + '"><span>' ;
								if ($(this).find('section').text() == 'Asia Pacific'){
									tabHTML +='ASPAC</span></a></li>';
								} else if ($(this).find('section').text() == 'Central America / Caribbean'){
								tabHTML +='C. America</span></a></li>';
								} else if ($(this).find('section').text() == 'North America'){
								tabHTML +='N. America</span></a></li>';
								} else if ($(this).find('section').text() == 'South America'){
								tabHTML +='S. America</span></a></li>';								
								} else if ($(this).find('section').text() == 'Middle East'){
								tabHTML +='M. East</span></a></li>';								
							} else {
								tabHTML += $(this).find('section').text() + '</span></a></li>';
							}
						}
						regionNameHD = $(this).find('section').text();
						sectionName[tabsCt] = $(this).find('section').text();
						tabsCt++;
					}
				}
			});
			
			
			$('#tabheader').empty();
			$('#tabheader').append(tabHTML);
			document.getElementById('tabheader').style.display="block";
			
			var HTML = '';
			contentCt = 0;
			regionNameHD = '';
			
			for (var i = 0; i < sectionName.length; i++) {
				HTML += '<ul class="country">';
					$(xml).find('Entry').each(function(){
						if (((($(this).find('section').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1) || ((($(this).find('country').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1)){
							if (sectionName[i] == $(this).find('section').text()) {
								HTML += '<li><a href="' + $(this).find('code').text() + '.htm">' + $(this).find('country').text() + '</a></li>';
							}
						}
					});
				HTML += '</ul>';
				
				$('#content' + i).empty();
				$('#content' + i).append(HTML);
				
				HTML = '';
			}
			tabs('.countryContent');
		}
	});
}
}

function search() {
	var search_count=0;
	var singlePage='';
	var query = document.getElementById('findloc').value;
	$.ajax({
		type: "GET",
		url: 'xml/country_info.xml',
		cache: false,
		dataType: "xml",
		success: function(xml){
			
			$(xml).find('Entry').each(function(){
				if (((($(this).find('section').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1) || ((($(this).find('country').text()).toLowerCase()).indexOf(query.toLowerCase())!=-1)){
					search_count++;
					singlePage = $(this).find('code').text();
				}
			});
			
			if(search_count == 1){	
				window.location.href = singlePage + '.htm';
			} else if (search_count == 0){		
				window.location.href ='search.htm?q=not found';
			} else {
				window.location.href ='search.htm?q=' + query;
			}

	}
	});
}

function onEnter( evt, frm ) {
var keyCode = null;

if( evt.which ) {
keyCode = evt.which;
} else if( evt.keyCode ) {
keyCode = evt.keyCode;
}
if( 13 == keyCode ) {
search();
return false;
}
return true;
}

