// visitor_info.js
// Contains helper functions for the VisitorInfoTag.java class.

// Validates the contact_us form on the contact_us.do page
function validateForm(formElem)
{
	if (!formElem) 
	{
		return false;
	}
	
	var isValid = true;		
	var errorStr = "";
	
	for (var i=0; i<formElem.elements.length; i++)
	{
		var elem = formElem.elements[i];
		if (elem && elem.className.indexOf("required") > -1)
		{
			if (isElementEmpty(elem))
			{
				errorStr += "The " + generateElementDesc(elem.name) + " field is required.\n";
			}
			else if (elem.name == "vi_emailaddress" && !isValidEmail(elem))
			{
				errorStr += elem.value + " is an invalid email address.\n";
			}
			else if (elem.name == "vi_mainphone" && !isValidPhone(elem))
			{
				errorStr += elem.value + " is an invalid phone number.\n"
			}
		}
	}
	
	if (errorStr.length > 0)
	{
		isValid = false;
		alert("The following errors occurred when submitting the form:\n\n" + errorStr);
	}
	
	return isValid;
}

// Checks to see if a form element has a value or not.
function isElementEmpty(elem)
{
	if (elem && elem.value.length == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Validates an email address.
function isValidEmail(elem)
{
	if (!elem)
	{
		return false;
	}
	
	var isValid = true;
	var str = elem.value;	
	//var re = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;

	if (!str.match(re))
	{
		isValid = false;
	}
	return isValid;
}

// Validates a phone number.
function isValidPhone(elem)
{
	if (!elem)
	{
		return false;
	}
	
	var isValid = true;
	var str = elem.value;
	//var re = /^([0-9]{11})$/;
	re = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
	
	if (!str.match(re))
	{
		isValid = false;
	}
	return isValid;
}
 
// Maps the id/name of a contact_us form element to a nice title which 
// can be used to when printing out an error message to the user. 
function generateElementDesc(name)
{
	if (!name)
	{
		return;
	}
	
	switch (name)
	{
		case "vi_subject":
			return "Subject";			
		case "vi_firstname":
			return "First Name";
		case "vi_lastname":
			return "Last Name";
		case "vi_address1":
			return "Address1";
		case "vi_address2":
			return "Address2";
		case "vi_city":
			return "City";
		case "vi_stateprovince":
			return "State / Province";
		case "vi_zippostalcode":
			return "Zip / Postal Code";
		case "vi_country":
			return "Country";
		case "vi_mainphone":
			return "Phone Number";
		case "vi_emailaddress":
			return "Email Address";
		case "vi_besttime":
			return "Best Time to Contact";
		case "vi_commentsquestions":
			return "Comments / Questions";
	}
}

// Toggles the state dropdown select box depending on the value of the
// selected option in the country dropdown select box.
function toggleStateDropDown()
{
	var countryElem = document.getElementById("vi_country");
	var stateRow = document.getElementById("row_stateprovince");
	
	if (countryElem && stateRow)
	{
		var value = countryElem.options[countryElem.selectedIndex].value;
		
		// If the user is  changing the country dropdown to US or CA from something else and the state dropdown is not visible,
		// make the state dropdown visible.
		if (value == "US" || value == "CA")
		{
			stateRow.style.display = "block";
			//alert(stateRow.style.display);
		}
		
		// If the user is changing the country dropdown from US or CA to something else and the state dropdown is visible,
		// hide the state dropdown.
		else if (value != "US" || value != "CA")
		{
			stateRow.style.display = "none";
			//alert(stateRow.style.display);
		}
	}
} 