var justchanged=false;
var justchangedportfolio=false;
var changingportfolionow=false;
var bottomportfolioview=0;//0=front, 1=edit
var firstbottomportfoliorun=1;
var haspopped=false;
var haspoppedout=false;
var updateid=0;
var lastupdateid=0;
var bottomrealajax;
function bottomcancelajax() {
bottomrealajax.abort();
updatebottomportfolio();
}
//do the ajax call to update the bottomportfolio
function updatebottomportfolio() {
updateid++;
var bottomtimeoutt=[];
var randomnumber=Math.floor(Math.random()*100001);
if((!changingportfolionow || justchangedportfolio) && bottomportfolioview==0 && ($("#bottomportfoliobox").is(':visible') || firstbottomportfoliorun)) {//do not attempt to update data if a portfolio change is loading, unless triggered by portfolio change. almost, add or remove must not be in progress. only continue if box is open
bottomtimeoutt[randomnumber] = setTimeout("bottomcancelajax();",20000);
bottomrealajax = $.ajax({
type: "POST",
cache:false,
url:"http://www.proinvestor.com/lib/calls/bottom/bottomportfolio.php",
data: ({
updateid:updateid,
activebottomportfolio:$("#bottomportfolioselect").val(),
firstrun:firstbottomportfoliorun
}),
success: function(data){
clearTimeout(bottomtimeoutt[randomnumber]);//cancel the timeout
//check if data is valid - catch the error to avoid javascript error and still do timeout loop
try {
var portfoliodata = JSON.parse(data);
if(Number(portfoliodata["updateid"])>lastupdateid) {
updatebottomportfoliodata(portfoliodata);
lastupdateid=portfoliodata["updateid"];
changingportfolionow=false;
firstbottomportfoliorun=0;
} else {
//console.log("too late. updateid returned "+portfoliodata["updateid"]+" and last is inserted is "+lastupdateid);
}
} catch(err) {
}
if(!justchangedportfolio) {
t=setTimeout("updatebottomportfolio()",10000);//only set timeout if coming from timeout
} else {
//console.log("is not updating next time. justchangedportfolio="+justchangedportfolio);
justchangedportfolio=false;//the portfolio change progress started
}
}
});
}
}
//receive data for bottom portfolio and process
function updatebottomportfoliodata(portfoliodata) {
if(portfoliodata["html"]) {
$("#bottomportfoliohtml").html(portfoliodata["html"]);//insert html if it's set
initializestockpicker();
} else {
//console.log("No html data here: '"+portfoliodata["html"]+"'");
//console.log(portfoliodata);
}
if(portfoliodata) {
//console.log(portfoliodata);
//$("#bottomportfolioshowbutton").hide();
//$("#bottomportfolioeditbutton").show();
//if(portfoliodata["error"]) {//replaced by new error system
// alert(portfoliodata["error"]);
//}
//onfocus="this.hasFocus=true" onblur="this.hasFocus=false"
//console.log(document.getElementById("bottomtrade").hasFocus);
//insert stockdata
if(portfoliodata["stockdata"]) {
var stockid=0;
for (var i in portfoliodata["stockdata"]) {
stockid=portfoliodata["stockdata"][i]["stockid"];
$("#bbid"+stockid).html(portfoliodata["stockdata"][i]["Ask"]);
$("#bask"+stockid).html(portfoliodata["stockdata"][i]["Bid"]);
$("#blast"+stockid).html(portfoliodata["stockdata"][i]["Last"]);
$("#bchange"+stockid).html(portfoliodata["stockdata"][i]["PercentChangeFromPrevious"]);
//$("#bticker"+stockid).html(portfoliodata["stockdata"][i]["Symbol"]);
$("#bnotification"+stockid).html(portfoliodata["stockdata"][i]["notification"]);
}
}
if(portfoliodata["errors"]) {
$("#portfoliobox_errordiv").html(portfoliodata["errors"]);
$("#portfoliobox_errordiv").show()
} else {
$("#portfoliobox_errordiv").hide();
}
if(portfoliodata["notifications"]) {
//console.log(portfoliodata["notifications"]);
//show number of new news in notification container, or hide notification container
if(Number(portfoliodata["notifications"])>0 && portfoliodata["notifications"]!="") {
$("#bottomportfolionotifications").show();
$("#bottomportfolionotifications").html(portfoliodata["notifications"]);
} else {
$("#bottomportfolionotifications").hide();
}
} else {
$("#bottomportfolionotifications").hide();
}
//switch to right view
if(bottomportfolioview==1) {//coming from edit, so go to edit
$("#editbottomportfolio").show();
$("#showbottomportfolio").hide();
$("#bottomportfolioshowbutton").show();
$("#bottomportfolioeditbutton").hide();
bottomportfolioview=0;
}
}
}
//quick remove stock from portfolio
function bottomremovestock(portfoliostockid) {
updateid=updateid+3;
bottomportfolioview=1;
$("#bottomportfoliohtml").html('
');
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomportfolio.php", {
activebottomportfolio:$("#bottomportfolioselect").val(),
removestock:portfoliostockid,
updateid:updateid
},
function(data){
try {
var portfoliodata = JSON.parse(data);
updatebottomportfoliodata(portfoliodata);
lastupdateid=portfoliodata["updateid"];
} catch(err) {
}
},"html");
}
//show or hide transaction data box
function toggle_addtransactiondata() {
$("#transactiondata").toggle();
}
//execute the trade
function bottom_exec_trade() {
updateid=updateid+3;//allow for 3 more updates before timeout
bottomportfolioview=1;
var tag=document.getElementById("bottomtrade").value;
var shares=document.getElementById("bottomshares").value;
var tradetype=document.getElementById("bottomtradetype").value;
if(document.getElementById("bottomtradeyear")) { //historical portfolio
var tradetime=$("#bottomtradeyear").val()+"-"+$("#bottomtrademonth").val()+"-"+$("#bottomtradedate").val()+" "+$("#bottomtradetime").val();
var tradecurrency=$("#bottomtradingcurrency").val();
var tradeprice=$("#bottompricepaid").val();
var tradingfees=$("#bottomtradingfees").val();
} else {
var tradetime=-1;
var tradecurrency=-1;
var tradeprice=-1;
var tradingfees=-1;
}
if(tag!="" && shares!="" && tradetime!="" && tradecurrency!="" && tradingfees!="") {
$("#bottomportfoliohtml").html('
');
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomportfolio.php", {
activebottomportfolio:$("#bottomportfolioselect").val(),
updateid:updateid,
shares:shares,
tag:tag,
tradetype:tradetype,
tradetime:tradetime,
tradecurrency:tradecurrency,
tradeprice:tradeprice,
tradingfees:tradingfees
},
function(data){
try {
var portfoliodata = JSON.parse(data);
updatebottomportfoliodata(portfoliodata);
lastupdateid=portfoliodata["updateid"];
} catch(err) {
}
},"html");
} else {
alert("Udfyld venligst alle de påkrævede felter for at handle!");
}
}
//clicked on sell or short-buy stock - expand the container and display default values
function newbottomtrade(tradetype,ticker,amount,price) {
$("#bottomtrade").val(ticker);
document.getElementById("tradetype"+tradetype).selected="selected";
$("#transactiondata").show()
$("#shares").val(amount);
$("#pricepaid").val(price);
}
//quick add stock to portfolio
function bottomaddstock() {
updateid=updateid+3;
bottomportfolioview=1;
var ticker=document.getElementById("bottomtrade").value;
if(ticker!="" && ticker!=" ") {
$("#bottomportfoliohtml").html('
');
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomportfolio.php", {
activebottomportfolio:$("#bottomportfolioselect").val(),
newstock:ticker,
updateid:updateid
},
function(data){
try {
var portfoliodata = JSON.parse(data);
updatebottomportfoliodata(portfoliodata);
lastupdateid=portfoliodata["updateid"];
} catch(err) {
}
},"html");
} else {
alert("Skriv venligst et symbol på en aktie, som du vil tilføje!");
}
}
//change the active bottomportfolio - call onchange event for select box
function changebottomportfolio() {
justchangedportfolio=true;
changingportfolionow=true;
$("#bottomportfoliohtml").html('
');
updatebottomportfolio();
$("#bottomportfolioshowbutton").hide();
$("#bottomportfolioeditbutton").show();
}
function formatBottomItem(row) {
return row[0] + " (" + row[1] + ")";
}
function formatBottomResult(row) {
var mysplit=row[1].split(",");
return mysplit[0];
}
function initializestockpicker() {
$(".bottomtrade").autocomplete('http://www.proinvestor.com/lib/calls/stockssuggest.php', {
width: 220,
matchContains: true,
formatItem: formatBottomItem,
multiple: false,
formatResult: formatBottomResult
});
}
//edit or show button clicked, so toggle the view
function edittrigger() {
$("#editbottomportfolio").toggle();
$("#showbottomportfolio").toggle();
$("#bottomportfolioshowbutton").toggle();
$("#bottomportfolioeditbutton").toggle();
}
//****************************** Bottom News functions *********************************//
function popbottomnewsdiv(){
togglebottomnewsdiv();
bottomportnewsdiv = window.open("http://www.proinvestor.com/subs/popups/newspopup.php",'bottomportfoliopopup','toolbar=no,scrollbars=no,location=no,statusbar=no,resizable=0,width=300,height=400');
}
var whichload = "all";
var t_2 = null;
function bottomnews_load(){
t_1 = setTimeout("bottomnews_load()",20000);
eval("bottomnews_"+whichload+"();");
}
function bottomnews_all(){
whichload = "all";
$("#btm_all").addClass("bottombuttonactive");
$("#btm_your").removeClass("bottombuttonactive");
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomnews.php", {
all:'all'
},
function(data){
//alert(data);
$('#bottomnewshtml').html(data);
},"html");
}
function bottomnews_port(){
whichload = "port";
$("#btm_your").addClass("bottombuttonactive");
$("#btm_all").removeClass("bottombuttonactive");
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomnews.php", {
all:'portfolio'
},
function(data){
//alert(data);
$('#bottomnewshtml').html(data);
},"html");
}
var popup_port = false;
var popup_news = false;
var popup_2 = null;
//var allSet = true;
function popupnews_all(){
popup_port = false;
// allSet = true;
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomnews.php", {
all:'all',
from:'popup',
time:''
},
function(data){// alert(data);
$('#bottomnewshtml').html(data);
},"html");
if(!popup_port && popup_news == true){
if(popup_2) clearTimeout(popup_2);
popup_1 = setTimeout("popupnews_all()",20000);
}
}
//popup_1 = setTimeout("popupnews_all()",20000);
function popupnews_port(){
popup_port = true;
// allSet = false;
$.post("http://www.proinvestor.com/lib/calls/bottom/bottomnews.php", {
all:'portfolio',
from:'popup',
time:''
},
function(data){// alert(data);
$('#bottomnewshtml').html(data);
},"html");
if(popup_port){
if(popup_1) clearTimeout(popup_1);
popup_2=setTimeout("popupnews_port()",20000);
}
}