/**
* Friction TV - contact.js
* 
* functions for contact page
* 
* @author 						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
*/

$(document).ready(function() 
{

	$('#contactFullName').focus();

});

/**
 * Friction TV - contact.js - setFieldColour()
 * 
 * sets the background color of a field
 * 
 * @author						Neil Young neil@friction.tv
 * @version						0.9
 * @todo						???
 * 
 * @param string 				form
 * @param string				field
 * @param string				colour
 * 
 */

function setFieldColour(form, field, colour) 
{

	document.getElementById(field).style.backgroundColor = colour;
	
}

/**
* Friction TV - contact.js - trim(stringToTrim)
* 
* trim a string
* 
* @author Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
* @param int stringToTrim
* 
*/

function trim(stringToTrim) 
{
	
	return stringToTrim.replace(/^\s+|\s+$/g,"");

}

/**
* Friction TV - contact.js - checkFullName()
* 
* trim a string
* 
* @author						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
* @param 	string				$fullName
* 
*/

function checkFullName(fullName) 
{

	fullName = trim(fullName);
	
	var names = fullName.split(" ");
	
	if (fullName == '')
	{

		var content = '<p>Please enter your full name.</p>';

		$('#fullname-error').addClass('contact-error').html(content)

		setFieldColour('contact', 'contactFullName', '#FF9F9F');

		$('#contactFullName').focus();
		
		return false;
		
	}
	else if (names[1] == undefined)
	{

		var content = '<p>Please provide us with your full name.</p>';

		$('#fullname-error').addClass('contact-error').html(content)

		setFieldColour('contact', 'contactFullName', '#FF9F9F');

		$('#contactFullName').focus();
		
		return false;
		
	}
	else
	{

		$('#fullname-error').removeAttr('class').html('');		

		setFieldColour('contact', 'contactFullName', '#B8F5B1');
		
		return true;
		
	}
	
}

/**
* Friction TV - contact.js - checkEmail()
* 
* check email address
* 
* @author						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
* @param 	string				$email
* 
*/

function checkEmail(email) 
{
	
	email = trim(email);
	
	validEmailRegExp = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/i;
	
	if (email == '')
	{

		var content = '<p>Please enter your Email Address.</p>';

		$('#email-error').addClass('contact-error').html(content)

		setFieldColour('contact', 'contactEmail', '#FF9F9F');

		$('#contactEmail').focus();

		return false;
		
	}
	else if (email.search(validEmailRegExp) == -1)
	{

		var content = '<p>Please enter a valid email address.</p>';

		$('#email-error').addClass('contact-error').html(content)

		setFieldColour('contact', 'contactEmail', '#FF9F9F');

		$('#contactEmail').focus();
		
		return false;
		
	}
	else
	{
			
		$('#email-error').removeAttr('class').html('');

		setFieldColour('contact', 'contactEmail', '#B8F5B1');

		$('#contactQuery').focus();

		return true;

	}
	
}

/**
* Friction TV - contact.js - checkQuery()
* 
* Check query
* 
* @author						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
* @param 	string				$query
* 
*/

function checkQuery(query) 
{

	query = trim(query);

	if (query == '')
	{

		var content = '<p>Please enter your query.</p>';

		$('#query-error').addClass('contact-error').html(content)

		setFieldColour('contact', 'contactQuery', '#FF9F9F');

		$('#contactQuery').focus();
		
		return false;
		
	}
	else
	{

		$('#fullname-error').removeAttr('class').html('');		

		setFieldColour('contact', 'contactQuery', '#B8F5B1');

		return true;

	}
	
}

/**
* Friction TV - contact.js - validateContact
* 
* contact
* 
* @author						Brent McDowell brent@nuguru.com
* @version						0.9
* @todo							???
* 
*/

function validateContact()
{

	var validateFullName = $('#contactFullName').val();
	
	var validateEmail = $('#contactEmail').val();
	
	var validateQuery = $('#contactQuery').val();

	if (!checkFullName(validateFullName))
	{
		
		validateFullNameError = true;
		
	}
	else
	{
	
		validateFullNameError = false;
		
	}
	
	if (!checkEmail(validateEmail))
	{
		
		validateEmailError = true;
		
	}
	else
	{
	
		validateEmailError = false;

	}

	if (!checkQuery(validateQuery))
	{
		
		validateQueryError = true;
		
	}
	else
	{
	
		validateQueryError = false;

	}

	if (validateFullNameError == false && validateEmailError == false && validateQueryError == false)
	{

		url = "http://" + document.location.hostname + "/ajax/?p=contact|1|" + validateFullName + "|" + validateEmail + "|" + validateQuery;

		var data = $.ajax({type: "POST", url: url, async: false}).responseText;

		var response = data;
		
		if (response == 1)
		{		

			var content = "<p><b>Thanks!</b><br />If we need to get in touch with you, we will do so shortly.</p>";

			$("#contactFullName").val("").css("background-color", "#FFF");

			$("#contactEmail").val("").css("background-color", "#FFF");

			$("#contactQuery").val("").css("background-color", "#FFF");

			$("#contact-result").addClass("contact-success").html(content);
	
		}	
			
	}

}