/*
	contact.js

This class is used to validate contact form data and 
	then post that data to the contact table in the raphaels
	database
*/

/* Saved this for reference only
function completedForm()
{
	var form = document.forms.contact;
	for (var i = 0; i < form.elements.length; i++)
	{
		if (!form.elements[i].value)
		{
			form.elements[i].style.backgroundColor = "#FF0000";
			var emptyFields = true;
		} else {
			form.elements[i].style.backgroundColor = "#FFFFFF";
		}
			
	}
	if (emptyFields)
	{
		return false;
	}
	return true;
}
*/

function validTelephone(str)
{
	// Strip all non-digits from the string
	str = str.replace(/[^\d]/g,"");
	// If there aren't 10 digits in the string
	if (str.length != 10)
	{
		// The phone number is invalid
		return false;
	}
	// Split the string into a properly formatted U.S. telephone number
	var areaCode = str.substring(0,3);
	var prefix = str.substring(3,6);
	var suffix = str.substring(6,10);
	return str = (areaCode + "-" + prefix + "-" + suffix);
}

function validZipCode(str)
{
	// Strip all non-digits from the string
	str = str.replace(/[^\d]/g,"");
	// If there aren't 10 digits in the string
	if (str.length != 5)
	{
		return false;
	}
	return str;
}

function validEmail(str)
{
	if (!str)
	{
		return false;
	}
	request = GetXmlHttpObject();
	var url = "validemail.php";
	url = url + "?email=" + str;
	request.open("POST",url,false);
	request.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
	request.send(null);
	if (request.responseText == 1)
	{
		return true;
	}
	return false;
}

function validState(str)
{
	str = str.toUpperCase();
	str = str.replace(/[^A-Z]/g,"");
	if (str.length != 2)
	{
		return false;
	}
	return str;
}

function getLocation(str)
{
	if (!str)
	{
		return false;
	}
	var form = document.forms["contact"];
	
	// Disable the city and state input fields
	// while we try to retreive them from the USPS.
	form.city.disabled=true;
	form.state.disabled=true;
	
	request = GetXmlHttpObject();
	var url = "zipcode.php";
	url = url + "?zipCode=" + str;
	request.onreadystatechange=updateLocation;
	request.open("GET",url,true);
	request.send(null);
}

function updateLocation()
{
	var form = document.forms["contact"];
	if (request.readyState == 4 || request.readyState == "complete")
	{
		xmlDoc = request.responseXML;
		form.city.value=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
		form.state.value=xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
		// Re-enable the city and state input fields.
		form.city.disabled=false;
		form.state.disabled=false;
	}
}

function postContactForm(arr)
{
	var req = new XMLHttpRequest();
	req.open("POST","submitcontactform.php",false);
	req.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
	req.send(arr);
	if (req.responseText == 1)
	{
		alert("Form submitted successfully. A representative will contact you shortly.");
		return true;
	} else {
		alert("There was an error submitting your form.");
			return false;
	}
}

function setInputError(field)
{
	field.setAttribute("class","error");
	field.setAttribute("className","error");
	field.focus();
}

function clearInputError(field)
{
	field.removeAttribute("class");
	field.removeAttribute("className");
}

function validateForm()
{
	var form = document.forms["contact"];

	// Make sure we get a first and last name
	if (!form.firstName.value)
	{
		setInputError(form.firstName);
		alert("Please supply your first name");
		return false;
	} else {
		clearInputError(form.firstName);
	}
	if (!form.lastName.value)
	{
		setInputError(form.lastName);
		alert("Please supply your last name");
		return false;
	} else {
		clearInputError(form.lastName);
	}

	// Validate email address
	var email = validEmail(form.email.value);
	if (!email)
	{
		setInputError(form.email);
		alert("Please enter a valid email address");
		return false;
	} else {
		clearInputError(form.email);
		// form.email.value = email;
	}
	
	if (form.telephone.value)
	{
		var telephone = validTelephone(form.telephone.value);
		if (!telephone)
		{
			setInputError(form.telephone);
			alert("Please enter a valid telephone number");
			return false;
		} else {
			clearInputError(form.telephone);
			form.telephone.value = telephone;
		}
	}

	if (form.state.value)
	{
		var state = validState(form.state.value);
		if (!state)
		{
			setInputError(form.state);
			alert("Please eneter a valid state");
			return false;
		} else {
			clearInputError(form.state);
			form.state.value = state;
		}
	}

	if (form.zipCode.value)
	{
		var zipCode = validZipCode(form.zipCode.value);
		if (!zipCode)
		{
			setInputError(form.zipCode);
			alert("Please enter a valid 5-digit zip code");
			return false;
		} else {
			clearInputError(form.zipCode);
		}
	}
	return true;
}
