
var searching = false;
var ContactForm = Form.extend({
	init: function()
	{
		var form = document.getElementsByTagName('form')[0];
		Event.addEvent(document.getElementById('_ctl0_primaryRegion_Contact_Method'), "change", this.contactMethod, true);
		//Event.addEvent(document.getElementById('_ctl0_primaryRegion_send'), "click", function() { return Form.validate(); }, false);
		// In Mozilla there is the default event handler for the submit event which we need to be  overridden.
		form.onsubmit = function() { return ContactForm.validate(); };
		document.getElementById('_ctl0_searchBtn').onclick = function () { searching = true; };
		//Event.addEvent(form, "submit", function() { return Form.validate(); }, true);
		
		// Make sure the Required abbr is on the correct label (this allows for Refresh)
		this.setContactLabels(document.getElementById('_ctl0_primaryRegion_Contact_Method'));
	}, // init
	
	validate: function()
	{
		var	isValid = true;
		
		if(!searching)
		{
			var firstname = document.getElementById('_ctl0_primaryRegion_First_Name');
			var lastname = document.getElementById('_ctl0_primaryRegion_Last_Name');
			var email = document.getElementById('_ctl0_primaryRegion_Email');
			var tel = document.getElementById('_ctl0_primaryRegion_Tel');
			var address = document.getElementById('_ctl0_primaryRegion_Address');
			var contactmethod = document.getElementById('_ctl0_primaryRegion_Contact_Method');
					
			Form().clearErrors();
			
			if (Form().isEmpty(firstname))
			{
				Form().addError("Please enter your first name");
				isValid = false;
			} // if
			
			if (Form().isEmpty(lastname))
			{
				Form().addError("Please enter your last name");
				isValid = false;
			} // if
			
			if (contactmethod.options[contactmethod.selectedIndex].value == "Telephone" && Form().isEmpty(tel))
			{
				Form().addError("Please enter a telephone number");
				isValid = false;
			} // if
			
			if (contactmethod.options[contactmethod.selectedIndex].value == "Email" && Form().isEmpty(email))
			{
				Form().addError("Please enter an email address");
				isValid = false;
			} // if
			
			if (contactmethod.options[contactmethod.selectedIndex].value == "Postal Address" && Form().isEmpty(address))
			{
				Form().addError("Please enter a postal address");
				isValid = false;
			} // if
			
			// Output error Message
			if (!isValid)
				Form().showErrors(document.getElementById('form'));
		}
		return isValid;
	}, // validate
	
	contactMethod: function(e)
	{
		var contact = Event.getSource(e);

		ContactForm.setContactLabels(contact);
	}, // contactMethod
	
	setContactLabels: function(contact)
	{
		var label;

		switch(contact.options[contact.selectedIndex].value)
		{
			case "Telephone":
				label = this.getLabel('Telephone');
				break;
			case "Email":
				label = this.getLabel('Email');
				break;
			case "Postal Address":
				label = this.getLabel('Address');
				break;
		} // switch
		
		if (label != null)
		{
			this.clearRequired();
			label.innerHTML = label.innerHTML + " <abbr title=\"Required field\">*</abbr>";
		} // if
	}, // setContactLabels
	
	clearRequired: function()
	{
		var labels = document.getElementsByTagName('label');
		for (var i = 0; i < labels.length; i++)
		{
			if (labels[i].innerHTML.indexOf('Telephone') == 0 || labels[i].innerHTML.indexOf('Email') == 0 || labels[i].innerHTML.indexOf('Address') == 0)
			{
				if (labels[i].innerHTML.toLowerCase().indexOf('<abbr') > -1)
					labels[i].innerHTML = labels[i].innerHTML.substring(0, labels[i].innerHTML.toLowerCase().indexOf('<abbr'));
			} // if
		} // for
	}, // clearRequired
	
	getLabel: function(text)
	{
		var labels = document.getElementsByTagName('label');
		
		for (var i = 0; i < labels.length; i++)
		{
			if (labels[i].innerHTML.indexOf(text) == 0)
				return labels[i];
		} // for
		
		return null;
	} // getLabel
});

/* Set the onload event handler */
Event.addEvent(this, "load", function() { ContactForm.init(); }, true);

