function find_parent(e, tag) {
	while (e.tagName.toLowerCase() != tag.toLowerCase()) {
		e = e.parentNode;
	}
	return e;
}

function find_child(e, tag) {
	return;
	if (e.nodeType==1) {
		if (e.tagName.toLowerCase() == tag.toLowerCase()) {
			return e;
		}
		else {
			if (e.childNodes && e.childNodes.length > 0)
			for (i=0; i<e.childNodes.length; i++) {
				return find_child(e.childNodes[i], tag);
			}
		}
	}
}


function playlist_order(e) {
	try {
		var tbody	= find_parent(e, "tbody");
		var trow	= find_parent(e, "tr");
		var tros	= tbody.rows[0].rowIndex;
		
		if (e.value < tbody.rows.length-1) {
			if (trow.rowIndex - tros <= e.value) {
				tbody.appendChild(trow);
			}
			tbody.insertBefore(trow, tbody.rows[e.value]);
		}
		else {
			tbody.appendChild(trow);
		}
		
		for (i=0; i<tbody.rows.length; i++) {
			var e = tbody.rows[i].getElementsByTagName("select")[0];
			e.selectedIndex = i;
		}
	} catch (e) {}
}

function playlist_remove(e) {
	try {
		var tbody	= find_parent(e, "tbody");
		var trow	= find_parent(e, "tr");
		tbody.removeChild(trow);
		
		for (i=0; i<tbody.rows.length; i++) {
			var e = tbody.rows[i].getElementsByTagName("select")[0];
			e.selectedIndex = i;
			e.remove(e.options.length-1);
		}
	} catch (e) {}
}

function playlist_clear() {
	var tbody	= find_child(document.getElementById('store_data'), "tbody");
	tbody.innerHTML = '';
	document.playlist_form.submit();
}

function playlist_add() {
	document.playlist_form.action = "playlist.php?c=add";
	playlist_submit();
}

function playlist_play() {
	document.playlist_form.action = "playlist.php?c=play";
	playlist_submit();
}

function playlist_gethtml() {
	document.playlist_form.action = "playlist.php?c=gethtml";
	playlist_submit();
}

function playlist_store() {
	document.playlist_form.action = "playlist.php?c=prestore";
	playlist_submit();
}

function playlist_submit() {
	document.playlist_form.submit();
}