/**
 * @author George Miller
 */
Ext.onReady(function(){
//initalize the quicktips
Ext.QuickTips.init();
//reference the default image
Ext.BLANK_IMAGE_URL = 'ext-3.0/resources/images/default/s.gif';

function submitForm(){
	var form = Ext.getCmp('emailform');
						
						if (form.form.isValid()) {
							Ext.MessageBox.show({
            title: 'Please wait',
           msg: 'Saving Edited Data...',
           progressText: 'Collecting items...',
           width:300,
           progress:true,
           closable:false
       });
							form.form.submit({
								params: {
									action: 'submit'
								},
								success: function(form, action){
									var name = action.result.name;
									// this hideous block creates the bogus progress
									var f = function(v){
										return function(){
											if (v == 12) {
												Ext.MessageBox.hide();
												Ext.MessageBox.show({
							title: 'Successful Entry',
							width: 400,
							height: 200,
							msg: 'Thank you '+name+',<br>You have successfully entered your details',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.INFO
						});
											}
											else {
												var i = v / 11;
												Ext.MessageBox.updateProgress(i, Math.round(100 * i) + '% completed');
											}
										};
									};
									for (var i = 1; i < 13; i++) {
										setTimeout(f(i), i * 200);
									}
							form.reset();
							
								},
								failure: function(form, action){
									if (action.result.errors.length > 0) {
										Ext.MessageBox.show({
							title: 'Error',
							width: 250,
							height: 150,
							msg: action.result.errors,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.WARNING
						})
									}
								}
							});
						}
						else {
							Ext.MessageBox.show({
							title: 'Error',
							width: 250,
							height: 150,
							msg: 'Please correct indicated errors',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.WARNING
						})
						}
}

var form = new Ext.form.FormPanel({
	width: 280,
	height: 150,
	id: 'emailform',
	labelAlign: 'top',
	renderTo: 'Form',
	labelWidth: 200,
	unstyled: true,
	keys: {
        key: [10,13],
        fn: function(){ submitForm() }
    },
	defaultType: 'textfield',
	url: 'includes/form_process.php',
	items: [{
		name: 'name',
		width: 250,
		allowBlank: false,
		fieldLabel: 'Name'
	},{
		name: 'email',
		width: 250,
		allowBlank: false,
		id: 'emailbox',
		vtype: 'email',
		fieldLabel: 'Email Address'
	}]
       ,buttons: [{
            text: 'Submit',
			iconCls: 'add16',
			scale: 'medium',
			handler: function(){
						submitForm();
					}
        },{
            text: 'Cancel',
			iconCls: 'cancel16',
			scale: 'medium',
			handler: function(){
				Ext.getCmp('emailform').getForm().reset();
			}
        }]	
});

//remote check the email
	//check the cg_code on blur
	var	emailBox = Ext.getCmp('emailbox');

	emailBox.on('blur', function(field){
		var value = field.getValue();
		Ext.Ajax.request({
					url: 'includes/check_email.php',
					waitMsg: 'Saving Data...',
					params: {
						email: value
					},
					success: function(response, options){
						var res = Ext.decode(response.responseText);
						if(res.msg == 'No'){
							emailBox.markInvalid('Email already taken, choose another');
						}
					}
				});
				});
});