// JavaScript Document
function checkEmpty(fieldName)
{
	if (document.getElementById(fieldName).value.replace(/^\s+/,"") != "")
	{
		return false;
	}
	return true;
}

function checkInvalidOption(selectName)
{
	if (document.getElementById(selectName).value != "Please Select")
	{
		return false;
	}
	return true;
}

function checkForm()
{
	//set up 
	var errorText = "The following required fields are empty or have invalid values: <br/><ul>";
	var errorCount = 0;
	
	//Check contact information
	if (checkEmpty("firstName"))
	{
		errorCount ++;
		errorText += "<li>First Name</li>";
	}
	if (checkEmpty("lastName"))
	{
		errorCount ++;
		errorText += "<li>Last Name</li>";
	}
	var goodEmail = document.getElementById("email").value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (!goodEmail)
	{
		errorCount ++;
		errorText += "<li>Email</li>";
	}
	if (checkEmpty("phone"))
	{
		errorCount ++;
		errorText += "<li>Phone</li>";
	}
	
	//Check company Information
	if (checkEmpty("companyName"))
	{
		errorCount ++;
		errorText += "<li>Company Name</li>";
	}
	if (checkEmpty("address1") && checkEmpty("address2"))
	{
		errorCount ++;
		errorText += "<li>Address</li>";
	}
	if (checkEmpty("city"))
	{
		errorCount ++;
		errorText += "<li>City</li>";
	}
	if (checkInvalidOption("state"))
	{
		errorCount ++;
		errorText += "<li>State</li>";
	}
	if (checkInvalidOption("country"))
	{
		errorCount ++;
		errorText += "<li>Country</li>";
	}
	if (checkEmpty("zip"))
	{
		errorCount ++;
		errorText += "<li>Postal Code</li>";
	}
	
	//Check product information
	if (checkEmpty("product"))
	{
		errorCount ++;
		errorText += "<li>Product</li>";
	}
	if (checkEmpty("dcversion"))
	{
		errorCount ++;
		errorText += "<li>Dynamic C Version</li>";
	}
/*	if (document.getElementById("purchased_from").value == "Other")
	{
		if (checkEmpty("special_text"))
		{
			errorCount ++;
			errorText += "<li>Purchased From</li>";
		}
	}
	*/
	
	var errorObj = document.getElementById("error");
	if (errorCount > 0)
	{
		
		errorObj.innerHTML = errorText + "</ul>";
		errorObj.style.display = "block";
		window.scroll(0,0);
	}
	else
	{
		errorObj.innerHTML = "";
		errorObj.style.display = "none";
		document.getElementById("reg_form").action = "confirm.php";
		document.getElementById("reg_form").submit();
	}
}

/*
function UpdateSelect()
{
	if (document.getElementById("purchased_from").value == "Other")
	{
		document.getElementById("special_text").style.display = "inline";
	}
	else
	{
		document.getElementById("special_text").style.display = "none";
	}
}
*/