﻿var PhotoSurvey = function(){

    var createCookie = function(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    };

    var readCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    };
    
    Ext.onReady(function(){
        if (!readCookie('DS_PhotoSurveyComplete') && readCookie('DS_PhotoSurvey')) {
            var win = new Ext.Window({
                listeners: {
                    'close': function(){
                        createCookie('DS_PhotoSurveyComplete', '1', 365);
                    }
                },
                layout: 'fit',
                buttons: [{
                    text: 'Submit',
                    handler: function(){
                        win.findById('frmSurvey').getForm().submit({
                            success: win.close,
                            failure: win.close,
                            scope: win
                        });
                        Ext.Msg.alert('Thank You!', 'Thank you for your feedback!');
                    }
                }],
                closable: true,
                constrain: true,
                draggable: false,
                height: 400,
                width: 600,
                modal: true,
                resizable: true,
                title: 'Photo Upload Survey',
                items: [{
                    autoScroll: true,
                    layout: 'fit',
                    border: false,
                    items: [{
                        xtype: 'panel',
                        layout: 'fit',
                        autoHeight: true,
                        border: false,
                        html: '<div style="text-align: center;"><img src="/images/national/dsheader.jpg" /></div>' +
                            '<div style="padding: 10px 40px; font-size: 12px;">' +
                            'Thank you for trying the new DigitalSports photo upload tool. ' +
                            'We want to be sure that our tool is serving you properly. ' +
                            'Please fill out the short one question survey below.' +
                            '</div>'
                    },{
                        xtype: 'form',
                        id: 'frmSurvey',
                        url: '/service/PhotoSurvey.ashx',
                        border: false,
                        bodyStyle: 'padding: 15px;',
                        labelWidth: 40,
                        items: [{
                            xtype: 'label',
                            style: 'font-size: 12px;',
                            text: 'Why did you choose not to upload photos at this time?'
                        },{
                            xtype: 'checkboxgroup',
                            hideLabel: true,
                            style: 'padding-left: 15px;',
                            defaults: { hideLabel: true },
                            columns: 1,
                            items: [{
                                xtype: 'checkbox',
                                name: 'Only_Curious',
                                boxLabel: 'I was only curious about uploading photos'
                            },{
                                xtype: 'checkbox',
                                name: 'No_Photos_To_Upload',
                                boxLabel: 'I do not have photos to upload at this time'
                            },{
                                xtype: 'checkbox',
                                name: 'Too_Confusing',
                                boxLabel: 'I found the tool too confusing'
                            },{
                                xtype: 'checkbox',
                                name: 'Wont_Give_Email',
                                boxLabel: 'I did not want to give my email'
                            }]
                        },{
                            xtype: 'textarea',
                            name: 'Other',
                            fieldLabel: 'Other',
                            height: 90,
                            width: 400,
                            labelStyle: 'padding-left: 15px;'
                        }]
                    }]
                }]
            });
            win.show();
            win.center();
        }
    });
    
    return {
        createSurveyCookie: function(){
            createCookie('DS_PhotoSurvey', '1', 365);
        }
    };
}();