//----------------------------------------------------------------------------------------------------------
/* Creates dropdown in alphabetical order from currency string received from the host!*/
//----------------------------------------------------------------------------------------------------------

var brand = document.getElementById("brand").value;
var currencyabbreviation = document.getElementById("currencyabbreviation").value;
var myCurrencyString = new Array(); // holds the currency string recieved from the online.-address
var CurrencyValues = new Array(); // holds the saleValues during the calculation
var counterLead = 0;

var myCurrencyString = document.getElementsByName("txhValInfo");
var myUTF8TranslatedString = myCurrencyString[0].value;
var myCurrencyArray = myUTF8TranslatedString.split("%");

/* 
Array myCurrencyArray[i] is holding numbers 1,5,9,13 (+4 interval) ... holds string "abreviationForCurrency NameOfCurrency"
Three mubers after 1, 5, 9, 13 and so forth  holds
2,6,10,14 = K (buying value) ...
3,7,11,15 = S (selling value) ...
4,8,12,16 = N (static value) ...
intil myCurrencyArray.length) 
*/

//insert date for currencyrates in introtext-span
var currencyUpdateField = document.getElementById("currencyupdate");
genDate = myCurrencyArray[0].substring(8,10)+'.'+myCurrencyArray[0].substring(5,7)+'.'+myCurrencyArray[0].substring(0,4);
if (currencyUpdateField != null) {
	currencyUpdateField.innerHTML = genDate + " ";
}
	
// Creates the options dropdowns for CurrencyA and B
for (i=1; i<myCurrencyArray.length-1; i=i+4) {

	ibuy = i+1; 
	isale = i+2; 
	imid = i+3;

	buyValue = myCurrencyArray[ibuy].slice(1);	
	saleValue = myCurrencyArray[isale].slice(1);
	midValue = myCurrencyArray[imid].slice(1);
	
	// strips the leading zeroes from string

	// strips leading zeroes
	buyValue = buyValue.replace(new RegExp(/0+/),"");
	saleValue = saleValue.replace(new RegExp(/0+/),"");
	midValue = midValue.replace(new RegExp(/0+/),"");
		
	CurrencyValues[counterLead] = new Array();
	CurrencyValues[counterLead]['name'] = myCurrencyArray[i].substring(6,30);
	CurrencyValues[counterLead]['buy'] = parseFloat(buyValue.replace(",","."));
	CurrencyValues[counterLead]['sale'] = parseFloat(saleValue.replace(",","."));
	CurrencyValues[counterLead]['middrate'] = parseFloat(midValue.replace(",","."));
	CurrencyValues[counterLead]['abbreviation'] = myCurrencyArray[i].substring(0,3);
	
	counterLead++;
}
	
// SORTS BY CURRENCY NAME IN ALPHABETICAL ORDER \\;
holderArr = new Array();
for (x=0; x<CurrencyValues.length; x++) {
	
	for(y = 0; y < (CurrencyValues.length-1); y++) {
  
		if (CurrencyValues[y]['name'] > CurrencyValues[y+1]['name']) {
			holderArr = CurrencyValues[y+1];
			CurrencyValues[y+1] = CurrencyValues[y];
			CurrencyValues[y] = holderArr;
		}
	}
	
}
// END SORT

// SETUP DROPDOWNS
var currencyFromDropdownElement = document.getElementById("currencyFromDropDown");
var currencyDropTodownElement = document.getElementById("currencyToDropDown");

// inserts dropdown in FromCurrency div, Ignore own currency
if (currencyFromDropdownElement) {
	for (i=0; i<CurrencyValues.length; i++) {
		if (CurrencyValues[i]['abbreviation']!==currencyabbreviation) {
			var optionElement=document.createElement('option');
			optionElement.text=CurrencyValues[i]['name']+' ('+ CurrencyValues[i]['abbreviation'] +')'
			optionElement.value = i;
			
			try
			{
				currencyFromDropdownElement.add(optionElement,null); // standards compliant
			}
			catch(ex)
			{
				currencyFromDropdownElement.add(optionElement); // IE only
			}
		}
	}
}

// inserts dropdown in ToCurrency div, Ignore own currency
if (currencyDropTodownElement) {
	for (i=0; i<CurrencyValues.length; i++) {
		if (CurrencyValues[i]['abbreviation']!==currencyabbreviation) {
			var optionElement=document.createElement('option');
			optionElement.text=CurrencyValues[i]['name']+' ('+ CurrencyValues[i]['abbreviation'] +')'
			optionElement.value = i;
			
			try
			{
				currencyDropTodownElement.add(optionElement,null); // standards compliant
			}
			catch(ex)
			{
				currencyDropTodownElement.add(optionElement); // IE only
			}
		}
	}
}
