// grab the form
var objForm = document.getElementsByTagName('form')[0];

// inputs object
if(objForm)
{
	// define the inputs
	var objInputs =
	{
		ctl00$MainContent$TxtOrganisation :
		{
			label : 'Organisation',
			object : objForm.ctl00$MainContent$TxtOrganisation,
			required : true
		},
		
		ctl00$MainContent$TxtAddressLine1 :
		{
			label : 'Address line 1',
			object : objForm.ctl00$MainContent$TxtAddressLine1,
			required : true
		},

		ctl00$MainContent$TxtAddressLine2 :
		{
			label : 'Address line 2',
			object : objForm.ctl00$MainContent$TxtAddressLine2,
			required : false
		},

		ctl00$MainContent$TxtAddressLine3 :
		{
			label : 'Address line 3',
			object : objForm.ctl00$MainContent$TxtAddressLine3,
			required : false
		},

		ctl00$MainContent$TxtTownOrCity :
		{
			label : 'Town/City',
			object : objForm.ctl00$MainContent$TxtTownOrCity,
			required : false
		},

		ctl00$MainContent$TxtCounty :
		{
			label : 'County',
			object : objForm.ctl00$MainContent$TxtCounty,
			required : false
		},

		ctl00$MainContent$TxtPostCode :
		{
			label : 'Post Code',
			object : objForm.ctl00$MainContent$TxtPostCode,
			required : true
		},
		
		ctl00$MainContent$TxtTelephone :
		{
			label : 'Telephone number',
			object : objForm.ctl00$MainContent$TxtTelephone,
			required : true
		},
		
		ctl00$MainContent$TxtEmail :
		{
			label : 'Email address',
			object : objForm.ctl00$MainContent$TxtEmail,
			required : false
		},
		
		ctl00$MainContent$TxtNumberPupils :
		{
			label : 'Number of pupils',
			object : objForm.ctl00$MainContent$TxtNumberPupils,
			required : false
		},
		
		ctl00$MainContent$TxtTypeSchool :
		{
			label : 'Type of school',
			object : objForm.ctl00$MainContent$TxtTypeSchool,
			required : false
		},
		
		ctl00$MainContent$TxtCampaignOrganiser :
		{
			label : 'Campaign organiser',
			object : objForm.ctl00$MainContent$TxtCampaignOrganiser,
			required : true
		},
		
		ctl00$MainContent$TxtSecondContact :
		{
			label : 'Second contact',
			object : objForm.ctl00$MainContent$TxtSecondContact,
			required : false
		},

		ctl00$MainContent$TxtPosition :
		{
			label : 'Position',
			object : objForm.ctl00$MainContent$TxtPosition,
			required : false
		},

		ctl00$MainContent$TxtReferral :
		{
			label : null,
			object : objForm.ctl00$MainContent$TxtReferral,
			required : false
		},

		ctl00$MainContent$TxtReferralOther :
		{
			label : 'Not in the list above? Please specify',
			object : objForm.ctl00$MainContent$TxtReferralOther,
			required : false
		}
	}

	// decide what to do with the input when it focuses/blurs
	function focusInput(objInput, blnFocus)
	{
		strLabel = objInputs[objInput.name].label;
		strValue = (objInput.tagName != 'SELECT')? objInput.value : objInput[objInput.selectedIndex].value;

		if ((blnFocus == true && strValue == strLabel) || (blnFocus == true && objInput.tagName == 'SELECT'))
		{
			if (objInput.tagName != 'SELECT') objInput.value = '';

			else if (strValue == '' || strValue == '--------------------------')
			{
				objInput.selectedIndex = 0;
				activateInput(objInput, false);

				return;
			}

			activateInput(objInput, true);
		}

		else if (blnFocus == false && strValue == '')
		{
			if (objInput.tagName != 'SELECT') objInput.value = strLabel;
			activateInput(objInput, false);
		}
	}

	function activateInput(objInput, blnActivate)
	{
		strLabel = objInputs[objInput.name].label;
		strValue = (objInput.tagName != 'SELECT')? objInput.value : objInput[objInput.selectedIndex].value;

		if (blnActivate == true)
		{
			if (objInput.className.indexOf('active') == -1)
			{
				objInput.className += ' active';
			}
		}
		else
		{
			if ((strValue == strLabel) || (strValue == ''))
			{
				objInput.className = objInput.className.replace('active', '');
			}
		}
	}

	function checkForm(blnInitialise)
	{
	    for (objName in objInputs)
		{
			objInput = objInputs[objName].object;
			strLabel = objInputs[objName].label;
			strValue = (objInput.tagName != 'SELECT')? objInput.value : objInput[objInput.selectedIndex].value;
			blnRequired = objInputs[objName].required;

			if (blnInitialise == true)
			{
				if (strValue == '' && objInput.tagName != 'SELECT')
				{
					focusInput(objInput, false);
				}
				else if ((strValue != strLabel && strLabel != null))
				{
					activateInput(objInput, true);
				}

				if (objInput.tagName == 'SELECT') objInput.onchange = function() { focusInput(this, true); };

				else
				{
					objInput.onfocus = function() { focusInput(this, true); };
					objInput.onblur = function() { focusInput(this, false); };
				}
			}
			
			else //submit
			{
				// validate
				if ((blnRequired == true) && (strValue == strLabel))
				{
					alert('Sorry, the "' + strLabel + '" field is required');
					objInput.focus();

					return false;
				}
			}
		}
	}

	// check a form exists
	if (typeof objForm != 'undefined')
	{
		checkForm(true);		
		objForm.onsubmit = checkForm;
	}
}