
/**
* @namespace TOTECS.util
* contains functions used for all interfaces
*/
TOTECS.util = function(){
	var messageDialog;
	var loadDialog;
	var errorDialog;
	var confirmDialog;
	var confirmYesFunction = function(){};
	var confirmNoFunction = function(){};
	
	/**
	 * Returns message dialog object
	 * @method getMessageDialog
	**/
	function getMessageDialog() {
		if(messageDialog==null)
			createMessageDialog();
			
		return messageDialog;
	}
	
	
	/**
	 * Resets confirm dialog callbacks
	 * @method resetConfirmCallbacks	 	 
	**/
	function resetConfirmCallbacks() {
		confirmYesFunction = function(){};
		confirmNoFunction = function(){};
	}
	
	/**
	 * Function to call when 'Yes' is clicked
	 * @method setConfirmYesFunction
	 *
	 * @param func	function
	**/
	function setConfirmYesFunction(func) {
		confirmYesFunction = func;
	}
	
	/**
	 * Function to call when 'No' is clicked
	 * @method setConfirmNoFunction
	 *
	 * @param func	function
	**/
	function setConfirmNoFunction(func) {
		confirmNoFunction = func;
	}
	
	/**
	 * Create a confirm dialog
	 * @method createConfirmDialog
	**/
	function createConfirmDialog()
	{
		confirmDialog = new YAHOO.widget.SimpleDialog("confirmDialog", {
			width: "370px",
			fixedcenter:true,
			modal:true,
			visible:false,
			draggable:false,
			close: true
		});

		var confirmDialogButtons = [
			{
				text: "OK",
				handler: function() {
					confirmYesFunction();
					confirmDialog.hide()
				},
				isDefault: true
			},
			{
				text: "Cancel",
				handler: function() {
					confirmNoFunction();
					confirmDialog.hide()
				},
				isDefault: true
			}
		];

		confirmDialog.setHeader("");
		confirmDialog.cfg.queueProperty("buttons", confirmDialogButtons);
		confirmDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
		confirmDialog.render(document.body);
	}
	
	/**
	 * Shows a confirm dialog
	 * @method showConfirmDialog
	 *  @param message {string}		text displayed in content of dialog
	 *  @param header {string} 		text displayed in head of dialog
	**/
	function showConfirmDialog(message, header) {
		if(confirmDialog==null)
			createConfirmDialog();
		
		confirmDialog.setBody(message);
		confirmDialog.setHeader(header);
		confirmDialog.show();
	}
	
	/**
	 * Create a message dialog
	 * @method createMessageDialog
	**/
	function createMessageDialog()
	{
		messageDialog = new YAHOO.widget.SimpleDialog("messageDialog", {
			width: "370px",
			fixedcenter:true,
			modal:true,
			visible:false,
			draggable:false,
			close: true
		});

		var messsageDialogButtons = [{
			text: "Ok",
			handler: function() {
				messageDialog.hide()
			},
			isDefault: true
		}];

		messageDialog.setHeader("");
		messageDialog.cfg.queueProperty("buttons", messsageDialogButtons);
		messageDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
		messageDialog.render(document.body);
	}
	
	/**
	 * Creates a load dialog
	 * @method createLoadDialog
	**/
	function createLoadDialog()
	{
		loadDialog = new YAHOO.widget.SimpleDialog("loadDialog", {
		width: "370px",
		fixedcenter:true,
		modal:true,
		visible:false,
		draggable:false,
		close: false
		});

		loadDialog.setHeader("");
		loadDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
		loadDialog.render(document.body);
	}
	
	/**
	 * Creates an error dialog
	 * @method createErrorDialog
	**/
	function createErrorDialog()
	{
		errorDialog = new YAHOO.widget.SimpleDialog("errorDialog", {
		width: "370px",
		fixedcenter:true,
		modal:true,
		visible:false,
		draggable:false,
		close: true
		});
		
		var errorDialogButtons = [{
			text: "Ok",
			handler: function() {
				errorDialog.hide()
			},
			isDefault: true
		}];
		errorDialog.cfg.queueProperty("buttons", errorDialogButtons);
		errorDialog.setHeader("");
		errorDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
		errorDialog.render(document.body);
	}
	
	/**
	 * Shows a load dialog
	 * @method showLoadDialog
	 *  @param message {string}		text displayed in content of dialog
	 *  @param header {string} 		text displayed in head of dialog
	**/
	function showLoadDialog(message, header)
	{
		if(loadDialog==null)
			createLoadDialog();
		
		loadDialog.setBody(message);
		loadDialog.setHeader(header);
		loadDialog.show();
	}
	
	/**
	 * Hide load dialog
	 * @method hideLoadDialog
	**/
	function hideLoadDialog()
	{
		if(loadDialog != null)
			loadDialog.hide();
	}
	
	/**
	 * Shows a message dialog
	 * @method showMessageDialog
	 *  @param message {string}		text displayed in content of dialog
	 *  @param header {string} 		text displayed in head of dialog
	**/
	function showMessageDialog(message, header)
	{
		if(messageDialog==null)
			createMessageDialog();
	
		messageDialog.setBody(message);
		messageDialog.setHeader(header);
		messageDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
		messageDialog.render();
		messageDialog.show();
		
	}
	
	/**
	 * Hide message dialog
	 * @method hideMessageDialog
	**/
	function hideMessageDialog()
	{	
		if(messageDialog != null)
			messageDialog.hide();
	}
	
	/**
	 * Shows an error dialog
	 * @method showErrorDialog
	 *  @param message {string}		text displayed in content of dialog
	 *  @param header {string} 		text displayed in head of dialog
	**/
	function showErrorDialog(message, header)
	{
		if(errorDialog==null)
			createErrorDialog();
	
		errorDialog.setBody(message);
		errorDialog.setHeader(header);
		errorDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_BLOCK);
		errorDialog.show();
	}
	
	/**
	 * Hide error dialog
	 * @method hideErrorDialog
	**/
	function hideErrorDialog()
	{
		if(errorDialog != null)
			errorDialog.hide();
	}
	
	/**
	* Gets the extension from a filename
	* @method getFileExt
	* @param FileUrl {string}		filename
	* @return extension
	*/
	function getFileExt(FileUrl){
		var FileExt = '';
		if(FileUrl.lastIndexOf('.') != -1){
			FileExt = FileUrl.substr(FileUrl.lastIndexOf('.') + 1);
		}
		return FileExt.toLowerCase();
	}
  
	/**public methods*/
	return{
		showLoadDialog:showLoadDialog,
		hideLoadDialog:hideLoadDialog,
		showMessageDialog:showMessageDialog,
		hideMessageDialog:hideMessageDialog,
		showErrorDialog:showErrorDialog,
		hideErrorDialog:hideErrorDialog,
		setConfirmYesFunction:setConfirmYesFunction,
		setConfirmNoFunction:setConfirmNoFunction,
		createConfirmDialog:createConfirmDialog,
		showConfirmDialog:showConfirmDialog,
		getMessageDialog:getMessageDialog,
		resetConfirmCallbacks:resetConfirmCallbacks,
		getFileExt:getFileExt
	}
}();

