Ext.ns("wt.sat");
Ext.onReady(function() {
	wt.initializer.init()
	wt.util.icon.reg('add.png','pencil.png','delete.png','book_edit.png','vcard.png',
							'report_user.png','book_add.png','information.png','report_go.png',
							'cancel.png','disk.png','arrow_right.png','arrow_left.png')
	function notValid() {
		Ext.Msg.alert('Error','You have not supplied values for all of the required fields.')
		
	}

	var releaseWin = new Ext.Window({
		id: 'releaseWin',
		//title: '<span style="font-size: 14px;">Release Authorization</span>',
		closable: false,
		width: 550,
		modal: true,
		items: [{
			xtype: 'panel',
			id: 'releaseText',
			style: 'margin: 10px;',
			unstyled: true,
			html: '<span style="font-size: 14px;">By submitting, each author/coauthor agrees to the following conditions:'+
					  '<ol style="font-size: 14px; list-style-position: inherit; margin: 25px; list-style-type: decimal;">'+
					  '<li style="margin-bottom: 5px;">The Small Satellite Conference is authorized to circulate abstracts to committee members and reviewers for evaluation and selection purposes.</li>'+
					  '<li style="margin-bottom: 5px;">Accepted abstracts will be posted on the Small Satellite website prior to the Conference and may be included in printed on-site materials.</li></ol><br/></span>'

		}/*{
			xtype: 'form',
			style: 'margin: 10px',
			id: 'yes_no',
			width: 500,
			frame: true,
			labelAlign: 'top',
			id: 'radioForm',
			items: [{
				xtype: 'radiogroup',
				fieldLabel: '<span style="font-size: 14px;">Do you have the required approvals?</span>',
				labelSeperator: '',
				unstyled: true,
				width: 100,
				items: [{
					boxLabel: '<span style="font-size: 14px;">Yes</span>',
					id: 'yes',
					itemId: 'yes',
					unstyled: true,
					name: 'radio',
					listeners: {
						check: function() {
							Ext.getCmp('releaseText').hide();
							Ext.getCmp('releaseForm').show()
							Ext.getCmp('non_release_form').hide()
							Ext.getCmp('continue').enable()
							Ext.getCmp('radioForm').hide()
							Ext.getCmp('releaseWin').syncShadow()
							Ext.getCmp('back').enable()
						}
					}
				},{
					boxLabel: '<span style="font-size: 14px;">No</span>',
					id: 'no',
					itemId: 'no',
					name: 'radio',
					listeners: {
						check: function() {
							Ext.getCmp('releaseText').hide();
							Ext.getCmp('releaseForm').hide()
							Ext.getCmp('non_release_form').show()
							Ext.getCmp('continue').enable()
							Ext.getCmp('radioForm').hide()
							Ext.getCmp('releaseWin').syncShadow()
							Ext.getCmp('back').enable()
						}
					}
				}]
			}]
		
		}*/,{
			xtype: 'form',
			style: 'margin: 10px;',
			unstyled: true,
			id: 'releaseForm',
			labelAlign: 'top',
			//hidden: true,
			items: [{
				xtype: 'textfield',
				fieldLabel: '<span style="font-size: 14px;">To validate that you agree, please enter your eye color as a digital signature</span>',
				id: 'authorization',
				allowBlank: false
			}]
		}/*,{
			xtype: 'form',
			style: 'margin: 10px;',
			hidden: true,
			unstyled: true,
			id: 'non_release_form',
			labelAlign: 'top',
			items: [{
				xtype: 'textarea',
				fieldLabel: '<span style="font-size: 14px;">Reason for not having approvals</span>',
				id: 'non_auth_reason',
				width: 500,
				height: 200,
				allowBlank: false
			}]
		}*/],
		fbar: ['->',{
			text: 'Continue',
			id: 'continue',
			iconCls: 'icon-arrow-right',
				handler: function() {
				if (Ext.getCmp('releaseForm').getForm().isValid() == false) {
					notValid()
					return false
				}
				Ext.getCmp('releaseWin').hide()
				Ext.Ajax.request({
					url: 'abstract-data',
					params: {
						task: 'AUTH',
						authorize: Ext.getCmp('authorization').getValue()
						
					},
					success: function(response) {
						var response = Ext.decode(response.responseText)
						tclVars.paper_id = response.data.paper_id
						Ext.getCmp('releaseWin').hide()
					}
				})
			}
		},{
			text: 'Cancel',
			iconCls: 'icon-cancel',
			handler: function() {
				window.location='http://www.smallsat.org'
			}
		}]
	})
	
	var authorsStore = new Ext.data.JsonStore({
		url: 'abstract-data',
		root: 'payload',
		fields: ['author_id','author_name','author_email','author_affiliation','author_order'],
		baseParams: {
			task: 'AUTHORS',
			paper_id: tclVars.paper_id
		}
	})
	var sessionStore = new Ext.data.JsonStore({
		url: 'abstract-data',
		root: 'payload',
		fields: ['session_id','session_title'],
		baseParams: {
			task: 'SESSIONC',
			paper_id: tclVars.paper_id,
			conf_id: tclVars.conf_id
		}
	})
	var authorWin = new Ext.Window({
		id: 'authorWin',
		title:'Add an author',
		modal: true,
		width: 308,
		items: [{
			xtype: 'form',
			id: 'authorForm',
			frame: true,
			labelAlign:'top',
			items: [{
					xtype: 'textfield',
					fieldLabel: 'Name',
					id: 'author_name',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Affiliation',
					id: 'author_affiliation',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Email',
					id: 'author_email',
					width: 282,
					allowBlank: false
				}]
		}],
		buttons: [{
			text: 'Save',
			iconCls: 'icon-disk',
			handler: function() {
				if (Ext.getCmp('authorForm').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (wt.sat.edit == true) {
					var task = 'EDIT'
					var author_id = Ext.getCmp('authorsGrid').getSelectionModel().getSelected().data.author_id
				} else {
					var task = 'NEW'
					var author_id = null
				}
				Ext.getCmp('authorForm').getForm().submit({
					url: 'abstract-data',
					params: {
						task: task,
						author_id: author_id,
						paper_id: tclVars.paper_id
					},
					success: function() {
						Ext.getCmp('authorsGrid').getStore().load({params:{paper_id:tclVars.paper_id}})
						Ext.getCmp('authorWin').hide()
						Ext.getCmp('authorForm').getForm().reset()
					}
				})
			}
		},{
			text: 'Cancel',
			iconCls: 'icon-cancel',
			handler: function() {
				Ext.getCmp('authorWin').hide()
				Ext.getCmp('authorForm').getForm().reset()
			}
		}]
	})

	var mainPanel = new Ext.Panel({
		title: 'Abstract Submission',
		id: 'mainPanel',
		width: 600,
		iconCls: 'icon-report-go',
		renderTo: 'form-container',
		style: 'margin-right: 10px',
		frame: true,
		items: [{
			xtype: 'form',
			title: 'Admin',
			id: 'admin',
			hidden: true,
			frame: true,
			collapsible: true,
			items: [{
				xtype: 'displayfield',
				id: 'paper_id',
				fieldLabel: 'Paper ID'
			},{
				xtype: 'textfield',
				id: 'abstract_id',
				fieldLabel: 'Abstract ID'
			},{
				xtype: 'textfield',
				id: 'pretty_paper_number',
				emptyText: 'Unassigned',
				fieldLabel: 'Paper Number'
			},{
				xtype: 'combo',
				id: 'status',
				store: ['Approved','Disaproved','Pending','Withdrawn','Alternate'],
				fieldLabel: 'Approval Status',
				width: 150,
				triggerAction: 'all'
			},{
				xtype: 'combo',
				id: 'session_id',
				store: sessionStore,
				fieldLabel: 'Session',
				width: 150,
				displayField: 'session_title',
				valueField: 'session_id',
				hiddenName: 'session_id',
				hiddenId:'hdsession_id',
				mode: 'local',
				triggerAction: 'all',
				emptyText: 'Unassigned'
			},{
				xtype: 'textfield',
				id: 'password',
				fieldLabel: 'Password'
			}]
		},{
			xtype: 'panel',
			title: 'Point of Contact',
			id: 'contactPanel',
			iconCls: 'icon-vcard',
			frame: true,
			collapsible: true,
			items: [{
				xtype: 'panel',
				layout: 'table',
				style: 'margin-top: 10px',
				layoutConfig: {
					columns: 2
				},
				items: [{
					xtype: 'form',
					id: 'contact_info',
					style: 'margin-left: 10px',
					labelAlign: 'top',
					items: [{
						xtype: 'textfield',
						fieldLabel: 'First Name',
						id: 'contact_firstname',
						allowBlank: false
					}]
				},{
					xtype: 'form',
					id: 'contact_info1',
					labelAlign: 'top',
					style: 'margin-left: 10px;',
					items: [{
						xtype: 'textfield',
						fieldLabel: 'Last Name',
						id: 'contact_lastname',
						allowBlank: false
					}]
				}]
			},{
				xtype: 'form',
				id: 'contact_info2',
				style: 'margin-left: 10px',
				labelAlign: 'top',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Title',
					id: 'contact_title',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Affiliation/Company',
					id: 'contact_affiliation',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Email',
					id: 'contact_email',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Phone',
					id: 'contact_phone',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Alternate Phone',
					id: 'contact_phone2',
					width: 282
				},{
					xtype: 'textfield',
					fieldLabel: 'Fax',
					id: 'contact_fax',
					width: 282
				},{
					xtype: 'textfield',
					fieldLabel: 'Address 1',
					id: 'contact_address1',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Address 2',
					id: 'contact_address2',
					width: 282
				}]
			},{
				xtype: 'panel',
				layout: 'table',
				layoutConfig: {
					columns: 3
				},
				items: [{
					xtype: 'form',
					id: 'contact_info3',
					labelAlign: 'top',
					style: 'margin-left: 10px',
					items: [{
						xtype: 'textfield',
						fieldLabel: 'City',
						id: 'contact_city',
						width: 122,
						allowBlank: false
					}]
				},{
					xtype: 'form',
					id: 'contact_info4',
					labelAlign: 'top',
					style: 'margin-left: 10px',
					items: [{
						xtype: 'textfield',
						fieldLabel: 'State',
						id: 'contact_state',
						width: 50,
						allowBlank: false
					}]
				},{
					xtype: 'form',
					id: 'contact_info5',
					labelAlign: 'top',
					style: 'margin-left: 10px',
					items: [{
						xtype: 'textfield',
						fieldLabel: 'Zip Code',
						id: 'contact_zip',
						width: 90,
						allowBlank: false
					}]
				}]
			},{
				xtype: 'form',
				id: 'contact_info6',
				labelAlign: 'top',
				style: 'margin-left: 10px;',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Country',
					id: 'contact_country',
					width: 282,
					allowBlank: false
				}]
			}]
		},{
			xtype: 'panel',
			title: 'Paper Presenter',
			iconCls: 'icon-report-user',
			frame: true,
			collapsible: true,
			style: 'margin-top: 10px;',
			items: [{
				xtype: 'form',
				id: 'presenter_info',
				style: 'margin-top: 10px; margin-left: 10px',
				labelAlign: 'top',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Name',
					id: 'presenter_name',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Title',
					id: 'presenter_title',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Affiliation',
					id: 'presenter_affiliation',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Email',
					id: 'presenter_email',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Phone',
					id: 'presenter_phone',
					width: 282,
					allowBlank: false
				}]
			}]
		},{
			xtype: 'panel',
			title: 'Primary Author',
			iconCls: 'icon-book-edit',
			frame: true,
			collapsible: true,
			style: 'margin-top: 10px;',
			items: [{
				xtype: 'form',
				id: 'author_info',
				style: 'margin-top: 10px; margin-left: 10px',
				labelAlign: 'top',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Name',
					id: 'prime_auth_name',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Affiliation',
					id: 'prime_auth_affiliation',
					width: 282,
					allowBlank: false
				},{
					xtype: 'textfield',
					fieldLabel: 'Email',
					id: 'prime_auth_email',
					width: 282,
					allowBlank: false
				}]
			}]
		},{
			xtype: 'grid',
			title: 'Additional Authors',
			iconCls: 'icon-book-add',
			frame: true,
			collapsible: true,
			style: 'margin-top: 10px;',
			id: 'authorsGrid',
			height: 200,
			store: authorsStore,
			columns: [{
				header: 'Name',
				dataIndex: 'author_name',
				width: 200
			},{
				header: 'Affiliation',
				dataIndex: 'author_affiliation',
				width: 200
			},{
				header: 'Email',
				dataIndex: 'author_email',
				width: 200
			}],
			buttonAlign: 'left',
			buttons: [{
				text: 'Add',
				iconCls: 'icon-add',
				handler: function() {
					wt.sat.edit = false
					Ext.getCmp('authorWin').show()
				}
			},{
				text: 'Edit',
				iconCls: 'icon-pencil',
				handler: function() {
					if(Ext.getCmp('authorsGrid').getSelectionModel().getCount() != 1) {
						Ext.Msg.alert('Error','Please select one author to edit')
						return false
					}
					Ext.getCmp('authorWin').show()
					wt.sat.edit = true
					Ext.getCmp('authorForm').load({
						url: 'abstract-data',
						params: {
							task: 'AUTHEDIT',
							author_id: Ext.getCmp('authorsGrid').getSelectionModel().getSelected().data.author_id
						}
					})
				}
			},{
				text: 'Delete',
				iconCls: 'icon-delete',
				handler: function() {
					if(Ext.getCmp('authorsGrid').getSelectionModel().getCount() != 1) {
						Ext.Msg.alert('Error','Please select one author to delete')
						return false
					}
					Ext.Ajax.request({
						url: 'abstract-data',
						params: {
							task: 'DELETE',
							author_id:Ext.getCmp('authorsGrid').getSelectionModel().getSelected().data.author_id
						},
						success: function() {
							Ext.getCmp('authorsGrid').getStore().reload()
						}
					})
				}
			}]
		},{
			xtype: 'panel',
			title: 'Abstract Information',
			iconCls: 'icon-information',
			frame: true,
			collapsible: true,
			style: 'margin-top: 10px;',
			items: [{
				xtype: 'form',
				id: 'abstractForm',
				style: 'margin-top: 10px; margin-left: 10px',
				labelAlign: 'top',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Paper Title',
					id: 'paper_title',
					width: 550,
					allowBlank: false
				},{
					xtype: 'textarea',
					fieldLabel: 'Abstract Body',
					emptyText: 'Enter the body of your abstract here. You should be able to cut and paste from' +
							' most applications. However, be aware that this is ascii text only. Special symbols will need' +
							' to be replaced with words. For example type microradians instead of trying to insert µrad from ' +
							'a word document.Separate paragraphs with a blank line.',
					id: 'abstract',
					width: 550,
					height: 200,
					allowBlank: false,
					maxLength: 4000,
					maxLengthText: 'There is a limit of 4000 characters for this field'
				}]
			}]
		}],
		buttons: [{
			text: 'Submit',
			iconCls: 'icon-disk',
			handler: function() {
				if (parseInt(tclVars.paper_admin_p) == 1) {
					Ext.getCmp('admin').getForm().submit({
						url: 'abstract-data',
						params: {
							task: 'ADSAVE',
							paper_id: tclVars.paper_id
						}
					})
				}
				if (Ext.getCmp('contact_info').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info1').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info2').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info3').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info4').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info5').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('contact_info6').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('presenter_info').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('author_info').getForm().isValid()==false) {
					notValid()
					return false
				}
				if (Ext.getCmp('abstractForm').getForm().isValid()==false) {
					notValid()
					return false
				}
				var sessions = []
				Ext.each(Ext.getCmp('suggested_sessions').items.items, function(t) {
					if (t.checked == true) {
						sessions.push(t.id)
					}
				})
				var suggested_sessions = sessions.toString()
				Ext.Ajax.request({
					url: 'abstract-data',
					params: {
						task: 'SUBMIT',
						contact_firstname: Ext.getCmp('contact_firstname').getValue(),
						contact_lastname: Ext.getCmp('contact_lastname').getValue(),
						contact_title: Ext.getCmp('contact_title').getValue(),
						contact_affiliation: Ext.getCmp('contact_affiliation').getValue(),
						contact_email: Ext.getCmp('contact_email').getValue(),
						contact_phone: Ext.getCmp('contact_phone').getValue(),
						contact_phone2: Ext.getCmp('contact_phone2').getValue(),
						contact_fax: Ext.getCmp('contact_fax').getValue(),
						contact_address1: Ext.getCmp('contact_address1').getValue(),
						contact_address2: Ext.getCmp('contact_address2').getValue(),
						contact_city: Ext.getCmp('contact_city').getValue(),
						contact_state: Ext.getCmp('contact_state').getValue(),
						contact_zip: Ext.getCmp('contact_zip').getValue(),
						contact_country: Ext.getCmp('contact_country').getValue(),
						presenter_name: Ext.getCmp('presenter_name').getValue(),
						presenter_title: Ext.getCmp('presenter_title').getValue(),
						presenter_affiliation: Ext.getCmp('presenter_affiliation').getValue(),
						presenter_email: Ext.getCmp('presenter_email').getValue(),
						presenter_phone: Ext.getCmp('presenter_phone').getValue(),
						prime_auth_name: Ext.getCmp('prime_auth_name').getValue(),
						prime_auth_affiliation: Ext.getCmp('prime_auth_affiliation').getValue(),
						prime_auth_email: Ext.getCmp('prime_auth_email').getValue(),
						paper_title: Ext.getCmp('paper_title').getValue(),
						abstract: Ext.getCmp('abstract').getValue(),
						suggested_sessions: suggested_sessions,
						paper_id: tclVars.paper_id,
						conf_id: tclVars.conf_id
					},
					success: function() {
						if (parseInt(tclVars.paper_admin_p) == 1) {
							window.location=tclVars.return_url
						} else {
							window.location="abstract3?paper_id="+parseInt(tclVars.paper_id)
						}
					}
				})
			}
		},{
			text: 'Cancel',
			iconCls: 'icon-cancel',
			handler: function() {
				window.location="/"
			}
		}]
	})
	Ext.Ajax.request({
		url: 'abstract-data',
		params: {
			task: 'SESSIONS',
			conf_id: tclVars.conf_id
		},
		success: function(response) {
			var response = Ext.decode(response.responseText)
			var objs = []
			Ext.each(response.payload, function(t) {
				objs.push({xtype:'checkbox',boxLabel: t.session_label+'-'+t.session_title,	id: t.session_id})
			})
			var group = new Ext.form.CheckboxGroup({
				labelSeparator: ' ',
				fieldLabel: 'Suggested Sessions <img src="http://static.usurf.usu.edu/resources/icons/information.png" onClick=window.open("sessiondetails") style="cursor: pointer;">',
				columns: 2,
				id: 'suggested_sessions',
				width: 500,
				items: objs
			})
			Ext.getCmp('abstractForm').add(group)
			Ext.getCmp('abstractForm').doLayout()
		}
	})
	if (tclVars.paper_id != "") {
		Ext.Ajax.request({
			url: 'abstract-data',
			params: {
				paper_id: tclVars.paper_id,
				task: 'LOAD'
			}, 
			success: function(response) {
				response = Ext.decode(response.responseText)
				Ext.getCmp('contact_firstname').setValue(response.data.contact_firstname)
				Ext.getCmp('contact_lastname').setValue(response.data.contact_lastname)
				Ext.getCmp('contact_title').setValue(response.data.contact_title)
				Ext.getCmp('contact_affiliation').setValue(response.data.contact_affiliation)
				Ext.getCmp('contact_email').setValue(response.data.contact_email)
				Ext.getCmp('contact_phone').setValue(response.data.contact_phone)
				Ext.getCmp('contact_phone2').setValue(response.data.contact_phone2)
				Ext.getCmp('contact_fax').setValue(response.data.contact_fax)
				Ext.getCmp('contact_address1').setValue(response.data.contact_address1)
				Ext.getCmp('contact_address2').setValue(response.data.contact_address2)
				Ext.getCmp('contact_city').setValue(response.data.contact_city)
				Ext.getCmp('contact_state').setValue(response.data.contact_state)
				Ext.getCmp('contact_zip').setValue(response.data.contact_zip)
				Ext.getCmp('contact_country').setValue(response.data.contact_country)
				Ext.getCmp('presenter_name').setValue(response.data.presenter_name)
				Ext.getCmp('presenter_title').setValue(response.data.presenter_title)
				Ext.getCmp('presenter_affiliation').setValue(response.data.presenter_affiliation)
				Ext.getCmp('presenter_email').setValue(response.data.presenter_email)
				Ext.getCmp('presenter_phone').setValue(response.data.presenter_phone)
				Ext.getCmp('prime_auth_name').setValue(response.data.prime_auth_name)
				Ext.getCmp('prime_auth_affiliation').setValue(response.data.prime_auth_affiliation)
				Ext.getCmp('prime_auth_email').setValue(response.data.prime_auth_email)
				Ext.getCmp('paper_title').setValue(response.data.paper_title)
				Ext.getCmp('abstract').setValue(response.data.abstract)
				var sessions = response.data.suggested_sessions.split(' ')
				Ext.each(sessions,function(t){
					if (t == "") {
						
					} else {
						Ext.getCmp(t).setValue(true)
					}
				})
				Ext.getCmp('authorsGrid').getStore().load({params: {paper_id:tclVars.paper_id}})
				
			}
		})
	} else if (tclVars.paper_admin_p != 1) {
		Ext.getCmp('releaseWin').show()
	} 
	if (parseInt(tclVars.paper_admin_p) == 1) {
		Ext.getCmp('admin').show()
		Ext.getCmp('session_id').getStore().load()
		Ext.getCmp('admin').getForm().load({
			url: 'abstract-data',
			params: {
				task: 'ADMIN',
				paper_id: tclVars.paper_id
			}
		})
	}

})
