
smsManager = function()
{
    this.smsApproveForm =null;
    this.codeTestForm =null;
    this.init();
};

smsManager.prototype = {
    init : function()
    {
        //debugger;
        //hosturl
        this.createCodeEnterForm();
        this.createSmsApproveForm();
        
    },
    createCodeEnterForm : function()
    {
        this.codeTestForm = new createForm({width:400,height:220,title:'הזנת קוד זיהוי'});
        var msg= "קוד זיהוי נשלח אל מכשירך. <br>";
        msg+="יש להמתין מספר שניית לקבלת הודעת הSMS. <br>";
        msg+="תוכן ההודעה צריך להיות: <br>";
        msg+="<b> קוד הזיהוי שלך לאתר זזים הוא:xxxx </b> <br>";
        msg+="יש להקיש את הקוד בדיוק כפי שהוא מופיע בהודעה משמאל לימין <br>";
        msg+="וללחוץ על אישור.";
        this.codeTestForm.addDiv({style:'sms-info-line-brake',width:1,height:15});
        this.codeTestForm.addDiv({text:msg,style:'sms-info-msg',width:390});
        this.codeTestForm.addDiv({style:'sms-info-line-brake',width:1,height:15});
        this.codeTestForm.addDiv({style:'sms-info-line-brake',width:1,height:1});
        this.codeTestForm.addDiv({style:'sms-info-msg',width:100,text:'קוד זיהוי:'});
        this.codeTestForm.addField({type:'text',name:'code',width:186,style:'sms-info-msg',dvalue:''});
        this.codeTestForm.addDiv({style:'sms-info-line-brake',width:1,height:5});
        //this.codeTestForm.addDiv({width:100,height:55,style:'sms-info-msg'});
        this.codeTestForm.addDiv({style:'sms-info-msg',width:100});        
        this.codeTestForm.addBtn({style:'sms-btn',text:'אישור',width:100,name:'ok'});
        this.codeTestForm.addDiv({style:'sms-info-msg',width:10});        
        this.codeTestForm.addBtn({style:'sms-btn',text:'ביטול',width:100,name:'cancle'});
        this.codeTestForm.addField({type:'obj',value:0,name:'sentNumber'});
        
        this.codeTestForm.btnArray.ok.on('click',this.smsCodeTest,this);
        this.codeTestForm.btnArray.cancle.on('click',this.codeTestForm.hide,this.codeTestForm);
    },
    createSmsApproveForm : function()
    {   
        this.smsApproveForm = new createForm({ width:500,height:400,title:'אישור מספר טלפון'});
        var msg = "על מנת שנוכל לשלוח הודעות SMS <br>" ;
        msg+="יש לבצע אימות של מספר הטלפון הנייד שלך<br>";
        msg+="<br>ביצוע התהליך הינו חד פעמי ואינו כרוך בתשלום.<br><br>";
        msg+="יש לוודא את מספר הטלפון הנייד ולהזין את המילה בתמונה.<br>";
        msg+="המערכת תשלח למספר קוד זיהוי בהודעת SMS אותו יש להזין בשדה קוד זיהוי<br><br>";
        
        msg+="במידה ולא יבוצע אימות למספר הטלפון לא ישלחו הודעות SMS מהאתר. <br><br>";
        
        this.smsApproveForm.addDiv({style:'sms-info-line-brake',width:1,height:15});
        this.smsApproveForm.addDiv({text:msg,style:'sms-info-msg',width:490});
        this.smsApproveForm.addDiv({style:'sms-info-msg',width:100,text:'מספר טלפון:'});
        this.smsApproveForm.addField({type:'text',name:'phone',width:100,value:'',dvalue:'',style:'sms-info-msg'});
        this.smsApproveForm.addDiv({style:'sms-info-msg',width:7,text:'-'});        
        this.smsApproveForm.addField({type:'select',name:'prefix',width:60,value:':בחר|050:050|052:052|054:054|057:057',dvalue:'',style:'sms-info-msg'});
        this.smsApproveForm.addDiv({style:'sms-info-line-brake',width:1,height:1});
        this.smsApproveForm.addDiv({width:100,height:55,style:'sms-info-msg'});
        
        this.cap = this.smsApproveForm.addDiv({style:'x-cap'});
        
        this.smsApproveForm.addDiv({style:'sms-info-line-brake',width:1,height:5});
        this.smsApproveForm.addDiv({style:'sms-info-msg',width:100,text:'המילה בתמונה:'});
        this.smsApproveForm.addField({type:'text',name:'cap',width:186,style:'sms-info-msg',dvalue:''});
        
        this.smsApproveForm.addDiv({style:'sms-info-line-brake',width:1,height:5});
        
        this.smsApproveForm.addDiv({width:100,height:55,style:'sms-info-msg'});
        this.smsApproveForm.addBtn({style:'sms-btn',text:'שלח קוד',width:100,name:'send_sms'});
        //this.smsApproveForm.addDiv({style:'sms-info-msg',width:7});        
        //this.smsApproveForm.addBtn({style:'sms-btn',text:'אישור',width:100,name:'ok'});
        this.smsApproveForm.addDiv({style:'sms-info-msg',width:7});        
        this.smsApproveForm.addBtn({style:'sms-btn',text:'ביטול',width:100,name:'cancle'});
        this.smsApproveForm.addField({type:'obj',value:0,name:'sentNumber'});
        // assign btn events
        this.smsApproveForm.btnArray.cancle.on('click',this.smsApproveForm.hide,this.smsApproveForm);
        this.smsApproveForm.btnArray.send_sms.on('click',this.sendSmsCode,this);
        
        
    },
    smsCodeTest : function()
    {
        var data=this.codeTestForm.getFormValsObj();
        
        if(data.code == "" || data.code.length != 4 || IsNumeric(data.code) == false)
        {
            alert('קוד הזיהוי שהוזן לא תקין  אנא ודא שקוד הזיהוי זהה לקוד שנשלח בהודעה');
            return;
        }
        
        Ext.Ajax.request({
                   url:hosturl + 'sms/smsCodeTest.aspx?',
                   success: function (conn, response, options){
                                var ans = eval(conn.responseText);
                                if(ans[0] == true)
                                {
                                    alert('מספר הטלפון אושר לשליחת הודעות טקסט');
                                    loginManager.CurrentUser.sms = 1;
                                    loginManager.updateUserDetail.sms = 1;
                                    this.codeTestForm.hide();
                                }
                                else
                                {
                                    if(ans.length == 2)
                                    {
                                        alert(ans[1]);
                                    }
                                    else if(ans.length == 3)
                                    {
                                        if(ans[2] <= 2)
                                        {
                                            alert(ans[1]);
                                        }
                                        else
                                        {
                                            alert('בוצעו יותר מדי ניסיונות להזנת קוד הזיהוי  אנא פנה לתמיכה להמשך התהליך');
                                            this.smsApproveForm.hide();
                                            this.smsApproveForm.blockShow = true;
                                        }
                                        
                                    }
                                }
                                
                            },
                   failure: function (conn, response, options) {alert('כישלון בחיבור לשרת בדוק את תקינות חיבור האינטרנט'); this.hideScreenBloker();},
                   params: { code: data.code },
                   scope : this
                   
                });
        
    },
    sendSmsCode : function()
    {
        var data=this.smsApproveForm.getFormValsObj();
        var msg = "";
        if(data.phone.length != 7 || IsNumeric(data.phone) == false ) 
        {
            msg+="מספר הטלפון שהוזן אינו חוקי";
        }
        if(data.prefix == '')
        {
            if(msg != "") msg+="\n";
            msg+="לא נבחרה קידומת";
        }
        if(data.cap == '')
        {
            if(msg != "") msg+="\n";
            msg+="חובה להזין את המילה בתמונה";
        }
        if(msg != "")
        {
            alert(msg);
            return;
        }
        this.tmpdata = data;
        
        
        
        
        Ext.Ajax.request({
                   url:hosturl + 'sms/sendSms.aspx?',
                   success: function (conn, response, options){
                                var ans = eval(conn.responseText);
                                var d = new Date();
                                //Ext.fly('sms-cap').dom.style.backgroundImage = "url(\"security/JpegImage.aspx?cap="+d.getTime()+"\")";
                                if (ans[0]== true)
                                {
                                    this.smsApproveForm.hide();
                                    this.codeTestForm.setFormVals();
                                    this.codeTestForm.show();
                                    //alert('קוד זיהוי נשלח כעת אל מספר הטלפון');
                                    loginManager.updateUserDetail.phone = this.tmpdata.phone;
                                    loginManager.updateUserDetail.prefix = this.tmpdata.prefix;
                                    loginManager.CurrentUser.phone = this.tmpdata.phone;
                                    loginManager.CurrentUser.prefix = this.tmpdata.prefix;
                                }
                                else
                                {
                                    alert(ans[1]);
                                }
                            },
                   failure: function (conn, response, options) {alert('כישלון בחיבור לשרת בדוק את תקינות חיבור האינטרנט'); this.hideScreenBloker();
                   var d = new Date();
                   
                   },
                   params: { data: this.smsApproveForm.getFormVals() },
                   scope : this
                   
                });
    },
    replaceCap : function()
    {
        
        var d = new Date();
        this.cap.dom.style.backgroundImage = "url(\""+hosturl+"security/JpegImage.aspx?cap="+d.getTime()+"\")";
        //Ext.fly('sms-cap').
    }
};
