function asciiOnly(aString) {
    var pattern = /[\x20-\x7f]/gi;
    var test = aString.match(pattern);
    if (!test) {
        return false;
    }
    return aString == test.join('');
};

function saveDialogInit(containerId, buttonId, gridController, saveFunction) {
    // Define various event handlers for Dialog
    var handleSubmit = function() {
        if (this.validate()) {
            var data = this.getData();
            saveFunction(gridController, data.gridName);
            this.hide();
        }
    };

    var handleCancel = function() {
        this.cancel();
    };

    // Instantiate the Dialog
    var saveGridDialog = new YAHOO.widget.Dialog(containerId,
							{ width: "30em",
							    fixedcenter: true,
							    visible: false,
							    modal: true,
							    constraintoviewport: true,
							    draggable: true,
							    postmethod: "manual",
							    buttons: [{ text: "Submit", handler: handleSubmit, isDefault: true },
								      { text: "Cancel", handler: handleCancel}]
							});

    // Validate the entries in the form
    saveGridDialog.validate = function() {
        var data = this.getData();
        var aName = data.gridName.replace(/^\s*|\s*$/, "");
        if (aName == "") {
            alert("Please enter a valid name.");
            return false;
        };

        if (!asciiOnly(aName)) {
            alert("Please use only common English characters.");
            return false;
        }
        return true;
    };

    // Render the Dialog
    saveGridDialog.render();

    saveGridDialog.manualSubmitEvent.subscribe(handleSubmit, saveGridDialog, true);

    YAHOO.util.Event.addListener(buttonId, "click", saveGridDialog.show, saveGridDialog, true);
}

/* ******************************************************************************************* */
function deleteDialogInit(containerId, buttonId, gridController, deleteFunction) {

    var handleYes = function() {
        deleteFunction(gridController);
        this.hide();
    }

    var handleNo = function() {
        this.hide();
    }

    var dialog = new YAHOO.widget.SimpleDialog(containerId, {
        width: "20em",
        fixedcenter: true,
        modal: true,
        visible: false,
        draggable: true,
        buttons: [{ text: "Yes", handler: handleYes },
    	{ text: "No", handler: handleNo, isDefault: true}]
    });

    dialog.setHeader("Delete");
    dialog.setBody("Delete currently selected grid?");
    dialog.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
    dialog.render();
    //YAHOO.util.Event.addListener(globals.DELETE_GRID_BUTTON, "click", dialog.show, dialog, true);
    return dialog;
}

