// File: readXML.js
// Thanks to http://www.javascriptkit.com/javatutors/round.shtml for the rounding!
// Start function when DOM has completely loaded 
$(document).ready(function()
{

	//Variables for GBP and USD conversions
	var GBPRate = 0; // Standard GBP Rate
	var GBPRate1 = 0;  // GBP < 3000   
	var GBPRate2 = 0;  // GBP 3000 - 13000
	var GBPRate3 = 0;  // GBP > 13000	
	
	var USDRate = 0; // Standard USD Rate
	var USDRate1 = 0;  // USD < 3,000
	var USDRate2 = 0;  // USD 3,000 - 13,000
	var USDRate3 = 0;  // USD > 13,000
	
	//Variables for currenct converter
	var fromCurrCountry = '';  //the country to be converted from
	var fromCurrSellNote = 0;  //the sellnote value of the country as above
	var toCurrCountry = '';    //the country to be converted to
	var toCurrSellNote = 0;    //the sellnote value of the country as above
	var conamt = 0;    //the anmount to be converted
	var answer = 0;	   //the answer to the conversion
	var rate = 0;      //the vale of 1 fromCurrency to toCurrency
	var invsrate = 0;  //the value of 1 toCurrency to fromCurrency (inverse rate)
	
	//create list of country codes to be excluded
	var arrayExclusions  = ["ILS","TRY","MYR","JOD","CLP","LTL","USD > 13,000","DOP","JMD","SGD","AED","SKK","KES","FJD","CNY","BBD","LVL","KRW","GBP 3,000 - 13,000","MUR","BRL","BGN","BMD","TWD","THB","GBP > 13,000","MXN","EEK","KWD","USD 3,000 - 13,000"];
	
	// Get and load the SOAP feed file	    
	$.get("http://www.bcu.ie/tbontb/wp-content/themes/Ballincollig-CU/scripts/proxy.php",function(xml)
	{
		/*===========================================================================*/
		/*  Currency Converter 
		/*===========================================================================*/
		
		//1. Generate the output for the select options
	    currSelect = '';
     	currSelect += '<select class="select" name="currselect">';
		currSelect += '<option value="1">Euro</option>';
		//1. Get the rates for GBP and USD
		$('XML_Serializer_Tag',xml).each(function(i)
		{	
		
			//get the current currency code
			currency = $(this).find("currency").text(); //The country code			
			
			//check if it's in the exceptions array
			//if 'false' then process currency
			//else skip it!			
			if (!isException(arrayExclusions,currency)) 
			{
				strEuroCol = $(this).find("sellNote").text(); 		//Get the euro figure(SellNote) as a string				
				countryName=getCountryName(currency);				//get the country of the currency
				
				//string should appear as such: <option value="strEuroCol">countryName</option>
				currSelect += '<option value="' + strEuroCol + '">' + countryName + '</option>';
		
			}			
		});
		currSelect += '</select>';
	    // Update the DIV called Content Area with the HTML string
	    $("li.currselect").append(currSelect);		
		
		//2. Perform conversion on button press
		$('.btnconvert').click(function (event) 
		{
			
			fromCurrCountry = $('#fromcurr select option:selected').text();
	        fromCurrSellNote = $('#fromcurr select').val();
	        toCurrCountry = $('#tocurr select option:selected').text();
	        toCurrSellNote = $('#tocurr select').val();
	        conamt = $('#amount').val();
			answer = conamt * (toCurrSellNote/fromCurrSellNote);
			rate = answer/conamt;
			invsrate = conamt/answer;
			
			/*Round potentially long integers: intAnswer = parseFloat(answer);*/
			anwserRounded=Math.round(answer*1000)/1000;  //round number				
		    strAnswer = anwserRounded+''; //convert this GBP back to a string*/
			rateRounded = Math.round(rate*1000)/1000;
			strRate = rateRounded+'';
			invsrateRounded  = Math.round(invsrate*1000)/1000;
			strInvsRate = invsrateRounded+'';
			
			//create html for answer
			currAnwser = '<br /><hr class="divider">';
			currAnwser += '<p class="currencybold">';
			currAnwser += conamt  + " " + fromCurrCountry + ' = ' + strAnswer + " " + toCurrCountry;
			currAnwser += '</p>';
			currAnwser += '<p align="center"> 1 ' + fromCurrCountry + ' = ' + strRate + '<br />';
			currAnwser += ' 1 ' + toCurrCountry + ' = ' + strInvsRate + '</p> <hr class="divider">';
			$('#currencyanswer').html(currAnwser);		
			
			
		});
		
		/*===========================================================================*/
		/*  Get the USD and GBP Rates
		/*===========================================================================*/			
		$('XML_Serializer_Tag',xml).each(function(i)
		{
	
			//get the currency figure
			currency = $(this).find("currency").text();				
			
			//store rates based on current currency value
			switch (currency)
			{
				//GBPRate1
				case "GBP":				
				strGBPRate = $(this).find("sellNote").text();		
				GBPRate = parseFloat(strGBPRate);
				break;
				
				//GBPRate1
				case "GBP < 3,000":				
				strGBPRate1 = $(this).find("sellNote").text();		
				GBPRate1 = parseFloat(strGBPRate1);
				break;
				
				//GBPRate2
				case "GBP 3,000 - 13,000":
				strGBPRate2 = $(this).find("sellNote").text();
				GBPRate2 = parseFloat(strGBPRate2);
				break;
				
				//GBPRate3
				case "GBP > 13,000":
				strGBPRate3 = $(this).find("sellNote").text();	
				GBPRate3 = parseFloat(strGBPRate3);
				break;
				
				//USDRate1
				case "USD":
				strUSDRate = $(this).find("sellNote").text();		
				USDRate = parseFloat(strUSDRate);
				break;
				
				//USDRate1
				case "USD < 3,000":
				strUSDRate1 = $(this).find("sellNote").text();		
				USDRate1 = parseFloat(strUSDRate1);
				break;
				
				//USDRate2
				case "USD 3,000 - 13,000":
				strUSDRate2 = $(this).find("sellNote").text();		
				USDRate2 = parseFloat(strUSDRate2);
				break;
				
				//USDRate3
				case "USD > 13,000":
				strUSDRate3 = $(this).find("sellNote").text();
				USDRate3 = parseFloat(strUSDRate3);
				break;
			}//close the switch
		});	//close the 'each' loop	
		
		//2. Start the table for the results
		myHTMLOutput = ''; //emptry string
	 	myHTMLOutput += '<table class="rates">';//top of table
		myHTMLOutput += '<thead>'
		myHTMLOutput += '<tr>'
	  	myHTMLOutput += '<th>Country</th><th class="eur"><img src="../images/flags/europeanunion.png" />1 EUR</th><th class="gbp"><img src="../images/flags/gb.png" />1 GBP</th><th class="usd"><img src="../images/flags/us.png" />1 USD</th>'; //table headings
		myHTMLOutput += '</tr>'
		myHTMLOutput += '</thead>'
		myHTMLOutput += '<tbody>'
		

		//3. Loop again and build up each individual row
		$('XML_Serializer_Tag',xml).each(function(i)
		{		
			//get the current currency code
			currency = $(this).find("currency").text(); //The country code
			
			//check if it's in the exceptions array
			//if 'false' then process currency
			//else skip it!			
			if (!isException(arrayExclusions,currency)) 
			{
				//alert("Current currency: " + currency + " is not in the exceptions list: Process row!");
				//start the row
				myHTMLOutput += '<tr>';			
			
				//Country Column
				//currency = $(this).find("currency").text(); //The country code
				countryName=getCountryName(currency);
				myHTMLOutput += '<td>' + countryName + ' (' + currency + ')' + '</td>';			
			
				//EURO Column
				strEuroCol = $(this).find("sellNote").text(); //Get the euro figure(SellNote) as a string
				intEuroCol = parseFloat(strEuroCol);				//Convert it to an int for maths				
				myHTMLOutput += '<td>'+ strEuroCol +  '</td>';	
			
				//alert("Country is: " + currency + "<br />" + "1 Euro is: " + strEuroCol);
			
				//work out the GBP Rate: sellNote / GBPRate1 
				intGBPCol = intEuroCol / GBPRate;	//result is huge number				
				var gbpRounded=Math.round(intGBPCol*1000)/1000  //round number				
				strGBPCol = gbpRounded+''; //convert this GBP back to a string
				myHTMLOutput += '<td>'+ strGBPCol +  '</td>';
							
				//alert("GBP Column is: " + strGBPCol);						
			
				//work out the USD Rate: sellNote / USDRate1 
				intUSDCol = intEuroCol / USDRate;	
				var usdRounded=Math.round(intUSDCol*1000)/1000  //round number	
				strUSDCol = usdRounded+''; //convert this GBP back to a string
				myHTMLOutput += '<td>'+ strUSDCol +  '</td>';					
						
				//alert("USD Column is: " + strUSDCol);
			
				myHTMLOutput += '</tr>';
		
			}
			else
			{
				//alert("current currency: " + currency + " is in the exceptions list: Skip row!");
				myHTMLOutput += '<!-- THIS CURRENCY NOT USED -->'				
			}				
				
				
		});//close the each loop
		
		//close the table body
		myHTMLOutput += '</tbody>'
		
		//Output the closing tags for our table
		myHTMLOutput += '</table>';
	
		// Update the DIV called Content Area with the HTML string
		$("#ContentArea").append(myHTMLOutput);
		
	});//close get xml file
	
});//close the ready document


//check if the current currency is in the exceptions array
function isException(arrayExclusions,currency)
{	
	var yesFlag = 0;
	//loop true our array
	//return true if country is found (meaning it should be skipped)
	//else return false
	for (var loop = 0; loop <arrayExclusions.length; loop++)
	{		
		//alert("Current currency is: " + currency +
				//". Current currency in loop is: " + arrayExclusions[loop]);
		if(arrayExclusions[loop]==currency)
		{
			//alert("We want to return TRUE now!");
			yesFlag =1;
		}
		
	}//end for
	
	if(yesFlag >= 1)
	{
	  return true;
	}
	else
	{
	  return false;
	}
}//close isException function

//returns the english version of the currency code
function getCountryName(currency)
{
	var country;
	
	switch (currency)
	{
		case 'ILS':
		country='Israeili New Shekel'
		return country;
		break;
		
		case 'TRY':
		country='TRY'
		return country;
		break;
		
		case 'MYR':
		country='Malaysian Ringgit'
		return country;
		break;
		
		case 'JOD':
		country='Jordanian Dinar'
		return country;
		break;
		
		case 'CLP':
		country='Chilean Pesol'
		return country;
		break;
		
		case 'LTL':
		country='Lithuanian Litas'
		return country;
		break;
		
		case 'USD > 13,000':
		country='United States Dollar  <br /> (Greater than 13,000)'
		return country;
		break;
		
		case 'CZK':
		country='Czech Koruna'
		return country;
		break;
		
		case 'DOP':
		country='Dominican Peso'
		return country;
		break;
		
		case 'JMD':
		country='Jamaican Dollar'
		return country;
		break;
		
		case 'SGD':
		country='Singapore Dollar'
		return country;
		break;
		
		case 'AED':
		country='United Arab Emirates Dirham UAE Dhm'
		return country;
		break;
		
		case 'CHF':
		country='Swiss Franc'
		return country;
		break;
		
		case 'SKK':
		country='Slovak Koruna'
		return country;
		break;
		
		case 'SEK':
		country='Swedish Krona'
		return country;
		break;
		
		case 'KES':
		country='Kenyan Shilling'
		return country;
		break;
		
		case 'FJD':
		country='Fiji Dollar'
		return country;
		break;
		
		case 'CNY':
		country='Chinese Yuan Renminbi'
		return country;
		break;
		
		case 'PLN':
		country='Polish Zloty'
		return country;
		break;
		
		case 'BBD':
		country='Barbados Dollar'
		return country;
		break;
		
		case 'AUD':
		country='Australian Dollar'
		return country;
		break;
		
		case 'LVL':
		country='Latvian Lats'
		return country;
		break;
		
		case 'HRK':
		country='Croatian Kuna'
		return country;
		break;
		
		case 'KYD':
		country='Cayman Islands Dollar'
		return country;
		break;
		
		case 'USD < 3,000':
		country='United States Dollars'
		return country;
		break;
		
		case 'ISK':
		country='Iceland Krona'
		return country;
		break;
		
		case 'ZAR':
		country='South African Rand'
		return country;
		break;
		
		case 'KRW':
		country='Korean Won'
		return country;
		break;
		
		case 'HKD':
		country='Hong Kong Dollar'
		return country;
		break;
		
		case 'CAD':
		country='Canadian Dollar'
		return country;
		break;
		
		case 'GBP 3,000 - 13,000':
		country='Pound Sterling  <br /> (3,000 to 13,000)'
		return country;
		break;
		
		case 'HUF':
		country='Hungarian Forint'
		return country;
		break;
		
		case 'GBP < 3,000':
		country='Pound Sterling'
		return country;
		break;
		
		case 'MUR':
		country='Mauritius Rupee'
		return country;
		break;
		
		case 'BRL':
		country='Brazilian Real'
		return country;
		break;
		
		case 'NOK':
		country='Norwegian Kroner'
		return country;
		break;
		
		case 'DKK':
		country='Danish Krone'
		return country;
		break;
		
		case 'IDR':
		country='Indonesian Rupiah'
		return country;
		break;
		
		case 'BGN':
		country='BGN'
		return country;
		break;
		
		case 'NZD':
		country='New Zealand Dollar'
		return country;
		break;
		
		case 'BMD':
		country='Bermudian Dollar'
		return country;
		break;
		
		case 'TWD':
		country='Taiwan Dollar'
		return country;
		break;
		
		case 'THB':
		country='Thai Baht'
		return country;
		break;
		
		case 'JPY':
		country='Japanese Yen'
		return country;
		break;
		
		case 'GBP > 13,000':
		country='Pound Sterling <br /> (Greater than 13,000)'
		return country;
		break;
		
		case 'MXN':
		country='MXN'
		return country;
		break;
		
		case 'EEK':
		country='Estonian Kroon'
		return country;
		break;
		
		case 'KWD':
		country='Kuwaiti Dinar'
		return country;
		break;
		
		case 'SAR':
		country='Saudi Riyal'
		return country;
		break;
		
		case 'USD 3,000 - 13,000':
		country='United States Dollar'
		return country;
		break;
		
		case 'EGP':
		country='Egyptian Pound'
		return country;
		break;
		
		case 'USD':
		country='American Dollars'
		return country;
		break;
		
		case 'GBP':
		country='Pound Sterling'
		return country;
		break;
		
	}//CLOSE Switch
}//close function
