// JavaScript Document to Control Contact Form on Home Page

//Clears form textbox or textarea when given the CSS selector
function clearText(selector) {
	$(selector).attr('value', '')
	//alert(selector);
};

//Resets the form textbox or textarea when given the CSS selector and the default value
function resetText(selector, valueText) {
	$(selector).attr('value', valueText)
}

//LISTENERS THAT LOAD WITH PAGE
$(document).ready(function() {
	
	//Create varibles to contain default values of the contact form textboxes or textareas
	var defaultName = 'Name';
	var defaultEmail = 'Contact E-mail Address';
	var defaultNumber = 'Contact Telephone Number';
	var defaultMessage = 'Message';
	
	
	//The below listeners will either clear or reset value of the textbox or textarea when they are clicked (focus) or moved away from (blur) respectively. Note that a value will only be reset if the value in the textbox or textarea is null or the same as the default value.
	
	//NAME
	
	$('input#form_name').focus(function() {
			
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
										
		if(itemValue == defaultName) {
			clearText(currentID);
		}
	});	
	
	$('input#form_name').blur(function() {
									   
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
		
		if((itemValue == defaultName) || (itemValue == '')) {
			resetText(currentID, defaultName);
		}
	});	
	
	//EMAIL
	
	$('input#form_email').focus(function() {
			
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
										
		if(itemValue == defaultEmail) {
			clearText(currentID);
		}
	});	
	
	$('input#form_email').blur(function() {
									   
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
		
		if((itemValue == defaultEmail) || (itemValue == '')) {
			resetText(currentID, defaultEmail);
		}
	});
	
	//TELEPHONE NUMBER
	
	$('input#form_telephone').focus(function() {
			
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
										
		if(itemValue == defaultNumber) {
			clearText(currentID);
		}
	});	
	
	$('input#form_telephone').blur(function() {
									   
		var itemValue = $(this).attr('value');
		var currentID = 'input#'+$(this).attr('id');
		
		if((itemValue == defaultNumber) || (itemValue == '')) {
			resetText(currentID, defaultNumber);
		}
	});
	
	//MESSAGE
	
	$('textarea#form_message').focus(function() {
			
		var itemValue = $(this).attr('value');
		var currentID = 'textarea#'+$(this).attr('id');
										
		if(itemValue == defaultMessage) {
			clearText(currentID);
		}
	});	
	
	$('textarea#form_message').blur(function() {
									   
		var itemValue = $(this).attr('value');
		var currentID = 'textarea#'+$(this).attr('id');
		
		if((itemValue == defaultMessage) || (itemValue == '')) {
			resetText(currentID, defaultMessage);
		}
	});
	
	
	
});


