function move_row_up(rownum)
{
	// make sure we're at least in the second data row
	if (rownum < 3)
	{
		return;
	}
	theTable = document.getElementById('selector_target');
	
	// store values of this row
	currentCellLeft = theTable.rows[rownum].cells[0].innerHTML;
	currentCellRight = theTable.rows[rownum].cells[1].innerHTML;
	
	// copy the row above into this row
	theTable.rows[rownum].cells[0].innerHTML = theTable.rows[rownum - 2].cells[0].innerHTML;
	theTable.rows[rownum].cells[1].innerHTML = theTable.rows[rownum - 2].cells[1].innerHTML;
	
	// copy stored values into the row above
	theTable.rows[rownum - 2].cells[0].innerHTML = currentCellLeft;
	theTable.rows[rownum - 2].cells[1].innerHTML = currentCellRight;
}

function move_row_down(rownum)
{
	// make sure we're at least in the second-from-last data row
	theTable = document.getElementById('selector_target');
	if (rownum >= theTable.rows.length - 2)
	{
		return;
	}
	
	// store values of this row
	currentCellLeft = theTable.rows[rownum].cells[0].innerHTML;
	currentCellRight = theTable.rows[rownum].cells[1].innerHTML;
	
	// copy the row above into this row
	theTable.rows[rownum].cells[0].innerHTML = theTable.rows[rownum + 2].cells[0].innerHTML;
	theTable.rows[rownum].cells[1].innerHTML = theTable.rows[rownum + 2].cells[1].innerHTML;
	
	// copy stored values into the row above
	theTable.rows[rownum + 2].cells[0].innerHTML = currentCellLeft;
	theTable.rows[rownum + 2].cells[1].innerHTML = currentCellRight;
}

function add_row(rownum)
{
	rowname = 'selector_label_' + rownum;
	theContent = document.getElementById(rowname);
	theTable = document.getElementById('selector_target');
	newRow = theTable.insertRow(-1);
	leftCell = newRow.insertCell(0);
	rightCell = newRow.insertCell(1);
	leftCell.className = 'tableListItem';
	leftCell.innerHTML = theContent.innerHTML;
	rightCell.className = 'tableListItem';
	rightCell.style.textAlign = 'right';
	rightCell.innerHTML =
		"<input type='submit' name='del' value='Delete' onclick='delete_row(this.parentNode.parentNode.rowIndex);'> <input type='button' name='up' value='&uarr;' onclick='move_row_up(this.parentNode.parentNode.rowIndex);'> <input type='button' name='down' value='&darr;' onclick='move_row_down(this.parentNode.parentNode.rowIndex);'>";
	newRow2 = theTable.insertRow(-1);
	ruleCell = newRow2.insertCell(0);
	ruleCell.setAttribute('colspan', 2);
	ruleCell.className = 'cellDotted';
	ruleCell.innerHTML = "<img class='imgRuleSmallDotted' src='i/blank.png?12011010' alt=' ' border='0' />";
}

function delete_row(rownum)
{
	document.getElementById('selector_target').deleteRow(rownum);
	document.getElementById('selector_target').deleteRow(rownum);
}

function renew_table(theTag)
{
	//theForm = document.forms.filterform;
	theForm = document.getElementById('sel_form_' + theTag);
	theValues = theForm.elements;
	url = "newsletter-selector-helper.html?table=" + theTag + "&";
	for (var i = 0; i < theValues.length; i++)
	{
		url += '&' + theValues[i].name + '=' + theValues[i].value;
	}
	//alert(url);
	xmlhttp.open("GET", url, true);
	document.getElementById('sel_x_results').innerHTML = "<p>Searching...</p>";
	
	// when data is available, update the div
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			document.getElementById('sel_x_results').innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.setRequestHeader('Accept', 'message/x-sitepilot-rpc');
	xmlhttp.send(null);
	
	// make sure the table div is showing
	if (selectedForm != '')
	{
		oldStyle = document.getElementById('sel_' + selectedForm).style;
		oldStyle.display = 'none';
		selectedForm = 'x_results';
		newStyle = document.getElementById('sel_' + selectedForm).style;
		newStyle.display = 'block';
		
		// show the "reset form" button
		resetFormButtonStyle = document.getElementById('reset_form').style;
		resetFormButtonStyle.display = 'inline';		
	}
	
	return false;
}

function reset_form()
{
	switch_form(document.getElementById('which_table').value);
}

var changed = false;
function switch_form(whichForm)
{
	if (selectedForm != '')
	{
		oldStyle = document.getElementById('sel_' + selectedForm).style;
		oldStyle.display = 'none';
	}
	newFormName = 'sel_' + whichForm;
	//alert(newFormName);
	newStyle = document.getElementById(newFormName).style;
	newStyle.display = 'block';
	selectedForm = whichForm;
	
	// show the "reset form" button
	//resetFormButtonStyle = document.getElementById('reset_form').style;
	//resetFormButtonStyle.display = 'none';
	
	if(!changed)
		document.category_form.which_table.options[0] = null;
		
	changed = true;
}

