/*
Global Business Program Form Validation and then some.
*/

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function submit1_onclick()

 {
	// Check that the contact name is there
	if (LEN(Trim(document.forms['ctl00'].contactname.value)) <=  1)
		{
			alert("Contact name is required");
			return false;
		}

	// Check that the company/org name is there
	if (LEN(Trim(document.forms['ctl00'].company.value)) <=  1)
		{
			alert("Company/Organization name is required");
			return false;
		}

	// Check the address
	if (LEN(Trim(document.forms['ctl00'].address.value)) <= 1 )
		{
			alert("Address is required");
			return false;
		}

	// Check the city
	if (LEN(Trim(document.forms['ctl00'].city.value)) < 2 )
		{
			alert("City is required");
			return false;
		}

	// Check the state
	if (LEN(Trim(document.forms['ctl00'].state.value)) < 2 )
		{
			alert("State is required");
			return false;
		}

	// Check the zip code
	if (LEN(Trim(document.forms['ctl00'].zip.value)) < 5 )
		{
			alert("Zip code is required");
			return false;
		}
	
	// Check that the primary telephone number exists
	if (LEN(Trim(document.forms['ctl00'].phone_primary.value)) <= 1 )
		{
			alert("A primary telephone number is required");
			return false;
		}		

	// Check that the secondary telephone number exists
	if (LEN(Trim(document.forms['ctl00'].phone_secondary.value)) <= 1 )
		{
			alert("A secondary telephone number is required");
			return false;
		}		

	// Check the email address
	if (LEN(Trim(document.forms['ctl00'].email.value)) <= 1 )
		{
			alert("An email address is required");
			return false;
		}

	// Make sure the email is valid
	if (!check_email(document.forms['ctl00'].email.value))
		{
			alert("Invalid email format");
			return false;
		}

	// Check that they have given the industry type
	if (LEN(Trim(document.forms['ctl00'].industry.value)) <= 1 )
		{
			alert("The industry your company/organization is in is required");
			return false;
		}

}
function check_email(e)
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
		{
			return (false);
		}
	}
	if (document.images)
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two))
		{
			return (-1);
		}

	}
}

function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
	  // We have a string with leading blank(s)...

	  var j=0, i = s.length;

	  // Iterate from the far left of string until we
	  // don't have any more whitespace...
	  while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		 j++;

	  // Get the substring from the first non-whitespace
	  // character to the end of the string...
	  s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	  // We have a string with trailing blank(s)...
	  var i = s.length - 1;	   // Get length of string
	  // Iterate from the far right of string until we
	  // don't have any more whitespace...
	  while (i > 0 && whitespace.indexOf(s.charAt(i)) != -1)
		 i--;
	  // Get the substring from the front of the string to
	  // where the last non-whitespace character is...
	  s = s.substring(0, i+1);
   }

   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

function LEN(str)
{
	var s = new String(str);
	var i = s.length; //Get the length of the veriable
	return i;
}

// validate SPA form
function validateForm()
{
	var formobj = document.spaform;
	if (formobj.first.value == '' || formobj.last.value == '' || formobj.email.value == '' || formobj.phone.value == '' || formobj.time.selectedIndex == 0 || formobj.therapist_pref[0].checked == false && formobj.therapist_pref[1].checked == false && formobj.therapist_pref[2].checked == false || formobj.payment.selectedIndex == 0)
	{
		document.getElementById("required").innerHTML = 
			"Please be sure to fill in all fields.";
		window.scrollTo(0,0);
		return false;
	}
	else if (formobj.email.value != formobj.cemail.value)
	{
		document.getElementById("required").innerHTML = 
			"The emails you provided do not match.";
		window.scrollTo(0,0);
		return false;
	}
	else
	{
		return true;
	}
}

// validate Talk To Us form
function validateTalk()
{
	var formobj = document.talktous;
	if (formobj.firstname.value == '' || formobj.lastname.value == '' || formobj.email.value == '' || formobj.comment.value == '')
	{
		document.getElementById("required").innerHTML = 
				"Please be sure to fill in all fields.";
		window.scrollTo(0,0);
		return false;
	}
	else if (formobj.email.value != formobj.confirm_email.value)
	{
		document.getElementById("required").innerHTML = 
				"The emails you provided do not match.";
		window.scrollTo(0,0);
		return false;
	}
	else
	{
		return true;
	}
}

// validate Talk To Us form
function validateGBP()
{
	var formobj = document.ct100;
	if (formobj.contactname.value == '' || formobj.company.value == '' || formobj.address.value == '' || formobj.city.value == '' || formobj.city.value == '' || formobj.industry.value == '' || formobj.state.value == '' || formobj.zip.value == ''|| formobj.industry.value == '' || formobj.phone_primary.value == ''|| formobj.phone_secondary.value == ''|| formobj.email.value == '')
	{
		document.getElementById("required").innerHTML = 
				"Please be sure to fill in all fields.";
		window.scrollTo(0,0);
		return false;
	}
	else
	{
		return true;
	}
}