﻿// JavaScript File
FeedBack = function()
{
    this.errorMsg = "Invalid data entered.";
    this.thankYouMsg ="Thank you for your feedback.";
    
    //this.siteId = SiteId;
    //this.IsBlueBox = IsBlueBox;
    //this.ContactUsRedirectUrl = ContactUsRedirectUrl;
    //SiteId,IsBlueBox,ContactUsRedirectUrl
    
    Ext.apply(Ext.form.VTypes, {
     'emailAddressValidate': function(){
         var re = /^(([^<>()[\]\\.,;:\s@""]+(\.[^<>()[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
         return function(v){
             return re.test(v);
         }
     }()
    });
    
     this.firstName= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'firstName',
	            width:230,
	            allowBlank:false
                });
       
   this.firstName.applyTo(Ext.get("firstName"));
   
    this.lastName= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'lastName',
	            width:230,
	            allowBlank:false
                });
       
    this.lastName.applyTo(Ext.get("lastName"));
    
     this.emailAddress= new Ext.form.TextField({
    fieldLabel: '',
    name: 'emailAddress',
    width:230,
    allowBlank:false,
    vtype :'emailAddressValidate'
    });
       
    this.emailAddress.applyTo(Ext.get("emailAddress"));  
    
      this.jobTitle= new Ext.form.TextField({
    fieldLabel: '',
    name: 'jobTitle',
    width:230,
    allowBlank:false
    });
       
    this.jobTitle.applyTo(Ext.get("jobTitle"));  
    
    this.comments = new Ext.form.TextArea({
                                            allowBlank:true,
                                            selectOnFocus : true,
                                            width:471,
                                            name:"comments"
                                        }); 
   this.comments.applyTo(Ext.get("comments"));
   Ext.get("comments").setHeight(120, false);
   
   
   this.questions = AJAX.API.GetWebsiteFeedbackQuestions().value;
   
   this.questionsHtml = Ext.get("questions");
   this.questionsHtml.update(this.questions);
   this.ddlQuestion = new Array();
   this.question = new Array();
   for (var i=1; i<=2; i++)
   {
        var dataCtr = Ext.get("answerData" + i);
        var data =null;
        var dataStore = null;
        
        if( dataCtr != null)
        {
            data = eval(dataCtr.dom.innerHTML);
            dataStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : data}); 
        }
       
        var transformCtr = Ext.get("ddlQuestion" + i);
        
        this.ddlQuestion[i] = new Ext.form.ComboBox({
                                    typeAhead: false,
                                    transform: transformCtr,
                                    store:dataStore,
                                    width:230,
                                    forceSelection:true,
                                    editable: false, 
                                    mode: 'local',
                                    valueField: 'value',
                                    displayField: 'text', 
                                    tpl: '<div class="x-combo-list-item"><div class="{class}">{text}</div></div>',
                                    allowBlank:false
                                });
        this.ddlQuestion[i].on('beforequery', this.onBeforeQuery, this);
        this.question[i] = Ext.get("question"+ i);
        dataCtr.update("");
        
   }
   this.btnSend = Ext.get("send");
   this.btnSend.on('click',this.onSendClick,this,{});
};

Ext.extend(FeedBack, Ext.util.Observable, {
     onBeforeQuery: function(combo, e)
       {
        combo.forceAll = true;
        combo.query="";
       },
       onSendClick:function()
       {
            var errorMessage = Ext.get("errMsg");
            var isValid = true;
            this.firstName.focus();
            this.lastName.focus();
            this.emailAddress.focus();
            this.jobTitle.focus();
            
           for (i=1; i<=2; i++)
           { 
                this.ddlQuestion[i].focus();
                if( !this.ddlQuestion[i].validate())
                {
                    isValid = false;
                }
                 
            }

            this.comments.focus();
            
            if( !this.firstName.validate() || 
                !this.lastName.validate() || 
                !this.emailAddress.validate() || 
                !this.jobTitle.validate() )
                {
                    isValid = false;
                }
            if(isValid == true)
            {
               var content = AJAX.API.GetContentFeedbackInstance().value;
               
               content.FirstName = this.firstName.getValue();
               content.LastName =this.lastName.getValue();
               content.EmailAddress =this.emailAddress.getValue();
               content.JobTitle = this.jobTitle.getValue();
               content.Question1 = this.question[1].dom.textContent;
               content.Answer1 = this.ddlQuestion[1].getValue();
               content.Question2 = this.question[2].dom.textContent;
               content.Answer2 = this.ddlQuestion[2].getValue();
               //content.Question3 = this.question[3].dom.textContent;
               //content.Answer3 = this.ddlQuestion[3].getValue();
               //content.Question4 = this.question[4].dom.textContent;
               //content.Answer4 = this.ddlQuestion[4].getValue();
               content.Comments =this.comments.getValue();
               var sendMail = AJAX.API.SendFeedBack(content);
               
               var ctrlsDiv = Ext.get("WebSiteFeedBackForm");
                   ctrlsDiv.hide(true);
                   ctrlsDiv.setHeight(0);
                   
                   var ThankYouCtrl = Ext.get("WebSiteFeedBackThankYouMessage");
                   ThankYouCtrl.dom.innerHTML = this.thankYouMsg ;
            }
            else
            {
                errorMessage.dom.innerHTML = this.errorMsg ;
            }
            
       }
       
       
});

