﻿// JavaScript File
EmailSignUp = function()
{
    this.errorMsg = "Invalid data entered.";
    this.thankYouMsg ="<br /><br />Thank you for signing up.";
    
    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.fName= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'fName',
	            width:230,
	            allowBlank:false
    });
       
    this.fName.applyTo(Ext.get("fName"));
   
    this.lName= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'lName',
	            width:230,
	            allowBlank:false
                });
       
    this.lName.applyTo(Ext.get("lName"));
    
    
    this.bName= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'bName',
	            width:230,
	            allowBlank:false
                });
       
    this.bName.applyTo(Ext.get("bName"));
       
    
    this.eAddress= new Ext.form.TextField({
    fieldLabel: '',
    name: 'eAddress',
    width:230,
    allowBlank:false,
    vtype :'emailAddressValidate'
    });
       
    this.eAddress.applyTo(Ext.get("eAddress")); 
    
    this.data = eval(AJAX.API.GetProductCategories().value);
    this.dataStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : this.data}); 
    
   this.bCategories = new Ext.form.ComboBox({
        typeAhead: false,
        transform: "bCategory",
        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.bCategories.on('beforequery', this.onBeforeQuery, this);
  
   this.btnSend = Ext.get("signUp");
   this.btnSend.on('click',this.onSignUpClick,this,{});
   
   this.pAnnouncements = new Ext.form.Checkbox({name:'pAnnouncements', boxLabel:'Product Announcements'});
   this.cNews = new Ext.form.Checkbox({name:'cNews', boxLabel:'Company News'});
   this.uEvents = new Ext.form.Checkbox({name:'uEvents', boxLabel:'Upcoming Events'});
   this.iNews = new Ext.form.Checkbox({name:'iNews', boxLabel:'Industry News & Information'});
   
   this.pAnnouncements.applyTo(Ext.get("pAnnouncements"));
   this.cNews.applyTo(Ext.get("cNews"));
   this.uEvents.applyTo(Ext.get("uEvents"));
   this.iNews.applyTo(Ext.get("iNews"));
   
};
Ext.extend(EmailSignUp, Ext.util.Observable, {
     onBeforeQuery: function(combo, e)
       {
            combo.forceAll = true;
            combo.query="";
       },
       onSignUpClick:function()
       {
            var errorMessage = Ext.get("errMsg");
            this.fName.focus();
            this.lName.focus();
            this.eAddress.focus();
            this.bName.focus();
            this.bCategories.focus();
            
            if( this.fName.validate() && this.lName.validate() && this.eAddress.validate()
                    && this.bCategories.validate() )
            {
                   var content = AJAX.API.GetContentEmailSignUpInstance().value;
                 
                   content.FirstName = this.fName.getValue();
                   content.LastName =this.lName.getValue();
                   content.EmailAddress =this.eAddress.getValue();
                   content.BusinessName = this.bName.getValue();
                   content.BusinessCategory=this.bCategories.lastSelectionText;
                   content.ProductAnnouncements =this.pAnnouncements.getValue();
                   content.CompanyNews =this.cNews.getValue();
                   content.UpcomingEvents=this.uEvents.getValue();
                   content.IndustryNewsAndInformation=this.iNews.getValue();
                   
                   var sendMail = AJAX.API.SendEmailSignUp(content);
                   
                   var ctrlsDiv = Ext.get("EmailSignUpForm");
                   ctrlsDiv.hide(true);
                   ctrlsDiv.setHeight(0);
                   
                   var ThankYouCtrl = Ext.get("ESignUpThankYouMessage");
                   ThankYouCtrl.dom.innerHTML = this.thankYouMsg ;
            }
             else
            {
                errorMessage.dom.innerHTML = this.errorMsg ;
            }
       }
});
