


function CreateViewPanel(rdbAdmin,databaseManager,selectPanel)
/* object to handle creation and renaming of views */
{
	this.rdbAdmin = rdbAdmin;
	this.dbMgr = databaseManager;
	this.selectPanel = selectPanel;
	this.panelId = 'create-view-panel';
	this.mode = 'create';
	this.view = '';
	this.schema = '';
	
	this.init_handlers = function()
	{
		var $this = this;
		// bind handlers to create button, and to form submit buttons
		$('#createNewViewBtn').click(function(){$this.show('create');});
		var $form = $('#'+this.panelId);
		$form.find('#create-view-btn').click(function() {
				$this.rdbAdmin.resetMessages(); 
				$this.saveView();
		});
		$form.find('#drop-view-btn').click(function() {
				$this.rdbAdmin.resetMessages(); 
				$this.dropView();
		});
		// create live  binding on view list struct
		$('#view-list .structure').live('click',function() {
			return $this.liveShow(this);
		});
	};

	this.liveShow = function(el)
	{
		var $vwname = $(el).find('a').attr('href');
		this.show('edit',$vwname);
		return false;
	};

	this.show = function(mode,view)
	{
		this.rdbAdmin.resetMessages(); 
		this.mode = mode;
		this.view = view;
		this.clearInputs();
		
		if (this.mode === 'edit') {
			$('#alter-view-tip').css('display','inline');
			$('#drop-view-btn').css('display', 'inline');
			this.loadViewDetails();
			this.rdbAdmin.setHeading("Alter View");
		} else {
			$('#alter-view-tip').css('display','none');
			$('#drop-view-btn').css('display', 'none');
			this.rdbAdmin.setHeading("Create View");
		}
		this.rdbAdmin.showPanel(this.panelId);
	};
	
	this.clearInputs = function()
	{
		$('#'+this.panelId+' input').val('');
		$('#'+this.panelId+' textarea').val('');
	};
	
	this.loadViewDetails = function()
	{
		var $this = this;
		var vp, view, schema;
		if (this.view.indexOf('.')>=0) {
			vp = this.view.split('.');
			view = vp[1];
			schema = vp[0];
		}
		else {
			view = this.view;
			schema = 'public';
		}
		function callback(vw) {
			if (vw.length>0) {
				$('#'+$this.panelId+' input').val(vw[0][1]);
				$('#'+$this.panelId+' textarea').val(vw[0][3]);				
			}
		}
		this.dbMgr.getView(schema,view,callback);
	};
	
	this.saveView = function()
	{
		var $this = this, name, sql;
		// functions to handle results of query submit
		function errback (err,msg) {
			$this.rdbAdmin.showErrorMessage('<pre>'+err.toString()+': '+msg+'</pre>');
		}
		function successcb(res) {
			$this.rdbAdmin.showWorkingMessage(res.status[1]);
			$this.rdbAdmin.updateViewList();
			// new name
			$this.view = name;
			$('#mainPageLink').click();
		}
		name = $('#'+this.panelId+' input').val();
		sql = $('#'+this.panelId+' textarea').val();
		sql = sql.replace(/%/g,'%%');
		if ((name === '') || (sql === '')) {
			alert('Please fill all fields!');
			return;
		}
		if (this.mode === 'edit') {
			sql = 'ALTER VIEW '+quoteIdentifier(this.view)+
			      ' RENAME TO '+quoteIdentifier(name);
		}
		else {
			sql = 'CREATE VIEW '+quoteIdentifier(name)+' AS '+sql;
		}
		this.dbMgr.sqlEngine.query(	{ 'q' :	sql,
									  'callback' : successcb,
						              'errback' : errback  });
	};
	
	this.dropView = function()
	{
		var $this = this;
		// functions to handle results of query submit
		function errback (err,msg) {
			$this.rdbAdmin.showErrorMessage('<pre>'+err.toString()+': '+msg+'</pre>');
		}
		function successcb(res) {
			$this.rdbAdmin.showWorkingMessage(res.status[1]);
			$this.rdbAdmin.updateViewList();
			$('#mainPageLink').click();
		}
		var sql;
		if (!confirm('Are you sure?')) {
			return false;
		}
		if (this.view !== '') {
			sql = "DROP VIEW "+quoteIdentifier(this.view);
		}
		else {
			return false;
		}
		this.dbMgr.sqlEngine.query(
				{'q' : sql,
				 'callback' : successcb,
				 'errback' : errback });
		return false;
	};	
}


//