/**
* Friction TV - companys.js
* 
* functions for login page
* 
* @author						Neil Young neil.young@neilyoungcv.com
* @version						0.9
* @todo							???
* 
*/

/**
* @var object http
*/

var http = createRequestObject();

/**
* @var boolean validate
*/

document.validate = false;

/**
* Friction TV - companys.js - createRequestObject
* 
* create an ajax request object
* 
* @author						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
*/

function createRequestObject() 
{
	
	try 
	{ 
		return new XMLHttpRequest(); 
	
	} 
	catch(e)
	{
		
		//alert("There was a problem with XMLHttpRequest");
		
	}
   
	try
	{ 
		
		return new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch (e)
	{
		
		//alert("There was a problem with ActiveXObject(Msxml2.XMLHTTP)");
		
	}
   
	try 
	{ 
		
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	
	} 
	catch (e) 
	{
		
		//alert("There was a problem with ActiveXObject(Microsoft.XMLHTTP)");
		
	}
   
	alert("XMLHttpRequest not supported");
   
	return null;
 
}

/**
 * Friction TV - contact.js - setFocus()
 * 
 * field to set focus of a form element (cross-browser)
 * 
 * @author						Neil Young neil@friction.tv
 * @version						0.9
 * @todo						???
 * 
 * @param string 				form
 * @param string 				field
 * @param boolean				select (Microsoft Internet Explorer only)
 * 
 */

function setFocus(form, field, select) 
{

	if (select === undefined)
	{
	
		select = false;
		
	}
	
	try 
	{
		
		var browser = navigator.appName;
		
		document.getElementById(field).focus();

		//ie6+7
		
		eval('document.' + form + '.' + field + '.focus();');
		
		if (browser == 'Microsoft Internet Explorer' && select == true)
		{
		
			eval('document.' + form + '.' + field + '.select();');
			
		}
		
	} 
	catch (ignore) {}
	
}

/**
 * Friction TV - signup.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 - signup.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 - signup.js - checkFullName()
* 
* trim a string
* 
* @author						Neil Young neil@friction.tv
* @version						0.9
* @todo							???
* 
* @param 	string				$fullName
* 
*/

function checkFullName(fullName) 
{
	
	var fullNameError = document.getElementById('fullname_error');
	
	fullName = trim(fullName);
	
	var names = fullName.split(" ");
	
	if (fullName == '')
	{
		
		var fullNameErrorText = 'Please enter your Full Name';
		
		fullNameError.setAttribute('class', 'error');
		
		document.getElementById('contact_name').setAttribute('autocomplete', 'off');
		
		// for IE6+7
		
		fullNameError.className = "error";

		fullNameError.innerHTML = '';

		fullNameError.innerHTML += fullNameErrorText;
		
		setFieldColour('contact_form', 'contact_name', '#FF9F9F');
		
		if (document.validate == false)
		{
			setFocus('contact_form', 'contact_name', true);
		}
		
		return false;
		
	}
	else if (names[1] == undefined)
	{
		
		var fullNameErrorText = 'Please provide your full name';
		
		setFieldColour('contact_form', 'contact_name', '#FF9F9F');
		
		fullNameError.setAttribute('class', 'error');
		
		document.getElementById('contact_name').setAttribute('autocomplete', 'off');
		
		// for IE6+7
		
		fullNameError.className = "error";
		
		fullNameError.innerHTML = '';

		fullNameError.innerHTML += fullNameErrorText;
		
		if (document.validate == false)
		{
			setFocus('contact_form', 'contact_name', true);
			
		}
		
		return false;
		
	}
	else
	{
		
		setFieldColour('contact_form', 'contact_name', '#B8F5B1');
		
		fullNameError.innerHTML = '';
		
		return true;
		
	}
	
}

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

function checkEmail(email) 
{
	
	var emailError = document.getElementById('email_error');
	
	email = trim(email);
	
	validEmailRegExp = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/i;
	
	if (email == '')
	{
		
		var emailErrorText = 'Please enter your Email Address';
		
		emailError.setAttribute('class', 'error');
		
		document.getElementById('contact_email').setAttribute('autocomplete', 'off');
		
		// for IE6+7
		
		emailError.className = "error";
		
		emailError.innerHTML = '';

		emailError.innerHTML += emailErrorText;
		
		setFieldColour('contact_form', 'contact_email', '#FF9F9F');
		
		if (document.validate == false)
		{
			setFocus('contact_form', 'contact_email', true);
		}
		
		return false;
		
	}
	else if (email.search(validEmailRegExp) == -1)
	{
	
		var emailErrorText = 'Please enter a valid email address.';
		
		emailError.setAttribute('class', 'error');
		
		document.getElementById('contact_email').setAttribute('autocomplete', 'off');
		
		// for IE6+7
		
		emailError.className = "error";
		
		emailError.innerHTML = '';

		emailError.innerHTML += emailErrorText;
		
		setFieldColour('contact_form', 'contact_email', '#FF9F9F');
		
		if (document.validate == false)
		{
			
			setFocus('contact_form', 'contact_email', true);
			
		}
		
		return false;
		
	}
	else
	{

			setFieldColour('contact_form', 'contact_email', '#B8F5B1');
						
			emailError.innerHTML = '';
			
			if (document.validate == false)
			{
			
				setFocus('contact_form', 'contact_message');
			
			}
							
			return true;
			
	}
	
}

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

function checkMessage(message) 
{
	
	var messageError = document.getElementById('message_error');
	
	message = trim(message);
	
	if (message == '')
	{
		
		var messageErrorText = 'Please enter a message';
		
		messageError.setAttribute('class', 'error');
		
		document.getElementById('contact_message').setAttribute('autocomplete', 'off');
		
		// for IE6+7
		
		messageError.className = "error";

		messageError.innerHTML = '';

		messageError.innerHTML += messageErrorText;
		
		setFieldColour('contact_form', 'contact_message', '#FF9F9F');
		
		if (document.validate == false)
		{
		
			setFocus('contact_form', 'contact_message', true);
		
		}
		
		return false;
		
	}
	else
	{
		
		setFieldColour('contact_form', 'contact_message', '#B8F5B1');
		
		messageError.innerHTML = '';
		
		return true;
		
	}
	
}

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

function validateForm() 
{
	
	document.validate = true;
	
	var validateFullName = document.getElementById('contact_name').value;
	
	var validateEmail = document.getElementById('contact_email').value;
	
	var validateMessage = document.getElementById('contact_message').value;
	
	//alert(validateFullName + ' ' + validateEmail + ' ' + validateMessage);
	
	fullNameError = checkFullName(validateFullName);
	
	emailError = checkEmail(validateEmail);
	
	messageError = checkMessage(validateMessage);
	
	if (fullNameError == false && emailError == false && messageError == false)
	{
		
		return false;	
	
	}
	else
	{
		
		url = 'http://' + document.location.hostname + '/ajax.php?call=contact_email&params=' + validateFullName + '|' + validateEmail + '|' + validateMessage;
		
		http.open('POST', url, false);
		
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		http.send(url);
		
		var responseArray = http.responseText;

        var responseSplit = responseArray.split("||");
   
        var responseType = responseSplit[0];
   
        var response = responseSplit[1];
         
        responseType = trim(responseType);
         
        if(responseType == "contact_email")
        {
       
            contactForm = document.getElementById('contact_script_form');
			
			if (response == "1")
            {
	
				contactForm.innerHTML = '';
				
				contactForm.innerHTML = '<p style="margin:0px; float:left; padding-top:10px">Thank you, ' + validateFullName + ', your email was sent successfully.<br/><br/>An email has been sent to ' + validateEmail + ' confirming your message submission.<br/><br/>Use the menu bar above to continue browsing my website.</p>';
					
			}
			else
			{
				
				contactForm.innerHTML = '';
				
				contactForm.innerHTML = '<p style="margin:0px; color:red; float:left">There was a problem submitting you message, please try again later.</p>';
				
			}
			
			
		}

		
	}
	
}




// done