function tbl2core (tblId) {
    if (tblId == 'TblCorePortf') {
        return(1);
    }
    if (tblId == 'TblTradingPortf') {
        return(0);
    }
    return(2);
}

function addItemPortfResp (req, tblId) {
    var resp = req.responseText.split(DELIMITER);
    
//    alert(resp[0]+' '+resp[1]+' '+resp[2]);
    if (resp[0] == "no") {
        alert(resp[1]);
       return;
    }

    var server_name = getServerName();
    if (tblId == 'TblTrackingPortf') {
        window.location = 'http://'+server_name+'/portf_track.php';
    } else {
        window.location = 'http://'+server_name+'/portf.php';
    }
    //displayInputPortf(tblId);
}

function addItemPortfReq (tblId) {
    var core, filter;
    var tbl = document.getElementById(tblId);
    var lastRow = tbl.rows.length -1;
    var url = 'portf_update_handler.php';
    
    var obj = document.getElementById(tblId+'symbol'+lastRow);
    if (obj.value.length < 1) {
        alert('Symbol is missing');
        obj.focus();
        return (false);
    }
    var data = 'symbol='+obj.value;

    obj = document.getElementById(tblId+'action'+lastRow);
    if (obj.value == 0) {
        alert('Action selection is missing');
        obj.focus();
        return (false);
    }
    data += '&action='+obj.value;

    core = tbl2core(tblId);
    if (core == 2) {
        obj = document.getElementById(tblId+'price'+lastRow);
        var pri = parseFloat(obj.value);
        if (pri <= 0) {
            alert('Price must be a positive number');
            obj.focus();
            return (false);
        }
        data += '&price='+pri;
    }

    obj = document.getElementById(tblId+'shares'+lastRow);
    filter = /^\d*$/;
    if (!filter.test(obj.value) || obj.value < 1) {
        alert('# of shares must be a positive number');
        obj.focus();
        return (false);
    }
    data += '&shares='+obj.value;
    data += '&note='+document.getElementById(tblId+'note'+lastRow).value;

    data += '&core='+core;
    
    xmlHttpReq(xmlHttpObj(), url, true, data, addItemPortfResp, tblId);    
}

function displayPerfNotAllowed (tblId, msg) {
    var tbl = document.getElementById(tblId);
    if (!tbl) {
        return;
    }
    var row = tbl.insertRow(tbl.rows.length);
    var td;
    
    td = row.insertCell(0);
    textNode = document.createTextNode(msg);
    td.style.width = '200px';
    td.appendChild(textNode);
    div.appendChild(tbl);
}

function displayPercentTbl (tbl, usr_id, summary, j) {
    var row = tbl.insertRow(tbl.rows.length);
    var cell = row.insertCell(0);
    var a = document.createElement('a');
    
    a.setAttribute('href', 'usr_portf.php?usrid='+usr_id);
    a.appendChild(document.createTextNode('Portfolio'));
    cell.className = 'centerBold';
    cell.style.width = '52px';
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
    cell.appendChild(a);

    cell = row.insertCell(1);
    cell.appendChild(document.createTextNode('by'));
    cell.style.width = '22px';
    cell.style.textAlign = 'center';
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
    
    a = document.createElement('a');
    a.setAttribute('href', 'usr_profile.php?usrid='+usr_id);
    if (summary) {
        if (usr_id.length > 14) {
            usr_id = usr_id.substring(0, 14) + '...';
        }
    }
    a.style.fontSize='14px';

    a.appendChild(document.createTextNode(usr_id));
    cell = row.insertCell(2);
    cell.style.textAlign='center';

    if (summary) {
        cell.style.width = '122px';
    } else {
        cell.style.width = '180px';
    }
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
    cell.appendChild(a);
}

function displayPercentInc (tbl, value, summary, j) {
    var row = tbl.rows[tbl.rows.length-1];
    var cell = row.insertCell(3);
    var color;
    
    // elem.style.fontSize=18;
    var img = document.createElement('img');

    cell.className = 'centerBold';
    cell.style.width= summary ? '22px': '44px';
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
    if (value < 0) {
        img.src = "images/downArrow.gif";
        img.alt = "+";
        color='red';
    } else {
        img.src = "images/upArrow.gif";
        img.alt = "-";
        color='green';
    }

    cell.appendChild(img);
    
    cell = row.insertCell(4);
    cell.style.color=color;
    cell.style.width= summary ? '46px': '70px';
    if (value<0) {
        value = -1 * value;
    }
    text_fmt = value+'%';
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
    cell.style.fontWeight='bold';

    if (!summary) {
        cell.style.fontSize = '16px';
    } else {
        cell.style.fontSize = '14px';
        cell.className='right';
    }

    cell.appendChild(document.createTextNode(text_fmt));    

    cell = row.insertCell(5);
    if (j%2 || !summary) {
        cell.style.background = '#f6f6f6';
    }
}

function displayPerf (tblId, owner_id, core) {
    var tbl = document.getElementById(tblId);
    if (!tbl) {
        return;
    }

    var row = tbl.insertRow(tbl.rows.length);
    var td;
    
    td = row.insertCell(0);
    td.style.width = '460px';
    var img = document.createElement('img');
    img.src = "perf_chart.php?owner="+owner_id+"&core="+core+"&num=60";
    
    a = document.createElement('a');
    a.setAttribute('href', "perf_chart.php?owner="+owner_id+"&core="+core);
    a.appendChild(img);
    
    td.appendChild(a);

    td = row.insertCell(1);
    var img2 = document.createElement('img');
    img2.src = "portf_pie_chart.php?owner="+owner_id+"&core="+core;
    td.appendChild(img2);
}

// flag: 0-update, 1-inc delete option, 2-no manage
function displayHoldingPortf (tblId, usrStkHnd, sym, action, price,
                              shares, atime, note, flag, eprice, etime) {
    var textNode;
    var tbl = document.getElementById(tblId);
    if (!tbl) {
        return;
    }
    var lastRow = tbl.rows.length;
    var row = tbl.insertRow(lastRow);
    var cell;
    var tracking=0;

    if (tblId == 'TblTrackingPortf') {
        tracking = 1;
    }
    
    // symbol cell
    cell = row.insertCell(0);
    textNode = document.createTextNode(sym);
    cell.appendChild(textNode);  

    // action cell
    cell = row.insertCell(1);

    var valstr, valstr2=0;
    if (action=='b') {
        valstr = 'Bought'
        valstr2 = 'Sell';
    } else if (action=='ss') {
        valstr = 'Short';
        valstr2 = 'Cover';
    } else {
        valstr = action;
    }

    // no select button if not manage
    if (flag==2) {
        valstr2 = 0;
    }
    
    if (valstr2) {
        var sel = document.createElement('select');
        sel.id = 'actionSel';
        sel.options[0] = new Option(valstr, '0');
        sel.options[1] = new Option(valstr2, 'c');
        sel.options[2] = new Option('add notes', 'm');
        if (flag==1) { // delete
            sel.options[3] = new Option('delete', 'd');
        }
        sel.style.fontSize = '9px';
        sel.style.width = '62px';
        sel.onchange = function() {
            var actionSel = document.getElementById('actionSel');
            if (sel.value == 'c') {
                isSameDaySaleReq(usrStkHnd, sym, tracking);
            } else if (sel.value == 'm') {
                var href = 'portf_notes.php?usrStkHnd='+usrStkHnd+'&symbol='+sym;
                document.location.href= href;                
            }  else if (sel.value == 'd') {
                delItemPortfReq(tblId, usrStkHnd);
            }
        }
        cell.appendChild(sel);
    } else {
        cell.appendChild(document.createTextNode(valstr));
    }

    // price cell
    cell = row.insertCell(2);
    textNode = document.createTextNode(price);
    cell.appendChild(textNode);

    // num shares
    cell = row.insertCell(3);
    textNode = document.createTextNode(shares);
    cell.appendChild(textNode);

    // date cell
    cell = row.insertCell(4);
    cell.style.fontSize = '9px';
    textNode = document.createTextNode(atime);
    cell.appendChild(textNode);

    // prev eod price or close price
    cell = row.insertCell(5);

    if (tracking && (eprice == 'enter')) {
        var elemPrice = document.createElement('input');
        elemPrice.className = 'small';
        elemPrice.type = 'text';
        elemPrice.id = 'price';
        elemPrice.name = 'price';
        elemPrice.size = 4;
        elemPrice.maxLength = 10;
        cell.appendChild(elemPrice);
    } else {
        textNode = document.createTextNode(eprice);
        cell.appendChild(textNode);
    }

    // prev eod time or close time
    cell = row.insertCell(6);
    cell.style.fontSize = '9px';
    textNode = document.createTextNode(etime);
    cell.appendChild(textNode);

    // % gain
    cell = row.insertCell(7);
    var per_gain, color='green';

    if (IsNumber(price) && IsNumber(eprice) && (price !=0)) {
        per_gain = (eprice-price)*100/price;
        if ((action=='ss') || (action=='Short')) {
            per_gain = -1*per_gain;
        }

        per_gain = formatFloat(per_gain);
        if (per_gain < 0) {
            color = 'red';
        }
        if (per_gain > 0) {
            per_gain = '+'+per_gain;
        }
    } else {
        per_gain = '*';
    }
    textNode = document.createTextNode(per_gain);
    cell.style.color=color;
    cell.style.fontWeight='bold';
    cell.appendChild(textNode);
    
    // note
    cell = row.insertCell(8);
    cell.className = 'left';
    cell.style.fontSize = '10px';
    cell.style.color = 'blue';
    var eachNote = note.split("^230^");    
    var i=0;
    while (eachNote[i]) {
        textNode = document.createTextNode(eachNote[i]);
        cell.appendChild(textNode);
        cell.appendChild(document.createElement('br'));
        i++;
    }

    return(10);
}

function displayInputPortf (tblId) {
    var tbl = document.getElementById(tblId);
    var lastRow = tbl.rows.length;
    var row = tbl.insertRow(lastRow);
    var td;
    
    // symbol cell	
    var cellSymbol = row.insertCell(0);
    var elem = document.createElement('input');
    elem.className = 'small';
    elem.type = 'text';
    elem.id = tblId+'symbol'+lastRow;
    elem.style.width = '40px';
    elem.maxLength = 10;
    cellSymbol.appendChild(elem);
  
    // action cell
    var cellAction = row.insertCell(1);
    cellAction.className = 'small';
    
    var sel = document.createElement('select');
    sel.id = tblId+'action'+lastRow;
    // option value="buy/sell/ss/c"
    sel.options[0] = new Option('Select', '0');
    sel.options[1] = new Option('Buy', 'b');
    sel.options[2] = new Option('Sell Short', 'ss');
    sel.style.fontSize = '9px';
    sel.style.width = '62px';
    cellAction.appendChild(sel);

    // price cell
    td = row.insertCell(2);
    var elemPrice;
    if (tblId == 'TblTrackingPortf') {
        elemPrice = document.createElement('input');
        elemPrice.className = 'small';
        elemPrice.type = 'text';
        elemPrice.id = tblId+'price'+lastRow;
        elemPrice.size = 4;
        elemPrice.maxLength = 10;
        td.appendChild(elemPrice);
    } else {
        elemPrice = document.createTextNode('*');
        td.appendChild(elemPrice);
    }
    
    // # of shares
    var cellAlloc = row.insertCell(3);
    var elemAlloc = document.createElement('input');
    elemAlloc.className = 'small';
    elemAlloc.type = 'text';
    elemAlloc.id = tblId+'shares'+lastRow;
    elemAlloc.style.width = '36px';
    elemAlloc.maxLength = 6;
    cellAlloc.appendChild(elemAlloc);

    // date cell
    var dateobj = new Date();

    var cellDate = row.insertCell(4);
    cellDate.style.fontSize = '9px';
    var elemMonth = document.createTextNode(month2str(dateobj.getMonth()+1)+' ');
    cellDate.appendChild(elemMonth);

    var elemDate = document.createTextNode(dateobj.getDate()+', ');
    cellDate.appendChild(elemDate);

    var elemYear = document.createTextNode(dateobj.getFullYear());
    cellDate.appendChild(elemYear);

    // price cell
    td = row.insertCell(5);
    td.appendChild(document.createTextNode('*'));
    
    // price cell
    td = row.insertCell(6);
    td.appendChild(document.createTextNode('*'));

    // price cell
    td = row.insertCell(7);
    td.appendChild(document.createTextNode('*'));
    
    // note
    var cellNotes = row.insertCell(8);
    var elemNotes = document.createElement('input');
    elemNotes.className = 'small';
    elemNotes.type = 'text';
    elemNotes.id = tblId+'note'+lastRow;
    elemNotes.style.width = '288px';
    elemNotes.maxLength = 80;
    cellNotes.appendChild(elemNotes);

}

function displaySince (div, period, since, core) {
    var period_arr_str = new Array ('1 DAY', '1 WEEK', '1 MONTH', '3 MONTH',
                                    '6 MONTH', '1 YEAR', '2 YEARS', '5 YEARS',
                                    'MAX');

    var tbl = document.createElement('table');
    tbl.className = 'tfmtFixedBig';

    var period_str = period_arr_str[period];
    var row = tbl.insertRow(tbl.rows.length);
    var td;
    
    td = row.insertCell(0);
    td.style.width='70px';
    var a = document.createElement('a');
    a.setAttribute('href', 'portfl.php?core='+core+'&period='+period);
    a.appendChild(document.createTextNode(period_str));
    a.style.color = 'black';
    td.style.background = "url('./images/beigeg.png')";
    td.style.fontWeight = 'bold';
    td.appendChild(a);

    td = row.insertCell(1);
    td.style.width='106px';
    textNode = document.createTextNode(' since'+since);
    //td.style.background = "url('./images/greeng.gif')";
    td.style.fontSize = '11px';
    td.appendChild(textNode);

    td = row.insertCell(2);
    //td.style.background = "url('./images/greeng.gif')";
    
    div.appendChild(tbl);
}

function delItemPortf(tblId)
{
    var tbl = document.getElementById(tblId);
    var numRows = tbl.rows.length;
    var i=1;

    while(i<=numRows) {
        if (tbl.rows(i) != undefined) {
            delChkBox = tbl.rows(i).cells(8).firstChild;
            if (delChkBox && delChkBox.checked) {
                //delItemPortfReq(tblId, tbl.rows(i).cells(0).firstChild);
                delItemPortfReq(tblId, delChkBox.id);
            } 
        }
        i++;
    }
}

function getPortfResp (req, params) {
    var resp = req.responseText.split(DELIMITER);
    var tblId = params[0];
    var manage = params[1];
    var cur_holding = params[2];
    var flag, i=2; // usr, since

    displayPortfTblHdr(tblId, cur_holding);
    // alert(resp[0]+' '+resp[1]+' '+resp[2]+' '+resp[6]);
    
    while(resp[i]){
        if (resp[i]=='PORTF') {
            i++;
            flag = manage ? resp[i+7] : 2;
            i += displayHoldingPortf(tblId, resp[i], resp[i+1], resp[i+2],
                                     resp[i+3], resp[i+4], resp[i+5],
                                     resp[i+6], flag, resp[i+8], resp[i+9]);
        } else if (resp[i]=='PERF') {
            i++;
            displayPerf(tblId+'Perf', resp[i], resp[i+1]);
            i += 2;
        } else if (resp[i]=='DENY') {
            displayPerfNotAllowed(tblId+'Perf', resp[i+1]);
            i += 2;            
        } else {
            i++;
        }
    }
    if (manage) {
        displayInputPortf(tblId);
    }
}

function getPortfRespForClose (req, tblId) {
    var resp = req.responseText.split(DELIMITER);
    var i=2, action, eprice;
    
    // alert(tblId+' '+resp[0]+' '+resp[1]);
    displayPortfTblHdr(tblId, 1);
    
    while(resp[i]){
        if (resp[i]=='PORTF') {
            i++;   
            action = resp[i+2];
            if (action == 'b') {
                action = 'sell';
            } else if (action == 'ss') {
                action = 'cover';
            }
            eprice = resp[i+8];
            if (eprice == '*') {
                eprice = 'enter';
            }
            i += displayHoldingPortf(tblId, resp[i], resp[i+1], action,
                                     resp[i+3], resp[i+4], resp[i+5],
                                     resp[i+6], 3, eprice, resp[i+9]);
        }
    }
}

function getPortfReq (tblId, ownerId, curHolding, manage) {
    var url = 'portf_list_handler.php';
    var data;

    data = 'owner='+ownerId+'&cur_holdings='+curHolding;
    data += '&core='+tbl2core(tblId);

    var p_arr = new Array(tblId, manage, curHolding);
    xmlHttpReq(xmlHttpObj(), url, true, data, getPortfResp, p_arr);
}

function isSameDaySaleResp (req, params) {
    var resp = req.responseText.split(DELIMITER);

    // alert(resp[0]);
    if (resp[0]=='same') {
        alert('same day sale is not allowed');
        return;
    }

    var usrStkHnd = params[0];
    var sym = params[1];
    var tracking = params[2];
    var href = 'portf_close.php?usrStkHnd='+usrStkHnd+'&symbol='+sym+'&tracking='+tracking;
    document.location.href= href;
}

function isSameDaySaleReq (usrStkHnd, sym, tracking) {
    var url = 'portf_list_handler.php';
    var data= 'usrStkHnd='+usrStkHnd+'&sameDaySale=1';
    var p_arr = new Array(usrStkHnd, sym, tracking);
    
    xmlHttpReq(xmlHttpObj(), url, true, data, isSameDaySaleResp, p_arr);
}

// user is trying to close position
function getPortfReqForClose (tblId, usrStkHnd) {
    var url = 'portf_list_handler.php';
    var data= 'usrStkHnd='+usrStkHnd;
    
    xmlHttpReq(xmlHttpObj(), url, true, data, getPortfRespForClose, tblId);
}

function delItemPortfResp (req, tblId) {
    var resp = req.responseText.split(DELIMITER);

    if (resp[0] == "no") {
        alert(resp[1]);
    }

    var server_name = getServerName();
    if (tblId == 'TblTrackingPortf') {
        window.location = 'http://'+server_name+'/portf_track.php';
    } else {
        window.location = 'http://'+server_name+'/portf.php';
    }
}

// request delete portf item to web server
function delItemPortfReq (tblId, usrStkHnd) {
    var url = 'portf_update_handler.php';
    var data = 'action=d&usrStkHnd='+usrStkHnd;
    
    xmlHttpReq(xmlHttpObj(), url, true, data, delItemPortfResp, tblId);
}

function hiveListForUsrResp (req, tblId) {
    var tbl = document.getElementById(tblId+'HL');
    var resp = req.responseText.split(DELIMITER);
    var rowLen = tbl.rows.length;
    var i=2;

    if (rowLen > 0) {
        tbl.deleteRow(rowLen-1);
    }

    if (resp[i]) {
        var row = tbl.insertRow(0);
        var cell = row.insertCell(0);
        var textNode = document.createTextNode("Hives to share: ");
        cell.appendChild(textNode);
    }

    while (resp[i]) {
        //alert(resp[i]);
        displayHivesFor(tblId, row, cell, resp[i++], true);
    }
}

function displayHivesFor (tblId, row, cell, hive_name, chked) {
    var elem = document.createElement('input');
    
    elem.type = 'checkbox';
    elem.name = tblId+hive_name;
//    elem.id = tblId+hive_name;
    elem.value = hive_name;

    elem.defaultChecked = chked;
    cell.appendChild(elem);
    cell.appendChild(document.createTextNode(hive_name+'  '));
}

function sharingPortfPolicyChange (elemSel, tblId) {
    var url = 'hivel_handler.php';
    var data = '?hivel_only=1&sess_usr=1';

    if (elemSel.value == '2') {
        xmlHttpReq(xmlHttpObj(), url+data, true, null, hiveListForUsrResp,
                   tblId);
    } else {
        var i=0, tbl = document.getElementById(tblId+'HL');
        // alert(tbl.rows.length);
        while(i<tbl.rows.length) {
            tbl.deleteRow(i++);
        }
    }
}

function portSharingResp (req, tblId) {
    var i=1, tbl, row, cell, textNode, share_w_world;
    var resp = req.responseText.split(DELIMITER);

    //alert(resp[0]+' '+resp[1]+' '+resp[2]+' '+resp[3]+' '+resp[4]);
    share_w_world = resp[0];
    displayPortfSharing(tblId, share_w_world);

    tbl = document.getElementById(tblId+'HL');
    if (!tbl) {
        return;
    }
    
    while (resp[i]) {
        if (resp[i] == 'HL') {
            if (resp[i+1] && resp[i+1] != 'HL') {
                row = tbl.insertRow(tbl.rows.length);
                cell = row.insertCell(0);
                if (share_w_world == 2) {
                    textNode = document.createTextNode("Hives to share: ");
                    cell.appendChild(textNode);
                }
            }
            i++;
        } else {
            displayHivesFor(tblId, row, cell, resp[i], resp[i+1]==1);
            i += 2;
        }
    }
}

function displayPortfSharing (selName, selIndex) {
    var sel = document.getElementById(selName);
    if (!sel) {
        return;
    }
    sel.options[0] = new Option('Share With', '0');
    sel.options[1] = new Option('Nobody', '1');
    sel.options[2] = new Option('Hive Members', '2');
    sel.options[3] = new Option('Everyone', '3');
    sel.selectedIndex = selIndex;
    sel.style.fontSize = "smaller";
}

function getPortfSharingReq (usrHnd, tblId) {
    var url = 'portf_share_handler.php?share='+tblId;
    xmlHttpReq(xmlHttpObj(), url, true, null, portSharingResp, tblId);
}

function displayPortfTblHdr (tblId, cur_holding) {
    var tbl = document.getElementById(tblId);
    if (!tbl) {
        return;
    }
    
    var row = tbl.insertRow(tbl.rows.length);
    var cell = row.insertCell(0);
    cell.className = 'centerBold';
    cell.style.width = '48px';
    cell.appendChild(document.createTextNode('Symbol'));

    cell = row.insertCell(1);
    cell.className = 'centerBold';
    cell.style.width = '58px';
    cell.appendChild(document.createTextNode('Action'));

    cell = row.insertCell(2);
    cell.className = 'centerBold';
    cell.style.width = '46px';
    cell.appendChild(document.createTextNode('Begin Price'));

    cell = row.insertCell(3);
    cell.className = 'centerBold';
    cell.style.width = '38px';
    cell.appendChild(document.createTextNode('# of Shares'));

    cell = row.insertCell(4);
    cell.className = 'centerBold';
    cell.style.width = '66px';
    cell.appendChild(document.createTextNode('Action Date'));

    var i=5;
    cell = row.insertCell(i++);
    cell.className = 'centerBold';
    cell.style.width = '46px';

    var endText;
    if (cur_holding == 0) {
        endText = 'End Price';
    } else {
        endText = 'Prev Close Price';
    }
    cell.appendChild(document.createTextNode(endText));

    cell = row.insertCell(i++);
    cell.className = 'centerBold';
    cell.style.width = '66px';
    if (cur_holding == 0) {
        endText = 'Close Date';
    } else {
        endText = 'Prev Close Date';
    }
    cell.appendChild(document.createTextNode(endText));
    

    cell = row.insertCell(i++);
    cell.className = 'centerBold';
    cell.style.width = '40px';
    cell.appendChild(document.createTextNode('% Gain'));

    cell = row.insertCell(i++);
    cell.className = 'centerBold';
    cell.style.width = '300px';
    cell.appendChild(document.createTextNode('Notes'));

}

function getPortfTopResp (req, p_arr) {
    var divId = p_arr[0], period_idx=p_arr[1], summary=p_arr[2], core=p_arr[3];
    var perc_tbl, tbl, tbl_id, a, h1, i=0, j=0, usr_id;
    var div = document.getElementById(divId);
    var resp = req.responseText.split(DELIMITER);

    // alert(resp[0]+' '+resp[1]+' '+resp[2]+' '+resp[3]);
    while(resp[i]){
        if (resp[i] == 'USRID') {
            j++;
            tbl_id = divId+'resp[i+1]'+j;

            perc_tbl = document.createElement('table');
            perc_tbl.className = 'tfmtFixed';

            usr_id = resp[i+1];
            displayPercentTbl(perc_tbl, usr_id, summary, j);
            div.appendChild(perc_tbl);

            if (!summary) {
                tbl = document.createElement('table');
                tbl.id = tbl_id;
                tbl.className ='tfmtInner';
                div.appendChild(tbl);
                displayPortfTblHdr(tbl_id, 1);

                var perf_tbl = document.createElement('table');
                perf_tbl.id = 'Perf'+divId+j;
                div.appendChild(perf_tbl);
                
                var deny_tbl = document.createElement('table');
                deny_tbl.id = 'Deny'+divId+j;

            }
            i += 2;
          
            while(resp[i] && (resp[i] != 'USRID')) {
                if (resp[i]=='PORTF') {
                    flag = 3;
                    i++;
                    action = resp[i+2];
                    if (action=='b') {
                        action = 'Bought';
                    } else if (action=='ss') {
                        action = 'Short';
                    } 
                    i += displayHoldingPortf(tbl_id, resp[i], resp[i+1],
                                             action, resp[i+3], resp[i+4],
                                             resp[i+5], resp[i+6],flag,
                                             resp[i+8], resp[i+9]);
                } else if (resp[i]=='PERF') {
                    i++;
                    displayPerf(perf_tbl.id, usr_id, resp[i+1]);
                    i += 2;
                } else if (resp[i]=='DENY') {
                    displayPerfNotAllowed(deny_tbl.id, resp[i+1]);
                    i += 2;
                } else if (resp[i]=='PERCENT') {
                    i++;
                    displayPercentInc(perc_tbl, resp[i], summary, j);
                } else {
                    i++;
                }
            } 
        } else if (resp[i] == 'SINCE') {
            displaySince(div, resp[i+1], resp[i+2], core);
            i+=3;
        } else {
            i++;
        }

        if (!summary) {
            div.appendChild(document.createElement('hr'));
            div.appendChild(document.createElement('br'));
        }
    }
}

function getPortfTopReq (divId, period, summary, core) {
    var url = 'portf_top_handler.php';
    var p_arr = new Array(divId, period, summary, core);
    var data;
    data = 'period='+period+'&summary='+summary+'&core='+core;

    xmlHttpReq(xmlHttpObj(), url, true, data, getPortfTopResp, p_arr);
}