/*------------------------------------------------------------------------------*
 * File Name: DBRoutines.OGS													*
 * Creation: GJL 3/21/2001														*
 * Purpose: General Purpose Dialog Builder Routines								*
 * Copyright (c) OriginLab Corp. 2000-2001 All Rights Reserved					*
 * 																				*
 * Modification Log:															*
 *------------------------------------------------------------------------------*/

//////////////////////////////////////////////////////////////////////////////////
// Routines to Populate and Sort Various Dialog Builder Controls
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// Section Name:SortLBXorCMBX
// Purpose:Alpha-numerically sorts a Dialog Builder list box or combox in
//	ascending order.
//	%1=Name of listbox or combox							(required)
//	%2="LBX" or "CMBX" as appropriate, default is "CMBX"	(optional)
// Sample Call:
//	%A="CoxPHM!AvailableDataLBX";
//	run.section(DBRoutines,SortLBXorCMBX,"%A" "LBX");
//////////////////////////////////////////////////////////////////////////////////

[SortLBXorCMBX]
	if(macro.nArg<1)
	{
		run.section(,ReportError,$DBRoutines.SortLBXorCMBX); 
		return 1;
	}

	// Initialize variables
	%W=%1;
	%V=%2;
	if("%V"!="LBX" && "%V"!="CMBX")
		%V="CMBX"; // CMBX is default
	DBjj=%W.numItems;
	if(DBjj<2)
		return 0;
	
	// Create temporary worksheet for sorting datasets
	if(exist(DBtempsort)==2)
		win -cd DBtempsort;
	win -t data create DBtempsort;
	win -o DBtempsort {worksheet -a 2;}

	// Populate temporary worksheet
	DBChoice=%W.Choice;
	loop(DBii,1,DBjj)
	{
		DBtempsort_A[DBii]$=%W.V$(DBii)$;
		if("%V"=="LBX")
			DBtempsort_B[DBii]=%W.A$(DBii);
	}
	
	// Sort temporary worksheet with list of datasets
	sort -w DBtempsort DBtempsort_A;
	
	// Re-populate listbox/combox
	loop(DBii,1,DBjj)
	{
		%W.V$(DBii)$=DBtempsort_A[DBii]$;
		if("%V"=="LBX")
			%W.A$(DBii)=DBtempsort_B[DBii];
	}
	if("%V"=="CMBX")
		%W.Choice=DBChoice;
		
	// Delete temporary worksheet and used variables
	if(exist(DBtempsort)==2)
		win -cd DBtempsort;
	delete -v DBii;
	delete -v DBjj;
	delete -v DBChoice;
	%V="";%W="";

	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:PopCMBXwZStringTokens
// Purpose:Populate one Dialog Builder combox or listbox with all tokens in the
//	%Z string variable. Returns the number of items added.
//	%1=Name of combox/listbox to populate								(required)
// Sample Call:
//	%A="ANOVA2Way!LevelsACMBX";
//	%Z="Level A1|Level A2|Level A3|Male|Female";
//	run.section(DBRoutines,PopCMBXwZStringTokens,"%A");
//////////////////////////////////////////////////////////////////////////////////

[PopCMBXwZStringTokens]
	// Check for required arguments
	if(macro.nArg<1)
	{
		run.section(,ReportError,$DBRoutines.PopCMBXwZStringTokens); 
		return 1;
	}
	
	// Initialize variables
	%V=%1;

	for(DBii=1;DBii>0;DBii++)
	{
		%W=%[%Z,#DBii,|];
		if(%[%W]==0)
			break;
		%V.V$(DBii)$=%W;
	}
	%V="";%W="";

	return DBii-1;
	
//////////////////////////////////////////////////////////////////////////////////
// Section Name:PopCMBXwNonTextData
// Purpose:Populate one Dialog Builder combox with all non-Text non-excluded
//	datasets and select the item that is the first selected column in the active
//	worksheet
//	%1=Name of combox to populate				(required)
//  %2=List of base worksheet names to exclude	(optional)
// Sample Call:
//	%A="TTestMean1Sample!DatasetNameCMBX";
//	%Z="Survival NLSF";
//	run.section(DBRoutines,PopCMBXwNonTextData,"%A" "%Z");
//////////////////////////////////////////////////////////////////////////////////

[PopCMBXwNonTextData]
	// Check for required arguments
	if(macro.nArg<1)
	{
		run.section(,ReportError,$DBRoutines.PopCMBXwNonTextData); 
		return 1;
	}
	
	// Initialize variables and get list of datasets
	%W=%1;
	%Z=%2;
	run.section(,GetNonTextDatasets);

	// Populate combox and select item	
	get DBtempsort_A -e DBjj;
	%W.Choice=0;
	loop(DBii,1,DBjj)
	{
		%W.V$(DBii)$=DBtempsort_A[DBii]$;
		if(DBtempsort_B[DBii]==1)
			%W.Choice=DBii;
	}
	if(%W.Choice==0 && %W.numItems>0)
		%W.Choice=1;		

	run.section(,CleanUp);

	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:Pop2CMBXSwNonTextData
// Purpose:Populate two Dialog Builder comboxes with all non-Text non-excluded
//	datasets and select the items that are the first and second selected
//	columns in the active worksheet.
//	%1=Name of first combox to populate			(required)
//	%2=Name of second combox to populate		(required)
//	%3=List of base worksheet names to exclude	(optional)
// Sample Call:
//	%A="TTestMean2Sample!Sample1DataCMBX";
//	%B="TTestMean2Sample!Sample2DataCMBX";
//	%Z="Survival NLSF";
//	run.section(DBRoutines,Pop2CMBXSwNonTextData,"%A" "%B" "%Z");
//////////////////////////////////////////////////////////////////////////////////

[Pop2CMBXSwNonTextData]
	// Check for required arguments
	if(macro.nArg<2)
	{
		run.section(,ReportError,$DBRoutines.Pop2CMBXSwNonTextData); 
		return 1;
	}

	// Initialize variables and get list of datasets
	%W=%1;
	%V=%2;
	%Z=%3;
	run.section(,GetNonTextDatasets);

	// Populate combox and select items	
	get DBtempsort_A -e DBjj;
	%W.Choice=0;
	%V.Choice=0;
	loop(DBii,1,DBjj)
	{
		%W.V$(DBii)$=DBtempsort_A[DBii]$;
		%V.V$(DBii)$=DBtempsort_A[DBii]$;
		if(DBtempsort_B[DBii]==1)
			%W.Choice=DBii;
		if(DBtempsort_B[DBii]==2)
			%V.Choice=DBii;
	}
	if(%W.Choice==0 && %W.numItems>0)
		%W.Choice=1;
	if(%V.Choice==0 && %V.numItems>0)
		%V.Choice=1;

	run.section(,CleanUp);
	%V="";
	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:PopLBXwNonTextData
// Purpose:Populate a Dialog Builder list box with all non-Text non-excluded
//	datasets and select all items that are selected in the active worksheet.
//	%1=Name of list box to populate					(required)
//  %2=List of base worksheet names to exclude		(optional)
// Sample Call:
//	%A="ANOVA1Way!AvailableDataLBX";
//	%Z="Survival NLSF";
//	run.section(DBRoutines,PopLBXwNonTextData,"%A" "%Z");
//////////////////////////////////////////////////////////////////////////////////

[PopLBXwNonTextData]
	// Check for required arguments
	if(macro.nArg<1)
	{
		run.section(,ReportError,$DBRoutines.PopLBXwNonTextData); 
		return 1;
	}
	
	// Initialize variables and get list of datasets
	%W=%1;
	%Z=%2;
	run.section(,GetNonTextDatasets);
	
	// Populate listbox and select items
	get DBtempsort_A -e DBjj;
	loop(DBii,1,DBjj)
	{
		%W.V$(DBii)$=DBtempsort_A[DBii]$;
		if(DBtempsort_B[DBii]>0)
			%W.A$(DBii)=1;
		else
			%W.A$(DBii)=0;
	}
	
	run.section(,CleanUp);
	
	return 0;
	
//////////////////////////////////////////////////////////////////////////////////
// Section Name:PopLBXwAllData
// Purpose:Populate a Dialog Builder list box with all non-excluded datasets
//	and select all items that are selected in the active worksheet.
//	%1=Name of list box to populate					(required)
//  %2=List of base worksheet names to exclude		(optional)
// Sample Call:
//	%A="ANOVA1Way!AvailableDataLBX";
//	%Z="Survival NLSF";
//	run.section(DBRoutines,PopLBXwAllData,"%A" "%Z");
//////////////////////////////////////////////////////////////////////////////////

[PopLBXwAllData]
	// Check for required arguments
	if(macro.nArg<1)
	{
		run.section(,ReportError,$DBRoutines.PopLBXwNonTextData); 
		return 1;
	}
	
	// Initialize variables and get list of datasets
	%W=%1;
	%Z=%2;
	run.section(,GetAllDatasets);
	
	// Populate listbox and select items
	get DBtempsort_A -e DBjj;
	loop(DBii,1,DBjj)
	{
		%W.V$(DBii)$=DBtempsort_A[DBii]$;
		if(DBtempsort_B[DBii]>0)
			%W.A$(DBii)=1;
		else
			%W.A$(DBii)=0;
	}
	
	run.section(,CleanUp);
	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:GetNonTextDatasets
// Purpose:Build and sort a list of all non-Text non-excluded datasets in an OPJ
//	and their selection status. Return list in hidden worksheet named DBtempsort.
//  %1=List of base worksheet names to exclude	(optional)
// Sample Call:
//	%A="Survival NLSF";
//	run.section(DBRoutines,GetNonTextDatasets,"%A");
//////////////////////////////////////////////////////////////////////////////////	

[GetNonTextDatasets]
	// Initialize variables and create hidden DBtempsort worksheet to hold list	
	%L=%H; // Remember active worksheet name
	if("%[%Z,1:1]"=="%") // If %Z is null (i.e. %Z==%2, or =%3, etc.)
		%Z="DBtempsort"; //  just exclude DBtempsort
	else
		%Z="%Z DBtempsort"; // Else exclude list in %Z plus DBtempsort
	if(exist(DBtempsort)==2)
		win -cd DBtempsort;
	win -t data create DBtempsort;
	win -o DBtempsort {worksheet -a 2;}
	DBii=1;
	DBselect=0;
	
	// Loop on all worksheets in OPJ
	doc -e W
	{
		if(run.section(,IncludeWorksheet)) // Include worksheet?
		{
			// Loop on all datasets in non-excluded worksheets
			doc -e D
			{
				// Non-Text dataset?
				if(%H!wks.col$(colnum(%C)).format!=2)
				{
					// Add dataset to list
					DBtempsort_A[DBii]$=%C;
					DBtempsort_B[DBii]=0; // Initialize as not selected
					// Was current worksheet active?
					if("%H"=="%L")
					{
						// Is current dataset selected?
						if(mod(wks.isColSel(colnum(%C)),2))
						{
							// Remember selection
							DBselect++;
							DBtempsort_B[DBii]=DBselect;
						}
					}
					DBii++;
				}
			}
		}
	}

	// Sort temporary worksheet with list of datasets
	sort -w DBtempsort DBtempsort_A;

	// Delete used variables and re-activate active worksheet
	delete -v DBii;
	delete -v DBselect;
	win -a %L;
	%L="";%Z=""; 
	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:GetAllDatasets
// Purpose:Build and sort a list of all non-excluded datasets in an OPJ and their
//	selection status. Return list in hidden worksheet named DBtempsort.
//  %1=List of base worksheet names to exclude	(optional)
// Sample Call:
//	%A="Survival NLSF";
//	run.section(DBRoutines,GetAllDatasets,"%A");
//////////////////////////////////////////////////////////////////////////////////	

[GetAllDatasets]
	// Initialize variables and create hidden DBtempsort worksheet to hold list	
	%L=%H; // Remember active worksheet name
	if("%[%Z,1:1]"=="%") // If %Z is null (i.e. %Z==%2, or =%3, etc.)
		%Z="DBtempsort"; //  just exclude DBtempsort
	else
		%Z="%Z DBtempsort"; // Else exclude list in %Z plus DBtempsort
	if(exist(DBtempsort)==2)
		win -cd DBtempsort;
	win -t data create DBtempsort;
	win -o DBtempsort {worksheet -a 2;}
	DBii=1;
	DBselect=0;
	
	// Loop on all worksheets in OPJ
	doc -e W
	{
		if(run.section(,IncludeWorksheet)) // Include worksheet?
		{
			// Loop on all datasets in non-excluded worksheets
			doc -e D
			{
				// Add dataset to list
				DBtempsort_A[DBii]$=%C;
				DBtempsort_B[DBii]=0; // Initialize as not selected
				// Was current worksheet active?
				if("%H"=="%L")
				{
					// Is current dataset selected?
					if(mod(wks.isColSel(colnum(%C)),2))
					{
						// Remember selection
						DBselect++;
						DBtempsort_B[DBii]=DBselect;
					}
				}
				DBii++;
			}
		}
	}

	// Sort temporary worksheet with list of datasets
	sort -w DBtempsort DBtempsort_A;

	// Delete used variables and re-activate active worksheet
	delete -v DBii;
	delete -v DBselect;
	win -a %L;
	%L="";%Z=""; 
	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:IncludeWorksheet
// Purpose:	Returns 1 if the begining characters in the active worksheet's name
//	are included in a list of worksheet base names; Otherwise, returns 0. The
//	list of worksheet base names to be excluded is held in %Z.
// Sample Call:
//	%Z="DBtempsort Survival NLSF"; // Base worksheet names to be excluded
//	run.section(DBRoutines,IncludeWorksheet);
//////////////////////////////////////////////////////////////////////////////////

[IncludeWorksheet]
	// Loop until item found in list or end of list is reached
	for(DBjj=1;DBjj>0;DBjj++) 
	{
		%U=%[%Z,#$(DBjj)]; // Get worksheet base name to exclude
		if("%U"=="")
		{
			delete -v DBjj;%U="";
			return 1; // End of list, include worksheet
		}
		if("%U"=="%[%H,1:%[%U]]")
		{
			delete -v DBjj;%U="";
			return 0; // Base name match, exclude worksheet
		}
	}
	return 1; // Never gets here

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CleanUp
// Purpose:Deletes temporary worksheet and variables.
// Sample Call:
//	run.section(,CleanUp);
//////////////////////////////////////////////////////////////////////////////////

[CleanUp]
	if(exist(DBtempsort)==2)
		win -cd DBtempsort;
	delete -v DBii;
	delete -v DBjj;
	%W="";%Z="";
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:PopCMBXwWKSCols
// Purpose:Populate Dialog Builder comboxes and list boxes with worksheet columns
//	and make appropriate selections
// Arguments:
//	%1=CMBX/LBX name				(required)
//	%2=Selected item				(required)
//		0 means select first item in list
//		1 means multiple worksheet selection (for list boxes only)
//		2 means select first selected worksheet column
//		3 means select last selected worksheet column
//		worksheet_column means select worksheet_column if in list 
//	%3=Condition where:				(required)
//		0 means add all columns
//		1 means add all Y columns
//		2 means add all non-text and non-Label columns
//		etc.
//	%4=Condition argument			(optional)
// Sample Call:
//	%A="TTestMean1Sample!DatasetNameCMBX";
//	%B=%C;
//	if(%[%B]==0) %B="0";
//	run.section(DBRoutines,PopCMBXwWKSCols,"%A" "%B" 0);
//////////////////////////////////////////////////////////////////////////////////

[PopCMBXwWKSCols]
	if(macro.nArg<3)
	{
		run.section(,ReportError,$DBRoutines.PopCMBXwWKSCols); 
		return 1;
	}
	%1.Choice=0;
	DBii=1; // Item index
	doc -e W
	{
		doc -e D
		{
			switch(%3)
			{
				case 0:
					run.section(,PopAddItemToCMBX,%1 %2);
					break;
				case 1:
					if(%H!wks.col$(colnum(%C)).type==1)
						run.section(,PopAddItemToCMBX,%1 %2);
					break;
				case 2:
					if(%H!wks.col$(colnum(%C)).format!=2)
						run.section(,PopAddItemToCMBX,%1 %2);
					break;
			} // end switch
		} // end doc -e D
	} // doc -e W
	if(%1.Choice==0) %1.Choice=(%1.numItems>0?1:0); 
	delete -v DBii;
	return 0;

[PopAddItemToCMBX]
	%1.V$(DBii)$=%C;
	switch(%2)
	{
		case 0:
			break;
		case 1:
			%1.A$(DBii)=mod(wks.isColSel(colnum(%C)),2);
			break;
		case 2:
			if(%1.Choice==0 && mod(wks.isColSel(colnum(%C)),2)==1)
				%1.Choice=DBii; 
			break;
		case 3:
			if(mod(wks.isColSel(colnum(%C)),2)==1)
				%1.Choice=DBii;
			break;		
		default:
			if("%2"=="%C") %1.Choice=DBii;
			break;
	}
	DBii+=1;
 	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Routines to Perform Input Checking on Various Dialog Builder Controls
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckDatasetExists
// Arguments:
//	%1=Error message 				(required)
//		set %1=0 for no output
//	%2=Dataset to check				(required)
// Sample Call:
//	%A=TTestMean1Sample!DatasetNameCMBX.V$(TTestMean1Sample!DatasetNameCMBX.Choice)$;
//	%W="The Sample dataset (%A) does not exist."
//	if(run.section(DBRoutines.OGS,CheckDatasetExists,"%W" "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckDatasetExists]
	if(macro.nArg<2 || exist(%2)!=1)
	{
		run.section(,ReportError,"%1");
		return 1;
	}
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckDatasetAllNumerics
// Arguments:
//	%1=Error message 				(required)
//		set %1=0 for no output
//	%2=Dataset to check				(required)
// Sample Call:
//	%A=ANOVA2Way!SelectedDataLCNTRL.V1_1$;
//	%W="The selected dataset (%A) must contain only numeric values."
//	if(run.section(DBRoutines.OGS,CheckDatasetAllNumerics,"%W" "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckDatasetAllNumerics]
	if(macro.nArg==2)
	{
		%V=%2;
		get %V -e DBnPts;
		sum(%V);
		if(sum.n==DBnPts)
		{
			%V="";
			delete -v DBnPts;
			return 0;
		}
		%V="";
		delete -v DBnPts;
	}
	run.section(,ReportError,"%1");
	return 1;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckSingleNumeric
// Arguments:
//	%1=Error message				(required)
//		set %1=0 for no output
//	%2=Item to check				(required)
// Sample Call:
//	%A=TTestMean1Sample!TestMeanEBX.V1$;
//	%W="Mean must be a single numeric value.";
//	if(run.section(DBRoutines.OGS,CheckSingleNumeric,"%W" (%A))!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckSingleNumeric]
	DBtest=%2;
	if(macro.nArg<2 || DBtest==0/0 || exist(%2)==4)
	{
		delete -v DBtest;
		run.section(,ReportError,"%1");
		return 1;
	}
	delete -v DBtest;	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckSingleNumericGTandLT
// Arguments:
//	%1=Error message					(required)
//		set %1=0 for no output
//	%2=Item must be greater than %2		(required)
//	%3=Item must be less than %3		(required)
//	%4=Item to check					(required)
// Sample Call:
//	%A=TTestMean1Sample!SignificanceLevelEBX.V1$;	
//	%W="Significance Level must be a single numeric value > 0 and < 1."; 
//	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%W" 0 1 (%A))!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckSingleNumericGTandLT]
	DBtest=%4;
	if(macro.nArg<4 || DBtest==0/0 || exist(%4)==4 || DBtest<=%2 || DBtest>=%3)
	{
		delete -v DBtest;
		run.section(,ReportError,"%1");
		return 1;
	}
	delete -v DBtest;	
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckMultipleNumericsGTandLT
// Arguments:
//	%1=Error message					(required)
//		set %1=0 for no output
//	%2=Items must be greater than %2	(required)
//	%3=Items must be less than %3		(required)
//	%4=List of items to check			(required)
// Sample Call:
//	%A=TTestMean1Sample!ConfidenceIntervalsEBX.V1$;
//	%W="Confidence Level(s) must be a comma separated list of numeric values > 0 and < 100.";
//	if(run.section(DBRoutines.OGS,CheckMultipleNumericsGTandLT,"%W" 0 100 "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckMultipleNumericsGTandLT]
	%W=%4;
	_DBTEST={%4};
	get _DBTEST -e DBjj;
	loop(DBii,1,DBjj)
	{
		DBtest=_DBTEST[DBii];
		if(macro.nArg<4 || DBtest==0/0 || exist(%[%W,#DBii,,])==4 || DBtest<=%2 || DBtest>=%3)
		{
			delete -v DBii;
			delete -v DBjj;
			delete -v DBtest;
			delete _DBTEST;
			%W="";
			run.section(,ReportError,"%1");
			return 1;
		}
	}
	delete -v DBii;
	delete -v DBjj;
	delete -v DBtest;
	delete _DBTEST;
	%W="";
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckMultiplePositiveIntegers
// Arguments:
//	%1=Error message					(required)
//		set %1=0 for no output
//	%2=List of items to check			(required)
// Sample Call:
//	%A=TTestMean1Sample!SampleSizeEBX.V1$;
//	%W="Sample Size(s) must be a comma separated list of positive Integers.";
//	if(run.section(DBRoutines.OGS,CheckMultiplePositiveIntegers,"%W" "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckMultiplePositiveIntegers]
	%W=%2;	
	_DBTEST={%W};
	get _DBTEST -e DBjj;
	loop(DBii,1,DBjj)
	{
		DBtest=_DBTEST[DBii];
		if(macro.nArg<2 || DBtest==0/0 || exist(%[%W,#DBii,,])==4 || DBtest<=0 || int(DBtest)!=DBtest)
		{
			delete -v DBii;
			delete -v DBjj;
			delete -v DBtest;
			delete _DBTEST;
			%W="";
			run.section(,ReportError,"%1");
			return 1;
		}
	}
	delete -v DBii;
	delete -v DBjj;
	delete -v DBtest;
	delete _DBTEST;
	%W="";
	return 0;
	
//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckCharLengthGEandLE
// Arguments:
//	%1=Error message						(required)
//		set %1=0 for no output
//	%2=Minimum number of characters			(required)
//	%3=Maximum number of characters			(required)
//	%4=String to check						(required)
// Sample Call:
//	%A=MyDialog!MyControlEBX.V1$;
//	%W="MyControl must contain between 6 and 10 characters.";
//	if(run.section(DBRoutines.OGS,CheckCharLengthGEandLE,"%W" 6 10 "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckCharLengthGEandLE]
	if(macro.nArg<4 || %[%4] < %2 || %[%4] > %3)
	{
		run.section(,ReportError,"%1");
		return 1;
	}
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckAllAlphaNumericChars
// Arguments:
//	%1=Error message						(required)
//		set %1=0 for no output
//	%2=String to check						(required)
// Sample Call:
//	%A=MyDialog!MyControlEBX.V1$;
//	%W="MyControl must contain only alpha-numeric characters.";
//	if(run.section(DBRoutines.OGS,CheckAllAlphaNumericChars,"%W" "%A")!=0) return 1;
//////////////////////////////////////////////////////////////////////////////////

[CheckAllAlphaNumericChars]
	if(macro.nArg<2) run.section(,ReportError,"%1");  
	DBjj=%[%2];
	for(DBii=1;DBii<=DBjj;DBii++)
	{
		DBASCII=asc(%[%2,DBii:DBii]);
		if(DBASCII<48 || (DBASCII>57 && DBASCII<65) || (DBASCII>90 && DBASCII<97) || (DBASCII>122))
		{
			delete -v DBjj;
			delete -v DBii;
			delete -v DBASCII;
			run.section(,ReportError,"%1");
			return 1;
		}
	}
	delete -v DBjj;
	delete -v DBii;
	delete -v DBASCII;
	return 0;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:CheckIfItemIsInList
// Purpose:Checks whether or not the passed item is in a combox or list box.
//	Depending on the value of the third argument, the item is added if it is not
//	found.  Depending on the value of the fourth argument, a Z string containing
//	a list of items in the list is created.  Returns 0 if an error occurs or if
//	the item is not found and not added.  Returns the index of the item if found
//	or if added.
//	%1=Dialog Builder combox or list box to check	(required)
//	%2=Item to check for in %1						(required)
//	%3=Add item or not?								(required)
//		%3=0 means do not add item
//		%3=1 means add item if not found
//	%4=Make Z string of items in list				(required)
//		%4=0 means don't make list
//		%4=1 means make space separated list
//		%4=2 means make space separated list with items in double quotes  
// Sample Call:
//	%A="ANOVA2Way!LevelsACMBX";
//	%B=ANOVA2Way!LevelsACMBX$;
//	if(run.section(DBRoutines.OGS,CheckIfItemIsInList,"%A" "%B" 1 2)<0) {// run some script};
//////////////////////////////////////////////////////////////////////////////////

[CheckIfItemIsInList]
	if(macro.nArg<4)
	{
		run.section(,ReportError,$DBRoutines.CheckIfItemIsInList);
		return 0;
	}
	%V=%1;
	%W=%2;
	DBaddItem=%3;
	DBZString=%4;
	switch(DBZString)
	{
		case 0:
			break;
		case 1:
			%Z="";
			break;
		case 2:
			%Z=""""; // Don't ask why
			break;
	}
	DBindex=0;
	DBjj=%V.numItems;
	for(DBii=1;DBii<=DBjj;DBii++)
	{
		%U=%V.V$(DBii)$;
		if("%W"=="%U")
			DBindex=DBii;
		switch(DBZString)
		{
			case 0:
				break;
			case 1:
				%Z=%Z %U;
				break;
			case 2:
				%Z=%Z "%U";
				break;
		}
	}
	if(DBindex==0 && DBaddItem==1)
	{
		%V.V$(DBii)$=%W;
		DBindex=DBii;
		switch(DBZString)
		{
			case 0:
				break;
			case 1:
				%Z=%Z %W;
				break;
			case 2:
				%Z=%Z "%W";
				break;
		}
	}
	if(DBindex<1 && DBaddItem==1)
		DBindex=0;
	if(DBindex<0)
		DBindex=0;	
	DBjj=%V.numItems;
	if(DBindex>DBjj)
		DBindex=0;
	if(DBZString==2)
		%Z=%[%Z,6:%[%Z]]; // strip off extra """"<space>
	%V="";%W="";%U="";
	delete -v DBii;
	delete -v DBjj;
	delete -v DBaddItem;
	delete -v DBZString;
	return DBindex;

//////////////////////////////////////////////////////////////////////////////////
// Section Name:ReportError
// Purpose:General Purpose Error reporting Routine
// Arguments:
//	%1=Error message			(required)
//		%1==0 forces no output
// Sample Call:
//	run.section(,ReportError,"%A must be a single numeric value.");
//////////////////////////////////////////////////////////////////////////////////

[ReportError]
	if(macro.nArg>0 && "%1"!="0")
	{
		type -b;			// BEEP!
		type -b %1;			// Type out the appropriate error message
		type -q "";         // Clear Status Bar
		return 0;
	}
	return 1;


//////////////////////////////////////////////////////////////////////////////////
// Routines to Load, Compile, and Enable Origin C Workspaces
//////////////////////////////////////////////////////////////////////////////////

//[LoadSFFTWorkspace]
	//if(run.LoadOC("OriginLab\SFFT.OCW",4)!=0)
	//{
		//run.section(DBRoutines.OGS,ReportError,$SFFT.SFFTCnotLoaded);
		//return 1;
	//}
	//return 0;
//
//[LoadStatisticsOnWorkspace]
	//if(run.LoadOC("OriginLab\StatisticsOn.OCW",4)!=0)	
	//{
		//run.section(DBRoutines.OGS,ReportError,$StatisticsOn.StatisticsOnCnotLoaded);
		//return 1;
	//}
	//return 0;
//
//[LoadNonParamTestsWorkspace]
	//if(run.LoadOC("OriginLab\NonParamTests.OCW",4)!=0)	
	//{
		//run.section(DBRoutines.OGS,ReportError,$NonParamTests.NonParamTestsCnotLoaded);
		//return 1;
	//}
	//return 0;

[LoadOMatWorkspace]
	if(run.LoadOC("OriginLab\OMat.OCW",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$OMat.OMatCNotLoaded);
		return 1;
	}
	return 0;

[LoadSurvivalAnalysisWorkspace]
	if(run.LoadOC("OriginLab\SurvivalAnalysis.OCW",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$SurvivalAnalysis.SACnotLoaded);
		return 1;
	}
	return 0;

[LoadOStatWorkspace]
	if(run.LoadOC("OriginLab\OStat.OCW",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$OStatCommon.OStatCnotLoaded);
		return 1;
	}
	return 0;

[SaveAndSetAtOC]
	SaveSysVarAtOC=@OC;
	@OC=1;
	return 0;

[RestoreAtOC]
	if(exist(SaveSysVarAtOC)==4)
	{
		@OC=SaveSysVarAtOC;
		delete -v SaveSysVarAtOC;
	}
	else
		@OC=1;
	return 0;