/*
* @project: TERA
* @version: Development (beta)
* @license: MIT (not for evil)
* @copyright: Yuriy Ivanov (Vtools) 2017-2019 [progr76@gmail.com]
* Web: https://terafoundation.org
* Twitter: https://twitter.com/terafoundation
* Telegram: https://web.telegram.org/#/im?p=@terafoundation
*/
var PayList = [];
var AttachItem;
var MapAccounts = {};
var LoadMapAfter = {};
var MapCheckTransaction = {};
var CanSendTransaction = 1;
var CurrentTR = {};
var MaxBlockNum = 0;
var WasAccountsDataStr;
function SetAccountsData(Data,AccountsDataStr)
{
if(!Data || !Data.result)
return ;
if($("idBtRun"))
$("idBtRun").style.display = (Data.arr.length ? '' : 'none');
if(AccountsDataStr === WasAccountsDataStr)
return ;
WasAccountsDataStr = AccountsDataStr;
var arr = Data.arr;
var Select = $("idAccount");
if(arr.length !== Select.options.length)
{
var options = Select.options;
options.length = arr.length;
}
MaxBlockNum = GetCurrentBlockNumByTime();
SetGridData(arr, "grid_accounts", "idMyTotalSum");
for(var i = 0; arr && i < arr.length; i++)
{
var Item = arr[i];
Item.MyAccount = true;
var Num = ParseNum(Item.Num);
if(!MapAccounts[Num])
MapAccounts[Num] = {};
CopyObjKeys(MapAccounts[Num], Item);
var option = Select.options[i];
var StrText = GetAccountText(Item, Num, 1);
if(option.text !== StrText)
CheckNameAccTo();
option.value = Num;
option.text = StrText;
}
var CurentValue = LoadMapAfter["idAccount"];
if(CurentValue)
{
Select.value = CurentValue;
delete LoadMapAfter["idAccount"];
}
SetCurCurencyName();
};
function CurTransactionToForm(bForce)
{
var Item = $("idTransaction");
if(Item.className === "" || bForce)
Item.value = GetJSONFromTransaction(CurrentTR);
};
function CheckNameAccTo()
{
MaxBlockNum = GetCurrentBlockNumByTime();
var ToID = ParseNum($("idTo").value);
if(!MapAccounts[ToID] || (MapAccounts[ToID].MustUpdate && MapAccounts[ToID].MustUpdate >= MaxBlockNum))
{
GetData("GetAccountList", {StartNum:ToID}, function (Data)
{
if(Data && Data.result === 1 && Data.arr.length)
{
var Item = Data.arr[0];
Item.UpdateData = Date.now();
MapAccounts[Item.Num] = Item;
SetNameAccTo();
}
});
}
SetNameAccTo();
};
function SetNameAccTo()
{
var Str = "";
var ToID = ParseNum($("idTo").value);
var element = $("idNameTo");
var Item = MapAccounts[ToID];
var StrTo = "To: " + GetAccountText(Item, ToID, 1);
if(element.innerText !== StrTo)
{
element.innerText = StrTo;
if(Item && Item.MyAccount)
element.className = "smallbold";
else
element.className = "";
}
};
function GetAccountText(Item,Num,bGetSum)
{
if(Item)
{
var text = Item.Name;
if(!text || text.length === 0)
text = Num;
else
text = "" + Num + ". " + text;
if(bGetSum)
{
var StrSum = SUM_TO_STRING(Item.Value, Item.Currency);
text += " (" + StrSum + ")";
}
return text;
}
else
{
return Num;
}
};
function OnEditIdTo()
{
CheckNameAccTo();
OnEditTransactionFields();
};
function OnEditTransactionFields()
{
if(IsVisibleBlock("edit_transaction"))
CreateTransaction();
SetCurCurencyName();
SaveValues();
};
function SetCurCurencyName()
{
var Num = ParseNum($("idAccount").value);
var Item = MapAccounts[Num];
if(Item)
{
$("idCoinName").innerText = CurrencyName(Item.Currency);
}
};
function CreateTransaction(F,CheckErr,Run)
{
CheckNameAccTo();
CheckSending();
var FromID = ParseNum($("idAccount").value);
if(CheckErr && FromID === 0)
{
SetError("Select valid 'From account'");
return ;
}
var StrTo = $("idTo").value.trim();
var bFindAcc = 0;
var ToPubKey = "";
var ToID = ParseNum(StrTo);
if(StrTo !== "" + ToID)
{
if(StrTo.length === 66 && (StrTo.substr(0, 2) === "02" || StrTo.substr(0, 2) === "03") && IsHexStr(StrTo))
{
ToID = 0;
ToPubKey = StrTo;
if(ToPubKey === PubKeyStr)
bFindAcc = 1;
}
else
{
if(CheckErr)
SetError("Valid 'Pay to' - required!");
return ;
}
}
if(CheckErr && ToID <= 0 && ToPubKey === "" && !AttachItem)
{
SetError("Valid 'Pay to' - required!");
return ;
}
var Description = $("idDescription").value.substr(0, 200);
var StrSum = $("idSumSend").value;
var indDot = StrSum.indexOf(".");
if(indDot >= 0)
{
var StrTER = StrSum.substr(0, indDot);
var StrCENT = StrSum.substr(indDot + 1);
}
else
{
var StrTER = StrSum;
var StrCENT = "0";
}
StrCENT = StrCENT + "000000000";
var Coin = {SumCOIN:ParseNum(StrTER), SumCENT:ParseNum(StrCENT.substr(0, 9))};
var OperationID = 0;
var Item = MapAccounts[FromID];
if(Item)
{
OperationID = Item.Value.OperationID;
}
var AttachBody = [];
if(AttachItem)
{
AttachBody = AttachItem.Data.Body;
if(!AttachBody)
AttachBody = [];
}
var ToPubKeyArr = [];
if(ToPubKey)
ToPubKeyArr = GetArrFromHex(ToPubKey);
var TR = {Type:111, Version:3, Reserve:0, FromID:FromID, OperationID:OperationID, To:[{PubKey:ToPubKeyArr, ID:ToID, SumCOIN:Coin.SumCOIN,
SumCENT:Coin.SumCENT}], Description:Description, Body:AttachBody, Sign:CurrentTR.Sign, };
Object.defineProperties(TR, {bFindAcc:{configurable:true, writable:true, enumerable:false, value:bFindAcc}});
Object.defineProperties(TR, {Run:{configurable:true, writable:true, enumerable:false, value:Run}});
if(JSON.stringify(TR) === JSON.stringify(CurrentTR))
{
if(F)
F(CurrentTR);
return ;
}
CurrentTR = TR;
GetSignTransaction(TR, "", function (TR)
{
CurTransactionToForm(true);
if(F)
F(TR);
});
};
function SignJSON(F)
{
if($("idSignJSON").disabled)
return ;
var TR = GetTransactionFromJSON();
if(!TR)
return ;
CurrentTR = TR;
GetSignTransaction(TR, "", function (TR)
{
CurTransactionToForm(true);
if(F)
F();
});
};
function CheckSending(bToStatus)
{
MaxBlockNum = GetCurrentBlockNumByTime();
var CanSend = IsPrivateMode();
var StrButton = "Send";
var StrButtonSign = "Sign JSON";
if(!CanSend)
{
StrButton = " ";
StrButtonSign = " ";
}
if(CanSend)
{
var FromID = ParseNum($("idAccount").value);
var Item = MapAccounts[FromID];
if(Item && Item.NextSendTime && Item.NextSendTime > MaxBlockNum)
{
if(bToStatus)
SetStatus("Transaction was sending. Wait... (" + Item.LastTransactionText + ")");
CanSend = false;
StrButton = "Wait...";
}
}
$("idSendButton").disabled = (!CanSend);
$("idSendButton").value = StrButton;
$("idSignJSON").disabled = (!CanSend);
$("idSignJSON").value = StrButtonSign;
return CanSend;
};
function AddWhiteList()
{
var ToID = ParseNum($("idTo").value);
if(ToID && $("idWhiteOnSend").checked)
localStorage["White:" + ToID] = 1;
};
function SendMoneyBefore()
{
if($("idSendButton").disabled)
return ;
var ToID = ParseNum($("idTo").value);
var Item = MapAccounts[ToID];
if(localStorage["White:" + ToID] || !$("idSumSend").value || Item && Item.MyAccount)
{
SendMoney();
}
else
{
var SumSend = parseFloat($("idSumSend").value).toLocaleString();
var StrTo = " to " + GetAccountText(Item, ToID);
$("idWhiteOnSend").checked = 0;
$("idOnSendText").innerHTML = "" + SumSend + " " + $("idCoinName").innerText + StrTo;
if(SumSend >= 100000)
$("idOnSendText").innerHTML += "