// stuff that gets generated in another javascript file:
// parameter_list[]
// parameter_names[]
// lists for all list and listorstring types
// select_param()


var rowsmade = 0;

function make_row()
{
	var table = document.getElementById("formtable");
	table.insertRow(table.rows.length-2);
	
	var row = table.rows[table.rows.length-3];
	row.id = "formrow" + rowsmade;
	
	row.insertCell(0);
	if (rowsmade != 0)
		row.cells[0].innerHTML = '<a href="javascript:void(0)" alt="Remove Row" onclick="remove_row(' + rowsmade + ')">remove</a>';
	row.insertCell(1);
	row.cells[1].innerHTML = get_parameter_list(rowsmade);
	
	rowsmade++;
}

function get_parameter_list(index)
{
	var retval = '<select id="parameter' + index + '" name="parameter' + index + '" onchange="select_param(' + index + ')">';
	retval += '<option></option>';
	for (var i = 0; i<parameter_list.length; i++)
		retval += '<option value="' + parameter_list[i] + '">' + parameter_names[i] + '</option>';
	retval += '</select>';
	return retval;
}

function get_numeric_comparator_list(index, callback)
{
	return '<select id="comparator' + index + '" name="comparator' + index + '" onchange="' + callback + '(' + index + ')">'
		+ '<option></option>'
		+ '<option value="equal">Equals</option>'
		+ '<option value="notequal">Does Not Equal</option>'
		+ '<option value="greaterthan">Greater Than</option>'
		+ '<option value="lessthan">Less Than</option>'
		+ '<option value="greaterthanequal">Greater Than or Equal</option>'
		+ '<option value="lessthanequal">Less Than or Equal</option>'
		+ '<option value="between">Between</option>'
		+ '</select>';
}

function get_boolean_list(index)
{
	return '<select id="value' + index + '" name="value' + index + '">'
		+ '<option></option>'
		+ '<option value="true">Yes</option>'
		+ '<option value="false">No</option>'
		+ '</select>';
}

function get_list_list(index, array)
{
	var retval = '<select id="value' + index + '" name="value' + index + '">';
	retval += '<option></option>';
	for (var i=0; i<array.length; i++)
		retval += '<option>' + array[i] + '</option>';
	retval += '</select>';
	return retval;
}

function get_tier_list(index)
{
	return '<select id="tiervalue' + index + '" name="tiervalue' + index + '">'
		+ '<option></option>'
		+ '<option value="1">Tier 1 (Warmup)</option>'
		+ '<option value="2">Tier 2 (Apprentice)</option>'
		+ '<option value="3">Tier 3 (Solid)</option>'
		+ '<option value="4">Tier 4 (Moderate)</option>'
		+ '<option value="5">Tier 5 (Challenging)</option>'
		+ '<option value="6">Tier 6 (Nightmare)</option>'
		+ '<option value="7">Tier 7 (Impossible)</option>'
		+ '</select>';
}

function get_timesignature_list(index)
{
	var retval = '<select id="timesigvalue' + index + '" name="timesigvalue' + index + '">';
	retval += '<option></option>'
	for (var i=0; i<timesignature_list.length; i++)
		retval += '<option>' + timesignature_list[i] + '</option>';
	retval += '</select>';
	return retval;
}

function remove_row(index)
{
	var table = document.getElementById("formtable");
	for (var i=0; i<table.rows.length; i++)
		if (table.rows[i].id == "formrow" + index)
		{
			table.deleteRow(i);
			return;
		}
}

function get_list_comparator_list(index)
{
	return '<select id="comparator' + index + '" name="comparator' + index + '">'
		+ '<option></option>'
		+ '<option value="equal">Equals</option>'
		+ '<option value="notequal">Does Not Equal</option>'
		+ '</select>';
}

function get_listorstring_comparator_list(index, array)
{
	return '<select id="comparator' + index + '" name="comparator' + index + '" onchange="select_listorstring_comparator(' + index + ',' + array + ')">'
		+ '<option></option>'
		+ '<option value="equal">Equals</option>'
		+ '<option value="notequal">Does Not Equal</option>'
		+ '<option value="contains">Contains</option>'
		+ '<option value="notcontains">Does Not Contain</option>'
		+ '<option value="startswith">Starts With</option>'
		+ '<option value="endswith">Ends With</option>'
		+ '</select>';
}

function get_timesignature_comparator_list(index)
{
	return '<select id="comparator' + index + '" name="comparator' + index + '">'
		+ '<option></option>'
		+ '<option value="only">Only</option>'
		+ '<option value="notonly">Not Only</option>'
		+ '<option value="contains">Contains</option>'
		+ '<option value="notcontains">Does Not Contain</option>'
		+ '</select>';
}

function trim_row(tablerow, length)
{
	while (tablerow.cells.length > length)
		tablerow.deleteCell(length);
}

function add_and(tablerow)
{
	tablerow.insertCell(tablerow.cells.length);
	tablerow.cells[tablerow.cells.length-1].innerHTML = '<a href="javascript:void(0)" onclick="make_row()">add row</a>';
}

function select_numeric_comparator(index)
{
	var selectbox = document.getElementById("comparator" + index);
	var value = selectbox.options[selectbox.selectedIndex].value;
	var tablerow = document.getElementById("formrow" + index);
	trim_row(tablerow, 3);
	if (value != "")
	{
		tablerow.insertCell(3);
		if (value == "between")
			tablerow.cells[3].innerHTML = '<input type="text" id="betweenlower' + index + '" name="betweenlower' + index + '" /> and <input type="text" id="betweenhigher' + index + '" name="betweenhigher' + index + '" />';
		else
			tablerow.cells[3].innerHTML = '<input type="text" id="const' + index + '" name="const' + index + '" />';
		add_and(tablerow);
	}
}

function select_listorstring_comparator(index, array)
{
	var selectbox = document.getElementById("comparator" + index);
	var value = selectbox.options[selectbox.selectedIndex].value;
	var tablerow = document.getElementById("formrow" + index);
	trim_row(tablerow, 3);
	if (value != "")
	{
		tablerow.insertCell(3);
		if (value == "equal" || value == "notequal")
			tablerow.cells[3].innerHTML = get_list_list(index, array);
		else
			tablerow.cells[3].innerHTML = '<input type="text" id="str' + index + '" name="str' + index + '" />';
		add_and(tablerow);
	}
}

function select_tier_comparator(index)
{
	var selectbox = document.getElementById("comparator" + index);
	var value = selectbox.options[selectbox.selectedIndex].value;
	var tablerow = document.getElementById("formrow" + index);
	trim_row(tablerow, 3);
	if (value != "")
	{
		tablerow.insertCell(3);
		if (value == "equal" || value == "notequal")
			tablerow.cells[3].innerHTML = get_tier_list(index);
		else if (value == "between")
			tablerow.cells[3].innerHTML = '<input type="text" name="betweenlower' + index + '" /> and <input type="text" name="betweenhigher' + index + '" />';
		else
			tablerow.cells[3].innerHTML = '<input type="text" name="const' + index + '" />';
		add_and(tablerow);
	}
}

function populate_number(index)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].innerHTML = get_numeric_comparator_list(index, "select_numeric_comparator");
}

function populate_boolean(index)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].colSpan = 2;
	tablerow.cells[2].innerHTML = "= " + get_boolean_list(index);
	add_and(tablerow);
}

function populate_list(index, array)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].innerHTML = get_list_comparator_list(index);
	tablerow.insertCell(3);
	tablerow.cells[3].innerHTML = get_list_list(index, array);
	add_and(tablerow);
}

function populate_tier(index)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].innerHTML = get_numeric_comparator_list(index, "select_tier_comparator");
}

function populate_listorstring(index, array)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].innerHTML = get_listorstring_comparator_list(index, array);
}

function populate_timesignature(index)
{
	var tablerow = document.getElementById("formrow" + index);
	tablerow.insertCell(2);
	tablerow.cells[2].innerHTML = get_timesignature_comparator_list(index);
	tablerow.insertCell(3);
	tablerow.cells[3].innerHTML = get_timesignature_list(index);
	add_and(tablerow);
}

function populate_orderby()
{
	var orderBySelect = document.getElementById("orderby");
	if (orderBySelect.length == 0)
		for (var i = 0; i<parameter_list.length; i++)
		{
			var option = document.createElement('option');
			option.text = parameter_names[i];
			option.value = parameter_list[i];
			try
			{
				orderBySelect.add(option, null);
			}
			catch (ex)
			{
				orderBySelect.add(option);
			}
		}
}

function load()
{
	populate_orderby();
	if (rowsmade == 0)
		make_row();
}

function select_combo(value, id)
{
	var comboBox = document.getElementById(id);
	for (var i=0; i<comboBox.length; i++)
	{
		if (comboBox.options[i].value == value || (comboBox.options[i].value == "" && comboBox.options[i].text == value))
		{
			comboBox.selectedIndex = i;
			return;
		}
	}
}

