﻿// JavaScript File
ContactUs = function()
{
    this.errorMsg = "Invalid data entered.";
    this.thankYouMsg ="<br /><br />Thank you for contacting us.";
    
    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.txtCompany= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'txtCompany',
	            width:230,
	            allowBlank:false
                });
       
   this.txtCompany.applyTo(Ext.get("txtCompany"));
   
        this.txtPhone= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'txtPhone',
	            width:230,
	            allowBlank:false
                });
       
   this.txtPhone.applyTo(Ext.get("txtPhone"));
   
    this.comments = new Ext.form.TextArea({
                                            allowBlank:true,
                                            selectOnFocus : true,
                                            width:471,
                                            name:"comments",
                                            allowBlank:false
                                        }); 
   this.comments.applyTo(Ext.get("comments"));
   
   Ext.get("comments").setHeight(120, false);
   
   
  this.data = eval(AJAX.API.GetDepartments().value);
  this.dataStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : this.data}); 
    
   this.department = new Ext.form.ComboBox({
        typeAhead: false,
        transform: "department",
        store:this.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.department.on('beforequery', this.onBeforeQuery, this);
   
   
     this.Statesdata = eval(AJAX.API.GetStates().value);
  this.StatesdataStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : this.Statesdata}); 
    
   this.states = new Ext.form.ComboBox({
        typeAhead: false,
        transform: "ddlState",
        store:this.StatesdataStore,
        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.states.on('beforequery', this.onBeforeQuery, this);
   
  this.BusinessCategoryData = eval(AJAX.API.GetBusinessCategories().value);
  this.BusinessCategoryDataStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : this.BusinessCategoryData}); 
    
   this.ddlBusinessCategory = new Ext.form.ComboBox({
        typeAhead: false,
        transform: "ddlBusinessCategory",
        store:this.BusinessCategoryDataStore,
        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.ddlBusinessCategory.on('beforequery', this.onBeforeQuery, this);

   this.btnSend = Ext.get("send");
   this.btnSend.on('click',this.onSendClick,this,{});
    
   
};

Ext.extend(ContactUs, Ext.util.Observable, {
     onBeforeQuery: function(combo, e)
       {
        combo.forceAll = true;
        combo.query="";
       },
       onSendClick:function()
       {
            
            var errorMessage = Ext.get("errMsg");
            
            this.firstName.focus();
            this.lastName.focus();
            this.emailAddress.focus();
            this.department.focus();
            this.comments.focus();
            this.txtCompany.focus();
            this.txtPhone.focus();
            this.states.focus();
            this.ddlBusinessCategory.focus();
            
            
            if( this.firstName.validate() && this.lastName.validate() && this.emailAddress.validate() && this.txtCompany.validate()&&
                    this.txtPhone.validate()&& this.states.validate() && this.ddlBusinessCategory.validate() && this.department.validate() && this.comments.validate())
            {
               var content = AJAX.API.GetContentContactUsInstance().value;
               
               content.FirstName = this.firstName.getValue();
               content.LastName =this.lastName.getValue();
               content.EmailAddress =this.emailAddress.getValue();
               content.Department =this.department.getValue();
               content.Comments =this.comments.getValue();
               content.Company = this.txtCompany.getValue();
               content.Phone = this.txtPhone.getValue();
               content.State = this.states.getValue();
               content.BusinessCategory = this.ddlBusinessCategory.getValue();
               
               var sendMail = AJAX.API.SendContactUs(content);
                
                this.firstName.reset();
                this.lastName.reset();
                this.emailAddress.reset();
                this.department.reset();
                this.comments.reset();
                this.txtCompany.reset();
                this.txtPhone.reset();
                this.states.reset();
                this.ddlBusinessCategory.reset();
               
               var ctrlsDiv = Ext.get("contactUsForm");
               ctrlsDiv.hide(true);
               ctrlsDiv.setHeight(0);
               
               var ThankYouCtrl = Ext.get("ThankYouMessage");
               ThankYouCtrl.dom.innerHTML = this.thankYouMsg ;
            }
            else
            {
                errorMessage.dom.innerHTML = this.errorMsg ;
            }
            
       }
       
       
});
