// grab the form
var objForm = document.getElementById('sales-form');

// inputs object
if (objForm)
{
	// define the inputs
	var objInputs =
	{
		organisation :
		{
			label : 'Organisation',
			object : objForm.organisation,
			required : false
		},

		name :
		{
			label : 'Name',
			object : objForm.name,
			required : true
		},

		email :
		{
			label : 'E-mail address',
			object : objForm.email,
			required : true
		},

		telephone :
		{
			label : 'Telephone number',
			object : objForm.telephone,
			required : false
		},

		country :
		{
			label : 'Country',
			object : objForm.country,
			required : false
		},

		enquiry :
		{
			label : 'Please enter your enquiry',
			object : objForm.enquiry,
			required : false
		}
	}

	// decide what to do with the input when it focuses/blurs
	function focusInput(objInput, blnFocus)
	{
		strLabel = objInputs[objInput.name].label;
		strValue = objInput.value;

		if ((blnFocus == true) && (strValue == strLabel))
		{
			objInput.value = '';
			activateInput(objInput, true);
		}

		else if ((blnFocus == false) && (strValue == ''))
		{
			activateInput(objInput, false);
			objInput.value = strLabel;
		}
	}

	function activateInput(objInput, blnActivate)
	{
		strLabel = objInputs[objInput.name].label;
	
		if (blnActivate == true)
		{
			if (objInput.className.indexOf('active') == -1)
			{
				objInput.className += ' active';
			}
		}
		else
		{
			if ((objInput.value == strLabel) || (objInput.value == ''))
			{
				objInput.className = objInput.className.replace('active', '');
			}
		}
	}

	function checkForm(blnCheck)
	{
		for (objName in objInputs)
		{
			objInput = objInputs[objName].object;
			strLabel = objInputs[objName].label;
			blnRequired = objInputs[objName].required;

			if (typeof blnCheck != 'undefined' && blnCheck == true)
			{
				if (objInput.value == '') focusInput(objInput, false);
				else if ((objInput.value != strLabel)) activateInput(objInput, true);
				objInput.onfocus = function() { focusInput(this, true); };
				objInput.onblur = function() { focusInput(this, false); };
			}
			
			else //submit
			{
				// validate
				if ((blnRequired == true) && (objInput.value == 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;
	}
}