// ==UserScript==
// @name           pipsv2
// @namespace      http://djce.org.uk/greasemonkey
// @description    Improvements to the PIPSv2 UI
// @include        http://nm-pips.bbc.redbeemedia.net/cgi-bin/pips/pipspublish/republish.pl/*
// ==/UserScript==

// On the "republish" page, sort the brand list by brand title
{
	var x = document.evaluate(
		'//select',
	       document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
	);

	var sorter = function(a,b) {
		return(a[1]<b[1] ? -1 : a[1]>b[1] ? +1 : 0);
	};

	var sort_select = function(s) {
		var arr = [];

		while (s.options.length > 1)
		{
			var opt = s.options[1];
			s.removeChild(opt);
			arr.push([opt, opt.text]);
		}

		arr.sort(sorter);

		for (var i=0; i<arr.length; ++i)
		{
			s.appendChild(arr[i][0]);
		}
	};

	for (var i=0; i<x.snapshotLength; ++i)
	{
		var e = x.snapshotItem(i);
		sort_select(e);
	}
}

