function expandMap(link,map,newSize,originalSize)
{
	// Get the link
	var thisElement = getElement(link);
	
	// If the link doesn't exist, give up
	if (thisElement == null)
		return;
		
	// Get the map
	var thisMapElement = getElement(map);
	
	// If the map doesn't exist, give up
	if (thisMapElement == null)
		return;
	
	// Set the new map size
	var size = 0;
	
	if (thisMapElement.getHeight() == originalSize+8)
	{
		// Change 'expand' to 'shrink'
		thisElement.title = 'Make the map smaller';
		thisElement.innerHTML = 'Shrink';
		
		// Set the size
		size = newSize;
	}
	else
	{
		// Change 'shrink' to 'expand'
		thisElement.title = 'Make the map bigger';
		thisElement.innerHTML = 'Expand';
		
		// Set the size
		size = originalSize;
	}
	
	new Effect.Morph(''+ map +'',{style:{height:''+ size +'px'}});
}

function contactForm(element)
{
	if (getQueryVariable('c') == '1')
		formFocus(element);
}

function formFocus(element)
{
	// Get the element
	var thisElement = getElement(element);
	
	// If the element doesn't exist, give up
	if (thisElement == null)
		return;
		
	thisElement.focus();
	
	scrollToElement(element);
}

function inputHelp(element,infobox)
{
	// Get the element
	var thisElement = getElement(element);
	
	// If the element doesn't exist, give up
	if (thisElement == null)
		return;
	
	// Styles
	var originalStyle = '';
	var newStyle = 'inputOver';
	var errorStyle = 'inputError';

	// Toggle the style on the element
	toggleStyle(thisElement,originalStyle,newStyle,errorStyle);

	// Toggle the infobox
	var infoBox = getElement(infobox);
	if (infoBox == null)
		return;
	
	// If the inner box allready exists, skip this bit
	if (getElement(infoBox.id +'Inner') == null)
	{
		// Add an inner div to put the text in
		var newDiv = document.createElement('div');
		newDiv.id = infoBox.id +'Inner';
		newDiv.className = 'infoBoxInner';
		newDiv.innerHTML = infoBox.innerHTML;
	
		// Get rid of the infobox text as it's now in the inner div
		// and attach the inner div
		infoBox.innerHTML = '';
		infoBox.appendChild(newDiv);
	}
		
	Effect.toggle(''+ infobox +'','appear',{duration:0.4});
}

function inputValidateEmail(element)
{
	// Styles
	var okStyle = '';
	var errorStyle = 'inputError';
	
	// Get the element
	var thisElement = getElement(element);
	
	// If the element doesn't exist, give up
	if (thisElement == null)
		return;
	
	// Validate the email
	// If it's ok, set the ok style, otherwise set the error style
	validateEmail(thisElement) == true ? thisElement.className = okStyle : thisElement.className = errorStyle;
}

function inputValidateLength(element,length)
{
	// Styles
	var okStyle = '';
	var errorStyle = 'inputError';

	// Get the element
	var thisElement = getElement(element);

	// If the element doesn't exist, give up
	if (thisElement == null)
		return;
	
	// Validate the text
	// If it's ok, set the ok style, otherwise set the error style
	validateLength(thisElement,length) == true ? thisElement.className = okStyle : thisElement.className = errorStyle;
}

function formValidate(element,errorElement)
{	
	// Get the element
	var thisElement = getElement(element);
	
	// If the element doesn't exist, give up
	if (thisElement == null)
		return false;
		
	// Validate the form
	if (validateForm(element))
		return true;
	else
	{
		// The form failed to validate, so show the error
		var thisErrorElement = getElement(errorElement);
		
		if (thisErrorElement != null)
		{
			// Show it...
			Effect.Appear(thisErrorElement.id,{duration:0.4});
		}
		
		return false;
	}
}

///////////////////////////////////
///////////////////////////////////
//
// CORE FUNCTIONS
//
///////////////////////////////////
///////////////////////////////////

function toggleElement(element)
{
	// Toggle
	Effect.toggle(element, 'appear', {duration:0.4});
}

function toggleStyle(element,originalStyle,newStyle,ignoreStyle)
{
	// If the element doesn't exist, try to find it
	var thisElement = getElement(element);
	
	// If it still doesn't exist, give up
	if (thisElement == null)
		return;
		
	// If the current style is the ignore style, return
	if (thisElement.className == ignoreStyle)
		return;
	else
	{
		// Check and change the style
		thisElement.className == originalStyle || thisElement.className.length == 0 || thisElement.className == null ? thisElement.className = newStyle : thisElement.className = originalStyle;
	}
}

function getElement(element)
{
	// If it's already ok then return
	if (element.id != null)
		return element;
	
	// Get the element
	//var thisElement = document.getElementById(element);
	var thisElement = $(element);

	// If the element doesn't exist, return nothing
	if (thisElement == null)
		return null;
	else
		return thisElement;
}

function validateEmail(element)
{
	// Get the element
	var thisElement = getElement(element);
	
	// If the element doesn't exist, give up
	if (thisElement == null)
		return false;
	
	// Check it's a valid email
	// This is just a really simple check
	// All it checks for is an @ and .
	var checkMe = thisElement.value;
	
	var posOfAt = checkMe.indexOf('@');
	
	if (posOfAt < 4)
		return false;
	
	if (checkMe.indexOf('.',posOfAt) > 2)
		return true;
	else
		return false;
}

function validateLength(element,length)
{
	// Get the element
	var thisElement = getElement(element);

	// If the element doesn't exist, give up
	if (thisElement == null)
		return false;
		
	// Check the length
	if (thisElement.value.length >= length)
		return true;
	else
		return false;
}

function validateForm(element)
{
	// Get the element
	var thisElement = getElement(element);

	// If the element doesn't exist, give up
	if (thisElement == null)
		return false;
		
	// Styles
	var errorStyle = 'inputError';
	
	// Loop through all the items and check they don't have an error style
	var i;
	for(i=0;i<thisElement.elements.length;i++)
	{
		if (thisElement.elements[i].className == errorStyle)
			return false;
	}
	
	return true;
}

function centerElement(element)
{
	// Get the element
	var thisElement = getElement(element);

	// If the element doesn't exist, give up
	if (thisElement == null)
		return false;
		
	var left = 0;
	
	left = (document.body.clientWidth/2) - (thisElement.style.width.replace(/px/,'')/2);
	
	thisElement.style.left = left + 'px';
}

function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function scrollToElement(element)
{
	var thisElement = $('name');
	
	var arr = thisElement.cumulativeOffset();
	var x = arr[0];
	var y = arr[1];
	
	window.scrollTo(x,y);
}