var corporateFormVisible = false;
var corporateForm;
var studentFormVisible = false;
var studentForm = '';
var teamExpanded = false;
var teamFormVisible = false;
var teamFormRendered = false;
var customYearField;

function createFormsInto(divName) {
	Ext.QuickTips.init();
	corporateForm = new Ext.FormPanel(
			{
				labelWidth : 75, // label settings here cascade unless
				// overridden
				frame : true,
				title : 'Company Representative Registration Info',
				bodyStyle : 'padding:5px 5px 0',
				width : 350,
				defaults : {
					width : 230
				},
				defaultType : 'textfield',

				items : [ {
					fieldLabel : 'Name',
					name : 'name',
					allowBlank : false
				}, {
					fieldLabel : 'Position',
					name : 'position'
				}, {
					fieldLabel : 'Company',
					name : 'company',
					allowBlank : false
				}, {
					fieldLabel : 'Email',
					name : 'email',
					vtype : 'email',
					allowBlank : false
				}, {
					name : 'isRegistration',
					value : 'true',
					hidden : true,
					hideLabel : true
				}, {
					name : 'registrationType',
					value : 'corporate',
					hidden : true,
					hideLabel : true
				} ],

				buttons : [ {
					text : 'Proceed to Payment',
					handler : function() {
						corporateForm
								.getForm()
								.submit(
										{
											url : 'registrationInsert.php',
											success : function(form, action) {
												var checkoutForm = document.forms["BB_BuyButtonForm"];
												checkoutForm.item_name_1.value = 'Corporate Benefit Ticket';
												checkoutForm.item_description_1.value = action.result.name
														+ ', '
														+ action.result.company;
												checkoutForm.item_price_1.value = '150.0';
												checkoutForm.submit();
											}
										});
					}
				} ]
			});
	var yearValues = [ [ '2011' ], [ '2012' ], [ '2013' ], [ '2014' ],
			[ 'Other' ] ];
	var yearBox = new Ext.form.ComboBox({
		fieldLabel : 'Year*',
		hiddenName : 'year',
		editable : false,
		allowBlank : false,
		store : new Ext.data.ArrayStore({
			fields : [ 'year' ],
			data : yearValues
		}),
		displayField : 'year',
		triggerAction : 'all',
		selectOnFocus : true,
		mode : 'local',
		emptyText : 'Select a year',
		listeners : {
			scope : this,
			'select' : function() {
				if (yearBox.getValue() == 'Other') {
					customYearField.show();
					customSchoolSelect.show();
					schoolMultiSelect.hide();
				} else {
					customYearField.hide();
					customSchoolSelect.hide();
					schoolMultiSelect.show();
				}
			}
		}
	});
	var schoolMultiSelect = new Ext.ux.form.MultiSelect({
		fieldLabel : 'School(s)*',
		name : 'schoolsMulti',
		width : 230,
		height : 90,
		store : [ [ 'wharton', 'The Wharton School' ],
				[ 'seas', 'School of Engineering and Applied Science' ],
				[ 'sas', 'School of Arts and Sciences' ],
				[ 'nursing', 'School of Nursing' ] ],
		validator : function(value) {
			if (yearBox.getValue() == 'Other' && this.getValue() == '')
				return true;
			else
				return false;
		}
	});
	var customSchoolSelect = new Ext.form.TextField({
		fieldLabel : 'School*',
		name : 'schoolsCustom',
		hidden : true,
		validator : function(value) {
			if (yearBox.getValue() == 'Other' && this.getValue() == '')
				return false;
			else
				return true;
		}
	});
	customYearField = new Ext.form.TextField({
		fieldLabel : 'Enter Year*',
		name : 'customYear',
		hidden : true,
		validator : function(value) {
			if (yearBox.getValue() == 'Other' && this.getValue() == '')
				return false;
			else if (yearBox.getValue() == 'Other' && this.getValue() != '')
				return true;
			else
				return true;
		}
	});
	studentForm = new Ext.FormPanel(
			{
				labelWidth : 90, // label settings here cascade unless
				// overridden
				frame : true,
				title : 'Student Registration Info',
				bodyStyle : 'padding:5px 5px 0',
				fileUpload : true,
				width : 350,
				defaults : {
					width : 230
				},
				defaultType : 'textfield',

				items : [
						{
							name : 'isRegistration',
							value : 'true',
							hidden : true
						},
						{
							name : 'registrationType',
							value : 'student',
							hidden : true
						},
						{
							fieldLabel : 'First Name*',
							name : 'firstName',
							allowBlank : false
						},
						{
							fieldLabel : 'Last Name*',
							name : 'lastName',
							allowBlank : false
						},
						{
							fieldLabel : 'Email*',
							name : 'email',
							vtype : 'email',
							allowBlank : false
						},
						yearBox,
						customYearField,
						schoolMultiSelect,
						customSchoolSelect,
						{
							fieldLabel : 'Major',
							name : 'major'
						},
						{
							xtype : 'fileuploadfield',
							id : 'form-file',
							emptyText : 'Upload resume',
							fieldLabel : 'Resume',
							name : 'resume-path',
							buttonText : '',
							buttonCfg : {
								iconCls : 'upload-icon'
							},
							validator : function(value) {
								var uploadExt = value.substr(value.length - 3,
										3);
								if (uploadExt == '')
									return true;
								if (uploadExt != 'pdf')
									return 'Resume must be in pdf format';
								else
									return true;
							}
						}
				/*
				 * , { xtype : 'fieldset', checkboxToggle : true, width : 325,
				 * title : 'Yes, participating in the case competition <span
				 * id="caseCompInfo" ext:qtip="More Info on the Case Competition
				 * coming soon!" href="blah"
				 * style="text-decoration:none;cursor:pointer">[?]</span>',
				 * autoHeight : true, id : 'teamFieldset', defaults : { width :
				 * 205 }, defaultType : 'textfield', onCheckClick : function() {
				 * if (teamExpanded) { this.collapse(); teamExpanded = false; }
				 * else { this.expand(); teamExpanded = true; } }, collapsed :
				 * true, items : [ { fieldLabel : 'Team Name*', name :
				 * 'teamName', validator : function(value) { if (teamExpanded &&
				 * this.getValue()=='') return 'Team Name Required'; else return
				 * true; } }, { fieldLabel : 'Teammate', name : 'team1' }, {
				 * fieldLabel : 'Teammate', name : 'team2' }, { fieldLabel :
				 * 'Teammate', name : 'team3' }, { fieldLabel : 'Teammate', name :
				 * 'team4' }
				 *  ] }
				 */],

				buttons : [
						{
							text : 'Proceed to Payment',
							handler : function(b, e) {
								if (studentForm.getForm().isValid())
									b.disable();
								studentForm
										.getForm()
										.submit(
												{
													url : 'registrationInsert.php',
													success : function(form,
															action) {
														var checkoutForm = document.forms["BB_BuyButtonForm"];
														checkoutForm.item_name_1.value = 'Corporate Benefit Ticket';
														checkoutForm.item_description_1.value = action.result.name
																+ ', '
																+ action.result.year;
														checkoutForm.item_price_1.value = '30.0';
														checkoutForm.submit();
													}
												});
							}
						}, {
							text : 'Reset',
							handler : function() {
								studentForm.getForm().reset();
							}
						} ]
			});
	studentForm.render(divName);
	studentForm.hide();
	corporateForm.render(divName);
	corporateForm.hide();
	/*
	 * new Ext.ToolTip({ target: 'caseCompInfo', html: 'More Info on the Case
	 * Competition coming soon!', showDelay:0 });
	 */
}

function showTeamCreationForm(divName) {
	if (!teamFormRendered) {
		teamForm = new Ext.FormPanel(
				{
					labelWidth : 90, // label settings here cascade unless
					// overridden
					frame : true,
					title : 'Case Competition Registration',
					bodyStyle : 'padding:5px 5px 0',
					fileUpload : true,
					width : 350,
					defaults : {
						width : 230
					},
					defaultType : 'textfield',
					items : [ {
						name : 'isRegistration',
						value : 'true',
						hidden : true
					}, {
						name : 'registrationType',
						value : 'casecomp',
						hidden : true
					}, {
						fieldLabel : 'Name*',
						name : 'name',
						allowBlank : false
					}, {
						fieldLabel : 'Team Name*',
						name : 'teamName',
						allowBlank : false
					}, {
						fieldLabel : 'Teammate',
						name : 'team1'
					}, {
						fieldLabel : 'Teammate',
						name : 'team2'
					}, {
						fieldLabel : 'Teammate',
						name : 'team3'
					}, {
						fieldLabel : 'Teammate',
						name : 'team4'
					} ],
					buttons : [ {
						text : 'Submit',
						id : 'casecompButton',
						handler : function(b, e) {
							if (teamForm.getForm().isValid())
								b.disable();
							teamForm
									.getForm()
									.submit(
											{
												url : 'registrationInsert.php',
												success : function(form, action) {
													teamForm.removeAll();
													teamForm.doLayout();
													var thanks = {
														xtype : 'displayfield',
														value : 'Thank you for submitting your team information!',
														hideLabel : true,
														width : 300
													}
													teamForm.insert(1, thanks);
													teamForm.doLayout();
													b.hide();
												}
											});
						}
					} ]
				});
		teamForm.render(divName);
		teamFormRendered = true;
		teamFormVisible = true;
	} else {
		if (teamFormVisible)
			teamForm.hide();
		else
			teamForm.show();
		teamFormVisible = !teamFormVisible;
	}
}

function fillForm(type) {
	switch (type) {
	case 'corporate':
		if (studentFormVisible) {
			studentForm.hide();
			studentFormVisible = false;
		}
		if (corporateFormVisible) {
			corporateForm.hide();
		} else {
			corporateForm.show();
		}
		corporateFormVisible = !corporateFormVisible;
		break;
	case 'student':
		if (corporateFormVisible) {
			corporateForm.hide();
			corporateFormVisible = false;
		}
		if (studentFormVisible) {
			studentForm.hide();
		} else {
			studentForm.show();
		}
		studentFormVisible = !studentFormVisible;
		break;
	}
}

function fillDivCorporateForm(divName) {
	if (corporateFormVisible) {
		corporateForm.hide();
		corporateFormVisible = false;
	} else if (corporateFormInitialized) {
		corporateForm.show();
		corporateFormVisible = true;
	} else {

	}
}

function fillDivStudentForm(divName) {
	if (studentFormVisible) {
		studentForm.hide();
		studentFormVisible = false;
	} else if (studentFormInitialized) {
		corporateForm.hide();
		corporateFormVisible = false;
		studentForm.show();
		studentFormVisible = true;
	} else {

	}
}
