//----------------------------------------------------------------------------------------------------------
/* Creates 2 dropdowns in alphabetical order from currency string received from the host!*/
//----------------------------------------------------------------------------------------------------------


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 optionsString1 = String;
var optionsString2 = String;

//------------------------------------------------------------------------------------------------------------
// result savings are in array currencyResultarray
currencyResultarray = new Array();
amountOfCalculations = 0;
//----------------------------------------------------------------------------------------------------------

var myCurrencyString = document.getElementsByName("txhValInfo");
var myUTF8TranslatedString = myCurrencyString[0].value;
var myCurrencyArray = myUTF8TranslatedString.split("%");
//alert(myCurrencyArray);

/* 
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 string in Body text!
genDate = myCurrencyArray[0].substring(8,10)+'.'+myCurrencyArray[0].substring(5,7)+'.'+myCurrencyArray[0].substring(0,4);
switch (brand)
{
case "DK":
	
	var topTextCurrency = 'Beregningerne er vejledende pr. ';
	topTextCurrency += genDate;
	topTextCurrency += ' og er ekskl. evt. gebyr.<br/>Vi g&#248;r opm&#230;rksom p&aring;, at kurserne er k&#248;bskurser, og at de er baseret p&aring; Nationalbankens valutakurser ovenn&#230;vnte dato kl. 14:15. Kurserne kan &#230;ndre sig i l&#248;bet af dagen alt efter udviklingen p&aring; valutamarkedet.';
	break;
case "SE":

	var topTextCurrency = 'K&#246;pkurs per ';
	topTextCurrency += genDate;
	topTextCurrency += '.';
	break;

case "NO":

	var topTextCurrency = 'Beregningene er veiledende per ';
	topTextCurrency += genDate;
	topTextCurrency += ' og eksklusiv eventuelle gebyrer.';
	break;
	
	default:
	
	var topTextCurrency = 'TEKST pr. ';
	topTextCurrency += genDate;
	topTextCurrency += ' og er ekskl. evt. gebyr.';
	break;
	
	break;

}

document.getElementById("CurrencyBodyText").innerHTML = topTextCurrency;
// insert date END...	
	
	
	
	
	
	
// 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(",","."));
		//alert('KØB: ' +CurrencyValues[counterLead]['buy'])
		CurrencyValues[counterLead]['sale'] = parseFloat(saleValue.replace(",","."));
		//alert('SALG: ' +CurrencyValues[counterLead]['sale'])
		CurrencyValues[counterLead]['middrate'] = parseFloat(midValue.replace(",","."));
		//alert('MIDDEL: ' +CurrencyValues[counterLead]['middrate'])
		CurrencyValues[counterLead]['abbreviation'] = myCurrencyArray[i].substring(0,3);
		
		
		// if brand currency then rate is 100.00!
		if (CurrencyValues[counterLead]['buy'] == 0) {
			var brandCurrency = CurrencyValues[counterLead]['abbreviation'];
			//alert(brandCurrency);
			// alert(CurrencyValues[counterLead]['name']);
			CurrencyValues[counterLead]['buy'] = 100;
			CurrencyValues[counterLead]['sale'] = 100;
			CurrencyValues[counterLead]['middrate'] = 100;
			
			}
		
		
		// nomalize currencies so it ALWAYS IS how much is 100 of Foreign Currency in Brand Currency!
		if (brand == "NIB" && CurrencyValues[counterLead]['buy'] != 100 || brand == "NOB" && CurrencyValues[counterLead]['buy'] != 100 || brand == "FI" && CurrencyValues[counterLead]['buy'] != 100) {
			// alert("konverterer valuta!");
			
			CurrencyValues[counterLead]['buy'] = 10000 / parseFloat(CurrencyValues[counterLead]['buy']);
			CurrencyValues[counterLead]['sale'] = 10000 / parseFloat(CurrencyValues[counterLead]['sale']);
			CurrencyValues[counterLead]['middrate'] = 10000 / parseFloat(CurrencyValues[counterLead]['middrate']);
		}
		
		
		
		
		
		
		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
	
		
	for (i=0; i<CurrencyValues.length; i++) {
	optionsString1 += '<option value='+i+'';
	
	if (CurrencyValues[i]['abbreviation'] == defaultDropDown1) { optionsString1 += ' selected="selected"'; }
	
	optionsString1 += '>'+CurrencyValues[i]['name']+' ('+ CurrencyValues[i]['abbreviation'] +')</option>';
	
	
	optionsString2 += '<option value='+i+'';
	
	if (CurrencyValues[i]['abbreviation'] == defaultDropDown2) { optionsString2 += ' selected="selected"'; }
	
	optionsString2 += '>'+CurrencyValues[i]['name']+' ('+ CurrencyValues[i]['abbreviation'] +')</option>';
	
	}
	
	// SETUP DD END
	
	if (defaultDropDown1 == "0") { optionsString1 = '<option value="-1" selected="selected">'+defaultDropDownText+'</option>'+ optionsString1}
	if (defaultDropDown2 == "0") { optionsString2 = '<option value="-1" selected="selected">'+defaultDropDownText+'</option>'+ optionsString2}
	

// ----------------------------------------------------------------------------------------------
// inserts dropdowns in divs

	document.getElementById("curFrom").innerHTML = '<select id="currencyFromDropDown" name="currencyFromDropDown" class="activemark required">' + optionsString1 + '</select><span class="reqmark">&nbsp;</span>';
	document.getElementById("curTo").innerHTML = '<select  id="currencyToDropDown" name="currencyToDropDown" class="activemark required">' + optionsString2 + '</select><span class="reqmark">&nbsp;</span>';
	

	
	
// ----------------------------------------------------------------------------------------------
// currencyToValueInArray function

function handleRatePopCurrencies(currencyRatePop, currencyRateFrom, currencyRateTo) { 

newwindow2=window.open('','name','height=200,width=150');
	var tmp = newwindow2.document;
	tmp.write('<html><head><title></title>');
	tmp.write('');
	tmp.write('</head><body><p>'+currencyRateFrom + ' / ' + currencyRateTo+'</p>');
	tmp.write('<p>'+currencyRatePop + ' ' + currencyRateFrom + ' for 100 ' + currencyRateTo+'</p>');
	tmp.write('<p>' + 10000/parseFloat(currencyRatePop) + ' ' + currencyRateTo + ' for 100 ' + currencyRateFrom+'</p>');
	tmp.write('</body></html>');
	tmp.close();




}





