﻿ var timerReset;
 var timerUpdate;
 var i1;
 var i2;
 var i3;
 var i4;
 var U;
 var ShowFieldsDef='AssetName.1,AssetCode.1,OperPlaceName.1,LastUpdate.1,LastPrice.1,DeltaPrev.1,Bid.1,Offer.1,OpenPrice.1,LowPrice.1,HighPrice.1,Waprice.1,Numtrades.1,VolToday.1,ValToday.1';
 var ShowFieldsTitleDef='Инструмент,Код,Площадка,Дата,Посл.,Изм.%,Спрос,Предл.,Откр.,Мин.,Макс.,Средн.,Кол-во сдел.,Объём (шт.),Объём (вал.)';
 
 //var calendar;
// var win;
 var ImagePath;
function InitData() {
    
   setPositionForSetup();
    //checkBrowser();
    preloadImages();
    LoadSetup();
    HideArrows();
    //settimer();
    sortTable('', 0);

}
function setPositionForSetup()
 {

    var s = document.getElementById('MarketSetup');
    var cal = document.getElementById("calendarImage");
    if (s && cal) {
        s.style.left = (relPosX('calendarImage') + 90) + 'px'; s.style.top = relPosY('calendarImage') + 'px'; 
    } 
}
function CloseCal() {
   
    if (calendar != null) {
        calendar.hide();
    }
}
function showSetup(e) {

    var xp = e.clientX - 100;
    if (!win) {
        win = new Ext.Window({
            title: 'Дополнительные настройки',
            //applyTo: 'd2',
            contentEl: 't22',
            x: xp,
            y: e.clientY,
            layout: 'fit',
            width: 200,
            autoHeight: true,
            closeAction: 'hide',
            resizable: false,
            plain: true,
            modal: true,
            buttons: [{
                text: 'OK',
                handler: function() {
                    ProcessFields(); win.hide();
                }
            }, {
                text: 'Отмена',
                handler: function() {
                    win.hide();
                }
}]
            });
        }
        win.show();
        var pr = GetCookie('StockDataRefresh');
        var p = document.getElementById('period');
       
        if (p != null) {
            if (pr!=null&&pr != '0') {
                document.getElementById('aupdate').checked = true;
                settimer();
            }

        }

    }

function newImage(arg) 
{ 
   rslt = new Image(); 
   rslt.src = arg; 
   return rslt; 
} 
function ReverseImage(elm1) {
    var elm;
   
    elm = document.getElementById(elm1);
  if(elm.src==i3.src)
  {
   elm.src=i4.src;
  }
  else
  {
    elm.src=i3.src;
  }
}
function GetCookie(name) 
{
    var cookie = " " + document.cookie;
       var search = " " + name + "=";
       var setStr = null;
       var offset = 0;
       var end = 0;
       if (cookie.length > 0) {
            offset = cookie.indexOf(search);
               if (offset != -1) {
                       offset += search.length;
                       end = cookie.indexOf(";", offset)
                       if (end == -1) {
                               end = cookie.length;
                       }
                       setStr = unescape(cookie.substring(offset, end));
               }
       }
       return(setStr);

}
function RestoreShowFields(fl)
{
 var fs=fl.split(',');
 var fst=ShowFieldsTitleDef.split(',');
 var fsd=ShowFieldsDef.split(',');
 var tr;

 for(i=0;i<fs.length;i++)
 {
    tr=document.getElementById('tsetup').rows[i+1];
    //fw
   
    if(isFF)tr.childNodes[2].firstChild.value=fs[i].substr(0,fs[i].length-2);
    //
    //ie
    if(isIE)tr.childNodes[1].firstChild.value=fs[i].substr(0,fs[i].length-2);
    //
    if(fs[i].indexOf(".1")!=-1)
    {
    //ff
    if(isFF)tr.childNodes[4].firstChild.lastChild.src=i4.src; 
        //
    //ie
    if (isIE) tr.childNodes[2].firstChild.lastChild.src = i4.src; 
       //     
    }
    else
    {
    //ff
      if(isFF)tr.childNodes[4].childNodes[0].firstChild.src=i3.src; 
        //
        //ie
      if(isIE) tr.childNodes[2].childNodes[0].firstChild.src=i3.src; 
       //
    }
    //ie
    for(j=0;j<fsd.length;j++)
    {
        if(fs[i].substr(0,fs[i].length-2)==fsd[j].substr(0,fsd[j].length-2))
        {
            if(isIE)tr.childNodes[3].innerText=fst[j];
            if(isFF)tr.childNodes[5].innerText=fst[j];
            break;
        }   
    }
        
 }
}
function LoadSetup() {
 
   if(GetCookie('StockDataFList')!=null)
   {
    document.getElementById('ShowFiled').value=GetCookie("StockDataFList");
    RestoreShowFields(document.getElementById('ShowFiled').value);
   }
   else
   {
    document.getElementById('ShowFiled').value=ShowFieldsDef;
   }
   if(GetCookie('StockDataSortField')!=null)
   {
    document.getElementById('Sort').value=GetCookie("StockDataSortField");
   }
   if (GetCookie('StockDataRefresh') != null) {
       var pr = GetCookie('StockDataRefresh');
       var p = document.getElementById('period');

      
       if (p != null) {

           if (pr != '0') {
               document.getElementById('aupdate').checked = true;
               //document.getElementById('period').value = pr;
           }

       }



   }
   else {
       document.getElementById('aupdate').checked = false;
   } 
   aton.msk.aton.ru.StockData.GetTimerInterval(onSuccessI);
}
function onSuccessI(res) {

    if (res != null) {
        document.getElementById('period').value = res;
        settimer();
    }
}
function HideArrows()
{
var RowCount=document.getElementById('tsetup').rows.length;
var r=document.getElementById('tsetup').rows[1];
if(isIE)
{
    document.getElementById('tsetup').rows[1].childNodes[4].style.display='none';
    document.getElementById('tsetup').rows[RowCount-1].children[5].style.display='none';
}
if(isFF)
{

    document.getElementById('tsetup').rows[1].childNodes[8].lastChild.style.display='none';
    document.getElementById('tsetup').rows[RowCount-1].childNodes[10].lastChild.style.display='none';
}

}
function ProcessFields()
{
    var s='';
    var tr;
   
 
    for(i=1;i<document.getElementById('tsetup').rows.length;i++)
    {
        tr=document.getElementById('tsetup').rows[i];
      if(isIE)
      {
          if (tr.childNodes[2].firstChild.lastChild.src == i4.src)
        {
            s=s+document.getElementById('tsetup').rows[i].childNodes[1].firstChild.value+'.1,';
        }
        else
        {
            s = s + document.getElementById('tsetup').rows[i].childNodes[1].firstChild.value + '.0,';
        }
      }
      if(isFF) {
       
       if (tr.childNodes[4].childNodes[0].firstChild.src == i4.src)
        {
           s=s+document.getElementById('tsetup').rows[i].childNodes[2].firstChild.value+'.1,'; 
        }
        else
        {
            s=s+document.getElementById('tsetup').rows[i].childNodes[2].firstChild.value+'.0,';
        }
        
      }
    }
     
    if(s.length>0)
    {
        document.getElementById('ShowFiled').value=s.substr(0,s.length-1);
        document.getElementById('d2').style.display='none';       
        sortTable('',1);
        settimer();
    }
}
function MoveUp(elm1) {
    var elm;
   
    elm = document.getElementById(elm1);
    var index;
    if (isIE) index = elm.parentElement.parentElement.parentElement.rowIndex;
    if (isFF) index = elm.parentNode.parentNode.parentNode.rowIndex;
    var RowCount=document.getElementById('tsetup').rows.length;
    MoveRow(document.getElementById('tsetup').rows[index],document.getElementById('tsetup').rows[index-1]);
    if(index==2)
    {
    if(isIE)
        {
       document.getElementById('tsetup').rows[1].children[4].style.display='none';      
       document.getElementById('tsetup').rows[2].children[4].style.display='block'; 
       }
       if(isFF)
       {
         document.getElementById('tsetup').rows[1].childNodes[8].childNodes[1].style.display='none';
          document.getElementById('tsetup').rows[2].childNodes[8].childNodes[1].style.display='block';
       }      
    }
    else if(index==RowCount-1)
    {  
    if(isIE)
    {
        document.getElementById('tsetup').rows[RowCount-1].children[5].style.display='none';      
        document.getElementById('tsetup').rows[RowCount-2].children[5].style.display='block';  
        }
        if(isFF)
        {
         document.getElementById('tsetup').rows[RowCount-2].childNodes[10].childNodes[0].style.display='block';
         document.getElementById('tsetup').rows[RowCount-1].childNodes[10].childNodes[0].style.display='none';
        }
              
    }
    else
    {         
       
    }
    
    
}
function MoveRow(curRow,newRow)
{
var crow_src;
var nrow_src;
var crow_title;
var nrow_title;
var crow_hf;
var nrow_hf;
if(isIE)
{
  crow_src=curRow.childNodes[2].firstChild.lastChild.src;
  nrow_src = newRow.childNodes[2].firstChild.lastChild.src;
  crow_title=curRow.childNodes[3].innerText;
  nrow_title=newRow.childNodes[3].innerText;
  crow_hf=curRow.childNodes[1].firstChild.value;
  nrow_hf=newRow.childNodes[1].firstChild.value; 
  curRow.childNodes[2].firstChild.lastChild.src=nrow_src;      
  newRow.childNodes[2].firstChild.lastChild.src=crow_src; 
  curRow.childNodes[3].innerText=nrow_title;
  newRow.childNodes[3].innerText=crow_title; 
  curRow.childNodes[1].firstChild.value=nrow_hf;
  newRow.childNodes[1].firstChild.value=crow_hf;
 }
 if(isFF) {

 crow_src = curRow.childNodes[4].firstChild.firstChild.src;
 nrow_src = newRow.childNodes[4].firstChild.firstChild.src;
  crow_title=curRow.childNodes[6].firstChild.nodeValue;
  nrow_title=newRow.childNodes[6].firstChild.nodeValue;
  crow_hf=curRow.childNodes[2].firstChild.value;
  nrow_hf=newRow.childNodes[2].firstChild.value;
  curRow.childNodes[4].childNodes[0].firstChild.src = nrow_src;
  newRow.childNodes[4].childNodes[0].firstChild.src = crow_src; 
  curRow.childNodes[6].firstChild.nodeValue=nrow_title;
  newRow.childNodes[6].firstChild.nodeValue=crow_title; 
  curRow.childNodes[2].firstChild.value=nrow_hf;
  newRow.childNodes[2].firstChild.value=crow_hf;
 }
 
}
function MoveDown(elm1)
{
    var index;
    var elm;
    elm = document.getElementById(elm1);
    if (isIE) index = elm.parentElement.parentElement.parentElement.rowIndex;
    if (isFF) index = elm.parentNode.parentNode.parentNode.rowIndex;
    var RowCount=document.getElementById('tsetup').rows.length;
    MoveRow(document.getElementById('tsetup').rows[index],document.getElementById('tsetup').rows[index+1]);
    
    if(index==RowCount-2)
    { 
      if(isIE)
      {       
        document.getElementById('tsetup').rows[RowCount-2].children[5].style.display='block';
        document.getElementById('tsetup').rows[RowCount-1].children[5].style.display='none';
      }
      if(isFF)
      {
         document.getElementById('tsetup').rows[RowCount-2].childNodes[10].childNodes[0].style.display='block';
         document.getElementById('tsetup').rows[RowCount-1].childNodes[10].childNodes[0].style.display='none';
      }
        
    }
    else if(index==1)
    {       
        
        if(isIE)
      {
        document.getElementById('tsetup').rows[1].children[4].style.display='none';
        document.getElementById('tsetup').rows[2].children[4].style.display='block';
        }
        if(isFF)
        {
          document.getElementById('tsetup').rows[1].childNodes[8].childNodes[1].style.display='none';
          document.getElementById('tsetup').rows[2].childNodes[8].childNodes[1].style.display='block';
        }
    }
    else
    {
       
        //document.getElementById('tsetup').moveRow(index,index+1);
    }



}
function changeQDate(b) {
    var d = new Date();
    var curD = new Date(d.getFullYear(),d.getMonth(),d.getDay());
    var selD = new Date(b.getFullYear(), b.getMonth(), b.getDay());
    var d3 = document.getElementById('aupdate');
   
        if (d3!=null) {
            if (curD.toString('Y/M/D') == selD.toString('Y/M/D')) {
                d3.disabled = false;
            }
            else {
                d3.disabled = true;
            }
        }
    
    var a1 = document.getElementById('dt_a')
    a1.value = b.dateFormat('d.m.y');
    changeDateLabel(a1.value);
    settimer();
    sortTable('', 0);
    return true;
}
function preloadImages() 
{ 
   i1 = newImage("/Images/arrow_up.gif");
   i2 = newImage("/Images/arrow_down.gif");
   i3 = newImage("/Images/checkbox_active.gif");
   i4 = newImage("/Images/checkbox_selected.gif");
   i5 = newImage("/Images/graf.jpg");
   i6 = newImage("/Images/del.gif");  
   
}

function settimer()
{
var interval;
  if(timerUpdate!=null)
      {
       window.clearInterval(timerUpdate);
       timerUpdate=null;
       
      }
      interval = '0';

      if (document.getElementById('aupdate').checked == true && document.getElementById('aupdate').disabled==false)
  {
    interval=document.getElementById('period').value*1000;
}
  
  var d1=document.getElementById('dt_a');
  if(interval>0)
  {  
    timerUpdate = window.setInterval("sortTable('',0)", interval); 
  }
  else
  {  
   ResetTable();	
  } 
  
 
}

function MarketSuccess(result) {

    var m = document.getElementById('MarketT');
   timerReset=setTimeout("ResetTable()",1500);
   if(m!=null)
   {
	if (result.ErrorMessage.length!=0)
	{
	m.innerHTML=result.ErrorMessage;
	}
	else
	{
    m.innerHTML=result.Table;
    
    }
   }
}
function ResetTable() {	

 if(isFF)
 {
     var t;

   var tb=document.getElementById('MarketT').firstChild;
   if(tb!=null)
   {
	 t=tb.rows;
	if(t!=null)
	{	
	for(i=1;i<t.length;i++)
	{
	    for(j=0;j<t[i].cells.length;j++)
	    {
	       t[i].cells[j].style.backgroundColor=''; 
	    } 		
	  
	}
	}
	}	
 }
 if (isIE) document.getElementById('MarketT').innerHTML = document.getElementById('MarketT').innerHTML.replace(/#DFDFFF/gi, "");							
   if(timerReset!=null)
   {
   timerReset=null;
   }
}
function MarketFail(res)
{

}

function sortTable(sf,ic)
{

    if(sf.length>0)
    {
        document.getElementById('Sort').value=sf;
    }
    else
    {
     sf= document.getElementById('Sort').value;
    }
    var d1=document.getElementById('dt_a');
    var ShowF = document.getElementById('ShowFiled').value;
    var per = document.getElementById('period').value;
    if (document.getElementById('aupdate').checked == false) per = 0;
    aton.msk.aton.ru.StockData.GetMarket('', '', ShowF, sf, per, d1.value, ic, ImagePath,U,MarketSuccess, MarketFail);
}
function getElementPosition(elemId)
{
    var elem = document.getElementById(elemId);
	
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}
function DeleteAsset(item)
{
    
    var sort=document.getElementById('Sort').value;
     var ShowF=document.getElementById('ShowFiled').value;
     var d1=document.getElementById('dt_a');
    if(item.length>0)
    {
        aton.msk.aton.ru.StockData.GetMarket('', item, ShowF, sort, document.getElementById('period').value, d1.value, 0, ImagePath,U, MarketSuccess, MarketFail);
    }
}

function onFault(res)
{

}
