﻿
Sticker = function() {
    this.view = '';
    this.zal = '';
    this.sel = '';
    this.searchExpr ='';
    this.dateFilter ='';
    this.containerId = '';   
    this.block = '';
    this.dvals = '';
	this.stickerId = '';
	this.pid = '';
	this.nparams = '';
	this.vzal = '';
	this.vsel = '';
	this.nadpis = '';
	this.records = '';
	this.paging = '';
	this.harmId = '';
	this.harmSel = '';
	this.fzal = false;
  this.filterObjId = '';
}


Sticker.DrillLvl = 0 ;
Sticker.TmpSel = '';
Sticker.TmpDVals = '';
Sticker.TmpVal = '';
Sticker.TmpObj = null;

Sticker.prototype =
{
    init: function(jsonCfg){
        if (jsonCfg != null) {
        
            this.stickerId = jsonCfg.stickerId;
            this.containerId = jsonCfg.containerId;
						      this.pid = jsonCfg.PID;
			      this.bindVars = jsonCfg.bindVars;
			      if( jsonCfg.nadpis != null )                         			      
                this.nadpis = jsonCfg.nadpis;                    
			      if( jsonCfg.block != null)
			        this.block = jsonCfg.block;
			      if( jsonCfg.nparams != null)
			        this.nparams = jsonCfg.nparams.replace(/\^/g,'\+');			      
            if( jsonCfg.zal != null)
                this.zal = jsonCfg.zal;
            if( jsonCfg.sel != null)
                this.sel = jsonCfg.sel;
            if( jsonCfg.searchExpr != null)
                this.searchExpr = jsonCfg.searchExpr;
            if( jsonCfg.dateFilter != null)
                this.dateFilter = jsonCfg.dateFilter;
            if( jsonCfg.dvals != null)
                this.dvals = jsonCfg.dvals;                    
            if( jsonCfg.vsel != null)
                this.vsel = jsonCfg.vsel;    
            if( jsonCfg.vzal != null)
                this.vzal = jsonCfg.vzal;   
           if( jsonCfg.records != null)
                this.records = jsonCfg.records;   
            if( jsonCfg.paging != null)
                this.paging = jsonCfg.paging;               
            if( jsonCfg.harmId != null)
                this.harmId = jsonCfg.harmId;   
			if( jsonCfg.view != null)
			    this.view = jsonCfg.view;   				
			
            if( jsonCfg.harmSel != null)
                this.harmSel = jsonCfg.harmSel;   
			if( jsonCfg.fzal != null)
                this.fzal = jsonCfg.fzal;   
   if( jsonCfg.filterObjId != null)
                this.filterObjId = jsonCfg.filterObjId;  
                
            if( jsonCfg.chainEvents == true)
            {
			        barISPage.chainEvent('refreshDataSource','sticker' + this.pid + '.Refresh');
				      barISPage.chainEvent('tdoklChanged','sticker' + this.pid + '.Refresh');
				   }
			      this.GetFilterInfo();
		    }
	  },
	  
	  
	Refresh: function(locationId)
	{	    	    	    
	    if(this.containerId == null )
	      {
			alert(this.stickerId);
		  }
		  
	    var stkLoc = 'stk' + this.containerId;
        if( typeof(locationId) !== 'undefined' && locationId != null) 
        {	
            stkLoc = locationId;
	    }		
		if( document.getElementById(stkLoc) == null)
		{
		    /*alert('Nie je mozne obnovit udaje do ' + stkLoc+'. Zmazavam objekt.');*/

            this.destroy();
            return;
		} 
		else 
	    Sticker.Refresh(this.containerId,this.stickerId,this.pid,this.bindVars,this.getTParams(),stkLoc);
	},	
	SetNadpis : function(txt)
	{

	this.nadpis = txt;
	  if(txt)
	  {
	  		if($get('mobj'+this.stickerId+'_dragno') != null)
				dragID = 'mobj'+this.stickerId+'_dragno';
			else
				dragID = 'mobj'+this.stickerId+'_drag';
		    
		    if($get(dragID) != null)
		    {
			    if($get('Hdr'+this.stickerId) != null)
			    {
				    if($get('Hdr'+this.stickerId) != null)
				    {
				       if(typeof StHeadTxt == 'function') StHeadTxt(dragID,''+$get('Hdr'+this.stickerId).innerHTML);
				       $get('Hdr'+this.stickerId).style.display = "none";
				    }
			    }      
			    else
			    {
				      if(typeof StHeadTxt == 'function') StHeadTxt(dragID,txt);
			    }
			}
			else //iPhone
			{
				if(typeof StHeadTxt == 'function') StHeadTxt('H'+this.stickerId,txt);
			}
		}	
	},		
  Search: function(expression)
	{	    	    	    
	  this.searchExpr = expression;
	  this.Refresh();
	}
	,
	
	SetDateFilter: function(expression,refresh)
	{
	  this.dateFilter = expression;
	  if( refresh) 
	    this.Refresh();	
	},
	
	RemoveBlock : function(blockId)
	{
	  var blockArray = this.block.split(',');
	  var found = false;
	  for( i = 0 ; i < blockArray.length -1 ; i++ )
	  {
	      if( blockArray[i] == blockId  || ( blockId == null && blockArray[i+2] != '' ) )
	      {
	        blockArray[i] = null;
	        blockArray[i+1] = null;
	        blockArray[i+2] = null;
	        found = true;
	        break;
	      }
	 }
	 
	 if(found)
	 {
		this.block = blockArray.join(',');
		this.Refresh();
	  }
	}
	,	
	ResetFilter : function(filterId)
	{
	
	  if( filterId == null || filterId == 'ALL' )
	  {
	      this.searchExpr ='';
	      this.dateFilter ='';	
	      this.RemoveBlock('');  
	  }
	  else if( filterId == 'DATE')
	  {
	      this.dateFilter ='';
	  }
	  else if( filterId == 'FTX' )
	  {
	    this.searchExpr ='';
	  }
	  else
	  {
	    this.RemoveBlock(filterId);
	  }	  
	  this.Refresh();	
	},
  GetFilterInfo : function()
	{
	  var result = new Array();
	  if( this.searchExpr != '' )	  
	    result[result.length] = { Id: 'FTX', Value: this.searchExpr, Label: '<img src="images/system/iphone/52x52/filter.png" title="FullText">'};
	  
	  if( this.dateFilter != '' )	  
	    result[result.length] =  { Id: 'DATE', Value: this.dateFilter, Label: '<img src="images/system/iphone/32x32/c/c_den.png" title="Dátum">'}; 
	  
	  var blockData = this.block.split(',');	  
	  for(i=0; i < blockData.length-2; i+=3)
	  {
	    if( blockData[i+2] != '' )
	    {
	        result[result.length] = { Id: blockData[i], Value: blockData[i+1], Label: blockData[i+2]}; 	    
	    }	  
	  }
		  
	  this.ShowFilterInfo(result);
	},
	
	ShowFilterInfo : function(res)
	{
	var fInfo = '';
	
	  textDate = new Array;
	  textDate[1] = 'Dnes'; 
	  textDate[8] = 'Včera'; 
	  textDate[2] = 'Akt. týždeň'; 
	  textDate[3] = 'Min. týždeň'; 
	  textDate[4] = 'Akt. mesiac'; 
	  textDate[7] = 'Min. mesiac'; 
      textDate[10] = 'Akt. kvartál'; 
      textDate[11] = 'Min. kvartál';  
      textDate[6] = 'Akt. rok';  
      textDate[9] = 'Min. rok'; 
	
	  for(i=0; i < res.length; i++)
	  {  
            fInfo += res[i].Label +': <a href="javascript: void(0)" onclick="sticker'+this.pid+'.ResetFilter(\''+res[i].Id+'\');">';
            if(res[i].Id == 'DATE')
                fInfo += textDate[res[i].Value]; 
	        else
	            fInfo += res[i].Value;
	        fInfo += '</a>, ';
	  }
	  
	   if(getE('sticker'+this.pid+'fInfo') != null && fInfo) 
	    getE('sticker'+this.pid+'fInfo').innerHTML = fInfo;
	   
	  //return fInfo;
	},
		
	Block : function( blockId,blockVal,blockLabel )
	{
	  
	  
	  var blockArray = this.block.split(',');
	  var found = false;
	  
	  for( i = 0 ; i < blockArray.length -2 ; i++ )
	  {
	      if( blockArray[i] == blockId )
	      {
	        found  = true;
	        blockArray[i+1] = blockVal;
	        blockArray[i+2] = blockLabel;
	        break;	        
	      }
	  }
	  
	  if( found )
	      this.block = blockArray.join(',');
	  else 
	  { 
	      if( this.block != '' )
	          this.block = this.block + ',';	  
	      this.block = this.block + blockId + ',' + blockVal + ',' + blockLabel;
	   }   
	 this.Refresh();
	    
	},	
	activateFilterZal : function(sel)
	{		
		this.sel =sel ;		
	},	
	changeSel : function(sel)
	{		
		if(this.fzal)
			this.SetNParam('search','',false);
		this.sel = sel;
		this.dvals = ''; //zrusime dd
if( sel == 9999 )
		{
		  if( this.filterObjId != '')
		  {
		    var filterObj = getE(this.filterObjId);
		    if( filterObj != null )
		    {
		        filterObj.style.display = '';
		        var inputs = filterObj.getElementsByTagName('INPUT');
		        if (inputs != null && inputs.length > 0 )
		        {
		            if( inputs[0].value != '' )		            
        		        inputs[0].select();
        		    else
        		        inputs[0].focus();
		        }
		        
		        var tables = getE('stk' + this.pid).getElementsByTagName('TABLE');
		        for(var rowid = 0; rowid < tables.length; rowid++)
		        {
		          if( tables[rowid].className.substr(0,7) == 'infoTbl' )
		          {		              
                tables[rowid].parentNode.removeChild(tables[rowid]);
              }		        
		      }
		    }
		  }
		}
		else
		{
		   this.Refresh();	
    }
	},	  	
	openValueMenu : function()
	{
	 InLinePopUp('VZAL' + this.vzal)
	},
	changeValue : function(vsel)
	{
	  if( this.vsel != vsel )
	  {
	    this.vsel = vsel;
	    this.Refresh();
	  }
	 
	},
	SetNParam : function(par,val,refresh)
	{
	 var apars = this.nparams.split(';');
	 var found = false;
	  for( i  = 0 ; i < apars.length; i+=2 )
	  {
	    if( apars[i] == par )
	    {
	      apars[i+1] = val;
	      found = true;
	    }
	  }	  
	  if(!found)
	  {
	    if( this.nparams == '' )
	        this.nparams = par+';'+val;
	    else
	        this.nparams += ';' + par+';'+val;	      
	  }
	  else
	  {   
	      this.nparams = apars.join(';');
	  }
	  
	  if( refresh == true )
	        this.Refresh();	
	},
	GetNParam : function(par)
	{
	  var apars = this.nparams.split(';');
	  for( i  = 0 ; i < apars.length; i+=2 )
	    if( apars[i] == par )
	        return apars[i+1];
	  return null;	
	},
	GetNParamNum : function(par)
	{
	  return Number(this.GetNParam(par));
	},
	GetNParamDT : function(par)
	{
	  var value = this.GetNParam(par);	  
	  if( value == '' || value == 'SYSDATE')
	    return new Date();   //.format('dd.MM.yyyy');
	  else
	  {
	    var dateA = value.split('.');
	    return new Date(dateA[1] + '/' + dateA[0] + '/' + dateA[2]);	  
	  }
	},
	SetNParamDT : function(par,valDT,refresh)
	{	  	
	  var stringValue = (new Date(valDT)).format('dd.MM.yyyy');
	  this.SetNParam(par,stringValue,refresh);
	},
	getPlace : function()
	{
	    return document.getElementById('place' + this.pid );
	},
	getTParams : function()
	{
	
	  var tparams = this.nparams;
	  if( tparams.length > 0 )
	      tparams =  tparams + ';';	  
	  tparams = tparams + 'pid;' + this.pid + ';' ;
	  if( this.zal != '' )
	      tparams = tparams + 'nzal;' + this.zal + ';'; 
	  if( this.sel != '' )
	      tparams = tparams + 'nsel;' + this.sel + ';'; 	
    if( this.searchExpr != '' )
	      tparams = tparams + 'search;' + this.searchExpr + ';'; 	
	  if( this.dateFilter != '' )
	      tparams = tparams + 'date;' + this.dateFilter + ';'; 	
	  if( this.block != '' )
	      tparams = tparams + 'block;' + this.block + ';'; 	
	  if( this.dvals != '' )
	      tparams = tparams + 'dvals;' + this.dvals + ';'; 	
	  if( this.vzal != '' )
	      tparams = tparams + 'vzal;' + this.vzal + ';'; 	
	  if( this.vsel != '' )
	      tparams = tparams + 'vsel;' + this.vsel + ';'; 	
	  if( this.records != '' )
	      tparams = tparams + 'records;' + this.records + ';'; 	
	  if( this.paging != '' )
	      tparams = tparams + 'paging;' + this.paging + ';'; 	
	  if( this.harmId != '' )
	      tparams = tparams + 'harmId;' + this.harmId + ';'; 	
    if( this.harmSel != '' )
	      tparams = tparams + 'harmSel;' + this.harmSel + ';'; 	
	  return escape(tparams).replace(/\+/g,'^');
	},	
	openHarm : function()
	{	
	  InLinePopUp('HARM' + this.pid)
	},
	changeHarm : function(sel)
	{		
	  this.harmSel = sel;
	  this.Refresh();
	},
	chooseDrill : function(val,clickObj,curSel,curVals)
	{		  	  
	  Sticker.TmpSel = curSel;
    Sticker.TmpDVals = curVals;
    Sticker.TmpVal = val;
    Sticker.TmpObj = clickObj;
	InLinePopUp('DDMenu' + this.stickerId)
	},
	
	
	drillDown : function(sel,focus)
	{
	
	  
  
	  
	  var loca = null;
	  var locationId = null;
	  var curSel = sel;
	  if( focus == true )
	  {	  
	    sel  = -sel;	    
	  }
	  else 
	  {
	    var locationId = 'ddtest' + Sticker.DrillLvl;
	    Sticker.DrillLvl = Sticker.DrillLvl + 1;
	    addRow(Sticker.TmpObj,locationId);
	    this.drillColor();       //vyfarbim rozbaleny riadok	  
	    loca = $get(locationId);
	    if (loca == null )
	    {
	      alert('Niekde to zakapalo');
	      return;
	    }
	    loca.style.paddingLeft = ''; // addrow
	  }
	  
	  
	  

	 this.sel = Sticker.TmpSel + ',' + curSel;
	 if( Sticker.TmpDVals == '' )
	      this.dvals =  Sticker.TmpVal;
	 else
	      this.dvals = Sticker.TmpDVals + ',' + Sticker.TmpVal;
	      
	 this.Refresh(locationId);	
	 
	},
	
	drillColor : function()
	{
	 try
	 {
	  var ts = Math.round((new Date()).getTime() / 1000);
	  
	  Sticker.TmpObj.style.display = "none";
	  Sticker.TmpObj.setAttribute("id","xDd"+ts);
	  
	  xImg = document.createElement('img');
	  xImg.setAttribute("src","Images/System/iphone/32x32/cancel.gif");
	  xImg.setAttribute("id","xImg"+ts);
      xImg.setAttribute("onclick","sticker"+this.pid+".drillColorNo(this);");
	  Sticker.TmpObj.parentNode.insertBefore(xImg, Sticker.TmpObj);
	  
	  Sticker.TmpObj.parentNode.parentNode.style.background = "#FFFFCC";
	  ValueTxt = Sticker.TmpObj.parentNode.parentNode.childNodes;
	  ValueTxt[ValueTxt.length-1].style.color = "red";
	  if(ValueTxt[ValueTxt.length-1].childNodes[0] != null) ValueTxt[ValueTxt.length-1].childNodes[0].style.color = "red";
	  }
	  catch(e)
	  {;}
	},
	
	drillColorNo : function(thisObj)
	{
		try{
		var p2 = thisObj.parentNode.parentNode.nextSibling; // del dd row
		p2.parentNode.removeChild(p2);
		
		thisObj.parentNode.removeChild(thisObj); // del img
		
		var TmpObj = $get('xDd'+thisObj.id.substr(4)); // pofarbim spat riadok
		TmpObj.style.display = '';
		TmpObj.parentNode.parentNode.style.background = "#fff";
	  
		ValueTxt = TmpObj.parentNode.parentNode.childNodes; // pofarbim spat text
		ValueTxt[ValueTxt.length-1].style.color = "black";
		if(ValueTxt[ValueTxt.length-1].childNodes[0] != null) ValueTxt[ValueTxt.length-1].childNodes[0].style.color = "black";
		}
		catch(e)
		{;}
	},	
	destroy : function()
	{	
		/*alert('Nalepka ' + this.stickerId + ' konci');*/
		barISPage.removeEvent('refreshDataSource','sticker' + this.pid + '.Refresh');
		barISPage.removeEvent('tdoklChanged','sticker' + this.pid + '.Refresh');
		eval('sticker' + this.stickerId + '=null');
	},	
	SubDetail : function( nid, nparams, location )
	{	
		iDetail(nid,nparams,'2',this.getPlace(location))	
	}
}


Sticker.Refresh = function(clientId,nid,pid,bindVars,tparams,location)
{  
   var nparams = '';
   if( typeof(tparams) !== 'undefined')
      nparams = tparams;
    var stkLoc = 'stk' + clientId;
   if( typeof(location) !== 'undefined' && location != null) 
   {	
        stkLoc = location;
	}
   Detail(nid,'$stateId;' +  barISPage.stateId + ';deviceMode;' + barISPage.deviceMode + ';pid;' + pid + ';reload;true;inlineMode;ajaxMode;objId;' + stkLoc + '$' + nparams);    
}


