﻿// JScript File
var InMulty               =  0 ; 
var Profit                =  0 ; 
var GridPortfolioRefresh  =  1 ;
var prmGridProtfolioIndex =  0 ; 
var CheckedPort           =  -1;
var prmIndex              =  0 ; 
var prmGetProfitMulty     =  false ; 
var prmindexMulty         =  0 ;   
var prmProfit             =  0 ;
var CurrentPortfolioAccount = 0;

function UpdateProfit(ret)
{
  var item = GridPortfolio.Table.GetRow(prmIndex);
      item.SetValue(15, ret , false);
}

function LoadPortfolioDataAfterMulty(ret)
{
    if (ret != "Error" )
    {
        var loadXML = eval(ret)
            GridPortfolio.load(loadXML);
            GridPortfolio.render();
            document.getElementById("Portfolio1_Leverage").innerHTML = CalcLeverage();
            document.getElementById('imgPortfolioGetdata').style.visibility = 'hidden';
    }
   
    document.body.style.cursor="default";
}

function SubmitMultyPoprtfolio(ret)
{
    visibaleAjaxPortfolio(1);      
    var prmAccountID    = CurrentPortfolioAccount;
    var prmRandom       = Math.floor( Math.random()*10000000);
    var URL             = "ServerAjax/ProtfolioAjaxGetData.aspx?prmRandom="+prmRandom+"&FromSession=1&PortfolioType=1&AccountID="+prmAccountID  ;
    repSubmit( URL ,"LoadPortfolioDataAfterMulty"); 
}




function SetDeleteMM (DataItem)
{
  visibaleAjaxPortfolio(1);
  var MMID   = GridPortfolio.get_table().getRow(DataItem).getMember('MMID').get_text();
  var InstrumentID = GridPortfolio.get_table().getRow(DataItem).getMember('InstrumentID').get_text();
  var SystemName = GridPortfolio.get_table().getRow(DataItem).getMember('SystemName').get_text();


  IsThereOpenPosition(InstrumentID, SystemName, CurrentPortfolioAccount);
  



  var prmRandom = Math.floor( Math.random()*10000000);  
  var URL       = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=1&prmRandom="+prmRandom+"&Delete=1&MMID=" + MMID ;
      repSubmit( URL ,"SubmitPoprtfolio");  
      GridPortfolio.deleteItem(GridPortfolio.getItemFromClientId(DataItem));
 }

function SetMultyDeleteMM ()
{
            var prmRandom  =  0;
            var URL        =  "";
            var MMID    =  "" ; 
            var In         =  false;
            for(itemIndex  = 0 ; itemIndex <   GridPortfolio.get_table().getRowCount(); itemIndex++ ) 
            {
                if(GridPortfolio.get_table().getRow(itemIndex).getMember(0).get_value())
                {
                    visibaleAjaxPortfolio(1);
                    In        =  true;
                    MMID     = GridPortfolio.get_table().getRow(itemIndex).getMember('MMID').get_text();
                    prmRandom   = Math.floor( Math.random()*10000000);  
                    URL         = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=1&prmRandom="+prmRandom+"&Delete=1&MMID=" + MMID ;
                    repSubmit( URL ,"SubmitPoprtfolio");  
                    GridPortfolio.deleteItem(GridPortfolio.getItemFromClientId(itemIndex.toString()));
                    --itemIndex ;
                }
            }
            
            if (!In)
            {
                alert(ErrStrings[77]);
                return false;   
            }
}

function GetinnerHtmlPortfolio(index)
{ 
index=index-2;
index                       = index-(eval(GridPosition.get_currentPageIndex()) *  eval(GridPosition.get_pageSize()));
var prmAccountID            = CurrentPortfolioAccount;
var prmAccountBalance       = GetAccountBalance(prmAccountID);


var mode                    = GridPortfolio.get_table().getRow(index).getMember('ModeTypeName').get_text();
var JsK                     = GridPortfolio.get_table().getRow(index).getMember('FixedLots').get_text();
var JsKFRBase               = GridPortfolio.get_table().getRow(index).getMember('Ratio_Base').get_text();
var JsPAcountBalance        = GridPortfolio.get_table().getRow(index).getMember('Frac_Percent').get_text();
var JsEditPortfoiloPips     = GridPortfolio.get_table().getRow(index).getMember('Frac_SL').get_text();
var JsPEAcountBalance       = prmAccountBalance;
var RatioDelta              = GridPortfolio.get_table().getRow(index).getMember('Ratio_Delta').get_text();
var IsReverseChecked        = GridPortfolio.get_table().getRow(index).getMember('AttributeID').get_text() == "1" ? "checked" : "" ;
var Profit  = 0 ; 
var fixL= false ;
var fixR= false ;
var fixF= false ;
if (mode == 'Fixed Lot')
    {
        fixL =true;
    }
if (mode == 'Fixed Ratio')
    {
        fixR=true;
    }

if (mode == 'Fixed Fractional')
    {
        fixF=true;
    }

var HTMLRadioFL = "";
var HTMLRadioFF = "";
var HTMLRadioFR = "";
 
    if (fixL)
      {  
          HTMLRadioFL = "<input onclick='FractionalRadio()'  value='FixLots'    checked='true'  name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFF = "<input onclick='FractionalRadio()'  value='FixFrac'    name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFR = "<input onclick='FractionalRadio()'  value='FixRatio'   name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
      }
   else if (fixR)   
      {          
          HTMLRadioFL = "<input onclick='FractionalRadio()' value='FixLots'    name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFF = "<input onclick='FractionalRadio()' value='FixFrac'    name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFR = "<input onclick='FractionalRadio()' value='FixRatio'   checked='true'   name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
      }
   else
        {   
          HTMLRadioFL = "<input onclick='FractionalRadio()'     value='FixLots'    name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFF = "<input onclick='FractionalRadio()'     value='FixFrac'    checked='true' name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
          HTMLRadioFR = "<input onclick='FractionalRadio()'     value='FixRatio'   name='RadioProtEdit' id='RadioProtEdit' type='radio' />";
        } 
      
   
var HTML="<table width='760' border='0' class='openPos' style='background-color:#AFC4F0;width:760px'>";
HTML += "   <tr><td width='760' align='right'>";
HTML += "      <img src='images/empty_10.gif' height='5'><img src='images/Info_Small.png' alt='" + ErrorMessage(118) + "' onclick='OpenLegend(9);' style='cursor:pointer'/>";
HTML += " <table><tr>";
HTML += " <td width='8'></td>";
HTML += " <td width='620'>";
HTML += " <table width='608' height='175' cellpadding='0' cellspacing='0' style='background-image:url(images/set_MM_bg.jpg)'>";
HTML += " <tr><td colspan='3' valign='top'>";
HTML += " <table width='606' height='135'>";
HTML += " <tr><td colspan='5' valign='top' class='CellHeader' align='left'>&nbsp;" + ErrorMessage(115) + "</td></tr><tr>";
HTML += " <td width='121' valign='top' align='left'><table width='121' cellpadding='0' cellspacing='0' border='0'><tr><td width='20'>" + HTMLRadioFL +"</td>";
HTML += " <td width='92' colspan='2' class='openPosB'>" + ErrorMessage(55) + ":</td>";
HTML += " </tr><tr><td colspan='3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type='text' value='"+ JsK +"' name='K' id='K' class='openPosTextBox' size='8' maxlength='4'>";
HTML += " k</td></tr></table></td><td width='239' valign='top' align='left'><table><tr>";
HTML += " <td width='20'>" + HTMLRadioFR + "</td><td colspan='2' class='openPosB' align='left'>&nbsp;" +   ErrorMessage(56) +  ":</td>";
HTML += " </tr><tr><td width='20' >&nbsp;</td><td width='123' align='left'>" + ErrorMessage(58) + " :</td>";
HTML += " <td width='50' class='openPosB'>";
HTML += " <input type='text' value='"+JsKFRBase+"' name='KFRBase' id='KFRBase' class='openPosTextBox' size='10'  maxlength='4'></td>";
HTML += " </tr><tr><td width='20' >&nbsp;</td><td width='123' align='left'>"+ErrorMessage(65)+":</td>";
HTML += " <td width='50' class='openPosB'><input type='text' value='"+RatioDelta+"' class='openPosTextBox' size='10' name='EditPortfoiloDelta' id='EditPortfoiloDelta' maxlength='8'></td>";
HTML += " </tr></table></td><td width='230' valign='top' align='left'><table><tr><td width='20' align='left'>" + HTMLRadioFF + "</td>";
HTML += " <td colspan='2' width='180' class='openPosB' align='left'>&nbsp;&nbsp;"  + ErrorMessage(57) +  ":</td>";
HTML += " </tr><tr><td width='20' >&nbsp;</td><td width='123' align='left'>%:</td>";
HTML += " <td width='50' class='openPosB' align='left'>";
HTML += " <input type='text' value='"+JsPAcountBalance+"' name='PAcountBalance' id='PAcountBalance' class='openPosTextBox' size='8' maxlength='3'></td>";
HTML += " </tr><tr><td width='20'>&nbsp;</td><td width='123' align='left'>"+ErrorMessage(66)+":</td>";
HTML += " <td width='50' class='openPosB'><input class='openPosTextBox' size='8' type='text' value='"+JsEditPortfoiloPips+"' name='EditPortfoiloPips' id='EditPortfoiloPips'   maxlength='5'></td>";
HTML += " </tr><tr><td width='20' >&nbsp;</td><td width='123' align='left'>" +ErrorMessage(67)+ ":</td>";
HTML += " <td width='50' class='openPosB'><input type='text' value='"+ JsPEAcountBalance +"' name='PEAcountBalance' id='PEAcountBalance' disabled class='openPosTextBox' size='12'></td>";
HTML += " </tr> </table></td></tr> </table> </td></tr>";
HTML += " <tr><td width='208' align='center' class='CellBlueHead'>&nbsp;</td>";
HTML += " <td width='46' align='right' class='CellBlueHead'><input value='0' name='Reverse' id='Reverse' type='checkbox'  " + IsReverseChecked +"/></td>";
HTML += " <td width='354' align='left' class='openPosB'>"+ErrorMessage(85)+"</td>";
HTML += " </tr></table></td><td width='144' valign='top'>";
HTML += " </td></tr></table><img src='images/empty_10.gif' width='10' height='10' />";
HTML += " <table style='width:760px'><tr><td width='563'></td>";
HTML += " <td width='24'><img src='Images/proceed.png' width='24' height='24' name='SbmPositionFrm' id='SbmPositionFrm' onclick='sendPortFoilioForm(" + prmIndex + ");'  style='cursor:pointer'/></td>";
HTML += " <td width='37' class='ButtonText'><span  style='cursor:pointer' class='ButtonText' onclick='sendPortFoilioForm(" + prmIndex + ");'>" + ErrorMessage(114)+ "</span></td>";
HTML += " <td width='23'></td>";
HTML += " <td width='31'><img src='Images/cancel.png' onclick='GridPortfolioCancel()'  style='cursor:pointer'/></td>";
HTML += " <td width='44' class='ButtonText'><span  style='cursor:pointer' class='ButtonText' onclick='GridPortfolioCancel();'>" + ErrorMessage(112)+"</span></td>";
HTML += " <td width='36'></td></tr></table></td></tr></table>";
          
return HTML;
        
    
}

function sendPortFoilioForm(DataItem)
{

var Reverse              =  document.frmWC.Reverse.checked ? "1" : "0" ;
var ReverseDesc          =  document.frmWC.Reverse.checked ? "Reversed" : "Normal" ;
var Mode                 =  "";
var ModeValue            =  "Fixed Lot";
var K                    =  document.frmWC.K.value;
var KFixed               =  document.frmWC.KFRBase.value;
var precentAcountBalance =  document.frmWC.PAcountBalance.value;
var Pips                 =  document.frmWC.EditPortfoiloPips.value;
var AccountBalance       =  document.frmWC.PEAcountBalance.value;
var RatioDelta           =  document.frmWC.EditPortfoiloDelta.value;
var CurrentLots          =  0;   
var AutoInc              =  GridPortfolio.get_table().getRow(DataItem).getMember('MMID').get_text();
var Profit               =  GridPortfolio.get_table().getRow(DataItem).getMember('Profit').get_text();  
var Pair                 =  GridPortfolio.get_table().getRow(DataItem).getMember('InstrumentName').get_text();
var InstrumentID         =  GridPortfolio.get_table().getRow(DataItem).getMember('InstrumentID').get_text();   
var prmPipValue          =  fn_PipsValueByID(InstrumentID);
var RatioBase            =  document.frmWC.KFRBase.value;
var ModeTypeID           =  1;

   
    for( i = 0; i < 3 ; i++ )
    {
        if( document.frmWC.RadioProtEdit[i].checked ==true)
            Mode  =  document.frmWC.RadioProtEdit[i].value;
    }

    if (!CheckPortfolioData(KFixed,RatioDelta,Pips,precentAcountBalance,RatioBase,K,Mode))
        { 
            return false;
        }
    
 if (Mode == 'FixLots')
    {
        ModeValue   = 'Fixed Lot';
        CurrentLots = K;
        ModeTypeID  = 1;
    }
 if (Mode == 'FixRatio')
    {
        ModeValue='Fixed Ratio';
        CurrentLots = GetCurrentLotsByProfit(Profit,RatioDelta,RatioBase)
        ModeTypeID  = 3;
    }
 if (Mode == 'FixFrac')
    {
     ModeValue   = 'Fixed Fractional';
     CurrentLots = GetCurrentLotsBypips(prmPipValue,AccountBalance,Pips,precentAcountBalance);
     ModeTypeID  = 2;
     }
 
 if(CurrentLots <= 0 )
    {
        alert(ErrorMessage(69));
        return false;
    }       
     
  visibaleAjaxPortfolio(1);
  var prmRandom = Math.floor( Math.random()*10000000);  
  var URL = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=1&prmRandom="+prmRandom+"&Delete=0&ProtfolioID=" + AutoInc + "&ModeTypeID="+ModeTypeID+"&K="+K+"&KFixed="+KFixed+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&AccountBalance="+AccountBalance+"&RatioDelta="+RatioDelta+"&Current="+CurrentLots + "&RatioBase=" + RatioBase + "&Reverse=" + Reverse + "&ModeTypeName=" + ModeValue ;
      repSubmit( URL ,"SubmitPoprtfolio");  
  var item = GridPortfolio.Table.GetRow(DataItem);
      item.SetValue(3,  ModeValue , false);
      item.SetValue(4,  CurrentLots , false);
      item.SetValue(5,  RatioDelta , false);
      item.SetValue(6,  precentAcountBalance , false);
      item.SetValue(8,  KFixed , false);
      item.SetValue(9,  Pips , false);
      item.SetValue(10, K , false);
      item.SetValue(12, AccountBalance , false);
      item.SetValue(18, ReverseDesc      , false);
      item.SetValue(20, Reverse , false);
      item.SetValue(21, ModeTypeID , false);
}
    


function SubmitPoprtfolio(ret)
{
    var res = ret ; 
        GridPortfolioCancel();
        document.getElementById("Portfolio1_Leverage").innerHTML = CalcLeverage();
        ChangeApplay(1);
        visibaleAjaxPortfolio(0); 
}
// ###########################################
      function ProtcallbackError()
        {
            alert(ErrorMessage(45));
        }
 function GridPortfolioEditStatus(status,DataItem)
        {
          insertTableRowPortfolio("Portfolio1_GridPortfolio_row_"+ DataItem , DataItem);
        }
 function insertTableRowPortfolio(where,index) 
    {
        var theTable, theTableBody
            theTable =document.getElementById(where).parentElement.parentElement;
            theTableBody = theTable.tBodies[0]
            index=index+2;

        var newRow = theTableBody.insertRow(index);
        var newCell=newRow.insertCell(0);

     newCell.colSpan=5;
     newCell.innerHTML="";
    }



var PortFolioEditRow=-1;  
var PortFolioEditRowActive=false;  
  
        
        
 function PortFolioEditItem(DataItemX)   
 { 
         GridPortfolio.editCancel();
         if(eval(PortFolioEditRow)==eval(DataItemX))
              {
                PortFolioEditRowActive=false;
                PortFolioEditRow=-1;
                return;
              } 
         if(PortFolioEditRowActive==true)
             {
              GridPortfolio.editCancel();
             }
                PortFolioEditRowActive=true;
                PortFolioEditRow=DataItemX;
                GridPortfolioRefresh=0;
                insertTableRowGridPortfolio("Portfolio1_GridPortfolio_row_"+ DataItemX , DataItemX);
                 
}
        
        
function insertTableRowGridPortfolio(where,index) 
{  
    var theTable, theTableBody
    
       theTable = document.getElementById(where).parentNode.parentNode;
        theTableBody = theTable.tBodies[0];
	    prmIndex = index;
        index=eval(index)+2;
	
        index=index-(eval(GridPortfolio.get_currentPageIndex())* eval(GridPortfolio.get_pageSize()));
      
        
        
    var newRow = theTableBody.insertRow(index);
    
    var newCell= newRow.insertCell(0);
        newCell.colSpan=13;
        newCell.innerHTML= GetinnerHtmlPortfolio(index);
        
        
        FractionalRadio()
}





function FractionalRadio()
{

var RadioP='FixLots';
for( i = 0; i < 3 ; i++ )
             {
           
              if( document.frmWC.RadioProtEdit[i].checked ==true)
                    RadioP  =  document.frmWC.RadioProtEdit[i].value;
             }
  if(RadioP=='FixLots')
    {
      document.frmWC.K.disabled = false;
      document.frmWC.KFRBase.disabled = true;
      document.frmWC.PAcountBalance.disabled = true;
      document.frmWC.EditPortfoiloPips.disabled = true;
      document.frmWC.PEAcountBalance.disabled = true;
      document.frmWC.EditPortfoiloDelta.disabled=true;
        
    } 
    if(RadioP=='FixRatio')
    {
      
      var prmAccountID = CurrentPortfolioAccount;
      var Pair          =  GridPortfolio.get_table().getRow(prmIndex).getMember('InstrumentID').get_text();
      var Provider      =  GridPortfolio.get_table().getRow(prmIndex).getMember('SystemID').get_text();
     
      var prmRandom = Math.floor( Math.random()*10000000);  
      var URL       = "ServerAjax/GetProfitAjaxSubmit.aspx?PortfolioType=1&prmRandom="+prmRandom+"&Pair=" + Pair + "&Provider="+Provider+"&AccountID="+prmAccountID;
      repSubmit( URL ,"UpdateProfit");  
      
      document.frmWC.EditPortfoiloDelta.disabled=false;
      document.frmWC.KFRBase.disabled = false;
      document.frmWC.PAcountBalance.disabled = true;
      document.frmWC.EditPortfoiloPips.disabled = true;
      document.frmWC.PEAcountBalance.disabled = true;
      document.frmWC.K.disabled = true;
    
    }
    if(RadioP=='FixFrac')
    {
    
      document.frmWC.K.disabled = true;
      document.frmWC.KFRBase.disabled = true;
      document.frmWC.EditPortfoiloDelta.disabled=true;
      document.frmWC.PAcountBalance.disabled = false;
      document.frmWC.EditPortfoiloPips.disabled = false;
     
      
    }
}

    
function GridPortfolioCancel()
{
    GridPortfolio.editCancel();
}



function PortFolioOpenMulti(state)
{
    var el  = document.getElementById("PortFolioMultiSelect");
    if (state == 0)
    {
        el.style.display = "none";
    }
    else
    {
        if ( el.style.display != "none"  && InMulty == 0) 
            {
             el.style.display = "none";
            }
        else {
                document.frmWC.MultyEditPortfoiloDelta.value = document.frmWC.hdnDefaultPortfolioDelta.value;
                
                var prmAccountID        = CurrentPortfolioAccount;
                var prmAccountBalance   = GetAccountBalance(prmAccountID);
                    document.frmWC.MultyPAcountBalance.value          = prmAccountBalance;
                    el.style.display = "inline";
             }
   }       
}

function PortFolioChangeCheckAll(value)
{

  var itemIndex = 0;
  GridPortfolio.beginUpdate();
      while(gridItem = GridPortfolio.get_table().getRow(itemIndex))
      {
        gridItem.SetValue(0, value);
        itemIndex++;
     }
     GridPortfolio.endUpdate();      
 
}

function DoMultyEdit()
{          
            var resault              = true ;
            var In                   = false;
            var Reverse              = document.frmWC.MulyReverse.checked ? "1" : "0" ;
            var ReverseDesc          =  document.frmWC.MulyReverse.checked ? "Reversed" : "Normal" ;
            var Mode                 = "";
            var ModeTypeName         = "Fixed Lot";
            var K                    = document.frmWC.MultyK.value;
            var KFixed               = document.frmWC.MultyKFRBase.value;
            var RatioBase            = document.frmWC.MultyKFRBase.value;
            var precentAcountBalance = document.frmWC.MultyPEAcountBalance.value;
            var Pips                 = document.frmWC.MultyEditPortfoiloPips.value;
            var AccountBalance       = document.frmWC.MultyPAcountBalance.value;
            var RatioDelta           = document.frmWC.MultyEditPortfoiloDelta.value;
            var AutoInc              = "";
            var Profit               = 0;
            var Pair                 = ""; 
            var prmPipValue          = "";
            var Provider             = "";
            var Mode                 = "";
            var SendPackage          = "";  
            var ModeTypeID           =  1;
            
           
            
            for( i = 0; i < 4 ; i++ )
            {
                if( document.frmWC.RadioProtEditMulty[i].checked ==true)
                    Mode  =  document.frmWC.RadioProtEditMulty[i].value;
            }
            
            if (Mode == 'Delete')
            {
                SetMultyDeleteMM();
                return;
            }
          
          
            if (Mode == 'FixLots')
            {
                ModeTypeName = 'Fixed Lot';
                ModeTypeID   = 1;
            }
            
            if (Mode == 'FixRatio')
            {
               ModeTypeName   = 'Fixed Ratio';
               ModeTypeID     = 3;
            }
           
            if (Mode == 'FixFrac')
            {
                ModeTypeName = 'Fixed Fractional';
                ModeTypeID   = 2 ;
            }
       
            if (!CheckPortfolioData(KFixed,RatioDelta,Pips,precentAcountBalance,RatioBase,K,Mode))
            { 
                return false;
            }
            
            InMulty     = 1 ;
            for(itemIndex = 0 ; itemIndex < GridPortfolio.get_table().getRowCount(); itemIndex++) 
            {
                if(GridPortfolio.get_table().getRow(itemIndex).getMember(0).get_value())
                {
                   In          = true;
                   if (Mode == 'FixFrac')
                   {
                  

                    prmPipValue = fn_PipsValueByID(GridPortfolio.get_table().getRow(itemIndex).getMember('InstrumentID').get_text());
                    CurrentLots = GetCurrentLotsBypips(prmPipValue,AccountBalance,Pips,precentAcountBalance);
                    SendPackage = SendPackage + GridPortfolio.get_table().getRow(itemIndex).getMember('MMID').get_text() + "," + CurrentLots + "|" ; 
                   }
                   else
                   {
                        SendPackage = SendPackage + GridPortfolio.get_table().getRow(itemIndex).getMember('MMID').get_text() + ",0|" ; 
                   }
                }
            }
           
            
            InMulty = 0 ; 
            if (!In)
            {
                alert(ErrStrings[77]);
                return false;   
            }
            else
            {
                visibaleAjaxPortfolio(1);  
                var prmRandom = Math.floor( Math.random()*10000000);
                var URL = "ServerAjax/PortfolioAjaxSubmitMulty.aspx?PortfolioType=1&prmRandom=" + prmRandom + "&SendPackage=" + SendPackage + "&Reverse="+Reverse + "&ModeTypeName=" + ModeTypeName + "&K=" + K + "&KFixed=" + KFixed + "&RatioBase=" + RatioBase + "&PAcountBalance=" + precentAcountBalance + "&Pips=" + Pips + "&AccountBalance=" + AccountBalance + "&RatioDelta=" + RatioDelta + "&ModeTypeID=" + ModeTypeID;
                    repSubmit( URL ,"SubmitMultyPoprtfolio");  
                    PortFolioOpenMulti(0); 
                    ChangeApplay(1);           
            }
            


}


function FractionalRadioMulty()
{
    var RadioP='FixLots';
    for( i = 0; i < 3 ; i++ )
             {
              if( document.frmWC.RadioProtEditMulty[i].checked ==true)
                    RadioP  =  document.frmWC.RadioProtEditMulty[i].value;
             }
 
  if(RadioP=='FixLots')
    {
      document.frmWC.MultyK.disabled = false;
      document.frmWC.MultyKFRBase.disabled = true;
      document.frmWC.MultyPAcountBalance.disabled = true;
      document.frmWC.MultyEditPortfoiloPips.disabled = true;
      document.frmWC.MultyPEAcountBalance.disabled = true;
      document.frmWC.MultyEditPortfoiloDelta.disabled=true;  
    } 
    if(RadioP=='FixRatio')
    {
      document.frmWC.MultyEditPortfoiloDelta.disabled=false;
      document.frmWC.MultyKFRBase.disabled = false;
      document.frmWC.MultyPAcountBalance.disabled = true;
      document.frmWC.MultyEditPortfoiloPips.disabled = true;
      document.frmWC.MultyPEAcountBalance.disabled = true;
      document.frmWC.MultyK.disabled = true;
    }
    if(RadioP=='FixFrac')
    {
      document.frmWC.MultyK.disabled = true;
      document.frmWC.MultyKFRBase.disabled = true;
      document.frmWC.MultyEditPortfoiloDelta.disabled=true;
      document.frmWC.MultyPEAcountBalance.disabled = false;
      document.frmWC.MultyEditPortfoiloPips.disabled = false;
    }
}

function GridPortfolio_onItemSelect(sender,eventArgs)
        {
        
         var DataItemX1= eventArgs.get_item().get_clientId();
         if(CheckedPort==DataItemX1)
            {
                GridPortfolio.editCancel();
                PortFolioEditRowActive=false;
                PortFolioEditRow=-1;
                CheckedPort = -1  ; 
                
            }
         else
            {   
                CheckedPort = -1  ;    
                PortFolioEditItem(DataItemX1);
            }
        }
function GridPortfolio_onItemCheckChange(sender, eventArgs)
{
    CheckedPort = eventArgs.get_item().get_clientId();
}

function GetPortfolioData(FromSession)
{
    visibaleAjaxPortfolio(1);
    var prmAccountID
    if (FromSession == 0) 
    {
        var index = document.frmWC.Portfolio1_ProtfolioAccount.selectedIndex;
            prmAccountID = document.frmWC.Portfolio1_ProtfolioAccount.options[index].value;
            CurrentPortfolioAccount = prmAccountID;
    }
    else
        prmAccountID = CurrentPortfolioAccount
        
    var prmRandom = Math.floor( Math.random()*10000000);
    var URL = "ServerAjax/ProtfolioAjaxGetData.aspx?prmRandom="+prmRandom+"&FromSession="+FromSession+"&PortfolioType=1&AccountID="+prmAccountID  ;
    document.body.style.cursor="wait";       
    repSubmit( URL ,"LoadPortfolioData");  
         
}

function LoadPortfolioData(ret)
{
  if (ret != "Error" )
    {
        var loadXML = eval(ret)
            GridPortfolio.load(loadXML);
            GridPortfolio.render();
            ChangeApplay(0);
            document.getElementById("Portfolio1_Leverage").innerHTML = CalcLeverage();
            document.getElementById('imgPortfolioGetdata').style.visibility = 'hidden';
        }
 visibaleAjaxPortfolio(0);
 document.body.style.cursor = "default";
 ToDisplayPortfolioMsg(CurrentPortfolioAccount);
}

function PortfolioApplay(ApplayAll) 

{

   visibaleAjaxPortfolio(1); 
   var prmRandom = Math.floor( Math.random()*10000000);
   var URL = "ServerAjax/PortfolioAjaxApplayChanges.aspx?prmRandom="+prmRandom+"&PortfolioType=0&AppLayAll="+ ApplayAll ;
   repSubmit( URL ,"ApplayResault"); 
}
function ApplayResault(ret)
{
  if (ret == "Ok" )
    {
        if (CurrentPortfolioAccount == CurrentPortfolioAccountAdd)
        {
            GetPortfolioData(0);
            GetPortfolioDataAdd(0);
        }
        else
        {
            GetPortfolioData(0);
        }
       ChangeApplay(0);
    }
    visibaleAjaxPortfolio(0);   
}


function ChangeApplay(On)
{
if (On == 1)
 {
 
 document.getElementById("imgApply").src = "Images/apply_on.gif"; 
 document.getElementById("imgrevert").src = "Images/revert.png"; 


 document.getElementById("spnrevert").removeAttribute("disabled");
 document.getElementById("spnApply").removeAttribute("disabled");

 document.getElementById("imgApply").removeAttribute("disabled");
 document.getElementById("imgrevert").removeAttribute("disabled");
 
 document.getElementById("imgApply").style.cursor = "pointer";
 document.getElementById("imgrevert").style.cursor = "pointer" ;

 document.getElementById("spnrevert").style.cursor = "pointer" ;                                    
 document.getElementById("spnApply").style.cursor  = "pointer" ;
 if (CheckIfApllayAllEnable()) 
        {
         document.getElementById("imgApplyAll").src = "Images/apply.png";
         document.getElementById("spnApplyAll").removeAttribute("disabled");
         document.getElementById("imgApplyAll").removeAttribute("disabled");
         document.getElementById("imgApplyAll").style.cursor = "pointer"
         document.getElementById("spnApplyAll").style.cursor = "pointer" ;
        }
 
 }
else
    {
     document.getElementById("imgApply").src = "Images/apply_off.png"; 
     document.getElementById("imgrevert").src = "Images/revert_off.png"; 
                                            
     document.getElementById("spnrevert").setAttribute("disabled",true);                                        
     document.getElementById("spnApply").setAttribute("disabled",true);
  
     
     document.getElementById("imgApply").setAttribute("disabled",true);                                        
     document.getElementById("imgrevert").setAttribute("disabled",true);
     
     document.getElementById("imgApply").style.cursor = "default";
     document.getElementById("imgrevert").style.cursor = "default" ;
      
     document.getElementById("spnrevert").style.cursor = "default" ;                                    
     document.getElementById("spnApply").style.cursor  = "default" ;

     
     if (CheckIfApllayAllEnable()) 
            {
             document.getElementById("imgApplyAll").src = "Images/apply_off.png"; 
             document.getElementById("spnApplyAll").setAttribute("disabled",true);           
             document.getElementById("imgApplyAll").setAttribute("disabled",true);
             document.getElementById("imgApplyAll").style.cursor = "default";
             document.getElementById("spnApplyAll").style.cursor = "default" ;
            }
    }
}

function visibaleAjaxPortfolio(On)
{
if (On == 1 )
 document.getElementById('imgPortfolioGetdata').style.visibility = 'visible';
else
 document.getElementById('imgPortfolioGetdata').style.visibility = 'hidden';
}



function CalcLeverage()
{
        var prmAccountID     = CurrentPortfolioAccount;
        var TotalLeverage    = 0 ; 
        var SumLeverage      = 0 ; 
        var balance          = CurrencyConvert(GetAccountBalance(prmAccountID)) ; 
        var RowCount         = GridPortfolio.get_table().getRowCount() ;
        showCustomFooterPortfolio();
        if (RowCount > 0)
        {
            for (Lindex = 0  ; Lindex < RowCount  ; Lindex ++)
            {
                PipsValue   =  fn_PipsValueByID(GridPortfolio.get_table().getRow(Lindex).getMember('InstrumentID').get_text());
                SumLeverage =  SumLeverage + ( PipsValue * 1000 *  GridPortfolio.get_table().getRow(Lindex).getMember('MaxPositions').get_text() * GridPortfolio.get_table().getRow(Lindex).getMember('CurrentSet').get_text() ) ;
            }
            if (balance == 0)
                {
                    return "N/A";
                }
            else
                {
                  return     ( "1:" + Math.round( (SumLeverage / balance)  * 100 ) / 100 );
                }
        }
        else
        {
            return "N/A" ;
        }
}




function SetPeCurve(DataItem)
{
    var Provider = "" ;
    var Pair = "" ;
    var Current = "" ;
    var ProviderName = "";
    var PairName = "";
        
        Pair            =  GridPortfolio.get_table().getRow(DataItem).getMember('InstrumentID').get_text();
        Provider        =  GridPortfolio.get_table().getRow(DataItem).getMember('SystemID').get_text();
        Current         =  GridPortfolio.get_table().getRow(DataItem).getMember('CurrentSet').get_text(); 
        Reverse         =  GridPortfolio.get_table().getRow(DataItem).getMember('AttributeID').get_text(); 
        ProviderName    =  GridPortfolio.get_table().getRow(DataItem).getMember('SystemName').get_text() ;
        PairName         =  GridPortfolio.get_table().getRow(DataItem).getMember('InstrumentName').get_text() ;
    
    var URL     =  "GraphSystem.aspx?Current="+Current +"&Provider="+Provider+"&Pair="+ Pair + "&Reverse="+Reverse + "&ProviderName="+ProviderName + "&PairName="+PairName;
        window.open(URL,'mywindow','left=50,top=50,scrollbars=1,menubar=0,resizable=1,toolbar=0,width=1050,height=850'); 
}



function PortfolioLegend()
{
var el    = document.getElementById("PortFolioMultiSelect");
if ( el.style.display != "none") 
    {
         OpenLegend(11);
    }
else {

        OpenLegend(4);
     }
}



 function onPagePortfolio(newPage)
  {
    // delay call so that Grid's client properties have their new values
    setTimeout("showCustomFooterPortfolio();",FooterDelay);

    return true;
  }

  function onLoadPortfolio()
  {
    showCustomFooterPortfolio();
  }
  
  
  
  function showCustomFooterPortfolio()
  {
    var gridId = "Portfolio1_GridPortfolio";
    if (isdefined(gridId))
    {
      
      var pagerSpan = 5; // should be at least 2
      var cssClass = "GridFooterText";

      var footer = buildPager(eval(gridId), pagerSpan,cssClass);
      document.getElementById("tdPagerPortfolio").innerHTML = "<div style='white-space:nowrap;display:inline;' class=\"" + cssClass + "\">" + footer + "</div>";

      footer = buildPageXofY(eval(gridId));
      document.getElementById("tdIndexPortfolio").innerHTML = "<div class=\"" + cssClass + "\">" + footer + "</div>";
    }
    else 
    {
      setTimeout("showCustomFooterPortfolio();", FooterDelay);
    }
 }



function portfolioCheckIfHedggingEnable()
{
        var index           = document.frmWC.Portfolio1_ProtfolioAccount.selectedIndex; 
        var Account = document.frmWC.Portfolio1_ProtfolioAccount.options[index].value ;
        PortfolioAllowHedgging = CheckIfHedggingEnable(Account);
        document.getElementById("Portfolio1_Hedgging").innerHTML =  PortfolioAllowHedgging == 1 ? "=Y" : "=N";
}