
myAddsList = function()
{
    this.type="guest";
    this.rowTemplate = null;
    this.myAddsArray = [];
    this.init();
    
};

myAddsList.prototype = {
    changetype : function(type)
    {
        if(type == 'user')
        {
            Ext.get('my_adds_login_msg').dom.style.display="none";
            Ext.get('my_adds_list_div').dom.style.display="block";
            Ext.Ajax.request({
            url : hosturl +'MyAdds/myaddslist.aspx',
            success : function(a)
                        {
                            
                            var ans = eval(a.responseText);
                            if(ans[0] == true)
                            {
                             this.myAddsArray = ans[1];
                             this.type = "user";
                            }
                            else
                            {
                                this.myAddsArray = [];
                                alert(ans[1]);
                            }
                            this.drawPage(1);
                        },
            failure :function()
                        {   
                            //alert('כישלון בהוספת רשומה  אנא נסה שוב מאוחר יותר');
                        },
            scope : this 
            });
        }
        else
        {
            Ext.get('my_adds_login_msg').dom.style.display="block";
            Ext.get('my_adds_list_div').dom.style.display="none";
            this.myAddsArray = [];
            this.drawPage(1);
            this.type="guest";
        }
    },
    init : function()
    {
        this.createMsg();
        this.rowTemplate  = new Ext.Template(
                  '<div class="row">',
                  '<div class="{invert}">',
                        '<div class="my_adds-Active" >{Active}</div>',
                        '<div class="my_adds-PDate" >{PDate}</div>',
                        '<div class="my_adds-EntryWhen">{EntryWhen}</div>',
                        '<div class="my_adds-RoomNumber">{RoomNumber}</div>',
                        '<div class="my_adds-ShekelRent">{ShekelRent}</div>',
                        '<div class="my_adds-Street">{Street}</div>',
                        '<div class="my_adds-city">{city}</div>',
                        '<div class="my_adds-AssetType">{AssetType}</div>',
                        '<div class="my_adds-addType">{AddType}</div>',
                   '</div>',
                   '</div>'

        );
        this.rowTemplate = this.rowTemplate.compile();
        this.currentPage = 1;
        this.pageSize = 10;
        this.pageRows = [];
        this.target = Ext.get('x_my_adds_list');
        this.pageRows=[];
        
    },
    createMsg : function()
    {
        
        var msgform = new createForm({width:500, height:200,target:'my_adds_login_msg_right',title:'פרסום נכסים',blockScreen:false,closebtn:false,divClass:'msgtxt'});
        msgform.form.dom.style.position="relative";
        
        //divClass
        msgform.addDiv({style:'br',height:20});
        msgform.addDiv({text:'על מנת לפרסם נכסים יש להרשם לאתר:'});
        msgform.addDiv({style:'br',height:40});
        var btnrow;
        msgform.addDiv({style:'btn',height:22,width:50});
        btnrow = msgform.addDiv({style:'btn',height:22,width:210});
        msgform.addBtn({width:190,text:'כניסה/רישום',name:'enter',target:btnrow})
        btnrow = msgform.addDiv({style:'btn',height:22,width:200});
        msgform.addBtn({width:190,text:'פרסום נכס',name:'adv',target:btnrow})
        
        msgform.btnArray.adv.on('click',function(){window.location = hosturl + 'MyAdds/newAdd.aspx';});
        
        msgform.btnArray.enter.on('click',loginManager.showLoginForm,loginManager);
        
        msgform.show();
        this.msgform = msgform;
    },
    drawPage : function(pageNo)
    {
        
        for (var i = 0; i < this.pageRows.length; i++)
        {
            this.pageRows[i].rowId = null;
            this.pageRows[i].marker = null;
            this.pageRows[i].removeAllListeners();
            this.pageRows[i].remove();
            this.pageRows[i] = null;
            
        }
        
        this.pageRows=[];
        //this.myAddsArray
        
        var combo = Ext.get('mylistselect');
        combo.dom.onchange = null;
        combo.dom.options.length = 0;
        for (var i = this.pageSize; i <  this.myAddsArray.length + this.pageSize; i += this.pageSize )
          {
            var el = document.createElement('option');
            el.value = i/this.pageSize;
            el.text = i/this.pageSize;
            combo.dom.options[ i/this.pageSize-1] = el;
          }
              
        if ( combo.dom.options.length == 1 || combo.dom.options.length == 0)
                Ext.get('x-asset_list_page_selector').dom.style.display = 'none'
            else
                Ext.get('x-asset_list_page_selector').dom.style.display = 'block'; 
                
        this.target.dom.innerHTML = '';
        
        if(pageNo == 0 && this.currentPage )
        {
          pageNo = this.currentPage;
          if(pageNo >  combo.dom.options.length)
          {
             pageNo = combo.dom.options.length-1;
          }   
        }
        
        if ( this.myAddsArray.length > 0)
        {
            if (pageNo)
            {
                if (pageNo - 1 <= combo.dom.options.length)
                {
                 this.currentPage = pageNo;
                }
                else
                {
                 this.currentPage = 1;
                }
            }
            else
            {
                this.currentPage = 1;
            }
            if (combo.dom.options.length > 1) 
            {
                combo.dom.options.selectedIndex = this.currentPage - 1;
                combo.dom.onchange = this.drawPageOnChange.createDelegate(this);
            }
        }
        
        for (var i = (this.currentPage - 1) * this.pageSize; i < (this.currentPage) * this.pageSize && i <  this.myAddsArray.length; i++)
        {   
            this.addRow(this.myAddsArray[i],i+1);
        }
    },
    addRow: function (dataCell,index)
    {
        //debugger;
        var tbl = this.target.dom;
        if(index %2 ==0 )
        {
            dataCell['invert'] = 'row';
        }
        else
        {
            dataCell['invert'] = 'zeb';
        }
        var el = this.rowTemplate.append(tbl,dataCell);
        el.rowId = dataCell['id'];
        
        Ext.get(el).on('click',function(a,b,c)
        {
            //alert(this.rowId);
            window.location  = hosturl + 'MyAdds/newAdd.aspx?addid=' + this.rowId;
        }
        ,el);
        
        this.pageRows[this.pageRows.length] = Ext.get(el);
    },
    drawPageOnChange: function()
    {   
        var combo = Ext.get('mylistselect');
        var pageNo = combo.dom.options.selectedIndex + 1;
        this.drawPage(pageNo);
    }
    
    
    
};



