String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

function openMenu(menuID,linkObj)
{
	//if the menu code is ready
	if(um.ready)
	{
		//find co-ordinates of link object
		var coords = {
			'x' : um.getRealPosition(linkObj,'x'),
			'y' : um.getRealPosition(linkObj,'y')
			};

		//increase y-position to place it below the link
		coords.y += (linkObj.offsetHeight + 4);

		//activate menu at returned co-ordinates
		um.activateMenu(menuID, coords.x + 'px', coords.y + 'px');
	}
}

function closeMenu(menuID)
{
	//if the menu code is ready
	if(um.ready)
	{
		//deactive menu
		um.deactivateMenu(menuID);
	}
}

function open_window(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function ws(width,height) {
	document.write('<img src="/images/spacer.gif" alt="" border="0" ' +
				   'height="'+height+'" width="'+width+'" />');
}

function permission_changed(value) {
	if (value == 0 || value == 1 || value == 6) {
		document.getElementsByName('user1[certified(1i)]')[0].disabled = true;
		document.getElementsByName('user1[certified(2i)]')[0].disabled = true;
		document.getElementsByName('user1[certified(3i)]')[0].disabled = true;
	} else {
		document.getElementsByName('user1[certified(1i)]')[0].disabled = false;
		document.getElementsByName('user1[certified(2i)]')[0].disabled = false;
		document.getElementsByName('user1[certified(3i)]')[0].disabled = false;
	}
}

function toggle_other_occupation(text_field_id, check_box_id) {
	$(text_field_id).disabled = !$(check_box_id).checked;
}

function toggle_product_search_row(product_id) {
	product_selected = $("products[" + product_id + "]").checked;
	$("product_discount_codes[" + product_id + "]").disabled = !product_selected;
	$("product_statuses[" + product_id + "]").disabled = !product_selected;
	
	document.getElementsByName("product_dates[" + product_id + "][year]")[0].disabled = !product_selected;
	document.getElementsByName("product_dates[" + product_id + "][month]")[0].disabled = !product_selected;
	document.getElementsByName("product_dates[" + product_id + "][day]")[0].disabled = !product_selected;
	
	document.getElementsByName("product_dates_end[" + product_id + "][year]")[0].disabled = !product_selected;
	document.getElementsByName("product_dates_end[" + product_id + "][month]")[0].disabled = !product_selected;
	document.getElementsByName("product_dates_end[" + product_id + "][day]")[0].disabled = !product_selected;
}

function toggle_product_purchase_dates() {
	c = $("no_purchase_date").checked;
	document.getElementsByName("purchase_date[year]")[0].disabled = c;
	document.getElementsByName("purchase_date[month]")[0].disabled = c;
	document.getElementsByName("purchase_date[day]")[0].disabled = c;
	
	document.getElementsByName("purchase_date_end[year]")[0].disabled = c;
	document.getElementsByName("purchase_date_end[month]")[0].disabled = c;
	document.getElementsByName("purchase_date_end[day]")[0].disabled = c;
}

function toggle_all_products(checked) {
	products = document.getElementsByName("products[]");
	for (i=0; i<products.length; i++) {
		products[i].checked = checked;
		toggle_product_search_row(products[i].value);
	}
}

function product_selected() {
	show_indicator();
	$('purchase_product_id').disabled = true;
	$('purchase_shipping_id').disabled = true;
}

function toggle_enable(id) {
	$(id).disabled = !$(id).disabled;
}

function toggle_section(id) {
	Element.toggle(id);
	toggle_arrow(id);
	toggle_form_field(id);
}

function toggle_arrow(id) {
	$(id + '_icon').src = Element.visible(id) ?
		'/images/admin/arrow_blue_down.png' :
		'/images/admin/arrow_blue_right.png';
}

function toggle_form_field(id) {
	if ($(id + '_status')) {
		$(id + '_status').value = Element.visible(id) ? 'open' : 'closed'
	}
}

function switch_page(page) {
	$('search_form').page.value = page;
	$('search_form').submit();
}

function export_search_results() {
	$('search_form').action = 'export';
	$('search_form').submit();
}

function show_indicator() {
	Element.show('indicator');
}

function hide_indicator() {
	Element.hide('indicator');
}

function get_exam_id(exam_id_override) {
	if (typeof(exam_id_override) != 'undefined') return exam_id_override;
	return $('exam_id') ? $('exam_id').value : '';
}

function get_format_id(format_id_override) {
	if (typeof(format_id_override) != 'undefined') return format_id_override;
	return $('format_id') ? $('format_id').value : '';
}

function show_terms_of_use() {
	open_window('/professional/terms_of_use_popup','terms_of_use','resizable=no,scrollbars=yes,status=no,width=550,height=450');
}

function show_code_of_ethics() {
	open_window('/professional/ethics_code_popup','ethics_code','resizable=no,scrollbars=yes,status=no,width=550,height=450');
}

function repopulate_select(id, items, selected_item) {
	$(id).options.length = 0;
	items.each(function(item, index) {
		$(id).options[index] = new Option(item[0], item[1]);
		if (item[1] == selected_item) $(id).selectedIndex = index;
	});
}

function dialog_loading() {
	Element.hide('error');
	Element.show('indicator_bar');
}

function disable_if_exists(id) {
	if ($(id)) $(id).disabled = true;
}

function enable_if_exists(id) {
	if ($(id)) $(id).disabled = false;	
}

function reset_select_if_exists(id) {
	$(id).selectedIndex = 0;
}
