//----------------------------------------------------------------------------
//   Source: currencycalc.js
//----------------------------------------------------------------------------

//Set default values for the currencies 
//that have fixed rates against the euro 
var eurovals = new Object();
eurovals['FRF'] = 6.559;
eurovals['ATS'] = 13.76030;
eurovals['PTE'] = 200.48200;
eurovals['ESP'] = 166.38600;
eurovals['BEF'] = 40.33990;
eurovals['NLG'] = 2.20371;
eurovals['FIM'] = 5.94573;
eurovals['DEM'] = 1.95583;
eurovals['IEP'] = 0.78756;
eurovals['ITL'] = 1936.27000;
eurovals['LUF'] = 40.33990;
eurovals['GRD'] = 340.75;

//Calculate the currency exchange rates
function calculate() 
{
	
	var select1 = document.currcalc.from_tkc;  
	var select2 = document.currcalc.to_tkc;	
	var select1_text = select1[select1.selectedIndex].text;
	var select2_text = select2[select2.selectedIndex].text;
	var fromval;	//Value of the currency to be converted
	var toval;		//Value of the currency to convert into


	//These error messages are hidden values in the HTML page
	//They get translated into foreign languages
	var errmsg1 = document.currcalc.translation1.value;
	var errmsg2 = document.currcalc.translation2.value;
	var errmsg3 = document.currcalc.translation3.value;
	var errorfound = 0; //flag invalid selection

	 //calculate the fixed Euro value
	 if (select1_text.match(/\(EUR\)/) != null)
     { 
		 var temp = select2_text.split("(");
		 var lastsplit = temp.length - 1;
		 var result = temp[lastsplit].match(/(FRF|ATS|PTE|ESP|BEF|NLG|FIM|DEM|IEP|ITL|LUF|GRD)/);
		 if (result != null)
         {
			fromval = 1;
			toval = eurovals[result[0]];
		 }
     }
	 
	  //calculate the fixed Euro value
     if (select2_text.match(/\(EUR\)/) != null)
     {
	 	var tmp = select1_text.split("(");
		var lstsplit = tmp.length - 1;
		var result = tmp[lstsplit].match(/(FRF|ATS|PTE|ESP|BEF|NLG|FIM|DEM|IEP|ITL|LUF|GRD)/);
	 	if (result != null)
	 	{
			toval = 1;
			fromval = eurovals[result[0]];
		}
     }
     
	 //set the prices
     if (fromval == null && toval == null)			
     {
 	 	 fromval = price[select1[select1.selectedIndex].value];
     	 toval = price[select2[select2.selectedIndex].value];
     }
	 
	 //check to make sure that there is a valid price/ valid currency for each
	 if ((fromval < 0) || (fromval == "") || isNaN(fromval) || (toval < 0) || (toval == "") || isNaN(toval)) {
		 if ((fromval == null) || (fromval <= 0)){
		 	if (select1_text.match(/Select/) == null){
		 		alert(select1_text + " data is currently not available.");
			}
			else{
				errorfound=1;
			}
		}
		if ((toval == null) || (toval <= 0)){
		 	if (select2_text.match(/Select/) == null){
		 		alert(select2_text + " data is currently not available.");
			}
			else{
				errorfound=1;
			}
		}
		if (errorfound){
			alert(errmsg1);
		}
		return false;
     }

	//assign the amount and check that it is valid, remove any punctuation 
	//amount is the number of the base currency units specified by the user
	var amount = document.currcalc.amount.value;
	if ((amount == "") || isNaN(amount) || (amount == 0) || (amount < 1)){ 
	 	alert(errmsg2);
		document.currcalc.amount.focus();
		return false;
	}
   	var pattern = /,/g;
   	amount = amount.replace(pattern, "");
	
	//show all results
	ShowResult(fromval, toval, amount);
}

//Display the results of the calculation
function ShowResult(fromval, toval, amount)
{

	var totval = amount * toval / fromval;		//total conversion rate
    var select1 = document.currcalc.from_tkc; 	//Base currency name from the HTML form
    var select2 = document.currcalc.to_tkc;   	//"Convert into" currency name from the HTML form
	var pagetype = document.currcalc.pagetype.value;
	var translate = document.currcalc.translation.value;
	var header = '';

	if (pagetype == "mkts"){
		header = '<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" width="100%">'
				+ '<TR BGCOLOR="#D2E1E8"><TD align="center">'; 
	}
	else{
		header = '<TABLE BGCOLOR="#DDEDD9" BORDER="0" CELLSPACING="0" CELLPADDING="0" width="100%">'
				+ '<TR BGCOLOR="#D8E6CC"><TD align="center">';
	}

	var all_results = header
				+ '<span class="style4">' 
				+ '<br>' + amount + ' ' 
				+ select1.options[select1.selectedIndex].text 
				+ ' = ' + round(totval)+ ' ' 
				+ select2.options[select2.selectedIndex].text
				+ '</span><br><br></TD></TR></TABLE><br>';
	
	 if ((navigator.appName.indexOf("Microsoft") != -1) || 
	 		  (parseInt(navigator.appVersion) >= 5)){
			document.getElementById("crncyres").innerHTML = all_results;
    } 
	//Print an error message for other browsers not supporting the DOM
	else {
		alert(errmsg3);
	}
}	

//Math function: round a number
function round(num) 
{
        if( num < 200000 )
        {
	  return Math.round( num * 10000 ) / 10000;
        }
        else if( num < 20000000 )
        {
	  return Math.round( num * 100 ) / 100;
        }
        else
        {
          return Math.round( num );  
        }
}


