
/*

	BBC History - Your Photos

	Javascript routines

	Ben Roberts
	ben@headsnet.com

	August 2006

*/

/* ~~~~~~~~~~~~~ Uploader submission form ~~~~~~~~~~~~~~~~ */

// FUNCTION: To setup the category select lists on page load,
// disabling them as appropriate
function initCategoryLists ()
{
	var fe = document.forms.user_upload.category_1;
	if (fe.options[fe.selectedIndex].value == '')
	{
		document.forms.user_upload.category_2.disabled = true;
	};
	var fe = document.forms.user_upload.category_2;
	if (fe.options[fe.selectedIndex].value == '')
	{
		document.forms.user_upload.category_3.disabled = true;
	};
};


// FUNCTION: To enable the next in a series of category select lists
// i.e. each time a selection is made in one list, the next one becomes available.
function categoryUnlock (fe)
{
	var n = fe.name;
	var v = fe.options[fe.selectedIndex].value;

	if (n == 'category_1' && v != '')
	{
		document.forms.user_upload.category_2.disabled = false;
	};
	if (n == 'category_2' && v != '')
	{
		document.forms.user_upload.category_3.disabled = false;
	};
};

// FUNCTION: To validate form fields before submission
function validateUploadForm (f)
{

	// Check a category hasn't been chosen more than once
	var c1 = bbcjs.forms.getValue(f.category_1);
	var c2 = bbcjs.forms.getValue(f.category_2);
	var c3 = bbcjs.forms.getValue(f.category_3);

	var sameCats = false;

	if (c1 == c2 && c1 != '' && c2 != '')
	{
		sameCats = true;
	}
	else if (c1 == c3 && c1 != '' && c3 != '')
	{
		sameCats = true;
	}
	else if (c2 == c3 && c2 != '' && c3 != '')
	{
		sameCats = true;
	}

	if (sameCats)
	{
		alert("Please ensure you haven't selected a category more than once");
		f.category_1.focus();
		return false;
	}


	// Do the rest of the form validation
	bbcjs.forms.required.object 			= bbcjs.regex.not_empty;
	bbcjs.forms.required.obj_title 			= bbcjs.regex.not_empty;
	bbcjs.forms.required.obj_description 	= bbcjs.regex.not_empty;
	bbcjs.forms.required.region 			= bbcjs.regex.not_empty;
	bbcjs.forms.required.locality 			= bbcjs.regex.not_empty;
	bbcjs.forms.required.category_1			= bbcjs.regex.not_empty;
	bbcjs.forms.required.user_name 			= bbcjs.regex.not_empty;
	bbcjs.forms.required.user_email			= bbcjs.regex.email;
	bbcjs.forms.required.user_agreement		= bbcjs.regex.not_empty;


	bbcjs.forms.req_err.object 				= "Please select an image to upload";
	bbcjs.forms.req_err.obj_title 			= "Please enter a title for the photo";
	bbcjs.forms.req_err.obj_description 	= "Please enter a description of the photo";
	bbcjs.forms.req_err.region 				= "Please select a region";
	bbcjs.forms.req_err.locality 			= "Please select a locality";
	bbcjs.forms.req_err.category_1 			= "Please choose at least 1 category";
	bbcjs.forms.req_err.user_name 			= "Please enter your name";
	bbcjs.forms.req_err.user_email 			= "Please enter a valid email address";
	bbcjs.forms.req_err.user_agreement		= "Please indicate that you have read and accepted the conditions of submission";

	return bbcjs.forms.validate(f);

};

/* ~~~~~~~~~~~~~ Search form ~~~~~~~~~~~~~~~~ */

// FUNCTION: To validate the search form to ensure user submits correct
// and/or minimum amount of information
function validateSearchForm (f)
{

	// Check at least 1 of the form fields has been filled in
	if (!bbcjs.forms.getValue(f.val_1_1) && !bbcjs.forms.getValue(f.val_2_1) &&
		!bbcjs.forms.getValue(f.val_3_1) && !bbcjs.forms.getValue(f.val_4_1) &&
		!bbcjs.forms.getValue(f.val_5_1) && !bbcjs.forms.getValue(f.val_6_1))
	{
		alert('You must choose at least one search criteria');
		return false;
	}

	// Check that the start date occurs before the end date
	if (bbcjs.forms.getValue(f.val_3_1) > bbcjs.forms.getValue(f.val_4_1))
	{
		alert('Please specify an end date, which must occur after the start date.');
		return false;
	}

	// Otherwise, return true and allow the form to submit
	return true;
};


// FUNCTION: To focus the first location within the locality select from a selected region
// Is triggered onchange() of region select list
function FilterLocalityList ()
{
	// Create references to our select lists
	var rl = document.getElementById('regionList');
	var ll = document.getElementById('localityList');

	// Get the currently selected region
	var r = bbcjs.forms.getValue(rl);

	// Define a flag we need for selecting the first
	// item in the resulting filtered list
	var first = false;

	// Loop through all locality select list options
	focusItem:
	for (var i=0; i < ll.options.length; i++)
	{
		// check the option has a parentNode
		if(ll.options[i].parentNode) {
			// Compare the region value with the label of the option's containing optgroup
			if(ll.options[i].parentNode.label == r) {
				// Select the first option with a matching optgroup then break the for loop
				ll.options[i].selected = true;
				break focusItem;
			}
		}
	};

	/* // copy taken for backup - NathanT
	// Loop through all locality select list options
	for (var i=0; i < ll.options.length; i++)
	{
		// Unhide all localities if "Any" region is selected
		if (r == '')
		{
			ll.options[i].style.display = 'block';
			ll.selectedIndex = 0;
		}
		// Show localities for the selected region
		else if (ll.options[i].className == r)
		{
			ll.options[i].style.display = 'block';

			// Select the first entry in the filtered list,
			// otherwise previously selected items that should
			// be filtered out will remain visible.
			if (!first)
			{
				first = true;
				ll.selectedIndex = i;
			};
		}
		// Hide all other localities
		else
		{
			ll.options[i].style.display = 'none';
		};
	};
	*/
};