function validateBookForm()
{
	if (isNaN($F('select_week')))
	{
		$('group_size_text').update('Please make a choice for your preferred Week!');
		$('num_passengers').hide();
		return false;
	}
	
	if ( $('accommodation_field_id').value.length == 0
			|| $('accommodation_type_field_id').value.length == 0
			||$('accommodation').value.length == 0)
	{
		$('group_size_text').update('Please make a choice for Accommodation!');
		$('num_passengers').hide();
		return false;
	}

	return true;
}

function setCapacity(capacity, minPersons)
{
	$('book-now-submit').disabled = false;
	var select = $('num_passengers');
	select.show();
	select.options.length = 0;

	if (capacity > 0)
	{
		$('group_size_text').update('How many going?:');
		if (!isNaN(minPersons) && minPersons > 0)
		{
			for(var m=minPersons; m <= capacity; m++)
				select.options[select.options.length] = new Option(m,m,false,false);
		}
		else
			select.options[select.options.length] = new Option(capacity, capacity, false, false);
	}
	else if (capacity == -1)
	{
		$('group_size_text').update('How many people? (maximum of 10):');
		select.options[select.options.length] = new Option(1, 1, false, false);
		for(var t=2; t <= 10; t++)
			select.options[select.options.length] = new Option(t,t,false,false);
	}
	else
	{
		$('group_size_text').update('You cannot book this.');
		select.hide();
		$('book-now-submit').readonly = true;
	}
}

function setFieldIds(type_id, field_id, name)
{
	$('accommodation_field_id').value = type_id;
	$('accommodation_type_field_id').value = field_id;
	$('accommodation').value = name;
}

function updateAccommDetails(accomm_select)
{
	var details = $F(accomm_select).split('_');
	var capacity = details[0].split('/');
	if (typeof capacity[1] == 'undefined')
		capacity[1] = 0;
	setCapacity(capacity[0], capacity[1]);
	setFieldIds(details[1], details[2], details[3]);
	return true;
}

function loadAccommOptions()
{
	if (isNaN($F('select_week')) || $F('select_week') == 0)
	{
		$('group_size_text').update('You cannot book this.');
		$('num_passengers').value = '';
		$('num_passengers').hide();
		$('book-now-submit').disabled = true;
		$('accommodation_field_id').value = '';
		$('accommodation_type_field_id').value = '';
		return false;
	}

	new Ajax.Updater('select_accommodation', '/ajax/group/bookingFormAccomm/', {
										method: 'post',
										parameters: {
											'select_week': $F('select_week')
										}
									});
	return true;
}

function loadUniGroups()
{
	new Ajax.Updater('select-group', '/ajax/home/uniGroups/', {
	    zmethod: 'post',
	    parameters: {
		    'select-university': $F('select-university')
	    }
	});
	return true;
}

// Facebook profile ID parser
// http://www.facebook.com/home.php#/profile.php?id=284300540&ref=profile
// http://www.facebook.com/profile.php?id=509102514#/profile.php?id=284300540&ref=profile
function facebookProfileId()
{
	var el = $('facebook_profile_id');
	var url = $F('facebook_profile_id');
	var val_el = $('facebook_verifying');
	var url_regex = new RegExp("=[0-9]*", "g");
	var url_match = new RegExp('http:\/\/[a-zA-Z]*\.?facebook\.com');
	var invalid_message = "The Facebook Profile ID you entered was invalid.";

	// only if a value has been entered into element
	if(url.length > 0)
	{
		// its a URL, parse
		if(isNaN(url))
		{
			// must be a facebook url
			if(url_match.match(url))
			{
				temp_string = url.match(url_regex);

				temp_string = temp_string[temp_string.length-2];
				facebook_profile_id = temp_string.replace("=","");

				// make sure we have a number
				if(isNaN(facebook_profile_id))
				{
					// we had no number, fail!
					alert(invalid_message);
					el.clear();
					val_el.show();
				}
				else { facebookProfileIdConfirm(facebook_profile_id); }
			}
			// wasnt from facebook
			else
			{
				alert(invalid_message);
				el.clear();
				val_el.show();
			}
		}
		// its a number just check
		else { facebookProfileIdConfirm(url); }
	}

	// always finish on a hide
	val_el.hide();
}

function facebookProfileIdConfirm(id)
{
	new Ajax.Request('/ajax/eventpromoter/confirmProfileId/', {
		'method': 'post',
		'parameters': {
			facebook_profile_id: id
		},
		onSuccess: function(transport) {
			name = transport.responseText;
			if(name.length > 0)
			{

				var confirmed = confirm('Can you confirm that your name on Facebook is ' + name + '?');
				if(confirmed)
					val = id
				else
					val = '';

				$('facebook_profile_id').value = val;
			}
		}
	});
}

// show hide "Other details" selection
function showOther()
{
	var wh = $F('additional_where_heard');
	if(wh == "Other")
		$('row_other_details').show();
	else
		$('row_other_details').hide();

}
//Event.observe(window, "load", showOther());

// complete for intuity
function fillName(name)
{
	if($F('poster_contact_name') == '')
		$('poster_contact_name').value = name;
}

// complete address
function fillEmail(email)
{
	if($F('poster_contact_email') == '')
		$('poster_contact_email').value = email;
}

function fillPhone(phone)
{
	if( $F('poster_contact_mobile') == '')
		$('poster_contact_mobile').value = phone;
}

/**
 * Show Group Verification if the chosen group category requires personal authentication.
 */
function requireGroupVerification()
{
	var rep = $('category_id').options[ $('category_id').selectedIndex ].text;
	var action = 'hide';	
	if (rep.indexOf('authorised') != -1)
		action = 'show';

	rows = $$('.group-verification');
	for(i=0; i < rows.length; i++)
	{
		if (action == 'show')
			rows[i].show();
		else
			rows[i].hide();
	}

	var pos = rep.indexOf('(');
	if (pos != -1)
		rep = rep.substring(0, pos);
	
	$('text_representing').update(rep);
}

function checkGroupCode()
{
	var group_name = $F('username');
	group_name = group_name.replace(/[^a-zA-Z0-9_-]/gi, "");

	new Ajax.Request('/ajax/eventpromoter/checkGroupCode/', {
		'method': 'post',
		'parameters': {
			'q': group_name
		},
		onSuccess: function(transport) {
			res = transport.responseText;
			if (res.response_message.length > 0)
				alert(res.response_message);
			$('username').value = res.group_name;
		}
	});
}

function generateGroupCode()
{
	var groupName = document.getElementById('group_name');
	if (!groupName && typeof(groupName)!="object")
		return false;

	var groupCode = document.getElementById('username');
	if (!groupCode && typeof(groupCode)!="object")
		return false;

	var tmpStr = groupName.value;
	tmpStr = tmpStr.replace(/[^a-zA-Z0-9_-]/gi, "");
	tmpStr = tmpStr.toLowerCase();

	groupCode.value = tmpStr;

	return true;
}