Ext.BLANK_IMAGE_URL = '/themes/en/images/s.gif';
Ext.namespace("Waps.modules")
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
String.prototype.stripHTML = function(){
    // What a tag looks like
    var matchTag = /<(?:.|\s)*?>/g; // tag that start with '<' and end with '>' like <p>, <td> etc
    var matchTag2 = /&(?:.|\s)*?;/g; // tags that start with '&'  and end with ';' like &nbsp;, &amp; etc
    var ret = this.replace(matchTag2, "");
    var ret = ret.replace(matchTag, "");
    return ret
};

Ext.override(Ext.form.CheckboxGroup, {
  
    afterRender: function() {
        var that = this;
        this.items.each(function(i) {
            that.relayEvents(i, ['check']);
        });
        
        Ext.form.CheckboxGroup.superclass.afterRender.call(this)
    }

});
Ext.override(Ext.form.RadioGroup, {
 
    afterRender: function() {
        var group = this;
        this.items.each(function(field) {
            // Listen for 'check' event on each child item
            field.on("check", function(self, checked) {
             
              // if checkbox is checked, then fire 'change' event on RadioGroup container
              if(checked)
                // Note, oldValue (third parameter in 'change' event listener) is not passed, 
                // because is not easy to get it
                group.fireEvent('change', group, self.getRawValue());
               
            });
        });
       
        Ext.form.RadioGroup.superclass.afterRender.call(this)
    }

}); 


var ExpandCollapse = function(id, minId, caller){

    Ext.get(minId).enableDisplayMode()
    Ext.get(id).enableDisplayMode()
    
    if (!Ext.get(minId).isDisplayed()) {
        Ext.get(minId).toggle(true)
        caller.innerHTML = "read more";
    }
    else 
        Ext.get(minId).toggle()
    
    if (!Ext.get(id).isDisplayed()) {
        Ext.get(id).toggle(true)
        caller.innerHTML = "collapse";
    }
    else 
        Ext.get(id).toggle()
    
    //
    //Ext.get(id).toggle(true)
    //console.log(Ext.get(minId))
}

Waps.modules.Register = function(options){
    this.options = options
    
    Ext.onReady(function(){
        if (this.options.ContactFormat == 1) 
            this.CreateContactForm()
        else 
            alert("contact form method not implemented.")
    }, this)
}

Ext.form.VTypes['phone'] = function(Z, v){
    var E = /^((\d{3}|\d{2})[-]?){1,2}(\d{7})$/;
    return E.test(Z);
}
Ext.form.VTypes.phoneText = "Not a valid phone number.  Must be in the format 050-1234562 or 04-1234562 or 04-123-4567 or 971-50-1234567";
Ext.form.VTypes.phoneMask = /[\d-]/;

Waps.modules.Register.prototype = {
    CreateContactForm: function(){
        Ext.namespace('Ext.staticData');
        Ext.staticData = [['Mr', 'Mr.', 'Mobile', 'Landline'], ['Miss', 'Miss.', 'Mobile', 'Mobile'], ['Mrs', 'Mrs.', 'Mobile', 'Landline']];
        Ext.staticDataPhone = [['Mobile'], ['Landline']];
        
        
        var store = new Ext.data.SimpleStore({
            fields: ['id', 'name', 'Pid', 'Pname'],
            data: Ext.staticData
        });
        var phoneType = new Ext.data.SimpleStore({
            fields: ['name'],
            data: Ext.staticDataPhone
        });
        var contactForm = new Ext.FormWindow({
            fieldList: [{
                //bodyStyle: 'padding:5px 0 ',
                layout: 'column',
                border: false,
                defaults: {
                    border: false
                    //bodyStyle: 'padding:5px 0 '
                },
                items: [ {
                    columnWidth: .4,
                    layout: 'form',
                    items: [new Ext.form.ComboBox({
                        store: store,
                        anchor: '90%',
                        typeAhead: true,
                        fieldLabel: 'Name',
                        mode: 'local',
                        allowBlank: false,
                        triggerAction: 'all',
                        emptyText: 'Select',
                        selectOnFocus: true,
                        displayField: 'name',
                        valueField: 'id',
                        hiddenName: 'Salutation',
                        editable: false
                    })]
                }, {
                    columnWidth: .3,
                    layout: 'form',
                    items: [{
                        xtype: 'textfield',
                        emptyText: 'First Name',
                        allowBlank: false,
                        hideLabel: true,
                        name: 'FirstName',
                        anchor: '90%'
                    }]
                }, {
                    columnWidth: .3,
                    layout: 'form',
                    items: [{
                        xtype: 'textfield',
                        emptyText: 'Last Name',
                        name: 'LastName',
                        hideLabel: true,
                        allowBlank: false,
                        anchor: '90%'
                    }]
                }]
            }, {
                xtype: 'textfield',
                fieldLabel: 'Position',
                name: 'Position',
                anchor: '55%'
            
                //allowBlank : false
            }, {
                ///columnWidth: .4,
                anchor: '55%',
                xtype: 'textfield',
                fieldLabel: 'Company',
                name: 'Company'
                //allowBlank : false,
            
            }, {
                layout: 'column',
                defaults: {
                    border: false
                },
                border: false,
                items: [{
                    //bodyStyle: 'padding:5px 0 ',
                    columnWidth: .4,
                    layout: 'form',
                    items: [new Ext.form.ComboBox({
                        store: phoneType,
                        anchor: '90%',
                        typeAhead: true,
                        fieldLabel: 'Primary Number',
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText: 'Select',
                        allowBlank: false,
                        selectOnFocus: true,
                        name: 'PType',
                        editable: false,
                        displayField: 'name',
                        valueField: 'id'
                    })]
                }, {
                    //bodyStyle: 'padding:5px 0 ',
                    columnWidth: .3,
                    layout: 'form',
                    items: [{
                        xtype: 'textfield',
                        emptyText: '050-1234562 or 971-50-1234567',
                        allowBlank: false,
                        hideLabel: true,
                        vtype: 'phone',
                        name: 'PhonePrimary',
                        anchor: '90%'
                    }]
                }]
            }, {
                layout: 'column',
                defaults: {
                    border: false
                },
                border: false,
                items: [{
                    //bodyStyle: 'padding:5px 0 ',
                    columnWidth: .4,
                    layout: 'form',
                    border: false,
                    items: [new Ext.form.ComboBox({
                        store: phoneType,
                        anchor: '90%',
                        typeAhead: true,
                        fieldLabel: 'Secondary Number',
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText: 'Select',
                        selectOnFocus: true,
                        editable: false,
                        displayField: 'name',
                        valueField: 'id',
                        name: 'SType'
                    })]
                }, {
                    //bodyStyle: 'padding:5px 0 ',
                    columnWidth: .3,
                    layout: 'form',
                    border: false,
                    items: [{
                        xtype: 'textfield',
                        emptyText: '050-1234562 or 971-50-1234567',
                        vtype: 'phone',
                        //allowBlank : false,
                        hideLabel: true,
                        name: 'PhoneSecondary',
                        anchor: '90%'
                    }]
                }]
            }, {
                columnWidth: .4,
                border: false,
                xtype: 'textfield',
                anchor: '55%',
                fieldLabel: 'Email Address',
                name: 'EmailAddress',
                vtype: 'email',
                allowBlank: false
            
            }, {
                columnWidth: .4,
                border: false,
                xtype: 'textfield',
                anchor: '55%',
                fieldLabel: 'Facsimile',
                name: 'Facsimile'
                //allowBlank : false,
            
            }, {
                columnWidth: .4,
                border: false,
                xtype: 'textarea',
                anchor: '55%',
                fieldLabel: 'Address',
                name: 'Address'
                //allowBlank : false,
            
            }, {
                columnWidth: .1,
                layout: 'form',
                border: false,
                items: [new Ext.form.ComboBox({
                    store: new Ext.data.JsonStore({
                        url: "/index.php/SiteDataService",
                        baseParams: {
                            FormId: "1",
                            fieldID: "1",
                            Module: 'Country'
                        },
                        autoLoad: true,
                        root: "data",
                        fields: ["name"]
                    }),
                    anchor: '55%',
                    typeAhead: true,
                    fieldLabel: 'Country',
                    hiddenName: 'Country',
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText: 'Select',
                    selectOnFocus: true,
                    //editable: false,
                    forceSelection: true,
                    displayField: 'name',
                    valueField: 'name'
                
                })]
            }, {
                columnWidth: .1,
                layout: 'form',
                border: false,
                anchor: '55%',
                fieldLabel: 'Complete the equation',
                name: 'captcha',
                xtype: 'botfield',
                labelWidth: 200,
                tooltip: {
                    tip: 'To verify you are not a robot, please enter the value that satisfies this equation:',
                    width: 220
                }
            }, {
				columnWidth: 1,
                layout: 'form',
                border: false,
                anchor: '90%',
				xtype: 'radiogroup',
                fieldLabel: 'How did you hear about the show?',
                itemCls: 'x-check-group-alt',
                columns: 1,
				allowBlank: false,
                items: [
                    {boxLabel: 'TV Advertisement', name: 'rb-vert', inputValue: 'TV Advertisement'},
                    {boxLabel: 'Radio Advertisement', name: 'rb-vert', inputValue: 'Radio Advertisement'},
                    {boxLabel: 'Outdoor Signage', name: 'rb-vert', inputValue: 'Outdoor Signage'},
                    {boxLabel: 'Newspapers, magazines, flyers and etc.', name: 'rb-vert', inputValue: 'Newspapers, magazines, flyers and etc.'},
                    {boxLabel: 'Friends', name: 'rb-vert', inputValue: 'Friends'}
                ]

			},{
				columnWidth: .9,
                layout: 'form',
                border: false,
                anchor: '90%',
				xtype: 'checkboxgroup',
                fieldLabel: 'What are your interests?',
                itemCls: 'x-check-group-alt',
                columns: 3,
				allowBlank: false,
                items: [
                    {boxLabel: 'Advertising', name: 'interests[]', inputValue: 'Advertising'},
					{boxLabel: 'Broadcasting', name: 'interests[]', inputValue: 'Broadcasting'},
					{boxLabel: 'Entertainment', name: 'interests[]', inputValue: 'Entertainment'},
					{boxLabel: 'Events', name: 'interests[]', inputValue: 'Events'},
					{boxLabel: 'Marketing', name: 'interests[]', inputValue: 'Marketing'},
					{boxLabel: 'Media', name: 'interests[]', inputValue: 'Media'},
					{boxLabel: 'Outdoor&nbsp;Advertising', name: 'interests[]', inputValue: 'Outdoor Advertising'},
					{boxLabel: 'Production', name: 'interests[]', inputValue: 'Production'},
					{boxLabel: 'Media', name: 'interests[]', inputValue: 'Media'},
					{boxLabel: 'Public&nbsp;Relation', name: 'interests[]', inputValue: 'Public Relation'},
					{boxLabel: 'Publishing', name: 'interests[]', inputValue: 'Publishing'},
					{boxLabel: 'Talent&nbsp;Zone', name: 'interests[]', inputValue: 'Talent Zone'},
					{boxLabel: 'Others', name: 'interests[]', inputValue: 'Others'}
                ]

			},{
				columnWidth: .4,
                layout: 'form',
                border: false,
                anchor: '55%',
				xtype: 'textfield',
				fieldLabel:'What do you expect to see?',
				allowBlank: false,
				name: 'expectToSee'
				
			},{
				columnWidth: .1,
                layout: 'form',
                border: false,
                anchor: '30%',
				xtype: 'radiogroup',
                fieldLabel: 'Subscribe to newsletters',
                itemCls: 'x-check-group-alt',
                columns: 1,
                vertical: true,
				allowBlank: false,
                items: [
                    {boxLabel: 'Yes', name: 'receiveNewsletter', inputValue: 'Yes'},
                    {boxLabel: 'No', name: 'receiveNewsletter', inputValue: 'No'}
                ]

			}],
            formUrl: "/index.php/SiteDataSavingService/Module,SaveRegistration/",
            width: 700,
            labelWidth: 150,
            title: "Follow up",
            frame: false,
            renderTo: this.options.renderTo
        
        })
        contactForm.showBlank("window.location = '/index.php'")
    }
}

