// remove add for no registerd users

AddRemover = function()
{
    this.searchForm = null;
    this.listForm = null;
    this.removeDialog = null;
    this.codeApproveFrom = null;
    this.init();
};

AddRemover.prototype = {
    init: function()
    {
        this.createSearchFrom();
    },
    showSearchBox : function()
    {      
        this.searchForm.show();
    },
    createSearchFrom : function()
    {   
        this.searchForm = new createForm({width:300,height:210,title:'הסרת מודעות',divClass:'removeAddText',analyticsname:'remove_add_search'});
        var msg = "אנו באתר זזים עושים את המיטב על מנת לספק <br>";
        msg+="לכם את המידע העדכני והמקיף ביותר.<br>";
        msg+="במידה וקיימת בלוח מודעה עם פרטיכם<br>";
        msg+="(מספר הטלפון שלכם) שלא אתם פרסמתם<br>";
        msg+="או שאתם מעוניינים להסירה<br>";
        msg+="ניתן לאתר את המודעה ולהסירה מהלוח.<br>";
        this.searchForm.addDiv({width:290, text:msg});
        this.searchForm.addDiv({height:5, style:'removeAddBr'});
        this.searchForm.addDiv({width:100,text:'מספר טלפון:'});
        this.searchForm.addField({type:'text',width:100,name:'phone'});
        this.searchForm.addDiv({width:9,text:'-'});
        var prefixList = '02:02|03:03|04:04|08:08|09:09|077:077|050:050|052:052|054:054|057:057|:בחר'
        this.searchForm.addField({type:'select',width:60,value:prefixList, dvalue:'',name:'prefix'});
        this.searchForm.addDiv({height:5, style:'removeAddBr'});
        this.searchForm.addDiv({width:170, height:22 });
        var div = this.searchForm.addDiv({width:100, height:22});
        this.searchForm.addBtn({target:div,text:'חפש' , name:'search',width:95 , style:'removeAddBtn'});
        
        this.searchForm.btnArray.search.on('click',this.searchBtnClick,this);
    },
    searchBtnClick : function()
    {
        var vals = this.searchForm.getFormValsObj();
        var msg ="";
        if (vals.prefix =="")
        {
            msg+="חובה לבחור קידומת ";
        }
        if(vals.phone == "")
        {
            if(msg != "") msg+="\n";
            msg+="חובה להזין מספר טלפון";
        }
        else
        {
            if(!IsNumeric( vals.phone) || vals.phone.length <6 )
            {
                if(msg != "") msg+="\n";
                msg+="מספר הטלפון שהוזן אינו חוקי";
            }
        }
        if (msg!= "")
        {
            alert(msg);
            return;
        }
        this.doSearch(vals);
    },
    doSearch : function(params)
    {

        this.searchForm.form.dom.style.zIndex = this.searchForm.form.dom.style.zIndex-1;
        Ext.Ajax.request({
            url:hosturl + 'RemoveAdd/search.aspx?',
            success: function (conn, response, options) {
                        var ar = eval(conn.responseText);
                        this.searchForm.form.dom.style.zIndex = this.searchForm.form.dom.style.zIndex+1;
                        if(ar.length == 0)
                        {
                            alert('לא נמצאו נכסים');
                        }
                        else 
                        {   
                            this.showAddsList(ar);
                            this.searchForm.hide();
                        }
                        
                        },
            failure: function (conn, response, options) {alert('שגיאה בחיבור לשרת אנא בדוק את תקינות החיבור האינטרנט');
                    this.searchForm.form.dom.style.zIndex = this.searchForm.form.dom.style.zIndex+1;
            },
            params: { phone: params.phone , prefix : params.prefix},
            scope : this
            }); 
    },
    createListScreen : function()
    {
        this.rowTemplate = new Ext.Template(
                  '<div class="row {invert}">',
                  '<div class="r-a-Phone">{Phone}</div>',
                  '<div class="r-a-Contact">{Contact}</div>',
                  '<div class="r-a-EntryWhen">{EntryWhen}</div>',
                  '<div class="r-a-ShekelRent">{ShekelRent}</div>',
                  '<div class="r-a-RoomNumber">{RoomNumber}</div>',
                  '<div class="r-a-vHouseNumber">{vHouseNumber}</div>',
                  '<div class="r-a-vStreet">{vStreet}</div>',
                  '<div class="r-a-vCity">{vCity}</div>',
                  '<div class="r-a-AssetType">{AssetType}</div>',
                  '<div class="r-a-cBox"><input id="removelistchk{id}" type="checkbox"  /></div>',
                  '</div>'
        );
        this.rowTemplate = this.rowTemplate.compile();
        this.listForm = new createForm({width:815, height:380,title:'רשימת נכסים' , top:0,analyticsname:'remove_add_asset_list'});
        this.listForm.listDiv = this.listForm.addDiv({style:'removeAddList'});
        this.listForm.addDiv({style:'removeAddBr',height:5});
        this.listForm.addDiv({style:'removeAddBtn',width:590});
        var div =this.listForm.addDiv({style:'removeAddBtn',width:110});
        this.listForm.addBtn({target:div,text:'חזור לחיפוש' , name:'back',width:95 , style:'removeAddBtn'});
        var div =this.listForm.addDiv({style:'removeAddBtn',width:95});
        this.listForm.addBtn({target:div,text:'הסר מסומנים' , name:'remove',width:95 , style:'removeAddBtn'});
        this.listForm.btnArray.back.on('click',function(){this.listForm.hide();this.searchForm.show();},this);
        this.listForm.btnArray.remove.on('click',this.showRemoveDialog,this);
    },
    replaceCap : function()
    {   
        var d = new Date();
        this.cap.dom.style.backgroundImage = "url(\""+hosturl+"security/JpegImage.aspx?cap="+d.getTime()+"\")";
        //Ext.fly('sms-cap').
    },
    createremoveDialog : function()
    {
        this.removeDialog = new createForm({width:400,height:290 , top:0 ,title:'אישור הסרת נכסים',divClass:'removeAddText'});
        var msg ="על מנת שנוכל להסיר את הנכס/ים יש לבצע תהליך אישור <br>";
        msg+="תהליך האישור אינו כרוך בתשלום.<br>";
        msg+="יש להקיש את המילה בתמונה ואת מספר הטלפון הנייד.<br>";
        msg+="המערכת תשלח קוד זיהוי למספר הטלפון<br>";
        msg+="לאחר אישור מספר הטלפון הנכס יוסר מהמערכת.<br>";
        this.removeDialog.addDiv({text:msg});
        this.removeDialog.addDiv({height:5, style:'removeAddBr'});
        this.removeDialog.addDiv({width:100});
        this.cap = this.removeDialog.addDiv({style:'x-cap'});
        this.removeDialog.addDiv({height:5, style:'removeAddBr'});
        this.removeDialog.addDiv({width:100,text:'המילה בתמונה:'});
        this.removeDialog.addField({name:'cap',width:190,type:'text' , value:'' , dvalue:''});
        this.removeDialog.addDiv({height:5, style:'removeAddBr'});
        this.removeDialog.addDiv({width:100,text:'מספר הטלפון:'});
        this.removeDialog.addField({name:'phone',width:195,type:'select' , dvalue:''});
        var prefix ="050|052|054|057|:בחר"
        this.removeDialog.addField({name:'phonenumber',width:115,type:'text', dvalue:''});
        this.removeDialog.addDiv({width:10});
        this.removeDialog.addField({name:'phoneprefix',width:65,type:'select' , value:prefix , dvalue:''});
        this.removeDialog.addDiv({height:9, style:'removeAddBr'});
        this.removeDialog.addField({type:'chk',name:'removeall',text:'הסרת כל הנכסים ממספר זה לשלושה חודשים',width:320});
        this.removeDialog.addDiv({height:5, style:'removeAddBr'});
        this.removeDialog.addDiv({width:260});
        var div = this.removeDialog.addDiv({width:100, height:22});
        this.removeDialog.addBtn({target:div,text:'אישור' , name:'ok',width:95 , style:'removeAddBtn'});
        this.removeDialog.btnArray.ok.on('click',this.sendSms,this);
    },
    sendSms : function()
    {
        var vals = this.removeDialog.getFormValsObj();
        var msg="";
        var phonenumber = "";
        
        if(this.removeDialog.phoneList == true)
        {
            if(vals.phone == "" )
            {
                msg='חובה לבחור מספר טלפון מהרשימה';
            }
            else
            {
                phonenumber = vals.phone;
            }
        }
        else
        {
            if(vals.phonenumber == "" || vals.phoneprefix == "")
            {
                msg = "חובה להזין מספר וקידומת";
            }
            else
            {
                if(this.isCellPhone(vals.phoneprefix+"-"+vals.phonenumber) == false)
                {
                    msg = "חובה להזין מספר טלפון תקין";
                }
                else
                {
                    phonenumber = vals.phoneprefix+"-"+vals.phonenumber;
                }
            }
        }
        
        if(vals.cap == "")
        {
            if(msg!= "") msg+="\n";
            msg+="חובה להזין את המילה בתמונה";
        }
        if(msg != "")
        {
            this.replaceCap();
            alert(msg);
            return;
        }
        
        this.removeDialog.form.dom.style.zIndex = this.removeDialog.form.dom.style.zIndex-1;
        Ext.Ajax.request({
            url:hosturl + 'RemoveAdd/sendsms.aspx?',
            success: function (conn, response, options) {
                        var ar = eval(conn.responseText);
                        this.removeDialog.form.dom.style.zIndex = this.removeDialog.form.dom.style.zIndex+1;
                        if(ar[0] == true)
                        {
                            this.removeDialog.hide();
                            this.showCodeApproveFrom();
                        }
                        else
                        {
                            alert(ar[1]);
                        }
                        },
            failure: function (conn, response, options) {alert('שגיאה בחיבור לשרת אנא בדוק את תקינות החיבור האינטרנט');
                    this.removeDialog.form.dom.style.zIndex = this.removeDialog.form.dom.style.zIndex+1;
            },
            params: { phone: phonenumber ,cap :vals.cap , removeall : vals.removeall },
            scope : this
            }); 
    },
    createCodeApproveFrom : function()
    {
        var form = new createForm({width:400,height:220,title:'הזנת קוד אישור',analyticsname:'remove_add_sms_sent'});
        var msg= "קוד אישור נשלח אל מכשירך. <br>";
        msg+="יש להמתין מספר שניית לקבלת הודעת הSMS. <br>";
        msg+="תוכן ההודעה צריך להיות: <br>";
        msg+="<b> קוד האישור שלך מזזים :xxxx </b> <br>";
        msg+="יש להקיש את הקוד בדיוק כפי שהוא מופיע בהודעה משמאל לימין <br>";
        msg+="וללחוץ על אישור.";
        form.addDiv({style:'sms-info-line-brake',width:1,height:15});
        form.addDiv({text:msg,style:'sms-info-msg',width:390});
        form.addDiv({style:'sms-info-line-brake',width:1,height:15});
        form.addDiv({style:'sms-info-line-brake',width:1,height:1});
        form.addDiv({style:'sms-info-msg',width:100,text:'קוד זיהוי:'});
        form.addField({type:'text',name:'code',width:186,style:'sms-info-msg',dvalue:''});
        form.addDiv({style:'sms-info-line-brake',width:1,height:5});
        //form.addDiv({width:100,height:55,style:'sms-info-msg'});
        form.addDiv({style:'sms-info-msg',width:100});        
        form.addBtn({style:'sms-btn',text:'אישור',width:100,name:'ok'});
        form.addDiv({style:'sms-info-msg',width:10});        
        form.addBtn({style:'sms-btn',text:'ביטול',width:100,name:'cancle'});
        form.addField({type:'obj',value:0,name:'sentNumber'});
        form.btnArray.ok.on('click',this.smsCodeTest,this);
        form.btnArray.cancle.on('click',form.hide,form);
        
        this.codeApproveFrom = form;
    },
    showCodeApproveFrom : function()
    {
        if(this.codeApproveFrom == null)
            this.createCodeApproveFrom();
            this.codeApproveFrom.setFormVals();
            this.codeApproveFrom.show();
    },
    smsCodeTest : function()
    {
        vals = this.codeApproveFrom.getFormValsObj();
        if(vals.code == "" || vals.code.length != 4 )
        {
            alert('קוד אישור לא תקין');
            return;
        }
        
        this.codeApproveFrom.form.dom.style.zIndex = this.codeApproveFrom.form.dom.style.zIndex-1;
        
        Ext.Ajax.request({
            url:hosturl + 'RemoveAdd/blockassets.aspx?',
            success: function (conn, response, options) {
                        var ar = eval(conn.responseText);
                        this.codeApproveFrom.form.dom.style.zIndex = this.codeApproveFrom.form.dom.style.zIndex+1;
                        if(ar[0] == true)
                        {
                            pageTracker._trackPageview("/remove_add_complited");
                            alert('הנכס/ים הוסרו בהצלחה');
                            this.codeApproveFrom.hide();
                        }
                        else 
                        {
                            alert(ar[1]);
                        }
                        },
            failure: function (conn, response, options) {alert('שגיאה בחיבור לשרת אנא בדוק את תקינות החיבור האינטרנט');
                    this.codeApproveFrom.form.dom.style.zIndex = this.codeApproveFrom.form.dom.style.zIndex+1;
            },
            params: { code: vals.code , ids : this.ids},
            scope : this
            }); 
        
    },
    isCellPhone : function(num)
    {
        if(num.indexOf('050')==0 || num.indexOf('052')==0 || num.indexOf('054')==0 || num.indexOf('057')==0)
        {
            if(num.replace(/-/g,'').length == 10)
               return true;
            else
                return false;
        }
        else 
            return false;
    },
    showRemoveDialog : function()
    {
        if(this.removeDialog == null)
            this.createremoveDialog();
        var phones="";
        var ids="";
        for(var i=1; i<this.listArray.length;i++)
        {
            if(Ext.get('removelistchk'+this.listArray[i].rowId).dom.checked == true)
            {
              if(ids !="") ids+="|";
              ids+=this.listArray[i].rowId;
              
              var pa =this.listArray[i].PhoneA;
              var pb  =this.listArray[i].PhoneB;
              if(this.isCellPhone(pa))
              {
                if(phones.indexOf(pa) == -1) 
                {
                    if(phones!="") phones+="|";
                    phones+=pa;
                }
              }
              if(this.isCellPhone(pb))
              {
                if(phones.indexOf(pb) == -1) 
                {
                    if(phones!="") phones+="|";
                    phones+=pb;
                }
              }
              
            }
            
        }
        
        if(ids == "")
        {
            alert('לא נבחרו נכסים');
            return;
        }
        else
        {
            this.ids= ids;
            
            this.replaceCap();
            if(phones != "")
            {
                phones+="|:בחר";
                this.removeDialog.fieldArray.phone.reLoadList(phones,'');
                this.removeDialog.fieldArray.phonenumber.mydiv.dom.style.display="none";
                this.removeDialog.fieldArray.phoneprefix.mydiv.dom.style.display="none";
                this.removeDialog.fieldArray.phone.mydiv.dom.style.display="block";
                this.removeDialog.phoneList = true;
                
            }
            else
            {
                this.removeDialog.fieldArray.phonenumber.mydiv.dom.style.display="block";
                this.removeDialog.fieldArray.phoneprefix.mydiv.dom.style.display="block";
                this.removeDialog.fieldArray.phone.mydiv.dom.style.display="none";
                this.removeDialog.phoneList = false;
            }
            this.listForm.hide();
            this.removeDialog.setFormVals();
            this.removeDialog.show();
        }
        //5803053
    },
    showAddsList :function(data)
    {
        if(this.listForm == null)
            this.createListScreen();

        
        if(this.listArray != null)
        {
            for (var i = 0; i < this.listArray.length; i++)
            {   
                this.listArray[i].rowId = null;
                //this.listArray[i].marker = null;
                Ext.get(this.listArray[i]).removeAllListeners();
                Ext.get(this.listArray[i]).remove();
                //this.listArray[i].removeAllListeners();
                //this.listArray[i].remove();
                this.listArray[i] = null;
            }
        }
        
        
        this.listArray = new Array();
        var title = {id:'all', AssetType:'סוג הנכס' , vCity :'עיר' , vStreet :'רחוב', vHouseNumber :'מספר' , RoomNumber:'חדרים' ,ShekelRent:'מחיר' ,EntryWhen :'ת.כניסה' ,Contact:'איש קשר' , Phone :'טלפון' ,invert:'removeAddList-title' }
        var el= this.rowTemplate.append(this.listForm.listDiv,title);
        el.rowId = 'all';
        Ext.get('removelistchkall').on('click',this.markAddForRemove,this);
        this.listArray[this.listArray.length] = el;
        for(var i=0; i<data.length ; i++)
        {   
            var p = (data[i].PhoneA =="" ? '':data[i].PhoneA ) + (data[i].PhoneB =="" ? '':' '+data[i].PhoneB )
            p=p.replace(/(\n|\r|\g|&nbsp|;)/g,'');
            data[i].Phone = '<acronym title="' + p.replace(/&nbsp;/,'') +'">' + p.substr(0,12).replace(/&nbsp;/,'') + "..." + '</acronym>';
            data[i].invert = (i % 2 == 0) ? 'removeAddList-invert' : ''
            var el= this.rowTemplate.append(this.listForm.listDiv,data[i]);
            el.PhoneA =data[i].PhoneA;
            el.PhoneB =data[i].PhoneB;
            el.rowId = data[i].id;
            this.listArray[this.listArray.length] = el;
        }
        
        this.listForm.show();
    },
    markAddForRemove : function(evt,el)
    {
        for(var i=1; i<this.listArray.length;i++)
        {
            Ext.get('removelistchk'+this.listArray[i].rowId).dom.checked = Ext.get('removelistchkall').dom.checked;
        }
        
    }
};