// grab the form
var objForm = document.getElementsByTagName('form')[0];

// inputs object
if(objForm)
{
	// define the inputs
	var objInputs =
	{
		ctl00$SecondaryContent$TxtContactName :
		{
			label : 'Contact name',
			object : objForm.ctl00$SecondaryContent$TxtContactName,
			required : true
		},

		ctl00$SecondaryContent$TxtJobTitle :
		{
			label : 'Job title',
			object : objForm.ctl00$SecondaryContent$TxtJobTitle,
			required : true
		},

		ctl00$SecondaryContent$TxtOrganisationName :
		{
			label : 'Organisation/group name',
			object : objForm.ctl00$SecondaryContent$TxtOrganisationName,
			required : true
		},

		ctl00$SecondaryContent$TxtTypeOrganisation :
		{
			label : 'Type of organisation/group',
			object : objForm.ctl00$SecondaryContent$TxtTypeOrganisation,
			required : true
		},

		ctl00$SecondaryContent$TxtNumberOfSupporters :
		{
			label : 'Number of supporters',
			object : objForm.ctl00$SecondaryContent$TxtNumberOfSupporters,
			required : false
		},

					
		ctl00$SecondaryContent$TxtAddress1 :
		{
			label : 'Address line 1',
			object : objForm.ctl00$SecondaryContent$TxtAddress1,
			required : true
		},

		ctl00$SecondaryContent$TxtAddress2 :
		{
			label : 'Address line 2',
			object : objForm.ctl00$SecondaryContent$TxtAddress2,
			required : false
		},

		ctl00$SecondaryContent$TxtAddress3 :
		{
			label : 'Address line 3',
			object : objForm.ctl00$SecondaryContent$TxtAddress3,
			required : false
		},

		ctl00$SecondaryContent$TxtTown :
		{
			label : 'Town/City',
			object : objForm.ctl00$SecondaryContent$TxtTown,
			required : false
		},

		ctl00$SecondaryContent$TxtCounty :
		{
			label : 'County',
			object : objForm.ctl00$SecondaryContent$TxtCounty,
			required : false
		},

		ctl00$SecondaryContent$TxtPostcode :
		{
			label : 'Post Code',
			object : objForm.ctl00$SecondaryContent$TxtPostcode,
			required : true
		},


		ctl00$SecondaryContent$TxtContactNo :
		{
			label : 'Contact number',
			object : objForm.ctl00$SecondaryContent$TxtContactNo,
			required : true
		},

		ctl00$SecondaryContent$TxtFaxNumber :
		{
			label : 'Fax number',
			object : objForm.ctl00$SecondaryContent$TxtFaxNumber,
			required : false
		},

		ctl00$SecondaryContent$TxtEmail :
		{
			label : 'Email address',
			object : objForm.ctl00$SecondaryContent$TxtEmail,
			required : true
		},

		ctl00$SecondaryContent$TxtConfirmEmail :
		{
			label : 'Confirm email address',
			object : objForm.ctl00$SecondaryContent$TxtConfirmEmail,
			required : true
		},


		ctl00$SecondaryContent$TxtWhyJoin :
		{
			label : 'Why did you decide to join the campaign?',
			object : objForm.ctl00$SecondaryContent$TxtWhyJoin,
			required : true
		},

		ctl00$SecondaryContent$TxtWhatMoney :
		{
			label : 'What will you do with the money that you raise?',
			object : objForm.ctl00$SecondaryContent$TxtWhatMoney,
			required : true
		},

		ctl00$SecondaryContent$TxtQuote :
		{
			label : 'Please provide a quote about the campaign which will be included in the press release',
			object : objForm.ctl00$SecondaryContent$TxtQuote,
			required : true
		},

		ctl00$SecondaryContent$TxtNewspapers :
		{
			label : 'Please tell us the name of your local newspaper(s)',
			object : objForm.ctl00$SecondaryContent$TxtNewspapers,
			required : true
		},


		ctl00$SecondaryContent$TxtPictureDescription :
		{
			label : 'If you have attached a picture please tell us the names of the people in it',
			object : objForm.ctl00$SecondaryContent$TxtPictureDescription,
			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;
	}
}
