var DS_pointer_x = 8;
var DS_pointer_y = 8;
var DS_pointer_go_x = 8;
var DS_pointer_go_y = 8;
var DS_pointer = null;
var DS_msgbox = null;

var DS_call_stack = new Array();
var DS_working = false;

function DS_showPointer()
{
	DS_call_stack.push("__DS_showPointer()");
	if (!DS_working) __DS_workStack();
}

function DS_hidePointer()
{
	DS_call_stack.push("__DS_hidePointer()");
	if (!DS_working) __DS_workStack();
}

function DS_clickPointer()
{
	DS_call_stack.push("__DS_clickPointer()");
	if (!DS_working) __DS_workStack();
}

function DS_doubleclickPointer()
{
	DS_call_stack.push("__DS_clickPointer()");
	DS_call_stack.push("__DS_wait(100)");
	DS_call_stack.push("__DS_clickPointer()");
	if (!DS_working) __DS_workStack();
}

function DS_movePointer(x, y)
{
	DS_call_stack.push("__DS_movePointer(" + x + ", " + y + ")");
	if (!DS_working) __DS_workStack();
}

function DS_jumpPointer(x, y)
{
	DS_call_stack.push("__DS_jumpPointer(" + x + ", " + y + ")");
	if (!DS_working) __DS_workStack();
}

function DS_wait(ms)
{
	DS_call_stack.push("__DS_wait(" + ms + ")");
	if (!DS_working) __DS_workStack();
}

function DS_focusField(field)
{
	DS_call_stack.push("__DS_focusField('" + field.replace("'", "\'") + "')");
	if (!DS_working) __DS_workStack();
}

function DS_setText(field, text)
{
	DS_call_stack.push("__DS_clearText('" + field.replace("'", "\'") + "')");
	DS_call_stack.push("__DS_addText('" + field.replace("'", "\'") + "','" + text.replace("'", "\'") + "')");
	if (!DS_working) __DS_workStack();
}

function DS_addText(field, text)
{
	DS_call_stack.push("__DS_addText('" + field.replace("'", "\'") + "','" + text.replace("'", "\'") + "')");
	if (!DS_working) __DS_workStack();
}

function DS_showMsgBox(text, xpos, ypos)
{
	DS_call_stack.push("__DS_showMsgBox('" + text.replace("'", "\'") + "'," + xpos + "," + ypos + ")");
	if (!DS_working) __DS_workStack();
}

function DS_hideMsgBox()
{
	DS_call_stack.push("__DS_hideMsgBox()");
	if (!DS_working) __DS_workStack();
}

function DS_addMsgBoxText(text)
{
	DS_call_stack.push("__DS_addMsgBoxText('" + text.replace("'", "\'") + "')");
	if (!DS_working) __DS_workStack();
}

function DS_callBack(func, enddemo)
{
	DS_call_stack.push("__DS_callBack('" + func.replace("'", "\'") + "'," + enddemo + ")");
	if (!DS_working) __DS_workStack();
}

function __DS_workStack()
{
	if (DS_call_stack.length == 0)
	{
		DS_working = false;
		window.status = "";
		return;
	}
	var ftc = DS_call_stack.shift();
	DS_working = true;
	window.status = "Demo running...";
	window.setTimeout(ftc, 0);
}

function __DS_showPointer()
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer.style.visibility = "visible";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_hidePointer()
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer.style.visibility = "hidden";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_clickPointer()
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer.style.backgroundImage = "url(pointer_click.gif)";
	window.setTimeout("__DS_unclickPointer()", 250);
}

function __DS_unclickPointer()
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer.style.backgroundImage = "url(pointer.gif)";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_movePointer(x, y)
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer_go_x = x;
	DS_pointer_go_y = y;
	window.setTimeout("__DS_pointerGo()", 0);
}

function __DS_pointerGo()
{
	var p = DS_pointer.style;
	
	if (DS_pointer_x < DS_pointer_go_x) DS_pointer_x++;
	if (DS_pointer_x > DS_pointer_go_x) DS_pointer_x--;
	if (DS_pointer_y < DS_pointer_go_y) DS_pointer_y++;
	if (DS_pointer_y > DS_pointer_go_y) DS_pointer_y--;
	
	p.left = DS_pointer_x;
	p.top = DS_pointer_y;

	if (DS_pointer_x != DS_pointer_go_x || DS_pointer_y != DS_pointer_go_y)
		window.setTimeout("__DS_pointerGo()", 0);
	else
		window.setTimeout("__DS_workStack()", 0);
}

function __DS_jumpPointer(x, y)
{
	if (DS_pointer == null) DS_pointer = document.getElementById("DS_pointer");
	DS_pointer.style.left = DS_pointer_x = x;
	DS_pointer.style.top  = DS_pointer_y = y;
	
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_wait(ms)
{
	window.setTimeout("__DS_workStack()", ms);
}

function __DS_focusField(field)
{
	var f = document.getElementById(field);
	if (f != null) f.focus();
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_clearText(field)
{
	var f = document.getElementById(field);
	if (f != null) f.value = "";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_addText(field, text)
{
	var f = document.getElementById(field);
	if (f != null && text != null && text.length > 0)
	{
		var l = text.charAt(0);
		text = text.slice(1);
		f.value += l;
		if (text.length > 0)
			window.setTimeout("__DS_addText('" + field.replace("'", "\'") + "','" + text.replace("'", "\'") + "')", (l==" "?0:500));
		else
			window.setTimeout("__DS_workStack()", 0);
	}
	else window.setTimeout("__DS_workStack()", 0);
}

function __DS_showMsgBox(text, xpos, ypos)
{
	if (DS_msgbox == null) DS_msgbox = document.getElementById("DS_msgbox");
	DS_msgbox.innerHTML = text;
	DS_msgbox.style.left = xpos;
	DS_msgbox.style.top = ypos;
	DS_msgbox.style.display = "block";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_hideMsgBox()
{
	if (DS_msgbox == null) DS_msgbox = document.getElementById("DS_msgbox");
	DS_msgbox.style.display = "none";
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_addMsgBoxText(text)
{
	if (DS_msgbox == null) DS_msgbox = document.getElementById("DS_msgbox");
	DS_msgbox.innerHTML += text;
	window.setTimeout("__DS_workStack()", 0);
}

function __DS_callBack(func, enddemo)
{
	eval(func);
	if (enddemo == 0) window.setTimeout("__DS_workStack()", 0);
}
