/**
 *  xdbgui.js
 *
 * +================================================================================================+
 * | Javascript utilities for use with the xmldbgui.php
 * |
 * +------------------------------------------------------------------------------------------------+
 * | Copyright:
 * |
 * | xdbgui.js: Utilities for use with the guis built from the xdbgui template
 * |
 * | Copyright (C) 2002-2003 Nigel Swinson, nigelswinson@users.sourceforge.net
 * |
 * | This program is free software; you can redistribute it and/or
 * | modify it under the terms of the GNU General Public License
 * | as published by the Free Software Foundation; either version 2
 * | of the License, or (at your option) any later version.
 * |
 * | This program is distributed in the hope that it will be useful,
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * | GNU General Public License for more details.
 * |
 * | You should have received a copy of the GNU General Public License
 * | along with this program; if not, write to the Free Software
 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * +================================================================================================+
 *
 * @author  Nigel Swinson
 * @link    http://sourceforge.net/projects/phpxmldb/
 * @version 1.1.2 beta
 * @CVS $Id:  Exp $
 */

// In order to avoid polluting the global namespace, we hide the default helpers in a CXDbGui object.

////////////////////////////////////////////////////////////////////////////////////////////////////
// Page object
 
// Constructor
window.CXDbGui=function(Page) {
	if (arguments.length <= 0) return;
	this.Init(Page);
}

// CXDbGui inherits from CPage
CXDbGui.prototype = new CPage();
CXDbGui.prototype.constructor = CXDbGui;
CXDbGui.superclass = CPage.prototype;

// Initialiser
CXDbGui.prototype.Init = function(Page) {
	CXDbGui.superclass.Init.call(this, Page);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

// Perform an AJAX callback using the data in the supplied form.  Display the results in the CallbackResponsePanel
CXDbGui.prototype.CallbackThisForm = function(Form) {
	this.ShowLoadingPanel();
	var oRequest = new AjaxCallback(this, 'CallbackThisFormComplete');
	var PostData = this.BuildPostData(Form);
	oRequest.Send(this.Page, PostData);
}

// Display the loading panel in the CallbackResponsePanel while we wait for the callback to complete
CXDbGui.prototype.BuildPostData = function(Form) {
	var PostData = 'ContentType=Ajax';
	// Mimic what would have happened if they clicked a submit button
	// http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#successful-controls
	for (iIndex = 0; iIndex < Form.elements.length; iIndex++) {
		var element = Form.elements[iIndex];
		// No name means the control isn't successful
		if (element.name == '') continue;		
		// Disabled controls are not successful
		if (element.disabled) continue;
		// All on check boxes are successful
		if (element.type == 'checkbox') {
			if (!element.checked) continue;
		}
		// Only the on radio may be successful
		if (element.type == 'radio') {
			if (!element.checked) continue;
		}
		// If it is a select input, then we may need to take the text rather than the value
		if (element.type == 'select-one') {
			if (element.value == '') {
				var value = element.options[element.selectedIndex].text;
				PostData += '&' + element.name + '=' + escape(value);
				continue;
			}
		}
		PostData += '&' + element.name + '=' + escape(Utf8.encode(element.value));
	}
	return PostData;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

// Display the loading panel in the CallbackResponsePanel while we wait for the callback to complete
CXDbGui.prototype.ShowLoadingPanel = function() {
	var oLoadingPanel = document.getElementById('CallbackLoadingPanel');
	if (oLoadingPanel) {
		var oPanel = document.getElementById('CallbackResponsePanel');
		if (oPanel) {
			oPanel.innerHTML = oLoadingPanel.innerHTML;
			window.location.href="#CallbackResponsePanel";
		}
	}
}

// Display the loading panel in the CallbackResponsePanel while we wait for the callback to complete
CXDbGui.prototype.HideLoadingPanel = function() {
	var oPanel = document.getElementById('CallbackResponsePanel');
	if (oPanel) {
		oPanel.innerHTML = '';
	}
}

// Called when the call completes
CXDbGui.prototype.CallbackThisFormComplete = function(oRequest) {
	var oPanel = document.getElementById('CallbackResponsePanel');
	if (!oPanel) {
		alert("Can't find the CallbackResponsePanel.  The results are shown here instead:" + oRequest.responseText);
	} else {
		oPanel.innerHTML = oRequest.responseText;
		window.location.href="#CallbackResponsePanel";
	}	
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Panel helpers

// Show the Add panel
CXDbGui.prototype.ShowActionPanel = function(PanelName) {	
	// Hide all the panels they didn't specify
	for (var iIndex in this.ActionPanelNames) {
		if (PanelName != this.ActionPanelNames[iIndex]) {
			this.HidePanel(this.ActionPanelNames[iIndex]);
		}
	}
	
	// Then toggle the one they did
	if (PanelName) this.TogglePanel(PanelName);
}


