/******************************************************\
 *           Ph-f.org - registration form
 *
 * Author: Piotr Banasiak
 * Date: 26.04.2009
 * Last modified: 15.05.2011
 \******************************************************/

/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

// Override Datefield to add initial datePicker value
Ext.override( Ext.form.DateField, {
    // private
    // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
    // Change: adds a 'defaultValue' option to be used if the picker is opened on an empty field
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        if(this.menu == null){
            this.menu = new Ext.menu.DateMenu();
        }
        Ext.apply(this.menu.picker,  {
            minDate : this.minValue,
            maxDate : this.maxValue,
            disabledDatesRE : this.disabledDatesRE,
            disabledDatesText : this.disabledDatesText,
            disabledDays : this.disabledDays,
            disabledDaysText : this.disabledDaysText,
            format : this.format,
            showToday : this.showToday,
            minText : String.format(this.minText, this.formatDate(this.minValue)),
            maxText : String.format(this.maxText, this.formatDate(this.maxValue))
        });
        this.menu.on(Ext.apply({}, this.menuListeners, {
            scope:this
        }));
          if( typeof this.defaultValue == 'string' ) {
             this.defaultValue = Date.parseDate( this.defaultValue, this.format );
          }
        this.menu.picker.setValue(this.getValue() || this.defaultValue || new Date());
        this.menu.show(this.el, "tl-bl?");
    }
});


// Add the additional 'advanced' VTypes
Ext.apply(Ext.form.VTypes, {
    daterange : function(val, field) {
        var date = field.parseDate(val);

        if(!date){
            return;
        }
        if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
            var start = Ext.getCmp(field.startDateField);
            start.setMaxValue(date);
            start.validate();
            this.dateRangeMax = date;
        } 
        else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
            var end = Ext.getCmp(field.endDateField);
            end.setMinValue(date);
            end.validate();
            this.dateRangeMin = date;
        }
        /*
         * Always return true since we're only using this vtype to set the
         * min/max allowed values (these are tested for after the vtype test)
         */
        return true;
    }
});


Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
  //  Ext.form.Field.prototype.msgTarget = 'side';

    //var bd = Ext.getBody();

    /*
     * ================ Registration Form =======================
     */

/*
    var getRegFormValues = function() {
	var o = {};

	var name1Value = Ext.getCmp('name1').getValue();
	o.name1 = name1Value;

	var surname1Value = Ext.getCmp('surname1').getValue();
	o.surname1 = surname1Value;

	return o;
    };
*/

    var amountCount = new Ext.Toolbar.TextItem('Kwota wpisowego: <b>100zł</b>');
    var amount = 200;
    var ifpartner = false;

    var fsf = new Ext.form.FormPanel({
        labelWidth: 175, // label settings here cascade unless overridden
//        url:'content_reg.php',
        standardSubmit: true,
        frame: true,
        title: 'Formularz rejestracji',
        bodyStyle: 'padding:5px 5px 0',
        //width: 455,
	//bbar: new Ext.StatusBar({
        //    id: 'amount-status',
        //    items: [amountCount, ' ']
        //}),
        items: [{
            xtype:'fieldset',
            autoHeight:true,
           // defaults: {width: 210},
            defaultType: 'textfield',
            items :[{
                    fieldLabel: 'Imię',
		    id: 'name1',
                    name: 'name1',
		    width: 300,
                    allowBlank: false
                },{
                    fieldLabel: 'Nazwisko',
                    id: 'surname1',
                    name: 'surname1',
		    width: 300,
                    allowBlank: false
                },{
                    fieldLabel: 'Tytuł naukowy',
                    name: 'title1',
		    width: 300
                },{
                    fieldLabel: 'Instytucja',
                    name: 'institution1',
		    width: 300
                }, {
                    fieldLabel: 'Email',
                    name: 'email1',
		    width: 300,
                    vtype: 'email',
                    allowBlank: false
		},{
		    fieldLabel: 'Adres',
		    name: 'address1',
		    width: 300,
                    allowBlank: false
		},{
		    fieldLabel: 'Kod pocztowy',
		    name: 'postcode1',
		    width: 300,
                    allowBlank: false
		},{
		    fieldLabel: 'Miasto',
		    name: 'city1',
		    width: 300,
                    allowBlank: false
		},{
		    xtype:'fieldset',
		    title: 'Dane do faktury',
		    collapsible: true,
		    collapsed: true,
		    autoHeight: true,
		    defaults: {width: 300},
		    defaultType: 'textfield',
		    tools: [{
               		id: 'help',
               		qtip: { title: 'Info', text: 'W przypadku potrzeby otrzymania faktury proszę kliknąć strzałkę (rozwinie się okienko) i podać dane.', dismissDelay: 10000 },
               		handler: function(event, toolEl, panel) { }
           	    }],
		    items :[{
			fieldLabel: 'Nazwa instytucji',
			name: 'fac_institution1'
		    },{
			fieldLabel: 'NIP',
			name: 'fac_nip1'
		    },{
			fieldLabel: 'Adres',
			name: 'fac_address1'
		    },{
			fieldLabel: 'Kod pocztowy',
			name: 'fac_postcode1'
		    },{
			fieldLabel: 'Miasto',
			name: 'fac_city1'
		    }
		    ]
                }
            ]
        },{
            xtype:'fieldset',
            checkboxToggle:true,
            title: 'Osoba towarzysząca',
            autoHeight:true,
            //defaults: {width: 210},
            defaultType: 'textfield',
            collapsed: true,
	    listeners: {
                'expand': {
                    fn: function(fieldpanel){
			    amount += 100;
			    Ext.fly(amountCount.getEl()).update('Kwota wpisowego: <b>'+amount+'zł</b>');
			    ifpartner = true;
			    //var accom01 = Ext.get('accom01');
			    //accom01.setDisabled(true);
	                },
                    buffer: 1 // buffer to allow the value to update first
                },
		'collapse': {
		    fn: function(fieldpanel) {
			    amount -= 100;
			    Ext.fly(amountCount.getEl()).update('Kwota wpisowego: <b>'+amount+'zł</b>');
			    ifpartner = false;
			    //var accom01 = Ext.get('accom01');
			    //accom01.setDisabled(false);
		    },
		    buffer: 1
		}
            },
	    tools: [{
		id: 'help',
		qtip: { title: 'Info', text: 'W przypadku przyjazdu z osobą towarzysząca proszę zaznaczyć pole (rozwinie się okienko) i podać dane.', dismissDelay: 10000 },
		handler: function(event, toolEl, panel) { }
	    }],
            items :[{
                    fieldLabel: 'Imię',
                    name: 'name2',
		    width: 300
                },{
                    fieldLabel: 'Nazwisko',
                    name: 'surname2',
		    width: 300
                },{
                    fieldLabel: 'Tytuł naukowy',
                    name: 'title2',
		    width: 300
                },{
                    fieldLabel: 'Instytucja',
                    name: 'institution2',
		    width: 300
                }, {
                    fieldLabel: 'Email',
                    name: 'email2',
		    width: 300,
                    vtype: 'email'
		},{
		    fieldLabel: 'Adres',
		    name: 'address2',
		    width: 300
		},{
		    fieldLabel: 'Kod pocztowy',
		    name: 'postcode2',
		    width: 300
		},{
		    fieldLabel: 'Miasto',
		    name: 'city2',
		    width: 300
		},{
		    xtype:'fieldset',
		    title: 'Dane do faktury',
		    collapsible: true,
		    collapsed: true,
		    autoHeight:true,
		    defaults: {width: 300},
		    defaultType: 'textfield',
		    tools: [{
               		id: 'help',
               		qtip: { title: 'Info', text: 'W przypadku potrzeby otrzymania faktury proszę kliknąć strzałkę (rozwinie się okienko) i podać dane.', dismissDelay: 10000 },
               		handler: function(event, toolEl, panel) { }
           	    }],
		    items :[{
			fieldLabel: 'Nazwa instytucji',
			name: 'fac_institution2'
		    },{
			fieldLabel: 'NIP',
			name: 'fac_nip2'
		    },{
			fieldLabel: 'Adres',
			name: 'fac_address2'
		    },{
			fieldLabel: 'Kod pocztowy',
			name: 'fac_postcode2'
		    },{
			fieldLabel: 'Miasto',
			name: 'fac_city2'
		    }
		    ]
                }
            ]
        },{
            xtype:'fieldset',
            title: 'Czas pobytu',
            autoHeight:true,
            defaultType: 'datefield',
	    items: [{
        	fieldLabel: 'Data przyjazdu',
	        name: 'startdt',
		id: 'startdt',
		minValue: '2011-09-12',
		maxValue: '2011-09-17',
		defaultValue: '2011-09-12',
		vtype: 'daterange',
                allowBlank: false,
		endDateField: 'enddt' // id of the end date field
	    },{
		fieldLabel: 'Data wyjazdu',
        	name: 'enddt',
        	id: 'enddt',
		minValue: '2011-09-12',
		maxValue: '2011-09-17',
		defaultValue: '2011-09-17',
        	vtype: 'daterange',
                allowBlank: false,
        	startDateField: 'startdt' // id of the start date field
      	    }]
        },{
	    xtype: 'fieldset',
            title: 'Noclegi',
            autoHeight: true,
//            defaults: {width: 210},
//            defaultType: 'radio',
	    tools: [{
		id: 'help',
		qtip: { title: 'Info', text: 'Proszę zaznaczyć wybraną opcję noclegu.', dismissDelay: 10000 },
		handler: function(event, toolEl, panel) { }
	    }],
	    items: [{
		xtype: 'radiogroup',
		itemCls: 'x-check-group-alt',
                hideLabel: true,
                allowBlank: false,
                anchor: '95%',
	   	items: [{
                    columnWidth: '.40',
            	    items :[{
                        xtype: 'label',
			text: 'Akademik',
			cls:'x-form-check-group-label'
                    },{
                        xtype: 'radio',
			id: 'accom01',
                    	boxLabel: 'pokój 1 os. z łazienką (60zł/os.)',
			inputValue: 'a1',
			autoWidth: true,
			name: 'accom'
                    },{
                        xtype: 'radio',
                    	boxLabel: 'pokój 2 os. (33zł/os. + 5zł/os.*)',
			inputValue: 'a2',
			autoWidth: true,
                    	name: 'accom'
                //  },{
		//        xtype: 'radio',
                //    	boxLabel: 'pokój 3 os. (33zł/os. + 5zł/os.*)',
		//	inputValue: 'a3',
		//	autoWidth: true,
                //    	name: 'accom'
                    }]
                },{
                    columnWidth: '.40',
                    items: [
                        {xtype: 'label', text: 'Hotel', cls:'x-form-check-group-label'},
                        {xtype: 'radio', boxLabel: 'pokój 1 os. z łazienką (70zł/os.)', name: 'accom', autoWidth: true, inputValue: 'h1l'},
                        {xtype: 'radio', boxLabel: 'pokój 1 os. (65zł/os.)', name: 'accom', autoWidth: true, inputValue: 'h1b'},
                        {xtype: 'radio', boxLabel: 'pokój 2 os. z łazienką (70zł/os.)', name: 'accom', autoWidth: true, inputValue: 'h2l'},
                        {xtype: 'radio', boxLabel: 'pokój 2 os. (65zł/os.)', name: 'accom', autoWidth: true, inputValue: 'h2b'}
                    ]
                },{
                    columnWidth: '.20',
                    items: [
                        {xtype: 'label', text: '-', cls:'x-form-check-group-label'},
                        {xtype: 'radio', boxLabel: 'bez noclegu', name: 'accom', autoWidth: true, inputValue: '0'}
                    ]
                }]
            },{
	    xtype: 'panel',
	    hideLabel: true,
	    autoHeight: true,
	    autoWidth: true,
	    //width: 400,
	    html: '<b>Uwaga!</b><br />*Dopłata 5zł/os naliczana jest w pokojach 2-os. w akademiku tylko za pierwszy nocleg.'
	    }]
/*
            items :[{
                    fieldLabel: 'pokój 1 os.',
                    name: 'accom'
                },{
                    fieldLabel: 'pokój 2 os.',
                    name: 'accom'
                },{
                    fieldLabel: 'pokój 3 os.',
                    name: 'accom'
                }
            ]
*/
        },{
            xtype: 'fieldset',
            title: 'Wyżywienie',
            autoHeight: true,
            defaults: {width: 300},
            defaultType: 'checkbox',
	    labelWidth: 275,
	    tools: [{
		id: 'help',
		qtip: { title: 'Info', text: 'Proszę zaznaczyć wybraną opcję wyżywienia. Brak selekcji oznacza wybór opcji bez posiłków.'},
		handler: function(event, toolEl, panel) { }
	    }],
            items :[{
                    fieldLabel: 'śniadanie - 10zł/os.',
		    inputValue: '1',
                    name: 'food-s'
                },{
                    fieldLabel: 'obiad - 12zł/os.',
		    inputValue: '1',
                    name: 'food-o'
                },{
                    fieldLabel: 'kolacja - 10zł/os.',
		    inputValue: '1',
                    name: 'food-k'
                },{
		    xtype: 'panel',
		    hideLabel: true,
		    autoHeight: true,
		    autoWidth: true,
		    //width: 400,
		    html: '<b>Uwaga!</b><br />Należy zaznaczyć wszystkie wybrane posiłki. Brak selekcji będzie oznaczał rezygnację z wyżywienia.<br />W dniu przyjazdu posiłki są wydawane po godz. 18:00 (kolacja).<br />W dniu wyjazdu posiłki są wydawane do godz. 09:30 (śniadanie).'
                }
            ]
        },{
            xtype: 'panel',
            title: 'Informacje',
            autoHeight: true,
	    html: '<p style="font-face: Verdana, sans-serif; font-size: 12px; line-height: 130%">Przypominamy, że warunkiem przyjęcia zgłoszenia uczestnictwa jest przesłanie wyliczonej (za nocleg i wyżywienie) kwoty oraz wpisowego (100 zł/os.) na konto:<br />Fundacja "Kocham moje miasto"<br />nr konta: <b>19 1140 1108 0000 2041 4100 1001</b><br />W tytule przelewu prosimy podać imię, nazwisko oraz dopisek: "FF 2011 wpisowe+koszty".<br />Osoby nie korzystające z noclegu i wyżywienia obowiązuje jedynie wpłata wpisowego.<br /><b>Ostateczny termin wpłat i rejestracji: 20 sierpnia 2011 r.</b></p>'
 // Osoby nie korzystające z noclegu i wyżywienia obowiązuje jedynie wpłata wpisowego.
        }],

        buttons: [{
            text: 'Wyślij',
	    handler: function() {
//	    	var reloadParams = getRegFormValues();
		fsf.getForm().getEl().dom.action = 'index.php?id=15';
		fsf.getForm().getEl().dom.method = 'POST';
		fsf.getForm().submit(); //{params: reloadParams});
	    }
        }]
    });

    infoPanel = new Ext.Panel({
            autoHeight: true,
	    html: '<div style="background-color: #dfe8f6"><p style="background-color: #dfe8f6">&nbsp;</p><p style="background-color: #dfe8f6; margin: 0px 10px; font-face: Verdana, sans-serif; font-size: 12px; line-height: 130%">Po zatwierdzeniu wypełnionego formularza na podany przez Państwa adres e-mail zostanie wysłane potwierdzenie rejestracji wraz z sumaryczną kwotą do zapłaty.</p><p style="background-color: #dfe8f6; font-face: Verdana, sans-serif; font-size: 12px; text-align: center;"> Dziękujemy! </p><p style="background-color: #dfe8f6 ">&nbsp;</p></div>'
    });

    fsf.render('register');
    infoPanel.render('info_panel');
/*
    var rfSubmit = function() {
	fsf.getForm().getEl().dom.action = 'content_reg.php';
	fsf.getForm().getEl().dom.method = 'POST';
	fsf.getForm().submit();
    };
*/
/*
    var regFormSubmit = function(a, e) {
	try {
	    var reloadParams = getRegFormValues();
//	    var submitParams = Ext.apply({}, reloadParams);
	    fsf.getForm().submit({
		clientValidation: true,
		url: 'content_reg.php',
		params: reloadParams,
		success: function(form, action) {
			Ext.Msg.alert("Success", action.result.msg);
		},
		failure: function(form, action) {
	          switch (action.failureType) {
		    case Ext.form.Action.CLIENT_INVALID:
			Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values");
	                break;
		    case Ext.form.Action.CONNECT_FAILURE:
			Ext.Msg.alert("Failure", "Ajax communication failed");
	                break;
		    case Ext.form.Action.SERVER_INVALID:
			 Ext.Msg.alert("Failure", action.result.msg);
		  } 
		}
	    });
	}
	catch (exc) {
	    Ext.Msg.alert('Błąd', exc);
	}
	return false;
    };
*/
/*
    fsf.getForm().submit({
	clientValidation: true,
	url: 'content_reg.php',
	params: {
	    name1: 'przyklad'
        },
	success: function(form, action) {
		Ext.Msg.alert("Success", action.result.msg);
	},
	failure: function(form, action) {
          switch (action.failureType) {
            case Ext.form.Action.CLIENT_INVALID:
                Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values");
                break;
            case Ext.form.Action.CONNECT_FAILURE:
                Ext.Msg.alert("Failure", "Ajax communication failed");
                break;
            case Ext.form.Action.SERVER_INVALID:
               Ext.Msg.alert("Failure", action.result.msg);
       	  }
    	}
    });
*/
});

