var cmode='csell';
var TMPelement;
var rate;
var CursorPos=0;
var SeT=false;

var today=rigatime.split(":");
var h=today[0];
var m=today[1];
var s=today[2];
var rates;
var pulstime;
var secFlash=true;

function SetCurr1(rate){
var a=document.forms['currency'].L1.options.selectedIndex;
var tmpcurr=document.forms['currency'].L2.options[document.forms['currency'].L2.options.selectedIndex].value;
var tmpcurrA=rate.sell[0].name;
var tmptext;
var tmp;
var tmprez=0;
if (cmode=='csell') {
 	tmptext='\u0412\u0430\u043C \u043F\u043E\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044F ';
 	tmprez=rate.buy[0].amount;
 	tmpcurr=rate.buy[0].name;
 	tmpcurrR=rate.buy[0].rate;
}
if (cmode=='cbuy') {
 	tmptext='\u0412\u044B \u043F\u043E\u043B\u0443\u0447\u0438\u0442\u0435 ';
 	tmprez=rate.sell[0].amount;
 	tmpcurr=rate.sell[0].name;
 	tmpcurrR=rate.sell[0].rate;
}

tmp=tmptext+FormatNumberSP(tmprez.toFixed(2))+' '+tmpcurr+' <span>K\u0443\u0440\u0441 '+tmpcurrR+'</span>';
$('#currcalc1').html(tmp);

// for OR values

$('#or0').html('');
$('#or1').html('');
$('#or2').html('');
var orTD=0;
var tmpcA=document.forms['currency'].L1.options[document.forms['currency'].L1.options.selectedIndex].value;;
var tmpcB=document.forms['currency'].L2.options[document.forms['currency'].L2.options.selectedIndex].value;;

if (cmode=='csell') {
	for (var i = 1; i < rate.sell.length; i++) {
		tmpcurr=rate.sell[i].name;
		if (tmpcurr!=tmpcA && tmpcurr!=tmpcB) {
			tmprez=rate.buy[i].amount;
			$('#or'+orTD).html(FormatNumberSP(tmprez.toFixed(2))+" "+tmpcurr);
			orTD++;
		}
	}
}
if (cmode=='cbuy') {
	for (var i = 1; i < rate.buy.length; i++) {
		tmpcurr=rate.buy[i].name;
		if (tmpcurr!=tmpcA && tmpcurr!=tmpcB) {
			tmprez=rate.sell[i].amount;
			$('#or'+orTD).html(FormatNumberSP(tmprez.toFixed(2))+" "+tmpcurr);
			orTD++;
		}
	}
}
}

function FormatNumberSP(nStr){
var nStr2;
nStr=TrimNum(nStr);
var inD=".";
var outD=".";
var sep="&nbsp;";
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	nStr2=nStr + nStrEnd;
	kd=nStr2.length;
	if (nStr2.substring(kd-3)==outD+'00') {
		nStr2=nStr2.substring(0,kd-3);
	}
	
cd=document.forms['currency'].summ.value;
cdP=cd.indexOf(',');	
if (cdP!=-1) nStr2=nStr2.split('.').join(',');
	return nStr2;
}

function FormatNumberSP2(nStr){
nStr=TrimNum(nStr);
if (nStr.indexOf('.')!=-1) {
	var inD=".";
	var outD=".";
}
if (nStr.indexOf(',')!=-1) {
	var inD=",";
	var outD=",";
}

var sep=" ";
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStrEnd = nStrEnd.substring(0, 3);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function TrimNum(a){
return a.split(' ').join('');
}

function SetComa(a) {
return a.split(',').join('.');
}


function populate(){
pulstime=setTimeout('GetPuls()',60000);
startTime();
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function startTime() {
s++;
if (s>59) {
	s=0;
	m++;
	if (m>59) {
		m=0;
		h++;
		if (h==24) {
			h=0;	
		}
	}
}
h=checkTime(h)
m=checkTime(m);
s=checkTime(s);

if (secFlash) {
	$('#clock').html(h+":"+m);
	secFlash=false;
} else {
	$('#clock').html(h+" "+m);
	secFlash=true;
}

t=setTimeout('startTime()',1000);
}



function checkTime(i){
if (i<10)  {i="0" + i;}
return Right(i,2);
}

function GetData() {
for (var i = 0; i < document.forms['currency'].L1.length; i++) {
	if (document.forms['currency'].L1.options[i].value=='EUR') {
		document.forms['currency'].L1.options.selectedIndex=i;
	}
}
for (var i = 0; i < document.forms['currency'].L2.length; i++) {
	if (document.forms['currency'].L2.options[i].value=='USD') {
		document.forms['currency'].L2.options.selectedIndex=i;
	}
}
GetRateNew();
}

function OneT(a,b){
if (a==b) return '1';
return a+'/'+b;
}

function GetPuls() {
$.ajax({
   type: "GET",
   url: "/sng.nsf/FormRBPulse?OpenAgent&conf=sngtrading_config",
   data: "",
   success: function(msg){
	$('#rbpuls').html(msg);
	pulstime=setTimeout('GetPuls()',60000);
	return;
	}
});
}

function GetRateNew() {
d1=document.forms['currency'].L1[document.forms['currency'].L1.options.selectedIndex].value;
d2=document.forms['currency'].L2[document.forms['currency'].L2.options.selectedIndex].value;
d3=(TrimNum(SetComa(document.forms['currency'].summ.value))*1);
$.ajax({
   type: "GET",
   url: "/sng.nsf/FormRBRatesAllSuper?OpenAgent&amount="+d3+"&from="+d1+"&to="+d2+"&conf=main&ch="+new Date().getTime(),
   data: "",
   success: function(msg){
	if (msg.charAt(0)=="{") {
		var tmp=eval("rate="+msg);
		SetCurr1(rate);
	}
	return;
	}
});
}

