var cmode='csell';
var TMPelement;
var rate;
var CursorPos=0;
var SeT=false;

$(document).ready(function(){
 $("#csell").click(function ()  { 
 	if (cmode=="cbuy") {
		$('#'+cmode).removeClass("on");
		cmode='csell';
		$('#'+cmode).addClass("on");
		SetCurr1(rate);
	}	
});
 $("#cbuy").click(function ()  { 
 	if (cmode=="csell") {
		$('#'+cmode).removeClass("on");
		cmode='cbuy';
		$('#'+cmode).addClass("on");
		SetCurr1(rate);
	}
});

$("#summ").focus(function (e)  {
	TMPelement=document.forms['currency'].summ.value;
	if (TMPelement!='100 000') return;
	document.forms['currency'].summ.value="";}
);
$("#summ").blur(function (e)  {
	if (document.forms['currency'].summ.value=="") document.forms['currency'].summ.value=TMPelement;
});

$("#summ").keypress(function (e)  {
CursorPos=document.forms['currency'].summ.selectionStart;
cd=document.forms['currency'].summ.value;
cdK=cd.indexOf('.');
cdP=cd.indexOf(',');
komats=false;
if (cdK>-1) komats=true;
if (cdP>-1) komats=true;

if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) && e.which!=32 && e.which!=46 && e.which!=44)  {return false;}

if (e.which==8) {
	CursorPos--;
	if (CursorPos<0) CursorPos=0;
	SeT=true;
}

if (e.which==46 || e.which==44) {
 	if (cdK!=-1 || cdP!=-1) return false;
	komats=true; 	
 }
 
if (cd.length>=15 && komats==false) {
 	if (e.which!=8 && e.which!=0) return false; 
	}
if (cdK>=18) {
	if (e.which!=8 && e.which!=0) return false;
}
if (cdP>=18) {
	if (e.which!=8 && e.which!=0) return false;
}
});

 $("#summ").keyup(function (e)  {
 	document.forms['currency'].summ.value=FormatNumberSP2(document.forms['currency'].summ.value);
if (SeT) {
	document.forms['currency'].summ.setSelectionRange(CursorPos, CursorPos);
	SeT=false;
}
 	GetRateNew();
 });
});

function SetCurr1(rate){
//alert (rate.sell[0].name);
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;
//alert (cmode);
if (cmode=='csell') {
 	tmptext='You will need ';
 	tmprez=rate.buy[0].amount;
 	tmpcurr=rate.buy[0].name;
 	tmpcurrR=rate.buy[0].rate;
 	//alert (rate.sell[0].amount);
}
if (cmode=='cbuy') {
 	tmptext='You will get ';
 	tmprez=rate.sell[0].amount;
 	tmpcurr=rate.sell[0].name;
 	tmpcurrR=rate.sell[0].rate;
}

tmp=tmptext+FormatNumberSP(tmprez.toFixed(2))+' '+tmpcurr+' <span>Rate '+tmpcurrR+'</span>';
$('#currcalc1').html(tmp);

// fot 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('.');
}


var today=rigatime.split(":");
var h=today[0];
var m=today[1];
var s=today[2];
var rates;
var pulstime;

function populate(){
GetData();
startTime();
pulstime=setTimeout('GetPuls()',60000);
}

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);
    }
}

var secFlash=true;
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() {
//alert ('GetData');
$.ajax({
   type: "GET",
   url: "/eng.nsf/FormRBPulse?OpenAgent&conf=sng_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: "/eng.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;
	}
});
}
