Просмотр файла converter/conv.js

Размер файла: 6.54Kb
var globalNoData=true;
var curs=new Array();
var nCurs=6;
var cb=new Array();
var forex=new Array();
var cash=new Array();
var forex_date;
var cb_date;
var cash_date;
				
		
function Currency(id,curId,fullName) {
	this.id=id;
	this.curId=curId;
	this.fullName=fullName;
}

function Init() {
	curs[0]=new Currency(0,"RUR","Российский рубль");
	curs[1]=new Currency(1,"USD","Доллар США");
	curs[2]=new Currency(2,"EUR","ЕВРО");
	curs[3]=new Currency(3,"JPY","Японская иена");
	curs[4]=new Currency(4,"DEM","Немецкая марка");
	curs[5]=new Currency(5,"GBP","Английский фунт стерлингов");
	for(i=0;i<36;i++) {
		forex[i]=0;
		cb[i]=0;
		cash[i]=0;
	}
forex[2.0*6+5.0]=0.6439;
forex[2.0*6+1.0]=1.018450;
forex[5.0*6+3.0]=194.07;
forex[5.0*6+1.0]=1.581750;
forex[1.0*6+3.0]=122.70;
forex[1.0*6+0.0]=31.8650;
forex[2.0*6+3.0]=124.9750;
forex_date='12-DEC-02';

cb[2.0]=1/32.1467;
cb[2.0*6]=32.1467;
cb[5.0]=1/50.1381;
cb[5.0*6]=50.1381;
cb[3.0]=1/25.8478;
cb[3.0*6]=25.8478;
cb[1.0]=1/31.86;
cb[1.0*6]=31.86;
cb_date='12-DEC-02';

cash[4.0]=1/(null);
cash[4.0*6]=13.50;
cash[2.0]=1/32.62117647;
cash[2.0*6]=32.39222222;
cash[1.0]=1/31.98382353;
cash[1.0*6]=31.93794118;
cash_date='13-DEC-02';

	
	updateToFrom();	
}
function updateToFrom() {
	source=document.forms[formName].source.options[document.forms[formName].source.selectedIndex].value;
	document.forms[formName].To.length=nCurs;
	document.forms[formName].From.length=0;
	for(i=0;i<nCurs;i++) {
		isConv=false; 
		for(j=0;j<nCurs;j++) {
			if(source=='forex') {
				if(forex[i*6+j]!=0) {
					isConv=true;
					break;
				}
			} else if(source=='cb') {
				if(cb[i*6+j]!=0) {
					isConv=true;
					break;
				}
			} else if(source=='cash') {
				if(cash[i*6+j]!=0) {
					isConv=true;
					break;
				}
			}			
		}
		if(isConv) {
			document.forms[formName].From.length++;
			document.forms[formName].From.options[document.forms[formName].From.length-1]=new Option(curs[i].fullName,curs[i].curId,false,false);
		}
		document.forms[formName].To.options[i]=new Option(curs[i].fullName,curs[i].curId,false,false);
	}
	if(document.forms[formName].From.length==0) {
		document.forms[formName].From.length=1;
		document.forms[formName].From.options[0]=new Option("Нет данных","",true,true);
		document.forms[formName].To.length=1;
		document.forms[formName].To.options[0]=new Option("Нет данных","",true,true);
		globalNoData=true;
	} else {
		globalNoData=false;
	}
	document.forms[formName].From.selectedIndex=0;
	document.forms[formName].To.selectedIndex=0;
	if(source=='forex') document.forms[formName].date.value=forex_date;
	else if(source=='cb') document.forms[formName].date.value=cb_date;
	else if(source=='cash') document.forms[formName].date.value=cash_date;
	
	changeTo();
	updateQuote();
}
function getCurId(cur) {
	for(i=0;i<nCurs;i++) {
		if(curs[i].curId==cur) return i;
	}
}
function getCurName(cur) {
	for(i=0;i<nCurs;i++) {
		if(curs[i].curId==cur) return curs[i].fullName;
	}
} 
function getQuote(fromCur,toCur,source) {
	if(source=='forex') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		return forex[fromId*nCurs+toId];
	} else if(source=='cb') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		return cb[fromId*nCurs+toId];
	} else if(source=='cash') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		return cash[fromId*nCurs+toId];
	}
}
function changeTo() { // Calling when onChange of From raised changing to select 
	if(globalNoData) return;
	choosedSource=document.forms[formName].source.options[document.forms[formName].source.selectedIndex].value;
	choosedCurId=getCurId(document.forms[formName].From.options[document.forms[formName].From.selectedIndex].value);
	document.forms[formName].To.length=0;
	if(choosedSource=='forex') {
		for(i=0;i<nCurs;i++) {
			if(forex[choosedCurId*nCurs+i]!=0) {
				document.forms[formName].To.length++;
				document.forms[formName].To.options[document.forms[formName].To.length-1]=new Option(curs[i].fullName,curs[i].curId,false,false);
			}
		}
	} else if(choosedSource=='cb') {
		for(i=0;i<nCurs;i++) {
			if(cb[choosedCurId*nCurs+i]!=0) {
				document.forms[formName].To.length++;
				document.forms[formName].To.options[document.forms[formName].To.length-1]=new Option(curs[i].fullName,curs[i].curId,false,false);
			}
		}
	} else if(choosedSource=='cash') {
		for(i=0;i<nCurs;i++) {
			if(cash[choosedCurId*nCurs+i]!=0) {
				document.forms[formName].To.length++;
				document.forms[formName].To.options[document.forms[formName].To.length-1]=new Option(curs[i].fullName,curs[i].curId,false,false);
			}
		}
	}
	document.forms[formName].To.selectedIndex=0;
	updateQuote();
}
function updateQuote() {
	if(globalNoData) {
		document.forms[formName].quote.value="";
		return;
	}
	fromCur=document.forms[formName].From.options[document.forms[formName].From.selectedIndex].value;
	toCur=document.forms[formName].To.options[document.forms[formName].To.selectedIndex].value;
	source=document.forms[formName].source.options[document.forms[formName].source.selectedIndex].value;
	if(source=='forex') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		quote=forex[fromId*nCurs+toId];
	} else if(source=='cb') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		if(toId>fromId) quote=cb[toId*nCurs+fromId];
		else quote=cb[fromId*nCurs+toId];
	} else if(source=='cash') {
		fromId=getCurId(fromCur);
		toId=getCurId(toCur);
		if(toId>fromId) quote= 1/cash[fromId*nCurs+toId];
		else quote=cash[fromId*nCurs+toId];
	}
	quote=""+quote;
	if((ind=quote.indexOf("."))>=0) {
		if(ind+5<quote.length-1) {
			quote=quote.substring(0,ind+5);
		}
	}
	document.forms[formName].quote.value=quote;
}

function compute() {
	if(globalNoData) return false;
	fromCur=document.forms[formName].From.options[document.forms[formName].From.selectedIndex].value;
	toCur=document.forms[formName].To.options[document.forms[formName].To.selectedIndex].value;
	source=document.forms[formName].source.options[document.forms[formName].source.selectedIndex].value;
	sum=parseInt(document.forms[formName].sum.value);
	if(isNaN(sum)) return false;
	/*if(acceptUserQuote) {
		quote=parseFloat(document.forms[formName].quote.value);
		if(!isNaN(quote)) {
			resSum=sum*quote*parseFloat(document.forms[formName].tax.options[document.forms[formName].tax.selectedIndex].value);
		}
	} else { */
		resSum=sum*getQuote(fromCur,toCur,source)*parseFloat(document.forms[formName].tax.options[document.forms[formName].tax.selectedIndex].value);
	/* } */
	resSum=""+resSum;
	if((ind=resSum.indexOf("."))>=0) {
		if(ind+3<resSum.length-1) {
			resSum=resSum.substring(0,ind+3);
		}
	}
	document.forms[formName].ressum.value=resSum;
	//updateToFrom();
	//alert("from="+fromCur+"\ntoCur="+toCur);
	return false;
}