var Inknal_ToolbarWidth = 0;
var Inknal_enter_toolbar_time;
var Inknal_leave_toolbar_time;
var Inknal_hiding = false;

function Logout()
{
	location.href=$url_dir+'/?command=logout';
}

function SaveDocument()
{
	var filename = prompt('Choose a name for your document:','');
	var randomnum = Math.round(10000*Math.random());
	var uniqueID = localip+'-'+randomnum;
	var data = 'request=post&uid='+uniqueID+'&data='+getSVGCode();
	var token = {id: uniqueID, filename: filename};

	if(filename != null)
		xmlPostData($url_dir+'/InkWidget/save.php', data, token, saveDocument_Return);
}

function saveDocument_Return(token, response) {
	if(response == 'OK') {
		xmlPostData($url_dir+'/save.php?token='+token.id+'&filename='+token.filename, '', null, saveDocument_Done);
	} else {
		alert('Unable to upload the image!');
	}
}

function saveDocument_Done(token, response) {
	if(response == 'SUCCESS') {
		alert('Document saved');
	} else {
		alert('Error saving document: '+response);
	}
}

function SetupToolbar()
{
	var tb = document.getElementById('toolbar_holder');
	var tc = document.getElementById('toolbar_content');
	var iwtb = document.getElementById('InkWidget_toolbar');
	
	tb.appendChild(iwtb);
	Inknal_ToolbarWidth = getWidth(iwtb);
	tc.style.width = '1px'; 
	iwtb.style.width = '1px';
	iwtb.style.visibility = 'visible';
	iwtb.style.position = 'relative';
}

function ShowToolbar()
{
	var iwtb = document.getElementById('toolbar_content');
	var width = getWidth(iwtb);
	
	if(Inknal_hiding)
		return;
	if(width < (Inknal_ToolbarWidth-10)) {
		iwtb.style.width = (width+10)+'px';
		setTimeout("ShowToolbar();", 10);
	} else {
		iwtb.style.width = Inknal_ToolbarWidth+'px';
	}
}

function HideToolbar()
{
	var iwtb = document.getElementById('toolbar_content');
	var width = getWidth(iwtb);
	
	if((width - 10) > 1) {
		Inknal_hiding = true;
		iwtb.style.width = (width-10)+'px';
		setTimeout("HideToolbar();", 10);
	} else {
		Inknal_hiding = false;
		iwtb.style.width = '1px';
	}
}

function TestOverToolbar()
{
	if(Inknal_leave_toolbar_time > Inknal_enter_toolbar_time)
	{
		var pallete = document.getElementById('color-select');
		
		if(pallete.style.visibility != 'hidden') {
			setTimeout("TestOverToolbar();", 300);
		} else {
			setTimeout("HideToolbar();", 10);
		}
	}
}

function OverToolbar()
{
	var d = new Date();
	
	Inknal_enter_toolbar_time = d.getTime();
	setTimeout("ShowToolbar();", 10);
}

function LeaveToolbar()
{
	var d = new Date();
	
	Inknal_leave_toolbar_time = d.getTime();
	setTimeout("TestOverToolbar();", 300);
}

function SetRule(type)
{
	if(type == 'Blank') {
		setBackground(null, 0, 0);
	} else if(type == 'Lined') {
		setBackground($url_dir+'/images/rule-lined.png', 39, 36);
	} else if(type == 'E-2') {
		setBackground($url_dir+'/images/rule-e2.png', 71, 71);
	}
}

/* From InkSurvey, consolidate in InkWidget? */
function requestDataExt(url, data, userFunction)
{
	http_request = false;
	if(window.XMLHttpRequest)
	{
		// Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		// IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() {
		if(http_request.readyState == 4) {
			userFunction(http_request);
		}
	}
	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.setRequestHeader("Content-length", data.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(data);
	return true;
}

/* From InkSurvey, consolidate in InkWidget? */
function xmlGetChildNode(parent, childName)
{
	for (x = 0; x < parent.childNodes.length; x++)
	{
		var sibl = parent.childNodes.item(x);
		
		if(sibl.childNodes.length > 0 && sibl.tagName == childName)
		{
			return sibl.childNodes.item(0);
		}
	}
	var response = {
		data : ""
	}
	return response;
}

function ListDocuments()
{
	requestDataExt($url_dir+'/list.php', '', listDocuments_Return);
}

function openDocument(docname, doctype, docid)
{
	var opendialog = document.getElementById('opendialog');
	opendialog.style.visibility = 'hidden';
	xmlGetData($url_dir+'/open.php?doctype='+doctype+'&docid='+docid, '', openDocument_Return);
}

function openDocument_Return(token, response)
{
	if(response.substr(0, 7) == 'ERROR: ') {
		alert('Failed to open document: '+response.substr(7));
	} else {
		loadImage(response);
	}
}

function addDocumentToList(docname, doctype, docid)
{
	var tbl = document.getElementById('doclist');
	var row;
	
	row = tbl.insertRow(0);
	row.id = 'select-document-'+docname;
	row.insertCell(0).innerHTML = docname;
	row.onclick = function(){openDocument(docname, doctype, docid);};
}

function listDocuments_Return(response)
{
	var opendialog = document.getElementById('opendialog');
	var xmldoc = response.responseXML;
	var root = xmldoc.getElementsByTagName('root')[0];

	var tbl = document.getElementById('doclist');
	var lastRow = tbl.rows.length;
	for(var i=0;i<lastRow;i++)
		tbl.deleteRow(0);
	for(var iNode = 0; iNode < root.childNodes.length; iNode++)
	{
		var node = root.childNodes.item(iNode);
		var docname = xmlGetChildNode(node, 'title').data;
		var doctype = xmlGetChildNode(node, 'type').data;
		var docid = xmlGetChildNode(node, 'id').data;
		addDocumentToList(docname, doctype, docid);
	}
	opendialog.style.left = 100;
	opendialog.style.top = 100;
	opendialog.style.visibility = 'visible';
}

