var current_country = readCookie('country');

window.onload = function()
{
	//get the select menu element that we will write the countries to
	select_element = document.getElementById('select_country');
	
	//get the element that we will write the methods to
	write_methods_el = document.getElementById('write_methods_root');
	left_write_methods_el = document.getElementById('left_write_methods_root');
	
	sendRequest('/lt/poker/real-money/xml/translations_01.1.xml',processTranslations); //constant so loaded in and stored
	
}


var translated_methods = [];
var translated_countries = [];

function processTranslations(translationsXML)
{
	
	var xmldoc = translationsXML.responseXML.getElementsByTagName('methods')[0];
	for (var i=0; i < xmldoc.childNodes.length; i++)
	{
		if (xmldoc.childNodes[i].nodeType != 1) continue;
		translated_methods[xmldoc.childNodes[i].nodeName] = xmldoc.childNodes[i].firstChild.data;
	}
	
	var xmldoc = translationsXML.responseXML.getElementsByTagName('countries')[0];
	for (var i=0; i < xmldoc.childNodes.length; i++)
	{
		if (xmldoc.childNodes[i].nodeType != 1) continue;
		translated_countries[xmldoc.childNodes[i].nodeName] = xmldoc.childNodes[i].firstChild.data;
	}
	sendRequest('/poker/real-money/xml/payments-01.6.xml',init);
}

//initialise the variable to hold the payments xml document
var paymentsXML;

function init(req)
{	
	//store the xml data for later use
	paymentsXML = req;
	
	makeDropdown();
	
	select_element.onchange = function()
	{
		var box = document.forms[0].select_country;
		var current_country = box.options[box.selectedIndex].value;
		createCookie('country',current_country,5)

		if (!current_country) { //no cookie set - build list for all countries
			buildAllMethodsList();
		}
		else { //cookie set - build list for country set in cookie
			buildMethodsList();			
		}	
	}
	
	if (!current_country) { //no cookie set - build list for all countries
			buildAllMethodsList();
		}
		else { //cookie set - build list for country set in cookie
			buildMethodsList();			
	}
}



function makeDropdown()
{
	//var countryList = getTranslations(req1,'countries');
	for (country in translated_countries)
	{
		var new_option = document.createElement('option');
		new_option.text = translated_countries[country];
		new_option.value = country;
		
		try {
			select_element.add(new_option, null); // standards compliant; doesn't work in IE
		}
		catch(e) {
			select_element.add(new_option); // IE only
		}
	}
	box = document.forms[0].select_country;
	setVals(box,current_country);
}





function getCountryMethods(req)
{
	var country_methods = [];
	country_methods['all_methods'] = []; //all methods for country
	country_methods['payments'] = []; //
	country_methods['cashouts'] = [];
	
	var current_country = readCookie('country');
	
	var xmldoc = paymentsXML.responseXML.documentElement.getElementsByTagName(current_country)[0];

	for (var i=0; i < xmldoc.childNodes.length; i++) // 2 - payments / cashouts
	{
		if (xmldoc.childNodes[i].nodeType != 1) continue;
		
		for (var j=0; j < xmldoc.childNodes[i].childNodes.length; j++)
		{
			if (xmldoc.childNodes[i].childNodes[j].nodeType != 1) continue;
			country_methods[xmldoc.childNodes[i].nodeName].push(xmldoc.childNodes[i].childNodes[j].nodeName);
			//build an arry containing all methods - overwrite duplicates using key
			country_methods['all_methods'][xmldoc.childNodes[i].childNodes[j].nodeName] = xmldoc.childNodes[i].childNodes[j].nodeName;
		}
	}
	return country_methods;
}



function getMethodDetails(allMethods)
{
	var methodDetails = [];
	//assign a default value to countryMethodsList if allMethods is not defined
	var countryMethodsList = (!allMethods) ? null : allMethods;
	var xmldoc = paymentsXML.responseXML.getElementsByTagName('methods')[0];

	for (var i=0; i < xmldoc.childNodes.length; i++) //method ids
	{
		if (xmldoc.childNodes[i].nodeType != 1) continue;
		
		if (!countryMethodsList) { //no list passed so getting all methods
			methodDetails[xmldoc.childNodes[i].nodeName] = [];
		}
		else { //list passed so getting country methods only
			if (inArray(countryMethodsList,xmldoc.childNodes[i].nodeName)) {
				methodDetails[xmldoc.childNodes[i].nodeName] = [];
			}
			else {
				continue;	
			}	
		}
		
		for (var j=0; j < xmldoc.childNodes[i].childNodes.length; j++) //method properties
		{
			if (xmldoc.childNodes[i].childNodes[j].nodeType != 1) continue;
			methodDetails[xmldoc.childNodes[i].nodeName][xmldoc.childNodes[i].childNodes[j].nodeName] = xmldoc.childNodes[i].childNodes[j].firstChild.data;
		}
	}
	return methodDetails;
}



function buildMethodsList()
{
	clear_contents(write_methods_el,'tr','methods');
	clear_contents(left_write_methods_el,'li','methods');
	clear_contents(left_write_methods_el,'li','second');

	var allMethods = getCountryMethods();
	var allMethodDetails = [];
	
	allMethodDetails = getMethodDetails(allMethods['all_methods']);

	for (methods in allMethodDetails)
	{
		//create main table
		//begin table row
		var tableRowEl = document.createElement('tr');
		tableRowEl.className = "methods";
		
		//begin image container cell
		var imgContainer = document.createElement('td');
		imgContainer.setAttribute("width", 130);
		
		//create link
		var methodLink = document.createElement('a');
		var url = '/poker/real-money/' + allMethodDetails[methods]['link'] + '/index.html';
		methodLink.setAttribute('href',url);
		methodLink.className = 'popup';
		
		
		//create image and add link
		var imgMethodLink = methodLink.cloneNode(true); //clone methodLink for reuse
		var imgSrc = '/poker/real-money/' + allMethodDetails[methods]['image'];
		var imgEl = document.createElement("img");
		imgEl.setAttribute("src",imgSrc);
		imgMethodLink.appendChild(imgEl);
		imgContainer.appendChild(imgMethodLink);
		
		//append to cell to row
		tableRowEl.appendChild(imgContainer);

		//begin link container cell
		var container = document.createElement('td');
		var txtMethodLink = methodLink.cloneNode(true); //clone methodLink for reuse
		var linkTxt = document.createTextNode(translated_methods[methods]);	
		txtMethodLink.appendChild(linkTxt);
		container.appendChild(txtMethodLink);
		
		//append to cell to row
		tableRowEl.appendChild(container);
		
		//begin isPayment container cell
		var pm_container = document.createElement('td');
		pm_container.className = 'method_box';
		var pm_img = document.createElement('img');
		var pm_img_src = (inArray(allMethods['payments'],methods))? '/poker/real-money/images/tick.gif' : '/poker/real-money/images/cross.gif';
		pm_img.setAttribute('src',pm_img_src);
		pm_container.appendChild(pm_img);
		//append to cell to row
		tableRowEl.appendChild(pm_container);
		
		//begin isCashout container cell
		var cm_container = document.createElement('td');
		cm_container.className = 'method_box';
		var cm_img = document.createElement('img');
		var cm_img_src = (inArray(allMethods['cashouts'],methods))? '/poker/real-money/images/tick.gif' : '/poker/real-money/images/cross.gif';
		cm_img.setAttribute('src',cm_img_src);
		cm_container.appendChild(cm_img);
		//append to cell to row
		tableRowEl.appendChild(cm_container);
		
		//create left nav
		
		//begin li
		var liEl = document.createElement('li');
		liEl.className = "methods";
		
		var leftMethodLink = methodLink.cloneNode(true); //clone for reuse
		var leftLinkTxt = linkTxt.cloneNode(true); //clone for reuse
		leftMethodLink.appendChild(leftLinkTxt);
		liEl.appendChild(leftMethodLink);
		
		/*********************************************/

		write_methods_el.appendChild(tableRowEl);
		left_write_methods_el.appendChild(liEl);
	}
	
	//append the closing li
	var liLast = document.createElement('li');
	liLast.className = "second";
	left_write_methods_el.appendChild(liLast);
	
	//ie6 doesn't support setAttribute for js popup so use function to attach events
	attach_popups();
	write_methods_el.style.display = '';	
	toggle_visibility('leftNav','h3','block');
}


//build two dom lists which will be appended to the appropriate element
function buildAllMethodsList()
{
	clear_contents(write_methods_el,'tr','methods');
	clear_contents(left_write_methods_el,'li','methods');
	write_methods_el.style.display = 'none';
}



/*
*	Check for value in an array

*/
function inArray(objSearch,strVal)
{
	for (method in objSearch)
	{
		if (objSearch[method] == strVal)
		return true;
	}
	return false;
}


/*
*	Toggle visisbility of elements with tag name
*/

function toggle_visibility(idref,tagname,mode)
{
	var x = document.getElementById(idref).getElementsByTagName(tagname);

	for (var i=0; i <  x.length; i++)
	{
		//if (x[i].className != 'method_heading') continue;
		x[i].style.display = mode;
	}
}



/*
*	Clear child elements of a node except those with classname == classN
*/

function clear_contents(el,type,classN)
{	
	var rows = el.getElementsByTagName(type);

	for(var i = rows.length-1; i >= 0; i--)
	{	
		if (rows[i].className == classN) {
			x = rows[i].parentNode;
			x.removeChild(rows[i]);
		}	
	}	
	return;
}



/*
*	set select element dropdown value
*/

function setVals(el,str) {

	for (i=0; i < el.options.length; i++)
	{
		if (el.options[i].value == str) {
			el.selectedIndex = i;
		}
	}
}


function createCookie(name,value,days) {

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}

	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for (var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}



function eraseCookie(name) {
	createCookie(name,"",-1);
}



function attach_popups()
{
	var allLinks = document.getElementsByTagName('a');

	for (var i=0, thisLink; thisLink = allLinks[i]; i++)
	{
		if (thisLink.className == 'popup') {
			thisLink.onclick = function()
			{
				var newWin = window.open(this.href,'popup','width=320,height=320,scrollbars=yes');
				return false;
			}
		}
	}
}
