﻿var tmpProfit         = 0;
var AddMMIndex        = 0 ; 
var prmAccountID      = 0 ;
var prmAccountBalance = 0 ;
var myProviders       = new Array();
var ArrayIndex        = 0;
var requestPortfolioPerformance=false;
var portFolioInstrument="";



function GridPortPer_SortChange(sender, eventArgs)
 {
 
        GridPortPer.Page(0);
 }


function callbackError()
        {
            alert(ErrorMessage(45));
        }


function GetProfit(ret)
{
   tmpProfit =  ret ; 
}   

function setArrayProfit(ret)
{
    ret = ret.split('|');
    var Profit     = ret[0];
    var Arrayindex = ret[1];
    myProviders[Arrayindex][5] = ret[0] ;
    
}

function AddFractionalRadio()
{
 var RadioP='FixLots';
 for( i = 0; i < document.frmWC.AddRadioProtEdit.length; i++ )
    {
       if( document.frmWC.AddRadioProtEdit[i].checked ==true)
           RadioP  =  document.frmWC.AddRadioProtEdit[i].value;
    }

 if(RadioP=='FixLots')
    {
      document.frmWC.NewMMK.disabled = false;
      document.frmWC.NewMMKFRBase.disabled = true;
      document.frmWC.NewMMPAcountBalance.disabled = true;
      document.frmWC.NewMMPortfoiloPips.disabled = true;
      document.frmWC.NewMMPortfoiloDelta.disabled=true;
    } 
 if(RadioP=='FixRatio')
    {
          var Pair      =  GridPortPer.get_table().getRow(AddMMIndex).getMember('InstrumentID').get_text();
          var Provider  =  GridPortPer.get_table().getRow(AddMMIndex).getMember('SystemID').get_text();
          var prmRandom = Math.floor( Math.random()*10000000);  
          var URL       = "ServerAjax/GetProfitAjaxSubmit.aspx?PortfolioType=2&prmRandom="+prmRandom+"&Pair=" + Pair + "&Provider="+Provider+"&AccountID="+prmAccountID;
              repSubmit( URL ,"GetProfit");  
      
              document.frmWC.NewMMPortfoiloDelta.disabled=false;
              document.frmWC.NewMMKFRBase.disabled = false;
              document.frmWC.NewMMPAcountBalance.disabled = true;
              document.frmWC.NewMMPortfoiloPips.disabled = true;
              document.frmWC.NewMMK.disabled = true;
    
    }
 
 if(RadioP=='FixFrac')
    {
      document.frmWC.NewMMK.disabled = true;
      document.frmWC.NewMMKFRBase.disabled = true;
      document.frmWC.NewMMPortfoiloDelta.disabled=true;
      document.frmWC.NewMMPAcountBalance.disabled = false;
      document.frmWC.NewMMPortfoiloPips.disabled = false;
     
    }
}
var PerEditRow=-1;  
var PerEditRowActive=false;
function GridPortPer_onItemSelect(sender,eventArgs)
        {
         GridPortPer.editCancel();
         var DataItemX= eventArgs.get_item().get_clientId();
         if(eval(PerEditRow)==eval(DataItemX))
              {
               PerEditRowActive=false;
                PerEditRow=-1;
                return;
              } 
         if(PerEditRowActive==true)
             {
               GridPortPer.editCancel();
             }
            PerEditRowActive=true;
            PerEditRow=DataItemX;
            insertTableRowPortPer("PortPer1_GridPortPer_row_"+ DataItemX , DataItemX);
        }
        
        
function insertTableRowPortPer(where,index) 
{  
   var  theTable, theTableBody
   var  theTable = document.getElementById(where).parentNode.parentNode;
        theTableBody = theTable.tBodies[0]
        index=eval(index)+2;
        AddMMIndex = index - 2  ;
        index=index-(eval(GridPortPer.get_currentPageIndex())*  eval(GridPortPer.get_pageSize()));
   var  newRow = theTableBody.insertRow(index);
   var  newCell= newRow.insertCell(0);
        newCell.colSpan= 16;
        newCell.innerHTML= GetinnerPortPerlHistory(index );
}
function GetinnerPortPerlHistory(index)
{

var selectindex         = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.selectedIndex; 
    prmAccountID        = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.options[selectindex].value ;
    prmAccountBalance   = GetAccountBalance(prmAccountID);
index             =  index-2;




var HTML = "<table width='775px' border='0' class='openPos' style='background-color:#AFC4F0;width:775px'>"
    HTML +="<tr><td width='775px'>"
    HTML +="<img src='images/empty_10.gif'  height='5'>"
    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' align='left'>"
 	HTML +="    <table width='606' height='135'>"
    HTML +="    <tr>"
    HTML +="    <td colspan='5' valign='top' class='CellHeader'>&nbsp;"+ ErrorMessage(115) +"</td>"
    HTML +=" </tr>"
    HTML +=" <tr>"
    HTML +=" <td width='121' valign='top' align='left'>"
    HTML +=" <table width='121'>"
    HTML +=" <tr>"
  	HTML +=" <td width='20'> <input onclick='AddFractionalRadio()' value='FixLots'  name='AddRadioProtEdit' id='AddRadioProtEdit' type='radio' checked/></td>"
    HTML +=" <td width='92' colspan='2' class='openPosB'>&nbsp;" +ErrorMessage(55) +":</td>"
    HTML +="</tr>"
    HTML +="<tr>"
  	HTML +="<td colspan='3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' class='style12' size='8'  name='NewMMK' value='10' id='NewMMK' maxlength='4'>:k</td></tr>"
    HTML +="</table></td>"
    HTML +="<td width='239' valign='top' align='left'>"
    HTML +="<table>"
    HTML +="<tr>"
    HTML +="<td width='20' ><input onclick='AddFractionalRadio()' value='FixRatio' name='AddRadioProtEdit' id='AddRadioProtEdit' type='radio' /></td>"
    HTML +="<td colspan='2' class='openPosB'>&nbsp;"+ErrorMessage(56) +":</td>"
    HTML +="</tr>"
    HTML +="<tr>"
    HTML +="<td width='20' >&nbsp;</td>"
    HTML +="<td width='123'>" +ErrorMessage(58) + ":</span> <span class='style13'>:</td>"
    HTML +="<td width='50' class='openPosB'><input type='text' value='10' disabled name='NewMMKFRBase' id='NewMMKFRBase'  class='style12' size='10' maxlength='4'></td>"
    HTML +="</tr>"
    HTML +="<tr>"
    HTML +="<td width='20' >&nbsp;</td>"
    HTML +="<td width='123'>" +ErrorMessage(65) + ":</td>"
    HTML +="<td width='50' class='openPosB'><input type='text' value='" + document.frmWC.hdnDefaultPortfolioDelta.value + "' disabled name='NewMMPortfoiloDelta' id='NewMMPortfoiloDelta' class='style12' size='10' maxlength='8'></td>"
    HTML +="</tr>"
    HTML +="</table></td>"
    HTML +="<td width='230' valign='top' align='left'>"
    HTML +="<table>"
    HTML +="<tr>"
    HTML +="<td width='20'><input onclick='AddFractionalRadio()' value='FixFrac'  name='AddRadioProtEdit' id='AddRadioProtEdit' type='radio' /></td>"
    HTML +="<td colspan='2' width='180' class='openPosB'>&nbsp;&nbsp;" +ErrorMessage(57) +":</td>"
    HTML +="</tr>"
    HTML +="<tr>"
    HTML +="<td width='20' >&nbsp;</td>"
    HTML +="<td width='123'>%:</td>"
    HTML +="<td width='50' class='openPosB'><input type='text' value='1' disabled name='NewMMPAcountBalance' id='NewMMPAcountBalance'  class='style12' size='8' maxlength='3'/></td>"
    HTML +="</tr>"
    HTML +="<tr>"
    HTML +="<td width='20' >&nbsp;</td>"
    HTML +="<td width='123'><span class='openPosTextBox'>"+ErrorMessage(66)+":</td>"
    HTML +="<td width='50' class='openPosB'> <input type='text' value='50'disabled name='NewMMPortfoiloPips' id='NewMMPortfoiloPips'  class='style12' size='8' maxlength='5'/></td>"
    HTML +="</tr>"
    HTML +="<tr>"
    HTML +="<td width='20' >&nbsp;</td>"
    HTML +="<td width='123'>"+ErrorMessage(67) +":</td>"
    HTML +="<td width='50' class='openPosB'><input disabled type='text' value=' " + prmAccountBalance + "' name='NewMMPEAcountBalance' id='NewMMPEAcountBalance'  class='style12' size='10'  /></td>"
    HTML +="</tr>"
    HTML +="</table>"
    HTML +="</td>"
    HTML +="</tr>"
 	HTML +="</table>"
    HTML +="</td></tr>"
    HTML +="<tr>"
    HTML +="<td width='208' align='center' class='CellBlueHead'>&nbsp;</td>"
    HTML +="<td width='46' align='right' class='CellBlueHead'><input value='0' name='NewMMReverse' id='NewMMReverse' type='checkbox' /></td>"
    HTML +="<td width='354' align='left' class='openPosB'>" +ErrorMessage(85) + "</td>"
    HTML +="</tr>"
    HTML +="</table>"
    HTML +="</td>"
    HTML +="<td width='144' valign='top'>"
    HTML +="</td>"
    HTML +="</tr>"
    HTML +="</table>"
    HTML +="<img src='images/empty_10.gif' width='10' height='10' />"
    HTML +="<table style='width:775px'>"
    HTML +="<tr>"
    HTML +="<td width='523'></td>"
    HTML +="<td width='24'><img src='images/Add.png' onclick='AddNewPortFoilio(); ' /></td>"
    HTML +="<td width='37' align='left' class='ButtonText'><span onclick='AddNewPortFoilio();'>" + ErrorMessage(116) + "</span></td>"
    HTML +="<td width='10'></td>"
    HTML +="<td width='31'><img src='images/cancel.PNG'  onclick='GridPortPer.editCancel();'/></td>"
    HTML +="<td width='44' class='ButtonText'><span onclick='GridPortPer.editCancel();'>"+ ErrorMessage(112) + "</span></td>"
    HTML +="<td width='36'></td>"
    HTML +="</tr>"
    HTML +="</table>"
    HTML +="</td>"
    HTML +="</tr>"
    HTML +="</table>"




return HTML;
}

function AddNewPortFoilio()
{

var Reverse              =  document.frmWC.NewMMReverse.checked ? "1" : "0" ;
var ReverseDesc          =  document.frmWC.NewMMReverse.checked ? "Reversed" : "Normal" ;
var Mode                 =  "";
var ModeValue            =  "Fixed Lot";
var K                    =  document.frmWC.NewMMK.value;
var RatioBase            =  document.frmWC.NewMMKFRBase.value;
var precentAcountBalance =  document.frmWC.NewMMPAcountBalance.value;
var Pips                 =  document.frmWC.NewMMPortfoiloPips.value;
var AccountBalance       =  document.frmWC.NewMMPEAcountBalance.value;
var RatioDelta           =  document.frmWC.NewMMPortfoiloDelta.value;
var CurrentLots          =  0;   
var Pair                 =  GridPortPer.get_table().getRow(AddMMIndex).getMember('InstrumentName').get_text();
var PairID               =  GridPortPer.get_table().getRow(AddMMIndex).getMember('InstrumentID').get_text();
var Provider             =  GridPortPer.get_table().getRow(AddMMIndex).getMember('SystemName').get_text();   
var ProviderID           =  GridPortPer.get_table().getRow(AddMMIndex).getMember('SystemID').get_text();   
var Maxposition          =  GridPortPer.get_table().getRow(AddMMIndex).getMember('MSP').get_text();         
var prmPipValue          =  fn_PipsValue(Pair);
var ModeTypeID           =  1;

for( i = 0; i < document.frmWC.AddRadioProtEdit.length; i++ )
    {
        if( document.frmWC.AddRadioProtEdit[i].checked ==true)
            Mode  =  document.frmWC.AddRadioProtEdit[i].value;
    }

  if (! CheckPortfolioData(RatioBase,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(tmpProfit,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;
    }       
    visibaleAjaxPortPerAdd(1);
    
    var Index =  ExistsPortfolio(PairID,ProviderID);
    if (Index >= 0 )
    {
       var AutoInc =  GridPortfolioAdd.get_table().getRow(Index).getMember('MMID').get_text();
       var item = GridPortfolioAdd.Table.GetRow(Index);
            item.SetValue(3,  ModeValue , false);
            item.SetValue(4,  CurrentLots , false);
            item.SetValue(5,  RatioDelta , false);
            item.SetValue(6,  precentAcountBalance , false);
            item.SetValue(8,  RatioBase , 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);
       
       
        var prmRandom = Math.floor( Math.random()*10000000);     
       
        var URL = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=2&prmRandom="+prmRandom+"&Delete=0&ProtfolioID=" + AutoInc + "&ModeTypeID="+ModeTypeID+"&K="+K+"&KFixed="+RatioBase+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&AccountBalance="+AccountBalance+"&RatioDelta="+RatioDelta+"&Current="+CurrentLots + "&RatioBase=" + RatioBase + "&Reverse=" + Reverse + "&ModeTypeName=" + ModeValue ;
            repSubmit( URL ,"addSubmitPoprtfolio");  
    }
    
    else
    {
        var prmRandom = Math.floor( Math.random()*10000000);  
        var URL = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=2&prmRandom="+prmRandom+"&Delete=2&ModeTypeID="+ModeTypeID+"&K="+K+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&Current="+CurrentLots+"&Pair="+Pair+"&Provider="+Provider+"&AccountID="+prmAccountID+"&RatioDelta="+RatioDelta+"&Profit="+tmpProfit+"&RatioBase="+ RatioBase + "&AccountBalance="+AccountBalance + "&MaxPos="+Maxposition + "&Reverse=" + Reverse +  "&ModeTypeName=" + ModeValue + "&ProviderID=" + ProviderID +"&PairID=" + PairID +"&ReverseDesc=" + ReverseDesc;
            repSubmit( URL ,"addSubmitPoprtfolio");  
    }

}


function ExistsPortfolio(InstrumentID,SystemID)
{

        var Find=0;
        var Bid = 0 ; 
        var CurrentPair = "" ; 
        var CurrentProvider = "" ; 
        
        for (i=0  ; i < GridPortfolioAdd.get_table().getRowCount() &&  Find==0 ; i++)
        {
            CurrentPair     = GridPortfolioAdd.get_table().getRow(i).getMember('InstrumentID').get_text();
            CurrentProvider = GridPortfolioAdd.get_table().getRow(i).getMember('SystemID').get_text();
             if (InstrumentID == CurrentPair && SystemID == CurrentProvider)
             {
              Find = 1;
              return  (i)   ;
             } 
        }
        return -1;
}


function addSubmitPoprtfolio(ret)    
{
     if (ret=="Ok")
        {
         GridPortPer.editCancel();
         GetPortfolioDataAdd(1);
         ChangeApplayAdd(1);
        }
     visibaleAjaxPortPerAdd(0);    
}


function LoadPortperData(ret) {
    if (ret != "Error") {
        var loadXML = eval(ret)
        GridPortPer.load(loadXML);
        GridPortPer.render();

    }
    showCustomFooterPortPer();
    visibaleAjaxPortPerAdd(0);
    GridPortPer.Page(0);
    searchPairs = "";
    searchSystems = "";
    requestPerformance = false;
    document.body.style.cursor = "default";
}



function visibaleAjaxPortPerAdd(On)
{
    if (On == 1 )
        document.getElementById('imgPortPerGetdata').style.visibility = 'visible';
    else
        document.getElementById('imgPortPerGetdata').style.visibility = 'hidden';
}


function Gridporper_onItemCheckChange(sender, eventArgs)
{

    var selectindex              = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.selectedIndex; 
        prmAccountID             = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.options[selectindex].value ;
        PerEditRow               = eventArgs.get_item().get_clientId();
        var Pair                 =  GridPortPer.get_table().getRow(PerEditRow).getMember('InstrumentName').get_text();
        var PairID               =  GridPortPer.get_table().getRow(PerEditRow).getMember('InstrumentID').get_text();
        var prmProvider             =  GridPortPer.get_table().getRow(PerEditRow).getMember('SystemName').get_text();   
        var ProviderID           =  GridPortPer.get_table().getRow(PerEditRow).getMember('SystemID').get_text();   
        var Maxposition          =  GridPortPer.get_table().getRow(PerEditRow).getMember('MSP').get_text();         
        var Found                 = false;
        
        if (!GridPortPer.get_table().getRow(PerEditRow).getMember(0).get_value())
        {
                      for(i=0; i < ArrayIndex && ! Found ; i ++)
                            if (myProviders[i][6] == PairID &&  myProviders[i][7] == ProviderID)
                            {
                                myProviders[i][4] = 1 ; 
                                Found = true;
                            } 
                       
                       
                       
                        if (!Found)    
                        {
                            var Provider    = new Array();
                                Provider[0] = Pair;
                                Provider[1] = prmProvider;
                                Provider[2] = Maxposition;
                                Provider[3] = PerEditRow;
                                Provider[4] = 1;
                                Provider[5] = 0;
                                Provider[6] = PairID;
                                Provider[7] = ProviderID;
                                myProviders[ArrayIndex++] = Provider ;
                                var prmRandom = Math.floor( Math.random()*10000000);  
                                var URL       = "ServerAjax/GetProfitAjaxSubmit4Multy.aspx?PortfolioType=2&prmRandom="+prmRandom+"&Pair=" + PairID + "&Provider="+ProviderID+"&AccountID="+prmAccountID + "&index="+ (ArrayIndex-1);
                                    repSubmit(URL ,"setArrayProfit"); 
                         }   
        }
      
      else
            {
            for(i=0; i < ArrayIndex && ! Found ; i ++)
                    if (myProviders[i][6] == PairID &&  myProviders[i][7] == ProviderID)
                        {
                            myProviders[i][4] = 0 ; 
                            Found = true;
                        } 
        
        }  

     
}


function OpenMultiPortPer(state)
{
    var el = document.getElementById("MultiSelectPortper");
    var elClear = document.getElementById("ClearAllPort");
    if ( el.style.display != "none" ) 
    {
        elClear.style.display = "none";
        el.style.display = "none";
        DisplayGridCheeckBox(0);
    }
    else 
      {
      DisplayGridCheeckBox(1);
      document.frmWC.ProtPerPortfoiloDelta.value = document.frmWC.hdnDefaultPortfolioDelta.value;
      var selectindex              = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.selectedIndex; 
          prmAccountID             = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.options[selectindex].value ;
          document.frmWC.ProtPerPAcountBalance.value   = GetAccountBalance(prmAccountID);
        el.style.display = "inline";
        elClear.style.display = "inline";
      }
}


function ChangeCheckAllPort(value)
{
     var itemIndex = 0;
     GridPortPer.beginUpdate();
        for(i=0 ; i < ArrayIndex ; i++)
        {
            itemIndex = myProviders[i][3];
            gridItem  = GridPortPer.get_table().getRow(itemIndex);
            gridItem.SetValue(0, value);
        }
     GridPortPer.endUpdate();
     resetArray();
    }

function PortPerFractionalRadioMulty()
{
    var RadioP='FixLots';
    for( i = 0; i < 3 ; i++ )
             {
              if( document.frmWC.RadioProtPerMulty[i].checked ==true)
                    RadioP  =  document.frmWC.RadioProtPerMulty[i].value;
             }
 
  if(RadioP=='FixLots')
    {
      document.frmWC.ProtPerK.disabled = false;
      document.frmWC.ProtPerKFRBase.disabled = true;
      document.frmWC.ProtPerEditPortfoiloPips.disabled = true;
      document.frmWC.ProtPerPEAcountBalance.disabled = true;
      document.frmWC.ProtPerPortfoiloDelta.disabled=true;  
    } 
    if(RadioP=='FixRatio')
    {
      document.frmWC.ProtPerPortfoiloDelta.disabled=false;
      document.frmWC.ProtPerKFRBase.disabled = false;
      document.frmWC.ProtPerEditPortfoiloPips.disabled = true;
      document.frmWC.ProtPerPEAcountBalance.disabled = true;
      document.frmWC.ProtPerK.disabled = true;
    }
    if(RadioP=='FixFrac')
    {
      document.frmWC.ProtPerK.disabled = true;
      document.frmWC.ProtPerKFRBase.disabled = true;
      document.frmWC.ProtPerPortfoiloDelta.disabled=true;
      document.frmWC.ProtPerPEAcountBalance.disabled = false;
      document.frmWC.ProtPerEditPortfoiloPips.disabled = false;
    }
}

function AddMultyProvider()

{

    var selectindex         = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.selectedIndex; 
        prmAccountID        = document.frmWC.PortfolioAdd1_ProtfolioAccountAdd.options[selectindex].value ;
        prmAccountBalance   = GetAccountBalance(prmAccountID);
    var Reverse              =  document.frmWC.ProtPerReverse.checked ? "1" : "0" ;
    var ReverseDesc          =  document.frmWC.ProtPerReverse.checked ? "Reversed" : "Normal" ;
    var URL                  =  "";
    var Mode                 =  "";
    var ModeValue            =  "Fixed Lot";
    var K                    =  document.frmWC.ProtPerK.value;
    var RatioBase            =  document.frmWC.ProtPerKFRBase.value;
    var precentAcountBalance =  document.frmWC.ProtPerPEAcountBalance.value;
    var Pips                 =  document.frmWC.ProtPerEditPortfoiloPips.value;
    var AccountBalance       =  document.frmWC.ProtPerPAcountBalance.value;
    var RatioDelta           =  document.frmWC.ProtPerPortfoiloDelta.value;
    var CurrentLots          =  0;   
    var prmPipValue          =  0;
    var prmRandom            =  0;
    var itemIndex            = -1;
    var In                   =  false;
    var ModeTypeID           =  1;

for( i = 0; i < document.frmWC.RadioProtPerMulty.length; i++ )
    {
        if( document.frmWC.RadioProtPerMulty[i].checked ==true)
            Mode  =  document.frmWC.RadioProtPerMulty[i].value;
    }
 
 if (! CheckPortfolioData(RatioBase,RatioDelta,Pips,precentAcountBalance,RatioBase,K,Mode))
        { 
            return false;
        }

  for(J =0; J < ArrayIndex ; J ++)
  {
        if (myProviders[J][4]== 1)
        {
           In = true;
           if (Mode == 'FixLots')
            {
                ModeValue   = 'Fixed Lot';
                CurrentLots = K;
                ModeTypeID= 1;
            }
 
           if (Mode == 'FixRatio')
            {
                ModeValue='Fixed Ratio';
                CurrentLots = GetCurrentLotsByProfit(myProviders[J][5],RatioDelta,RatioBase);
                ModeTypeID= 3;
            }
            
            if (Mode == 'FixFrac')
            {
                prmPipValue = fn_PipsValue(myProviders[J][0]) 
                ModeValue   = 'Fixed Fractional';
                CurrentLots = GetCurrentLotsBypips(prmPipValue,AccountBalance,Pips,precentAcountBalance);
                ModeTypeID= 2;
             }
 
            if(CurrentLots > 0  )
            {
                    visibaleAjaxPortPerAdd(1);
                    prmRandom = Math.floor( Math.random()*10000000);  
                    itemIndex = ExistsPortfolio(myProviders[J][6],myProviders[J][7]);
                    if ( itemIndex >= 0 )
                    {
                        var AutoInc =  GridPortfolioAdd.get_table().getRow(itemIndex).getMember('MMID').get_text();
                        var item = GridPortfolioAdd.Table.GetRow(itemIndex);
                            item.SetValue(3,  ModeValue , false);
                            item.SetValue(4,  CurrentLots , false);
                            item.SetValue(5,  RatioDelta , false);
                            item.SetValue(6,  precentAcountBalance , false);
                            item.SetValue(8,  RatioBase , 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);
                            
                            
                        URL = "ServerAjax/ProtfolioAjaxSubmit.aspx?PortfolioType=2&prmRandom="+prmRandom+"&Delete=0&ProtfolioID=" + AutoInc + "&ModeTypeID="+ModeTypeID+"&K="+K+"&KFixed="+RatioBase+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&AccountBalance="+prmAccountBalance+"&RatioDelta="+RatioDelta+"&Current="+CurrentLots + "&RatioBase=" + RatioBase + "&Reverse=" + Reverse + "&ModeTypeName=" + ModeValue ;
                        repSubmit( URL ,"MultyaddSubmitPoprtfolio");  
                       
                        if (J == (ArrayIndex-1))
                        {
                            OpenMultiPortPer(0);
                            GetPortfolioDataAdd(1);
                            ChangeApplayAdd(1);
                            visibaleAjaxPortPerAdd(0); 
                        }
                    }
                    else
                    {
                    
                        if(J == (ArrayIndex-1))
                             URL = "ServerAjax/MultyProtfolioAjaxSubmitAdd.aspx?Last=1&prmRandom="+prmRandom+"&Delete=2&ModeTypeID="+ModeTypeID+"&K="+K+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&Current="+CurrentLots+"&Pair="+myProviders[J][0]+"&Provider="+myProviders[J][1]+"&AccountID="+prmAccountID+"&RatioDelta="+RatioDelta+"&Profit="+ myProviders[J][5] +"&RatioBase="+ RatioBase + "&AccountBalance="+prmAccountBalance + "&MaxPos="+ myProviders[J][2] + "&Reverse=" + Reverse +  "&ModeTypeName=" + ModeValue + "&ProviderID=" + myProviders[J][7] +"&PairID=" + myProviders[J][6] +"&ReverseDesc=" + ReverseDesc;
                        else
                             URL = "ServerAjax/MultyProtfolioAjaxSubmitAdd.aspx?Last=0&prmRandom="+prmRandom+"&Delete=2&ModeTypeID="+ModeTypeID+"&K="+K+"&PAcountBalance="+precentAcountBalance+"&Pips="+Pips+"&Current="+CurrentLots+"&Pair="+myProviders[J][0]+"&Provider="+myProviders[J][1]+"&AccountID="+prmAccountID+"&RatioDelta="+RatioDelta+"&Profit="+ myProviders[J][5] +"&RatioBase="+ RatioBase + "&AccountBalance="+prmAccountBalance + "&MaxPos="+ myProviders[J][2] + "&Reverse=" + Reverse +  "&ModeTypeName=" + ModeValue + "&ProviderID=" + myProviders[J][7] +"&PairID=" + myProviders[J][6] +"&ReverseDesc=" + ReverseDesc;
                      
                       repSubmit( URL ,"MultyaddSubmitPoprtfolio");  
                   } 
          } 
       } 
   }
   ChangeCheckAllPort(false);
   if (!In)
    {
        alert(ErrStrings[77]);
        return false;   
    }
   
  
}


function resetArray()
{
    myProviders = new Array();
    ArrayIndex  = 0;
}

function MultyaddSubmitPoprtfolio(ret)    
{
     if (ret=="OkALL")
        {
         GetPortfolioDataAdd(1);
         OpenMultiPortPer(0);
         ChangeApplayAdd(1);
         visibaleAjaxPortPerAdd(0);    
        }
        
        
        
}


function PotPerEditItem(DataItemX)   
 { 
        GridPortPer.editCancel();
        if(eval(PerEditRow)==eval(DataItemX))
              {
               PerEditRowActive=false;
                PerEditRow=-1;
                return;
              } 
         if(PerEditRowActive==true)
             {
               GridPortPer.editCancel();
             }
            PerEditRowActive=true;
            PerEditRow=DataItemX;
            insertTableRowPortPer("PortPer1_GridPortPer_row_"+ DataItemX , DataItemX);
}



function GetSystemInfoPortPer(DataItemX)
{
    var URL = "SystemInfo.aspx?SystemID=" + GridPortPer.get_table().getRow(DataItemX).getMember('SystemID').get_text();
    window.open(URL,"SystemInfo","left=50,top=50,scrollbars=1,menubar=0,resizable=0,toolbar=0,width=665px,height=400");
}


   function onPagePortPer(newPage)
  {
    // delay call so that Grid's client properties have their new values
    setTimeout("showCustomFooterPortPer();",FooterDelay);

    return true;
  }

  function onLoadPortPer()
  {
    showCustomFooterPortPer();
  }
  
  function GridPortPer_Load(sender, eventArgs)
   {
    showCustomFooterPortPer();
  }
  function showCustomFooterPortPer()
  {
    var gridId = "PortPer1_GridPortPer";
    
    if (isdefined(gridId))
    {
      
     
      var Page  = "Page";
      var of    = "of";
      var items = "items";
      var First = "Orit";
      var Last = "Inbal";
      
      var pagerSpan = 5; // should be at least 2
      var cssClass = "GridFooterText";

      var footer = buildPager(eval(gridId), pagerSpan, cssClass);
      document.getElementById("tdPagerPortPer").innerHTML = "<div style='white-space:nowrap;display:inline;' class=\"" + cssClass + "\">" + footer + "</div>";

      footer = buildPageXofY(eval(gridId));
      document.getElementById("tdIndexPortPer").innerHTML = "<div class=\"" + cssClass + "\">" + footer + "</div>";
    }
    else 
    {
      setTimeout("showCustomFooterPortPer();", FooterDelay);
    }
  }


  function GetPortperData() {
      var rangeToWs = "0";
      var preDefineDate = "1";
      var index = document.frmWC.PortPer1_TimeFrame.selectedIndex;
      var Range = document.frmWC.PortPer1_TimeFrame.options[index].value;
      document.body.style.cursor = "default";
      searchPairs = document.frmWC.PortPer1_PortDDInstrumentList.options[document.frmWC.PortPer1_PortDDInstrumentList.selectedIndex].value;
      searchSystems = document.frmWC.PortPer1_PortDDSystemsList.options[document.frmWC.PortPer1_PortDDSystemsList.selectedIndex].value;
      var Pips = document.frmWC.PortPer1_PortDDPips.options[document.frmWC.PortPer1_PortDDPips.selectedIndex].value;
      var MDD = document.frmWC.PortPer1_PortDDMDD.options[document.frmWC.PortPer1_PortDDMDD.selectedIndex].value;
      var RAR = document.frmWC.PortPer1_PortDDRAR.options[document.frmWC.PortPer1_PortDDRAR.selectedIndex].value;
      var MSP = document.frmWC.PortPer1_PortDDMSP.options[document.frmWC.PortPer1_PortDDMSP.selectedIndex].value;
      var TradesCount = document.frmWC.PortPer1_PortDDTrades.options[document.frmWC.PortPer1_PortDDTrades.selectedIndex].value;


      var prmRandom = Math.floor(Math.random() * 10000000);
      var URL = "ServerAjax/PerformanceAjaxGetData.aspx?Pips=" + Pips + "&MDD=" + MDD + "&RAR=" + RAR + "&MSP=" + MSP + "&TradesCount=" + TradesCount + "&rangeToWs=" + rangeToWs + "&preDefineDate=" + preDefineDate + "&prmRandom=" + prmRandom + "&Range=" + Range;
      document.body.style.cursor = "wait";
      requestPerformance = true;
      repSubmit(URL, "LoadPortperData");

  }



  function portperFilterOpen() {

      var el = document.getElementById("divPorPerFilter");
      var gif = document.getElementById("imgPortPerFilter");
      var text = document.getElementById("spnPortPerFilter");

      if (el.style.display != "none") {
          gif.src = "Images/open_lng_ico.png";
          el.style.display = "none";
          text.innerHTML = "&nbsp;" + ErrorMessage(127);
          document.frmWC.PortPer1_PortDDMSP.selectedIndex = 0;
          document.frmWC.PortPer1_PortDDSystemsList.selectedIndex = 0;
          document.frmWC.PortPer1_PortDDInstrumentList.selectedIndex = 0;
          document.frmWC.PortPer1_PortDDTrades.selectedIndex = 0;
          document.frmWC.PortPer1_PortDDRAR.selectedIndex = 0;

      }
      else {
          text.innerHTML = "&nbsp;" + ErrorMessage(128);
          gif.src = "Images/close_lng_ico.png";
          el.style.display = "inline";
      }
  }

  function PortPerReset() {
      var el = document.getElementById("divFilter");
      el.style.display = 'none';

      document.frmWC.PortPer1_TimeFrame.selectedIndex = 1;
      document.frmWC.PortPer1_PortDDPips.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDMDD.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDMSP.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDSystemsList.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDInstrumentList.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDTrades.selectedIndex = 0;
      document.frmWC.PortPer1_PortDDRAR.selectedIndex = 0;

  }