
var CurrencyItems = { Id : 0, Name : 1, DisplayedName : 2 };
var OptionInfoItems = { Index : 0, Value : 1, Text : 2 };
var NOT_FORMATTED_FIELDS = new Array();
var not_format_postfix = '_notformat';
var USD='USD';
var EUR='EUR';
var RUB='RUB';
var txtUSDRateId = 'USD';
var txtEURRateId = 'EUR';
var slctDefaultCurrencyId = 'DefaultCurrency';
var slctCurrentCurrencyId = 'CurrentCurrency';
var currenciesInfoId = 'CurrenciesInfo';
var defaultCurrencyId = 'CurrencyId';
var fldLastCurrencyId = 'LastCurrencyId';
var changeCurrencyFlagClientId = 'ChangeCurrencyFlag';

function CheckCurrency(currency){

	var txtUSDRate = document.all[txtUSDRateId];
	var txtEURRate = document.all[txtEURRateId];
	var selectElement = document.all[slctDefaultCurrencyId];
	if(!selectElement) selectElement = document.all[slctCurrentCurrencyId];
	var CurrenciesInfo = document.all[currenciesInfoId];
	if(CurrenciesInfo) CurrenciesInfo = CurrenciesInfo.value.split(',')

	if(selectElement){
		if(currency == RUB) SetOption(selectElement, CurrenciesInfo, RUB, FormatNumber('1'));
		if(currency == USD && txtUSDRate) SetOption(selectElement, CurrenciesInfo, USD, txtUSDRate.value);	
		if(currency == EUR && txtEURRate) SetOption(selectElement, CurrenciesInfo, EUR, txtEURRate.value);
	}
}

function SetOption(selectElement, CurrenciesInfo, currency, rate){
	if(selectElement && CurrenciesInfo){
		if(parseFloat(rate)>0){
			if(GetOptionInfo(selectElement, currency, false)[OptionInfoItems.Index] == -1)
				selectElement.options[selectElement.length] = GetNewOption(CurrenciesInfo, currency);
		}else{
			if(GetOptionInfo(selectElement, currency, false)[OptionInfoItems.Index] != -1)
				selectElement.options[GetOptionInfo(selectElement, currency, false)[OptionInfoItems.Index]]=null;
		}
	}
}

function GetOptionInfo(selectElement, text, isVal){
	try{
		for(i=0; i < selectElement.length; i++){
			if( ( (isVal) ? selectElement.options[i].value : selectElement.options[i].text.toLowerCase() ) == text.toLowerCase() )
				return new Array(i, selectElement.options[i].value, selectElement.options[i].text);
		}
		return new Array(-1,'','');
	}catch(exception){ return new Array(-1,'',''); }
}


function GetNewOption(CurrenciesInfo, currencyName){
	try{
		for(i=0; i < CurrenciesInfo.length; i++){
		var CurrencyInfo = CurrenciesInfo[i].split('=');
		if( CurrencyInfo[CurrencyItems.Name].toLowerCase() == currencyName.toLowerCase() )
			return new Option(CurrencyInfo[CurrencyItems.DisplayedName], CurrencyInfo[CurrencyItems.Id]);
		}
		return null;
	}catch(exception){ return null; }
}


function SetDefaultCurrency(){
	try{
		var selectElement = document.all[slctDefaultCurrencyId];
		if(!selectElement) selectElement = document.all[slctCurrentCurrencyId];
		var defaultCurrency = document.all[defaultCurrencyId];

		if(selectElement && defaultCurrency){	
			var index = GetOptionInfo(selectElement, defaultCurrency.value, true)[OptionInfoItems.Index];
			if(index != -1) selectElement.options[index].selected = true;
		}
	}catch(exception){}
}

function SetLastCurrency(){
	try{
		var selectElement = document.all[slctCurrentCurrencyId];
		document.all[fldLastCurrencyId].value = selectElement.options[selectElement.selectedIndex].value;
	}catch(exception){}
}

function OnChangeCurrency(){
	try{
		document.all[changeCurrencyFlagClientId].value = 1;
		for(i=0; i<NOT_FORMATTED_FIELDS.length; i++)
		document.all[NOT_FORMATTED_FIELDS[i]].value = 
			FormatNumber((toFlt(document.all[NOT_FORMATTED_FIELDS[i] + not_format_postfix].value) / GetCurrencyMultiplier()));
	}
	catch(exception){}
}

function ChangeNotformatSum(obj){
	try{ document.all[obj.id + not_format_postfix].value = document.all[obj.id].value;} catch(exception){}
}

function InitCurrencies(){
	CheckCurrency(RUB);
	CheckCurrency(USD);
	CheckCurrency(EUR);
	SetDefaultCurrency();
}

function SetNotformatValue(objId, newVal){
	var obj = document.all[objId + not_format_postfix];
	if(obj) obj.value = newVal;
}

function GetSumByCurrency(sum){
	try{ return sum * GetCurrencyMultiplier(); } catch(exception){ return 1; }
}

function SetValueByCurrency(objId, sum){
	try{
		var newVal, obj = document.all[objId];
		if(!obj) return;
		if(obj.innerText){
			newVal = GetSumByCurrency((sum != null) ? toFlt(sum) : toFlt(obj.innerText));
			obj.innerText =  FormatNumber(newVal);
			//SetNotformatValue(obj.id, newVal);
			return;
		}
		newVal = GetSumByCurrency((sum != null) ? toFlt(sum) : toFlt(obj.value));
		if(obj.value) obj.value =  FormatNumber(newVal);
		if(NOT_FORMATTED_FIELDS.join().indexOf(obj.id) > -1) SetNotformatValue(obj.id, newVal);
	}
	catch(exception){}
}

function InitCurrenciesCalc(){
	for(i=0; i<arguments.length; i++) NOT_FORMATTED_FIELDS.push(arguments[i])
}
