var total=149;

Ext.apply(Ext.form.VTypes, {
	password: function(value, field) {
	if(field.initialPasswordField) {
	    var pass = Ext.getCmp(field.initialPasswordField);
	    return (value == pass.getValue());
	}
	return true;	
	},
	passwordText: 'Podano błędne hasła!'
});

Ext.apply(Ext.form.VTypes, {
    numeric: function(v) {
	return /^\d{1,3}$/.test(v);
    },
    numericText: 'Wpisz poprawną liczbę gości (1-50)'
});

Ext.apply(Ext.form.VTypes, {
    codep: function(v) {
	return /^\d\d\-\d\d\d$/.test(v);
    },
    codepText: 'Wpisz poprawny kod pocztowy (XX-XXX)'
});


Ext.onReady(function(){

    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'under';
//    var button = Ext.get('clickLogin');

    function makePrice() {
	var p = Ext.get('price');
	var size = Ext.get('sizeComboId').getValue();
	var abo = Ext.get('aboComboId').getValue();
//	var total;
	var w=1;
	if(abo && size) {
	    if(size=='2000MB' && abo=='jeden miesiąc') {total=33;w=0;}
	    if(size=='2000MB' && abo=='pół roku') {total=179;}
	    if(size=='2000MB' && abo=='rok') {total=333;}
	    if(size=='5000MB' && abo=='jeden miesiąc') {total=44;w=0;}
	    if(size=='5000MB' && abo=='pół roku') {total=222;}
	    if(size=='5000MB' && abo=='rok') {total=399;}
	    if(size=='10000MB' && abo=='jeden miesiąc') {total=69;w=0;}
	    if(size=='10000MB' && abo=='pół roku') {total=388;}
	    if(size=='10000MB' && abo=='rok') {total=699;}
	    if(size=='50000MB' && abo=='jeden miesiąc') {total=199;w=0;}
	    if(size=='50000MB' && abo=='pół roku') {total=1099;}
	    if(size=='50000MB' && abo=='rok') {total=1990;}
	    var guest = Ext.get('regGuestId').getValue();
	    guest=guest-1;
//	    if(w==1) {
//		var j=total*0.10;
//		total=total+(j*guest);
//	    } else {
		total=total+(40*guest);
//	    }
	    
//	    total = total * 100;
//	    total = Math.round(total)/100;

	    p.update('<center><br><b>Cena: '+total+' zł</b><br><br></center>');
	}
    }
    var button2 = Ext.get('clickReg');

     
    var reg = new Ext.FormPanel({
	id:'regPanel',
        labelWidth:150,
        url:'/cgi-bin/reg.pl', 
	params: {price: total},
        frame:true, 
        title:'Zakładanie max.edysk', 
        defaultType:'textfield',
	monitorValid:true,
//	keys: [{key: [10,13], fn: function(){ Ext.getCmp('loginbutton').fireEvent('click')}}],
        items:[
	    {
		xtype: 'combo',
		name: 'regSize',
		id: 'sizeComboId',
		fieldLabel:'pojemność max.edysk',
		mode:'local',
		width: 130,
		store: new Ext.data.SimpleStore({
		fields: ['id', 'size'],
		    data: [
		    ['1', '2000MB'],
		    ['2', '5000MB'],
		    ['3', '10000MB'],
		    ['4', '50000MB']
		    ]
		}),
		displayField:'size',
		triggerAction:'all',
		valueField:'id',
		value:'1',
		allowBlank:false,
		blankText:'Proszę wybrać pojemność max.edysk',
		listeners: {
		    'change': function() {
			makePrice();
		    }
		}
		
	    },{
		xtype: 'combo',
		name: 'regAbo',
		id: 'aboComboId',
		fieldLabel:'czas trwania abonamentu',
		mode:'local',
		triggerAction:'all',
		width: 130,
		store: new Ext.data.SimpleStore({
		fields: ['id', 'time'],
		    data: [
		    ['1', 'jeden miesiąc'],
		    ['6', 'pół roku'],
		    ['12', 'rok']
		    ]
		}),
		displayField:'time',
		valueField:'id',
		value:'6',
		allowBlank:false,
    		blankText:'Proszę okres trwania abonamentu',
		listeners: {
		    'change': function() {
			makePrice();
		    }
		}

	    },{
		xtype:'textfield',
		fieldLabel: 'liczba gości',
		name:'regGuest',
		id:'regGuestId',
		value:'1',
		disable:true,
		vtype:'numeric',
		listeners: {
		    'change': function() {
			makePrice();
		    }
		}
	
	    },
	    new Ext.Panel({html:'<center><br><b>Cena: 149 zł</b><br><br></center>',id:'price'}),
	    {
                fieldLabel:'nick', 
                name:'regUsername',
		id:'regUsernameId',
                allowBlank:false,
		vtype:'alphanum',
		blankText:'Proszę podać nick'
            },{ 
                fieldLabel:'hasło', 
                name:'regPassword', 
		id:'regPassword',
                inputType:'password',
		vtype:'password',
                allowBlank:false,
		blankText:'Proszę podać hasło'
            },{ 
                fieldLabel:'powtórz hasło', 
                name:'regPassword2', 
                inputType:'password', 
		vtype:'password',
                allowBlank:false,
		initialPasswordField: 'regPassword',
		blankText:'Proszę podać hasło'
            },{ 
                fieldLabel:'e-mail', 
                name:'regEmail', 
                allowBlank:false,
		vtype:'email',
		vtypeText:'Wprowadź poprawny adres email',
		blankText:'Proszę podać adres email'
            },{
		fieldLabel:'firma lub nazwisko',
		name:'regCompany',
		allowBlank:false,
		blankText:'Proszę wpisać firmę lub nazwisko'
	    },{
		fieldLabel:'ulica i numer domu',
		name:'regAddress',
		allowBlank:false,
		blankText:'Proszę wpisać ulicę i numer domu'
	    },{
		fieldLabel:'kod pocztowy',
		name:'regCode',
		allowBlank:false,
		blankText:'Proszę wpisać kod pocztowy',
		vtype:'codep'
	    },{
		fieldLabel:'miasto',
		name:'regCity',
		allowBlank:false,
		blankText:'Proszę wpisać miasto'
	    },{
		fieldLabel:'telefon',
		name:'regPhone',
		allowBlank:true,
		blankText:'Proszę wpisać numer telefonu'
	    },{
		fieldLabel:'nip',
		name:'regNip',
		allowBlank:true
	    }],
 
        buttons:[{ 
		id: 'regButton',
                text:'Zakładam',
                formBind: true,	
                handler:function(){ 
                    reg.getForm().submit({ 
                        method:'POST', 
			params: {ran: 'test', price: total},
                        waitTitle:'Zakładam', 
                        waitMsg:'Proszę czekać...',
                        success:function(form, action){ 
				obj = Ext.util.JSON.decode(action.response.responseText);
//				Ext.Msg.alert('confirm', action.response.responseText);
		                        var redirect = 'http://www.edysk.pl/rejestracjamax_ok.html'; 
		                        window.location = redirect;
                        },
 
                        failure:function(form, action){ 
//                            if(action.failureType == 'server'){
                                obj = Ext.util.JSON.decode(action.response.responseText); 
                                Ext.Msg.alert('Rejestracja', obj.message);
				Ext.get('regUsernameId').focus();
//                            }else{ 
//                                Ext.Msg.alert('Uwaga!', 'Serwer nie odpowiada: ' + obj.error.reason); 
//                            } 
//                            reg.getForm().reset(); 
                        } 
                    }); 
                }
	

            }] 
    });
 
 
    var win2 = new Ext.Window({
	id:'win2reg',
        layout:'fit',
        width:370,
        height:500,
	autoScroll:true,
	closeAction: 'hide',
        closable: true,
	modal: true,
        resizable: false,
        plain: true,
        border: false,
        items: [reg]
	});

    if(button2 != null)
    {
	button2.on('click', function() {
	    win2.show();
	});
    }

    if(document.location == "http://maxnew.edysk.pl/indexz.html") {
	win2.show();
    }

});     			
