//All the functions that Xbase supports are here. You are adviced not to edit this file as it could break scripting.
//However, you are free to look over it to see what functions Xbase supports.

//*** The tools class provides general things that javascript missed out (multi-button alerts for instance) ***
var Tools = function() {}
//Don't use Javascript's default alert function. It won't work.
Tools.Alert = function(title, message) { window.XbaseController.xwAlert__(title, message); }

//Returns one of the constants defined below
Tools.ChoiceAlert = function(title, message, mainButton, secondaryButton, otherButton) {
	return window.XbaseController.xwChoiceAlert_____(title, message, mainButton, secondaryButton, otherButton);
}
Tools.ChoiceAlert.Default = "Default";
Tools.ChoiceAlert.Secondary = "Secondary";
Tools.ChoiceAlert.Third = "Third";
Tools.ChoiceAlert.Error = "Error";

Tools.Log = function(message) { window.XbaseController.xwLog_(message); }
Tools.Print = function(message) { window.XbaseController.xwPrint_(message); }



var Xbase = function() {}
Xbase.Quit = function(message) { window.XbaseController.xwQuit(); }
Xbase.Hide = function(message) { window.XbaseController.xwHide(); }
Xbase.HideOthers = function(message) { window.XbaseController.xwHideOthers(); }
Xbase.BringToFront = function(message) { window.XbaseController.xwBringToFront(); }
//Preferences for scripts are kept in **their own namespace** so don't try and access Xbase's private preferences
Xbase.SetPreference = function(key, value) { window.XbaseController.xwSetPreference__(key, value); }
Xbase.GetPreference = function(key) { return window.XbaseController.xwGetPreference_(key); }
Xbase.Version = window.XbaseController.xwXbaseVersion();


var Document = function() {}
Document.Close = function(shouldSave) { window.XbaseController.xwClose_(shouldSave) }
Document.Save = function() { window.XbaseController.xwSave() }
Document.Minimize = function() { window.XbaseController.xwMinimize() }
Document.Deminimize = function() { window.XbaseController.xwDeminimize() }
Document.Zoom = function() { window.XbaseController.xwZoom() }
Document.PrintReport = function(reportName) { return window.XbaseController.xwPrintReport_(reportName) }

//Scripts run in a different namespace, so this is distinct from using include which will carry over globals
Document.RunScript = function(scriptName) { return window.XbaseController.xwRunScript_(scriptName) }
Document.Formula = function(f) { return window.XbaseController.xwEvaluateFormula__(null, f) }


//PRIVATE function
function Wka2Jsa(arr)
{
	var a = new Array();
	for (i = 0; i < arr.length; i++)
	{
		a.push(arr[i]);
	}
	return a;
}

var Form = function() {}
Form.Rowid = window.XbaseController.xwFormCurrentRowid();
Form.LastEditedField = window.XbaseController.xwFormCurrentLastEditedField();
Form.ControlNamed = function(n) {
	var controlObject = 
	{
		name:n,
		get value()
		{
			 return window.XbaseController.xwFormControlValue_(this.name);
		},
		set value(x)
		{
			window.XbaseController.xwFormSetControlValue__(this.name, x);
		},
		get numValue()
		{
			 return Number(window.XbaseController.xwFormControlValue_(this.name));
		},
		set numValue(x)
		{
			window.XbaseController.xwFormSetControlValue__(this.name, String(x));
		},
		get tooltip()
		{
			return this.attr("Tooltip");
		},
		set tooltip(x)
		{
			this.setAttr("Tooltip", x);
		},
		get enabled()
		{
			return this.attr("Enabled");
		},
		set enabled(x)
		{
			this.setAttr("Enabled", x);
		},
		get visible()
		{
			return this.attr("Visible");
		},
		set visible(x)
		{
			this.setAttr("Visible", x);
		},
		get label()
		{
			return this.attr("Label");
		},
		set label(x)
		{
			this.setAttr("Label", x);
		},
		
		attr: function(propertyName)
		{
			return window.XbaseController.xwFormGetControlAttribute__(this.name,propertyName);
		},
		setAttr: function (propertyName, newValue)
		{
			window.XbaseController.xwFormSetControlAttribute___(this.name,propertyName,newValue);
		}
	};
	
	return controlObject;
}

Form.SetGlobal = function(key, value) { window.XbaseController.xwFormSetGlobal__(key, value); }
Form.GetGlobal = function(key) { return window.XbaseController.xwFormGetGlobal_(key); }
Form.ContentTable = window.XbaseController.xwFormContentTable();
Form.Name = window.XbaseController.xwFormName();



var Data = function() {}
Data.TableNames = Wka2Jsa(window.XbaseController.xwTableNames());
Data.TypeForTableField = function (tableName, fieldName) { return window.XbaseController.xwDatatypeForTable_field_(tableName, fieldName) };
Data.FieldNamesForTable = function(tableName) { return Wka2Jsa(window.XbaseController.xwFieldNamesForTable_(tableName)) }
Data.NumRows = function (tableName) { return window.XbaseController.xwRowCountForTable_(tableName) }
Data.ValuesArray = function(tableName, fieldName) { return Wka2Jsa(window.XbaseController.xwDataValuesArray__(tableName, fieldName)) }
Data.IndexOfID = function(table, rowid) { return window.XbaseController.xwIndexOfID__(table, rowid) }
Data.IDAtIndex = function(table, index) { return window.XbaseController.xwIDAtIndex__(table, index) }
Data.ValueAtIndex = function(tableName, fieldName, index) { return window.XbaseController.xwDataValue___(tableName, fieldName, Data.IDAtIndex(tableName,index)) } //return Data.ValueByID(tableName, fieldName, Data.IDAtIndex(tableName, index)) }
Data.ValueWithID = function(tableName, fieldName, rowid) { return window.XbaseController.xwDataValue___(tableName, fieldName, rowid) }

Data.SetValueAtIndex = function(tableName, fieldName, index, newValue) { return window.XbaseController.xwSetDataValue____(tableName, fieldName, Data.IDAtIndex(tableName,index), newValue) }
Data.SetValueWithID = function(tableName, fieldName, rowid, newValue) { return window.XbaseController.xwSetDataValue____(tableName, fieldName, rowid, newValue) }

Data.AddRowAtEnd = function(tableName) { return window.XbaseController.xwAddRowAtEnd_(tableName) }
Data.RemoveRowAtIndex = function(tableName, index) { return window.XbaseController.xwRemoveRowWithID__(tableName, Data.IDAtIndex(tableName, index)) }
Data.RemoveRowWithID = function(tableName, rowid) { return window.XbaseController.xwRemoveRowWithID__(tableName, rowid) }