function SetupTableDnD()
{
    $('.outlinetbl').tableDnD({
        onDrop: function(table, row) {
        	s = $.tableDnD.serialize();
        	t = s.split('&')
        	ord = ''
        	for (i = 0 ; i < t.length; i++) {
        		tt = t[i].split('[]=');
        		catid = tt[0];
        		catid = catid.replace('cat_', '');
        		fldid = tt[1];
        		if (fldid == '') break
        		fldid = fldid.replace('field_', '');
        		ord += fldid + ','
        	}
        	url = 'worker_outlines.asp?action=arrange&catid='+catid+'&ord='+ord
        	//alert(url)
            $.getScript(url);
            ZebraTable(table);
        },
        dragHandle: "dragHandle"

    });
}

function SubmitKeyboard(textArea, form)
{
	o = form;
	while (o && o.tagName.toLowerCase() != "div" && o.id != "dialog")
	{
		o = o.parentNode;
	}
	if (o != null)
		SubmitEntry(o);
}

function SubmitEntry(what)
{
	id = $("#theid").val();
	a = $("#answer").val();
	if (a == "") {
		alert('You cannot submit nothing');
		return;
	}
	q = $("#customquestion").val();
	nq = $("#newquestion").val();
	outlineid = $("#outlineid").val();
	catid = $(what).dialog('option', 'setcatid')
	url = "worker_outlines.asp?action=editoutlineentrysubmit&id="+id+"&outlineid="+outlineid+"&catid="+catid;
	//alert(url)
	v = '#answer_'+id;
	$('#field_'+id).html('<td id=loading class="hdr" align=center colspan=5 style="padding:4px;"><b>Loading...</b></td>')

	$.ajax({
		type: "POST",
		dataType: "script",
		url: url,
		data: {
			answer: a,
			question: q,
			newquestion: nq
		}
	});

	//.load(url, {answer: a})
	SetupTableDnD();
	$(what).dialog('close');
}

$(document).ready(function() {
	$('.outlinetbl tr:nth-child(even)').addClass("er");
	$('.outlinetbl tr:nth-child(odd)').addClass("or");
	SetupTableDnD();
	HideLinks();
	var h = 275;
	var w = 575;
	opt = $.cookie("dialogoptions");
	if (opt != null) {
		h = parseInt(opt.split(",")[1]);
		w = parseInt(opt.split(",")[0]);
	}
	$("#dialog").dialog({
		autoOpen: false,
		height: h,
		width:w,
		modal: false,
		resizable: true,
		buttons: {
			'Submit': function() {
				SubmitEntry(this);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		open: function(event, ui) {
			opt = $.cookie("dialogoptions");
			if (opt != null) {
				opt = opt.split(',')
				//alert(parseInt(opt[0]))
				//alert(parseInt(opt[1]))
				//$("#dialog").width(parseInt(opt[0]))
				//$("#dialog").height(parseInt(opt[1]))
			}
			
			id = $(this).dialog('option', 'setid');
			fieldID = $(this).dialog('option', 'setfieldid');
			q = $(this).dialog('option', 'setquestion');
			$("#newquestion").val(q);
			if (q == '') q = 'Enter Custom Question here'
			if (id == 0 || fieldID == 0) {
				q = "<input id=customquestion name=customquestion class=\"input LargeText\" value=\""+q+"\" size=50>"
			}
			$("#question").html(q);
			$("#answer").val($(this).dialog('option', 'setanswer'));
			$("#answer").height($("#dialog").height()-$("#question").height()-22);
			//alert($("#dialog").width())
			$("#answer").width($("#dialog").width());
			$("#theid").val(id);
		},
		resize: function(event, ui) {
			$("#answer").height($("#dialog").height()-$("#question").height()-22);
			$("#answer").width($("#dialog").width());
		},
		resizeStop: function(event, ui) {
			//alert($.cookie("dialogoptions"))
			opt = [$("#dialog").width(), $("#dialog").height()]
			$.cookie("dialogoptions", opt, { expires: 365 });
		},
		dragStop: function(event, ui) {

		}
	});

});


function ZebraTable(table)
{
            $(table).find("tr:nth-child(even)").each(
			function(i) {
				 $(this).removeClass("or");
				 $(this).removeClass("er");
				 $(this).addClass("er");
			}
			);
           $(table).find("tr:nth-child(odd)").each(
			function(i) {
				 $(this).removeClass("or");
				 $(this).removeClass("er");
				 $(this).addClass("or");
			}
      );
    HideLinks();
}

function HideLinks()
{
    $(".action").hide();
    $(".orow").bind("mouseenter",function(){
      $(this).find(".action").show()
    }).bind("mouseleave",function(){
      $(this).find(".action").hide()
    });

}

function EditField(id, catID)
{
	url = 'worker_outlines.asp?action=editoutlineentry&id='+id+'&catid='+catID+'&outlineid='+$("#outlineid").val()
	//alert(url)
	$.getScript(url);
	return;
}

function DeleteField(id)
{
	if (!confirm('Are you sure you want to delete this entry?'))
		return;
	url = 'worker_outlines.asp?action=deleteoutlineentry&id='+id
	//alert(url)
	$.getScript(url);
}

function ToggleCatOpen(id)
{
	var element = document.getElementById("charcat"+id);
	var head = document.getElementById("charcatmore"+id);
	var isopen;

	if (element.style.display == 'none') {
		isopen = true;
		$(element).slideDown("fast")
		$(head).attr("src", "images/chevronup.png")
	} else {
		isopen = false;
		$(element).slideUp("fast")
		$(head).attr("src", "images/chevrondown.png")
	}
	var cook = RemoveFromMultipleCookie(getMultipleCookie('flags', 'cch'), ''+id, !isopen);
	setMultipleCookie("flags", "cch", cook+',');
}

function ShowFields(catid)
{
	url = "worker_outlines.asp?action=showfields&catid=" + catid;
	$.getScript(url);
}


function AddCustomField(catid)
{
	url = "worker_outlines.asp?action=addcustomfield&catid=" + catid
	$.getScript(url);
	EditField(0, catid);
}

var multCnt = 1;
function AddEntry(id)
{
	var v = document.getElementById('mult_new_'+id);
	var tbl = document.getElementById('mult_'+id);
	var i = 'mult '+id+' '+(-multCnt)
	var txt = '<input type=input class=input name="'+i+'" id="'+i+'" size=80 maxlength=255>'
	multCnt += 1;
	var r = tbl.insertRow(-1);
	var c = r.insertCell(0);
	c.innerHTML = txt;
	document.getElementById(i).focus();
	return false;
}

function EditQuestion()
{
	var fieldID = document.getElementById('theid').value;
	q = document.getElementById('question')
	if (q.children.length >= 1)
		return;
	var question = q.innerHTML;
	ret = prompt('Enter the question for this field', question);
	if (ret == null)
		return;
	q.innerHTML = ret;
	$("#newquestion").val(ret);
	//url = "worker_outlines.asp?action=changequestion&id="+id+"&question="+ret;
	//$.getScript(url);
}

function OpenCloseAllOutlineCat(openit, fields)
{
	var fldlist = fields.split(",");
	for (var i = 0; i < fldlist.length; i++) {
		if (fldlist[i] == '')
			continue;
		var element = document.getElementById("charcat"+fldlist[i]);
		var head = document.getElementById("charcatmore"+fldlist[i]);
		if (element == null || head == null)
			continue;
		if (openit) {
			isopen = true;
			$(element).slideDown("fast")
			$(head).attr("src", "images/chevronup.png")
		} else {
			isopen = false;
			$(element).slideUp("fast")
			$(head).attr("src", "images/chevrondown.png")
		}
		var cook = RemoveFromMultipleCookie(getMultipleCookie('flags', 'cch'), ''+fldlist[i], !openit);
		setMultipleCookie("flags", "cch", cook);
	}
}

function ShowAllFields(fields)
{
	var fldlist = fields.split(",");
	for (var i = 0; i < fldlist.length; i++) {
		url = "worker_outlines.asp?action=showfields&catid=" + fldlist[i] + '&ignoreerror=1';
		$.getScript(url);	
	}
}

function DeleteCategory(id)
{
	if (confirmSubmit('Are you sure you want to delete this category?\nYou will also delete all entries in this category!')) {
		Goto("tools.asp?page=workshop&action=deletecategoryfromoutline&id="+id);
	}
}

function MoveCategory(id, m)
{
	Goto("tools.asp?page=workshop&action=moveoutlinecategory&id="+id+"&m="+m);
}

function RenameCategory(id)
{
	var name = document.getElementById('catname_'+id).innerHTML
	var ret = prompt('Rename category', name);
	if (ret == null)
		return;
	
	var url = "tools.asp?page=workshop&action=renameoutlinecategory&id="+id+"&name="+ret;
	Goto(url);
}

function GetHighestZIndex()
{
	return 1;
}

function CategoryMenu(what, id)
{
	var t = document.getElementById('g_tt');
	if (t == null) return;
	var h = '<table class="colbdr or" cellspacing=0 width=120>';
	h += '<tr><td OnClick="MoveCategory('+id+', -1);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Move up</span></td></tr>'
	h += '<tr><td OnClick="MoveCategory('+id+', 1);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Move down</span></td></tr>'
	h += '<tr><td OnClick="MoveCategory('+id+', -10);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Move to top</span></td></tr>'
	h += '<tr><td OnClick="MoveCategory('+id+', 10);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Move to bottom</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="RenameCategory('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Rename Category</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="DeleteCategory('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Delete Category</span></td></tr>'
	h += '</table>';
	t.innerHTML = h
	SetTooltipPosition(what);
	t.style.display = "block";
	t.style.width = "220px";
	$(t).css('z-index', GetHighestZIndex()+10);

}

function DeleteOutline(id)
{
	if (confirmSubmit('Are you sure you want to delete this outline?')) {
		Goto('tools.asp?page=workshop&action=deleteoutline&id='+id);
	}
}

function DuplicateOutline(id)
{
	Goto('tools.asp?page=workshop&action=duplicateoutline&id='+id);
}

function EmailLink(id)
{
	Goto('tools.asp?page=workshop&action=mailcharacter&id='+id);
}

function AddToMyPage(id)
{
	Goto('tools.asp?page=workshop&action=addtomypage&id='+id);
}

function RemoveFromMyPage(id)
{
	Goto('tools.asp?page=workshop&action=removefrommypage&id='+id);
}

function AlwaysShowFields()
{
	Goto('tools.asp?page=workshop&action=alwaysshowfields');
}

function SetAccess(id, which)
{
	Goto('tools.asp?page=workshop&action=setaccess&id='+id+'&which='+which);
}

function AddCustomCategory(id)
{
	name = prompt('What do you want your new category to be called?');
	if (name == null)
		return;
	Goto('tools.asp?page=workshop&action=addcustomcategory&id='+id+'&name='+name);
}

function OutlineMenu(what, id, userLevel, funInfoID, access)
{
	var t = document.getElementById('g_tt');
	if (t == null) return;
	h = '<table class="colbdr or" cellspacing=0 width=170>';
	h += '<tr><td OnClick="AddCustomCategory('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Add Custom Category</span></td></tr>'
	h += '<tr><td OnClick="AlwaysShowFields();" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Toggle always show fields</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'

	if (access < 1)
		h += '<tr><td OnClick="SetAccess('+id+', 1);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Make Linkable</span></td></tr>'
	if (access < 2)
		h += '<tr><td OnClick="SetAccess('+id+', 2);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Make Public</span></td></tr>'
	if (access < 4)
		h += '<tr><td OnClick="SetAccess('+id+', 4);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Add to My Page</span></td></tr>'
	if (access >= 1)
		h += '<tr><td OnClick="SetAccess('+id+', 0);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Make Private</span></td></tr>'
	if (access >= 4)
		h += '<tr><td OnClick="SetAccess('+id+', 2);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Remove from My Page</span></td></tr>'
	h += '<tr><td OnClick="EmailLink('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Email Link</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="DuplicateOutline('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Duplicate Outline</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="DeleteOutline('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Delete Outline</span></td></tr>'

	if (userLevel >= 10) {
		
	}
	h += '</table>';
	t.innerHTML = h
	SetTooltipPosition(what);
	t.style.display = "block";
	t.style.width = "220px";

}
