function setValue (p_src, p_dest)
{
	if	(p_src.selectedIndex == -1)
		return;
	var value = p_src.options[p_src.selectedIndex].value;
	if	(value == '')
		return;
	var dest = document.getElementById (p_dest);
	if	(dest == null)
		return;
	dest.value = value;
}

function matchValue (p_src, p_menu)
{
	if	(p_src.value == '')
		return;
	var value = p_src.value.toUpperCase();
	var menu = document.getElementById (p_menu);
	if	(menu == null)
		return;
	var options = menu.options;
	var len = options.length;
	for (var i = 0; i < len; i++)
	{
		if	(value != options[i].value.toUpperCase())  continue;
		menu.selectedIndex = i;
		if	(menu.onchange)
		{
            try { menu.onchange() } catch(err) { };
        }
		break;
	}
}

var CW_SC_set_name = new Array();
function setName (p_to, p_from1, p_from2)
{

	var dest = document.getElementById (p_to);
	if	(dest == null)	return;
	if	(CW_SC_set_name[p_to] == null)
		CW_SC_set_name[p_to] = (dest.value == '')? 0: 1;
	if	(CW_SC_set_name[p_to] == 1)
		return;

	var f1 = document.getElementById (p_from1);
	if	((f1 == null)||(f1.value == ''))
		return;
	var f2 = document.getElementById (p_from2);
	if	((f2 == null)||(f2.value == ''))
		return;
	dest.value = f1.value + ' ' + f2.value;
}

function changedName (p_to)
{

	var dest = document.getElementById (p_to);
	if	(dest == null)	return;
	CW_SC_set_name[p_to] = (dest.value == '')? 0: 1;
}

var CW_SC_CPU_menus = new Array ('ma', 'c_manuf', 'f', 'c_family',
	'mo', 'c_model', 'p', 'c_part' );
var CW_SC_menu_style = 0;
var CW_SC_prev_ma = '';
var CW_SC_prev_f = '';

function getCPUMenus(p_sel, p_bench)
{

	var value;
	var obj;
	var	i;
	var	field;
	var par;
	var params = 's=' + ((p_sel == null)? '': escape(p_sel)) +
		'&ms=' + CW_SC_menu_style;
	for (i = 0; i < CW_SC_CPU_menus.length; i += 2)
	{
		par = CW_SC_CPU_menus[i];
		field = CW_SC_CPU_menus[i + 1] + '_opt';
		obj = document.getElementById(field);
		if	(obj == null)	continue;
		if	(obj.selectedIndex == -1)	continue;
		value = obj.options[obj.selectedIndex].value;
		if	(value == '')	continue;
		value = escape(value);
		params += '&' + par + '=' + value.replace(/\+/, '%2B');
	}
	var url = (p_bench)? '/cgi-bin/ajax/bench_parts.pl':
		'/cgi-bin/ajax/cpu_parts.pl';

	new Ajax.Request(url,
	{
		method: 'get',
		parameters: params,
		onSuccess: function(transport)
		{
			var text = transport.responseText || '';
			if (text != '')
			{
				var i;
				var j;
				var html_arr = text.split(/\~\~\~/);
				if	(html_arr[0] == 'DIV')
				{
					for (i = 0, j = 0; i < CW_SC_CPU_menus.length; i += 2, j++)
					{
						obj = document.getElementById('div_' + CW_SC_CPU_menus[i + 1]);
						obj.innerHTML = (html_arr[j + 1] == null)? '': ' ' + html_arr[j + 1] + ' ';
					}
				}
				// Set values for blank form fields if necessary
				else if (html_arr[0] == 'SET')
				{
					var kv;
					var js_arr = html_arr[1].split(/\s*\n\s*/);
					for (i = 0; i < js_arr.length; i++)
					{
						if	(!js_arr[i].match(/=/))  continue;
						kv = js_arr[i].split(/\s*=\s*/);
						if	(kv[1] == '')	continue;
						if	(kv[1] == undefined)	continue;
						obj = document.getElementById(kv[0]);
						if	((obj != null)&&(obj.value == ''))
						{
							obj.value = kv[1];
							if	(obj.onchange)
							{
					            try { obj.onchange() } catch(err) { };
					        }
						}
					}
				}
			}
		}
	});
}

function getBenchResults(p_par)
{

	var params = 'result=1';
	var	obj = document.getElementById ('c_family');
	if	(obj != null)
		params += '&f=' + obj.value.replace(/\+/, '%2B');
	obj = document.getElementById ('c_part');
	if	(obj != null)
		params += '&p=' + obj.value.replace(/\+/, '%2B');
	obj = document.getElementById ('c_part_ids');
	if	(obj != null)
		params += '&i=' + obj.value.replace(/\+/, '%2B');
	obj = document.getElementById ('c_test');
	if	(obj == null)
	{
		alert ("Internal error: select box 'c_test' not found!");
		return;
	}
	var i = obj.selectedIndex;
	if	(i == -1)
	{
		alert ("Please select benchmark");
		return;
	}

	if	(p_par)
	{
		i += p_par;
		if	(i < 0)		return;
		if	(obj.options[i] == null)	return;
		obj.selectedIndex = i;
	}

	//	Enable/disable buttons
	var but = document.getElementById ('BB_PREV');
	if	(but != null)
		but.disabled = (i == 0)? true: false;
	but = document.getElementById ('BB_NEXT');
	if	(but != null)
		but.disabled = (obj.options[i + 1] == null)? true: false;

	params += '&b=' + escape(obj.options[i].value);

	new Ajax.Request('/cgi-bin/ajax/bench_parts.pl',
	{
		method: 'get',
		parameters: params,
		onSuccess: function(transport)
		{
			var text = transport.responseText || '';
			if (text == '')	return;
			var obj = document.getElementById ('c_benchmarks');
			if	(obj == null)
				window.status ("Internal error: div tag 'c_benchmarks' not found");
			else
				obj.innerHTML = text;
			BarReload();
		}
	});
}

function	OnOff (p_id, p_text, p_text_on, p_text_off)
{
	var obj = document.getElementById (p_id);
	if	(obj == null)
		return false;
	var show_it = (obj.style.display == 'none')? 1: 0;
	obj.style.display = (show_it)? 'block': 'none';
	if	(p_text == null)
		return false;
	obj = document.getElementById (p_text);
	if	(obj != null)
		obj.innerHTML = (show_it)? p_text_on: p_text_off;

	return false;
}

function	AddToSelect (p_from, p_to, p_hidden)
{

	var from_obj = document.getElementById (p_from);
	if	(from_obj == null)	return;
	var sel = from_obj.selectedIndex;
	if	(sel == -1)  return;
	var from_options = from_obj.options;

	var	to_obj = document.getElementById(p_to);
	if	(to_obj == null)	return;
	var to_options = to_obj.options;

	var value;
	var text;
	for (var i = 0; i < from_obj.length; )
	{
		if	((!from_options[i].selected)||(from_options[i].value == ''))
		{
			i++;
			continue;
		}
		value = from_options[i].value;
		if	(value != '')
		{
			text = value.replace(/_/g, ' ');
			to_options[to_obj.length] = new Option (text, value, false, 0);
		}
		from_options[i] = null;
	}

	//	If the destination scrollbox contains option with blank value -
	//	delete it
	if	((to_obj.length > 1)&&(to_options[0].value == ''))
		to_options[0] = null;

	if	(sel >= from_obj.length)
		sel = from_obj.length - 1;
	from_obj.selectedIndex = sel;
	CopySelectToHidden(to_obj, p_hidden);
}

function	DeleteSelected (p_select, p_text, p_hidden)
{

	var obj = document.getElementById(p_select);
	if	(obj == null)	return;
	if	(obj.selectedIndex == -1)	return;
	var len = obj.length;
	var options = obj.options;
	for (var i = len - 1; i >= 0; i--)
	{
		if	(options[i].selected)
			options[i] = null;
	}
	if	((obj.length == 0)&&(p_text != null)&&(p_text != ''))
		obj.options[0] = new Option (p_text, '', false, 0);
	CopySelectToHidden(obj, p_hidden);
}

function	CopySelectToHidden (p_sel, p_hidden)
{

	if	((p_hidden == null)||(p_hidden == ''))	return;
	var hidden_obj = document.getElementById (p_hidden);
	if	(hidden_obj == null)	return;
	//	Update hidden field with a list of all choices
	var values = new Array ();
	var len = p_sel.length;
	var options = p_sel.options;
	for (var i = 0; i < len; i++)
	{
		if	(options[i].value != '')
			values.push (options[i].value);
	}
	hidden_obj.value = values.join('|');
}
