/*
 * PureTrak Contact Us Page Javascript File
 * Copyright(c) 2008-2009, Pure Voice Communictions, Inc.
 * http://www.puretrak.com
 * @author Patrick Hallinan
 *
 */
 
Ext.onReady(function() {
	// -- Enable quick tips - feedback for any errors
	Ext.QuickTips.init();
	
	// 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;
		}
	});
		
	var countries_cb_store = new Ext.data.Store({
		autoLoad	: true,
		proxy: new Ext.data.HttpProxy({
			url		: '/checkout/get_countries_list',
			method	: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			idProperty		: 'value',
			root			: 'data',
			successProperty	: 'success',
			fields	: [
				{name: 'value', type: 'string'},
				{name: 'option', type: 'string'}
			]
		})
	});
	
	var select_contact_form_panel = new Ext.FormPanel({
		title		: 'Contact Selection',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 135,
		renderTo	: 'contact_selection_form_container',
		items	: [{
			xtype			: 'combo',
			width			: 225,
			name			: 'contact_selection',
			fieldLabel		: 'Select Contact Reason',
			hiddenName		: 'view',
			store			: new Ext.data.SimpleStore({
				fields	: ['selection_id', 'selection_title'],
				data	: [[0,'General Information'],[1,'Sales Question'],[2,'Returns / Refunds'],[3,'Team Sponsorship'],[4,'Event Sponsorship'],[5,'Product Reviews']]
			}),
			mode			: 'local',
			valueField		: 'selection_id',
			displayField	: 'selection_title',
			emptyText		: 'Select a reason...',
			typeAhead		: false,
			readOnly		: true,
			forceSelection	: true,
			triggerAction	: 'all',
			allowBlank		: true,
			listeners		: {
				select	: {
					fn	: function(combo, value) {
						// -- Display correct region input field (pass country value)
						toggle_contact_form(value.data.selection_id);
					}
				}
			}
		}]
	});
	
	function toggle_contact_form(id) {
		var form_array = new Array('contact_general_form_container','contact_sales_form_container','contact_return_refund_form_container','contact_team_sponsorship_form_container','contact_event_sponsorship_form_container','contact_product_review_form_container');
		var element_shown = false;
		
		// -- Loop through to slide current form up if needed
		for(var i=0; i<form_array.length; i++) {
			var element = Ext.get(form_array[i]);
			if(element.isVisible() && i != id) {
				element.slideOut('t', {
					easing	: 'easeOut',
					duration: 1,
					useDisplay	: true
				});
				var e = Ext.get(form_array[id]);
				e.pause(1.2).slideIn('t', {
					easing	: 'easeOut',
					duration: 1
				});
				element_shown = true;
			}
		}
		if(! element_shown) {
			var e = Ext.get(form_array[id]);
			if(! e.isVisible()) {
				e.slideIn('t', {
					easing	: 'easeOut',
					duration: 1
				});
			}
		}
	}
	
	var general_info_form_panel = new Ext.FormPanel({
		title		: 'General Information',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_general_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			fieldLabel	: 'Subject',
			name		: 'subject',
			width		: 350,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Message',
			name	: 'message',
			width	: 490,
			height	: 250,
			allowBlank	: false
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				general_info_form_panel.getForm().submit({
					url		: '/contact/submit_general_information',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						general_info_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});
	
	var sales_question_form_panel = new Ext.FormPanel({
		title		: 'Sales Question',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_sales_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			fieldLabel	: 'Company',
			name		: 'company',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Subject',
			name		: 'subject',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Message',
			name	: 'message',
			width	: 490,
			height	: 250,
			allowBlank	: false
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				sales_question_form_panel.getForm().submit({
					url		: '/contact/submit_sales_question',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						sales_question_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});
	
	var return_refund_form_panel = new Ext.FormPanel({
		title		: 'Returns / Refunds',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_return_refund_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			xtype		: 'numberfield',
			fieldLabel	: 'Order ID#',
			name		: 'order_id',
			width		: 150,
			maxLength	: 100,
			minLength	: 1,
			allowBlank	: false,
			allowNegative	: false,
			allowDecimals	: false
		},{
			fieldLabel	: 'Subject',
			name		: 'subject',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Message',
			name	: 'message',
			width	: 490,
			height	: 250,
			allowBlank	: false
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				return_refund_form_panel.getForm().submit({
					url		: '/contact/submit_return_refund',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						return_refund_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});
	
	var team_sponsorship_form_panel = new Ext.FormPanel({
		title		: 'Team Sponsorship',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_team_sponsorship_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			fieldLabel	: 'Your position with the team',
			name		: 'team_position',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Team Name',
			name		: 'team_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Team website URL',
			name		: 'team_url',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype			: 'combo',
			width			: 250,
			id				: 'team_country_cb',
			name			: 'team_country',
			hiddenName		: 'team_country',
			fieldLabel		: 'Team\'s Primary Country',
			store			: countries_cb_store,
			mode			: 'local',
			valueField		: 'value',
			displayField	: 'option',
			emptyText		: 'Select a country...',
			typeAhead		: true,
			selectOnFocus	: true,
			forceSelection	: true,
			allowBlank		: false,
			triggerAction	: 'all'
		},{
			xtype			: 'combo',
			width			: 250,
			name			: 'sponsorship_type',
			fieldLabel		: 'What type of sponsorship are you seeking from PureTrak?',
			hiddenName		: 'sponsorship_type',
			store			: new Ext.data.SimpleStore({
				fields	: ['type_id', 'type_name'],
				data	: [['Product Sponsprship', 'Product Sponsprship'],['Financial Sponsorship','Financial Sponsorship'],['Other Partnership','Other Partnership']]
			}),
			mode			: 'local',
			valueField		: 'type_id',
			displayField	: 'type_name',
			emptyText		: 'Select a sponsorship type...',
			typeAhead		: false,
			selectOnFocus	: true,
			readOnly		: true,
			forceSelection	: true,
			triggerAction	: 'all',
			allowBlank		: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'List all past LAN events for the previous 12 months with team placements',
			name	: 'past_events',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'List all planned events for the next 12 months',
			name	: 'planned_events',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What other sponsors does your team currently have?',
			name	: 'other_sponsors',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What can you provide PureTrak?',
			name	: 'provided_to_puretrak',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What would you like to see provided to you by PureTrak?',
			name	: 'provided_by_puretrak',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Additional Comments',
			name	: 'additional_comments',
			width	: 490,
			height	: 160
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				team_sponsorship_form_panel.getForm().submit({
					url		: '/contact/submit_team_sponsorship',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						team_sponsorship_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});
	
	var event_sponsorship_form_panel = new Ext.FormPanel({
		title		: 'Event Sponsorship',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_event_sponsorship_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			fieldLabel	: 'Your position with the event',
			name		: 'event_position',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Event Name',
			name		: 'event_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Event website URL',
			name		: 'event_url',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Event Location',
			name		: 'event_location',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype			: 'combo',
			width			: 250,
			name			: 'sponsorship_type',
			fieldLabel		: 'What type of sponsorship are you seeking from PureTrak?',
			hiddenName		: 'sponsorship_type',
			store			: new Ext.data.SimpleStore({
				fields	: ['type_id', 'type_name'],
				data	: [['Product Sponsprship', 'Product Sponsprship'],['Financial Sponsorship','Financial Sponsorship'],['Other Partnership','Other Partnership']]
			}),
			mode			: 'local',
			valueField		: 'type_id',
			displayField	: 'type_name',
			emptyText		: 'Select a sponsorship type...',
			typeAhead		: true,
			selectOnFocus	: true,
			forceSelection	: true,
			triggerAction	: 'all',
			allowBlank		: false
		},{
			xtype		: 'datefield',
			fieldLabel	: 'Event Start Date',
			name		: 'event_start_date',
			id			: 'event_start_date',
			vtype		: 'daterange',
			endDateField: 'event_end_date',
			allowBlank	: false
		},{
			xtype		: 'datefield',
			fieldLabel	: 'Event End Date',
			name		: 'event_end_date',
			id			: 'event_end_date',
			vtype		: 'daterange',
			startDateField: 'event_start_date',
			allowBlank	: false
		},{
			xtype	: 'numberfield',
			fieldLabel	: 'Expected number of participants (not including spectators)',
			name	: 'estimated_participants',
			width	: 200,
			allowDecimals	: false,
			allowNegative	: false,
			minValue	: 5,
			allowBlank	: false
		},{
			xtype	: 'numberfield',
			fieldLabel	: 'Estimated number of expected spectators',
			name	: 'estimated_spectators',
			width	: 200,
			allowDecimals	: false,
			allowNegative	: false,
			minValue	: 5,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'List all planned events for the next 12 months if this is a series',
			name	: 'planned_events',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What media coverage is expected for your event? Both offline and online.',
			name	: 'media_coverage',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What other sponsors does your event currently have?',
			name	: 'other_sponsors',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What can you provide PureTrak?',
			name	: 'provided_for_puretrak',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'What would you like to see provided to you by PureTrak?',
			name	: 'provided_by_puretrak',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Additional Comments',
			name	: 'additional_comments',
			width	: 490,
			height	: 160
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				event_sponsorship_form_panel.getForm().submit({
					url		: '/contact/submit_event_sponsorship',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						event_sponsorship_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});
	
	var product_review_form_panel = new Ext.FormPanel({
		title		: 'Event Sponsorship',
		width		: 530,
		autoHeight	: true,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:10px;',
		labelWidth	: 150,
		labelAlign	: 'top',
		defaultType	: 'textfield',
		renderTo	: 'contact_product_review_form_container',
		items	: [{
			fieldLabel	: 'First/Last Name',
			name		: 'full_name',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Email Address',
			name		: 'email_address',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false,
			vtype		: 'email'
		},{
			fieldLabel	: 'Your position with the review organization',
			name		: 'review_position',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			fieldLabel	: 'Website URL where review will be published',
			name		: 'review_url',
			width		: 275,
			maxLength	: 250,
			minLength	: 5,
			allowBlank	: false
		},{
			xtype			: 'combo',
			width			: 250,
			id				: 'review_country_cb',
			name			: 'review_country',
			hiddenName		: 'review_country',
			fieldLabel		: 'Country of publication',
			store			: countries_cb_store,
			mode			: 'local',
			valueField		: 'value',
			displayField	: 'option',
			emptyText		: 'Select a country...',
			typeAhead		: true,
			selectOnFocus	: true,
			forceSelection	: true,
			allowBlank		: false,
			triggerAction	: 'all'
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Which products are you interested in reviewing?',
			name	: 'interested_products',
			width	: 490,
			height	: 160,
			allowBlank	: false
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Delivery address for products',
			name	: 'delivery_address',
			width	: 490,
			height	: 160
		},{
			xtype	: 'textarea',
			fieldLabel	: 'Additional Comments',
			name	: 'additional_comments',
			width	: 490,
			height	: 160
		}],
		buttons	: [{
			text	: 'Submit Inquiry',
			width	: 150,
			handler	: function() {
				product_review_form_panel.getForm().submit({
					url		: '/contact/submit_product_review',
					method	: 'POST',
					waitMsg	: 'Submitting your inquiry, please wait...',
					success	: function(form, action) {
						product_review_form_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Inquiry Sent',
							msg		: 'Your message has been sent, please allow up to 72 hours for a reply. Thank you!',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.INFO,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							Ext.Msg.show({
								title	: 'Unable to send inquiry',
								msg		: 'ERROR: '+action.result.error_message,
								buttons	: Ext.Msg.OK,
								icon	: Ext.MessageBox.INFO,
								minWidth: 350
							});
						}
					}
				});
			}
		}]
	});

});
