<?php require "_header.php"; ?>
<?php
if ($_SESSION["VedantnrCollegeId"] != "" && isset($_SESSION["VedantnrCollegeId"])) {
	$college_id = $_SESSION["VedantnrCollegeId"];
	$mode = "edit";

	if ($EDIT == "false") {
		redirectToPage();
	}

	$limit = 1;
	$start = 0;
	$condition = "";
	$order_field = "";
	$order_type = "";
	$objCollege = new CollegeMgmt();
	$out = $objCollege->getAllRecords($limit, $start, $condition, $order_field, $order_type);

	if (dbRows($out)) {
		$row = dbArray($out);
		extract($row);
	}
} else {
	$mode = "add";
	if ($ADD == "false") {
		redirectToPage();
	}
	$college_id = 0;
}
?>
<div id="main-content">
	<script language="javascript" type="text/javascript">
		var formMode = '<?php echo $mode; ?>';
		var college_id = '<?php echo $college_id; ?>';
		var add = '<?php echo $mode; ?>';
		Ext.onReady(function () {
			Ext.QuickTips.init();

			/* START FILE UPLOAD CODE */

			var picFiles = {
				columnWidth: .7,
				layout: 'form',
				labelAlign: 'top',
				items: [{
					xtype: 'textfield',
					fieldLabel: 'Current',
					labelSeparator: '',
					name: 'currPic',
					id: 'currPic',
					readOnly: true,
					disabled: true,
					anchor: '100%'
				}, {
					xtype: 'textfield',
					fieldLabel: 'New (JPG or JPEG only)',
					labelSeparator: '',
					name: 'newPic',
					width: 270,
					id: 'newPic',
					style: 'width: 300px',
					inputType: 'file',
					allowBlank: false
				}
				]
			}
			var pictUploadForm = new Ext.FormPanel({
				frame: true,
				bodyStyle: 'padding:5px',
				width: 435,
				layout: 'column',
				url: 'logoupload.php',
				method: 'POST',
				fileUpload: true,
				items: [picFiles],
				buttons: [{
					text: 'Upload Picture',
					handler: function () {
						var theForm = pictUploadForm.getForm();
						if (!theForm.isValid()) {
							Ext.MessageBox.show({
								title: 'Change Picture',
								msg: 'Please select a picture',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
							return;
						}
						if (!validateFileExtension(Ext.getDom('newPic').value)) {
							Ext.MessageBox.show({
								title: 'Change Picture',
								msg: 'Only JPG or JPEG please.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
							return;
						}
						theForm.submit({
							params: { act: 'setPicture', id: college_id },
							waitMsg: 'Uploading picture'
						});
					}
				}, {
					text: 'Reset',
					handler: function () {
						pictUploadForm.getForm().reset();
					}
				}
				]
			});

			var winPicture = new Ext.Window({
				title: 'File Upload Form',
				height: 180,
				width: 435,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				border: false,
				autoDestroy: true,
				items: pictUploadForm
			});

			pictUploadForm.on({
				actioncomplete: function (form, action) {
					if (action.type == 'load') {
						var varpic = action.result.data;

						Ext.getDom('pic').src = varpic.file;
						Ext.getCmp('currPic').setValue(varpic.file);
					}
					if (action.type == 'submit') {
						var varpic = action.result.data;
						Ext.getDom('pic').src = varpic.file;
						CollegeForm.getForm().findField('college_logo').setValue(varpic.photoname);
						Ext.getCmp('currPic').setValue(varpic.file);
						Ext.getDom('newPic').value = '';
						winPicture.hide();
					}
				}
			});
			pictUploadForm.getForm().load({
				params: { act: 'getPicture', id: college_id, photoname: '<?php print $college_logo; ?>' },
				waitMsg: 'Loading'
			});

			/* END FILE UPLOAD CODE */

			var college_name = new Ext.form.TextField({
				name: 'college_name',
				id: 'college_name',
				labelSeparator: '',
				fieldLabel: 'Name:',
				width: 300,
				allowBlank: false
			});

			var college_id = new Ext.form.Hidden({
				name: 'college_id',
				id: 'college_id'
			});

			var college_logo = new Ext.form.Hidden({
				name: 'college_logo',
				id: 'college_logo'
			});

			var college_address = new Ext.form.TextArea({
				name: 'college_address',
				id: 'college_address',
				fieldLabel: 'Address:',
				width: 300,
				height: 80,
				labelSeparator: '',
				allowBlank: true
			});

			var college_phone = new Ext.form.TextField({
				name: 'college_phone',
				id: 'college_phone',
				labelSeparator: '',
				fieldLabel: 'Phone:',
				width: 300,
				allowBlank: false
			});

			var college_email = new Ext.form.TextField({
				name: 'college_email',
				id: 'college_email',
				labelSeparator: '',
				fieldLabel: 'Email:',
				width: 300,
				allowBlank: false
			});

			var college_url = new Ext.form.TextField({
				name: 'college_url',
				id: 'college_url',
				labelSeparator: '',
				fieldLabel: 'URL:',
				width: 300,
				allowBlank: false
			});

			var CollegeForm = new Ext.FormPanel({
				labelWidth: 60,
				url: 'save-college.php',
				method: 'post',
				fileUpload: true,
				frame: true,
				autoHeight: true,
				bodyStyle: 'padding:5px 5px 0',
				items: [{
					xtype: 'container',
					autoEl: {},
					border: false,
					layout: 'column',
					anchor: '100%',
					items: [{
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .78,
						items: [college_id, college_logo, college_name, college_address, college_phone, college_email, college_url]
					}, {
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .22,
						items: [{
							xtype: 'box',
							name: 'photo',
							id: 'photo',
							style: 'padding:12px 0px 0px 0px',
							autoEl: {
								tag: 'div',
								html: '<img id="pic" src="photo/college/default.gif" alt="photograph" class="img-contact" width="<?php echo $SiteImageWidth ?>" height="<?php echo $SiteImageHeight ?>"/>'
							}
						}, {
							style: 'padding:5px 0px 0px 0px',
							xtype: 'button',
							text: 'Logo Upload',
							width: 100,
							handler: function () {
								winPicture.show();
								Ext.getDom('newPic').size = 30;
							}
						}]
					}]
				}]
			});

			var CollegeWindow = new Ext.Window({
				title: 'College Information',
				width: 525,
				height: 460,
				modal: true,
				autoHeight: true,
				border: false,
				layout: 'fit',
				closeAction: 'close',
				shadow: false,
				collapsible: true,
				items: CollegeForm,
				buttonAlign: 'center',
				buttons: [{
					text: 'Save',
					handler: function () {
						clearEmptyText(CollegeForm);
						CollegeForm.getForm().submit({
							waitMsg: 'Saving...',
							success: function () {
								Ext.MessageBox.show({
									title: 'Message',
									msg: 'Data has been saved',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.INFO
								});
								
							},
							failure: function () {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please fill complete form.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
							}
						});
					}
				}, {
					text: 'Close',
					handler: function () {
						CollegeWindow.close();
					}
				}]
			});
			var varWidth = (screen.width - CollegeWindow.width) / 2;
			CollegeWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			CollegeWindow.show();
			CollegeForm.getForm().findField('college_name').focus(true, 500);

			if (formMode == 'edit') {
				<?php
				$college_address = replaceSpecial($college_address);
				?>
				//Ext.getDom('pic').src = '<?php //print $SchoolImagePath.$school_photo; ?>';
				Ext.getCmp('college_name').setValue('<?php echo $college_name; ?>');
				Ext.getCmp('college_id').setValue('<?php echo $college_id; ?>');
				Ext.getCmp('college_address').setValue('<?php echo $college_address; ?>');
				Ext.getCmp('college_phone').setValue('<?php echo $college_phone; ?>');
				Ext.getCmp('college_email').setValue('<?php echo $college_email; ?>');
				Ext.getCmp('college_url').setValue('<?php echo $college_url; ?>');
			}

		});
	</script>
</div>
<?php require "_footer.php"; ?>