function Splash_OnContent(data,urn)
{
	SetContent('mDs_splash',data);	
}

function LaunchPreview()
{
	if (FindComp("mDs_splash")) return;
	wnd_n = CreateWindowA(
			"mDs",
			'splash',
			"Welcome to AEWindow Alpha Preview",
			"theme_default",
			0, 0,
			330, 340,
			WMF_MOVEABLE | WMF_CENTERED,
			wSTR,
			0,0,0,0,
			100,
			"dialog_border"
	);
	new doRequest("wnd/splash.html",Splash_OnContent,null,'json');
}

function Splash_ButtonContinue_Click()
{
	CloseWindow("mDs","mDs_splash");
	MainMenu_OnCreate();
}

function MainMenu_OnContent(data,urn)
{
	SetContent('mDs_alphapreview',data);	
}

function MainMenu_OnCreate()
{
	wnd_n = CreateWindowA(
			"mDs",
			'alphapreview',
			"AEWindow Alpha Preview",
			"theme_default",
			0, 0,
			440, 340,
			WMF_MOVEABLE | WMF_CENTERED | WMF_CLOSE,
			wSTR,
			0,0,0,0,
			100,
			"dialog_border"
	);
	new doRequest("wnd/main.html",MainMenu_OnContent,null,'json');
}

var empty_id = 0;
function Main_CreateEmptyWindow()
{
	wnd_n = CreateWindowA(
			"mDs",
			'empty' + empty_id,
			"Empty Window",
			"theme_default",
			-1, -1,
			440, 340,
			WMF_MOVEABLE | WMF_CLOSE | WMF_RESIZEABLE,
			'empty window test',
			0,0,0,0,
			100,
			"border"
	);	
	empty_id = empty_id+1;
}

function CoolYes()
{
	DisplayMessage('you need to see what comes next ;)',0,0);	
}

function CoolNo()
{
	DisplayMessage('ahhhh :(',0,0);	
}

function ReportWindow_OnContent(data,urn)
{
	SetContent('mDs_report',data);	
}

function CreateReportWindow()
{
	if (FindComp("mDs_report")) return;
	wnd_n = CreateWindowA(
			"mDs",
			'report',
			"Viewing report #1",
			"theme_default",
			0, 0,
			240, 400,
			WMF_MOVEABLE | WMF_CENTERED | WMF_CLOSE,
			wSTR,
			0,0,0,0,
			100,
			"border"
	);
	new doRequest("wnd/report.html",ReportWindow_OnContent,null,'json');
}

var current_datarow = null;
function DataWindow_OnData(data,urn)
{
	current_datarow = null;
	obj = ParseJSON(data);
	tbl = FindComp("sampledata_dataview");
	while (tbl.rows.length > 0) tbl.deleteRow(tbl.rows.length - 1);
	
	var newRow = tbl.insertRow(tbl.rows.length);
	var newCell = newRow.insertCell(0);
	newCell.width = "100";
	newCell.innerHTML = "Name";
	var newCell2 = newRow.insertCell(1);
	newCell2.innerHTML = "Comment";
	newRow.style.backgroundColor = '#EEEEEE'
	newCell.style.borderTop = '1px solid #CCCCCC';
	newCell2.style.borderTop = '1px solid #CCCCCC';
	newCell.style.borderBottom = '1px solid #CCCCCC';
	newCell2.style.borderBottom = '1px solid #CCCCCC';
		
	for (var i=0;i<obj.length;i++)
	{
		var newRow = tbl.insertRow(tbl.rows.length);
		var newCell = newRow.insertCell(0);
		newCell.width = "100";
		newCell.height = 25;
		newCell.innerHTML = obj[i].name;
		var newCell2 = newRow.insertCell(1);
		newCell2.innerHTML = obj[i].comment;
		newRow.onclick = new Function("if (current_datarow != null){current_datarow.style.backgroundColor='#FFFFFF';current_datarow.style.color='#000000';}this.style.backgroundColor='#006AA1';this.style.color='#FFFFFF';current_datarow=this;");
		newRow.onmouseover = new Function("if (this != current_datarow) { this.style.backgroundColor='#FFFF99'; }");
		newRow.onmouseout = new Function("if (this != current_datarow) { this.style.backgroundColor='#FFFFFF'; }");
	
		newCell.style.borderBottom = '1px solid #CCCCCC';
		newCell2.style.paddingLeft = '5px';
		newCell2.style.borderLeft = '1px dashed #CCCCCC';		
		newCell2.style.borderBottom = '1px solid #CCCCCC';		
	}
	/*
	var newRow = tbl.insertRow(tbl.rows.length);
	var newCell = newRow.insertCell(0);
	newCell.width = "100";
	newCell.innerHTML = col0;
	var newCell2 = newRow.insertCell(1);
	newCell2.innerHTML = col1;				
	*/
}

function SampleData_ActionCB(data,urn)
{
	obj = ParseJSON(data);
	if (obj.msg == "OK")
	{
		DataWindow_GetData();
	}
}

function SampleData_Delete()
{
	if (current_datarow == null) return;
	
	name=encodeURIComponent(current_datarow.childNodes[0].innerHTML);
	comment=encodeURIComponent(current_datarow.childNodes[1].innerHTML);
	new doRequest("/sampledata/?name=" + name + "&comment=" + comment,SampleData_ActionCB,null,'json',document.body,1,0,"DELETE",null);	
}

function SampleData_Post(name,comment,newname,newcomment)
{
	new doRequest("/sampledata/",SampleData_ActionCB,null,'json',document.body,1,0,"POST","name=" + encodeURIComponent(name) + "&comment=" + encodeURIComponent(comment) + "&newname=" + encodeURIComponent(newname) + "&newcomment=" + encodeURIComponent(newcomment) );
}

function SampleData_Put(name,comment)
{
	new doRequest("/sampledata/?name=" + name + "&comment=" + comment,SampleData_ActionCB,null,'json',document.body,1,0,"PUT","name=" + encodeURIComponent(name) + "&comment=" + encodeURIComponent(comment) );
}

function SampleData_ConfirmDelete()
{
	if (current_datarow == null)
	{
		DisplayMessage("Select a record first!");
		return;
	}
	DisplayMessageQuestion('Delete Record?',0,'SampleData_Delete()',null);	
}

function DataWindow_GetData(query)
{
	if (query == null)
	{
		new doRequest("/sampledata/",DataWindow_OnData,null,'json');
	}
	else
	{
		new doRequest("/sampledata/?q=" + encodeURIComponent(query),DataWindow_OnData,null,'json');
	}
}

function DataWindow_OnContent(data,urn)
{
	SetContent('mDs_data',data);	
	InitInput('search',null);
	DataWindow_GetData();	
}

function CreateDataWindow()
{
	if (FindComp("mDs_data")) return;
	wnd_n = CreateWindowA(
			"mDs",
			'data',
			"Sample Data Window",
			"theme_default",
			0, 0,
			460, 400,
			WMF_MOVEABLE | WMF_CENTERED | WMF_CLOSE,
			wSTR,
			0,0,0,0,
			100,
			"border"
	);
	new doRequest("wnd/data.html",DataWindow_OnContent,null,'json');
}

var sampledatanewrecordid=0;
function SampleData_NewRecordWindow()
{
	wnd_n = CreateWindowA(
			"mDs",
			'insert' + sampledatanewrecordid,
			"Insert New Record",
			"theme_default",
			0, 0,
			300, 150,
			WMF_MOVEABLE | WMF_CENTERED | WMF_CLOSE,
			'<div style="overflow:visible" onMouseDown="wnd_ignore_nextcmd=1;"><form name="fi' + sampledatanewrecordid + '" onSubmit="return false">Name: <input type="text" name="name" value="" class="input" style="width:200px" onmousedown="wnd_ignore_nextcmd=1;"><br><br>Comment: <input type="text" name="comment" value="" class="input" style="width:200px"><br><br><div style="text-align:right"><a href="javascript:nop()" class="button" onclick="SampleData_Put(document.fi' + sampledatanewrecordid + '.name.value, document.fi' + sampledatanewrecordid + '.comment.value);CloseWindow(\'mDs\',\'mDs_insert' + sampledatanewrecordid + '\');"><span>Insert</span></a></div></form></div>',
			0,0,0,0,
			100,
			"border"
	);
}

var sampledataeditrecordid=0;
function SampleData_EditRecordWindow()
{
	if (current_datarow == null)
	{
		DisplayMessage("Select a record first!");
		return;
	}
	name=current_datarow.childNodes[0].innerHTML;
	comment=current_datarow.childNodes[1].innerHTML;
	
	wnd_n = CreateWindowA(
			"mDs",
			'edit' + sampledataeditrecordid,
			"Edit Record",
			"theme_default",
			0, 0,
			300, 150,
			WMF_MOVEABLE | WMF_CENTERED | WMF_CLOSE,
			'<div style="overflow:visible" onMouseDown="wnd_ignore_nextcmd=1;"><form name="fe' + sampledataeditrecordid + '" onSubmit="return false">Name: <input type="text" name="newname" value="' + name + '" class="input" style="width:200px" onmousedown="wnd_ignore_nextcmd=1;"><br><br>Comment: <input type="text" name="newcomment" value="' + comment + '" class="input" style="width:200px"><br><br><div style="text-align:right"><a href="javascript:nop()" class="button" onclick="SampleData_Post(\'' + name + '\', \'' + comment + '\', document.fe' + sampledataeditrecordid + '.newname.value, document.fe' + sampledataeditrecordid + '.newcomment.value);CloseWindow(\'mDs\',\'mDs_edit' + sampledataeditrecordid + '\');"><span>Save</span></a></div></form></div>',
			0,0,0,0,
			100,
			"border"
	);
}

function ContentUpdate(data,urn)
{
		obj = ParseJSON(data);
		new doRequest("/wk" + obj.window, function(data,urn) { SetContent('mDs_/wk'+obj.window,'<div style="width:100%;height:100%;overflow:auto" onMouseDown="wnd_ignore_nextcmd=1;">' + data + '</div>');},null,'json');
}

function PostContent(urn,frm)
{
	objfrm = FindComp(frm);	
	new doRequest(urn,ContentUpdate,null,'json',document.body,1,0,"POST","wikicontent=" + encodeURIComponent(objfrm.wikicontent.value) );
	DisplayMessage('Page saved!',0,0);
	//CloseWindow("mDs","mDs_" + urn);
}

function CreateContent_Window(name,title)
{
	if (FindComp("mDs_" + name)) return;
	wnd_n = CreateWindowA(
			"mDs",
			name,
			title,
			"theme_default",
			-1, -1,
			460, 400,
			WMF_MOVEABLE | WMF_CLOSE | WMF_CENTERED | WMF_RESIZEABLE,
			wSTR,
			0,0,0,0,
			100,
			"dialog_border"
	);
	

	new doRequest(name, function(data,urn) { SetContent('mDs_'+name,'<div style="width:100%;height:100%;overflow:auto" onMouseDown="wnd_ignore_nextcmd=1;">' + data + '</div>');},null,'json');
}

function NewPage_Window()
{
	wnd_n = CreateWindowA(
			"mDs",
			"insertpage",
			"Enter Page Name (No spaces)",
			"theme_default",
			-1, -1,
			300, 70,
			WMF_MOVEABLE | WMF_CLOSE | WMF_CENTERED | WMF_ONTOP,
			'<form name="insertpage" onSubmit="return false"><input name="pagename" value="" style="width:222px" class="input" onmousedown="wnd_ignore_nextcmd=1;">&nbsp;<a href="javascript:nop()" onclick="CreateContent_Window(\'/wk/\' + document.insertpage.pagename.value + \'/edit\',\'Editing: /\' + document.insertpage.pagename.value + \'\');CloseWindow(\'mDs\',\'mDs_insertpage\')" class="button"><span>OK</span></a></form>',
			0,0,0,0,
			100,
			"dialog_border"
	);
	document.insertpage.pagename.focus();
}