/* vbs.js - JavaScript wrapper for VBScript functions
	usage: vbs.MsgBox("MsgBox Text", vbs.vbOKOnly, "MsgBox Title");
	etc...
*/

// For each function, for each possible number of arguments, we
// will create one VB function.
// For each function, we create one JS function. This has a switch
// statement for each possible number of arguments, which calls the
// right VB function. 
// Example:
// Function vbsTest0()
//     vbsText0=Test()
// End Function
// Function vbsTest1(a0)
//     vbsText1=Test(a0)
// End Function
// Function vbsTest2(a0,a1)
//     vbsText2=Test(a0,a1)
// End Function
// vbs.Test = function() {
//     switch (arguments.length) {
//         case 0:return vbsText0();
//         case 1:return vbsText1(arguments[0]);
//         case 2:return vbsText2(arguments[0],arguments[1]);
//     }
//     throw new Error(0x800a01c2, "Wrong number of arguments or invalid property assignment: 'Test'");
// }

var vbs = {
	vbOKOnly: 0,
    vbOKCancel: 1,
    vbAbortRetryIgnore: 2,
    vbYesNoCancel: 3,
    vbYesNo: 4,
    vbRetryCancel: 5,
    vbCritical: 16,
    vbQuestion: 32,
    vbExclamation: 48,
    vbInformation: 64,
    vbDefaultButton1: 0,
    vbDefaultButton2: 256,
    vbDefaultButton3: 512,
    vbDefaultButton4: 768,
    vbApplicationModal: 0,
    vbSystemModal: 4096
};
{
	// List of functions and the number of arguments they accept.
	var aVBFunctions = {
		"Abs":[1],
		"Array":[0,1,2,3,4,5,6,7,8,9,10], // Arbitrary number of arguments... limited to 10
		"Asc":[1],
		"Atn":[1],
		"CBool":[1],
		"CByte":[1],
		"CCur":[1],
		"CDate":[1],
		"CDbl":[1],
		"Chr":[1],
		"CInt":[1],
		"CLng":[1],
		"Cos":[1],
		"CreateObject":[1,2],
		"CSng":[1],
		"CStr":[1],
		"Date":[0],
		"DateAdd":[3],
		"DateDiff":[3,4,5],
		"DatePart":[2,3,4],
		"DateSerial":[3],
		"DateValue":[1],
		"Day":[1],
		"Escape":[1],
		"Eval":[1],
		"Exp":[1],
		"Filter":[2,3,4],
		"Fix":[1],
		"FormatCurrency":[1,2,3,4,5],
		"FormatDatTime":[1,2],
		"FormatNumber":[1,2,3,4,5],
		"FormatPercent":[1,2,3,4,5],
		"GetLocale":[0],
		"GetObject":[0,1,2],
		"GetRef":[1],
		"Hex":[1],
		"Hour":[1],
		"InputBox":[1,2,3,4,5,7],
		"InStr":[2,3,4],
		"InStrRev":[2,3,4],
		"Int":[1],
		"IsArray":[1],
		"IsDate":[1],
		"IsEmpty":[1],
		"IsNull":[1],
		"IsNumeric":[1],
		"IsObject":[1],
		"Join":[1,2],
		"LBound":[1,2],
		"LCase":[1],
		"Left":[2],
		"Len":[1],
		"LoadPicture":[1],
		"Log":[1],
		"LTrim":[1],
		"Mid":[2,3],
		"Minute":[1],
		"Month":[1],
		"MonthName":[1,2],
		"MsgBox":[1,2,3,5],
		"Now":[0],
		"Oct":[1],
		"Replace":[3,4,5,6],
		"RGB":[3],
		"Right":[2],
		"Rnd":[0,1],
		"Round":[1,2],
		"RTrim":[1],
		"ScriptEngine":[0],
		"ScriptEngineBuildVersion":[0],
		"ScriptEngineMajorVersion":[0],
		"ScriptEngineMinorVersion":[0],
		"Second":[1],
		"SetLocale":[1],
		"Sgn":[1],
		"Sin":[1],
		"Space":[1],
		"Split":[1,2,3,4],
		"Sqr":[1],
		"StrComp":[2,3],
		"String":[2],
		"StrReverse":[1],
		"Tan":[1],
		"Time":[0],
		"Timer":[0],
		"TimeSerial":[3],
		"TimeValue":[1],
		"Trim":[1],
		"TypeName":[1],
		"UBound":[1,2],
		"UCase":[1],
		"Unescape":[1],
		"VarType":[1],
		"Weekday":[1,2],
		"WeekDayName":[3],
		"Year":[1]
	}
	var sVBCode = ""; // In case we can't use window.execScript
	for (var sFunction in aVBFunctions) {
		var aJSCases = [];
		for (var iArgn in aVBFunctions[sFunction]) {
			iArgc = aVBFunctions[sFunction][iArgn];
			var aVBArgs = [];
			var aJSArgs = [];
			if (iArgc > 0) {
				for (var i = 0; i < iArgc; i++) {
					aVBArgs.push("a" + i.toString());
					aJSArgs.push("arguments[" + i.toString() + "]");
				}
			}
			var sVBArgs = "(" + aVBArgs.join(",") + ")";
			var sJSArgs = "(" + aJSArgs.join(",") + ")";
			var sVBFunction = "vbs" + sFunction + iArgc.toString();
			aJSCases.push("\t\tcase " + iArgc + ":return " + sVBFunction + "(" + aJSArgs.join(",") + ");\n");
			var sVBFunctionCode = 
				"Function " + sVBFunction + sVBArgs + "\n" +
				"\t" + sVBFunction + "=" + sFunction + sVBArgs + "\n" +
				"End Function\n";
			if (typeof(window.execScript) == "undefined") {
				sVBCode += sVBFunctionCode;
			} else {
				window.execScript(sVBFunctionCode, "VBScript");
			}
		}
		var sJSFunctionCode = 
			"\tswitch(arguments.length) {\n" + 
				aJSCases.join("") + 
			"\t}\n" +
			"\tthrow new Error(0x800a01c2, \"Wrong number of arguments or invalid property assignment: '" + sFunction + "'\");";
		vbs[sFunction] = new Function(sJSFunctionCode);;
	}
	if (typeof(window.execScript) == "undefined") {
		document.write("xSCRIPT language=\"VBScript\">" + sVBCode + "x/SCRIPT>");
	}
}
