// short_and_long_validation.js
// Validates both generic 'short forms' and 'long forms'.
//
// Copyright 2007-2009 Complete Online Marketing Solutions
// Authors: Matt Szubrycht <matts@bmihosting.com>, Roy Laurie <rlaurie@binarycult.com>
//
// Requires MooTools library.

var gFormType = 0;
var gFormElem = null;

function checkform() {} // override depreciated onSubmit attempts

window.addEvent('domready', function()
{

	var FORMTYPE_UNKNOWN	= 0;
	var FORMTYPE_SHORT		= 1;
	var FORMTYPE_LONG		= 2;



	// Determine the gFormType by checking to see which id exists.
	if ($('shortform'))  {
		gFormType = FORMTYPE_SHORT
		gFormElem = $('shortform');
	}
	else if ($('form1')) {
		gFormType = FORMTYPE_LONG;
		gFormElem = $('form1');
	}


	if (gFormElem)

	{
		gFormElem.onSubmit = null; // html-loaded events

		gFormElem.addEvent('submit', function(e)
		{
		(new Event(e)).stop();


		if (gFormType == FORMTYPE_LONG)
		{

			// First name required.
			if (gFormElem.first_name.value.length < 2 || gFormElem.first_name.value == '')
			{
				alert('Please enter your First Name');
				gFormElem.first_name.focus();
				return false;
			}

			// Last name required.
			if (gFormElem.last_name.value.length < 2 || gFormElem.last_name.value == '')
			{
				alert('Please enter your Last Name');
				gFormElem.last_name.focus();
				return false;
			}



			// At least one phone number is required.
			if(gFormElem.homephone.value != '' || gFormElem.homephone2.value != '' || gFormElem.homephone3.value != '')
			{
				if(!areDigits(gFormElem.homephone.value) || gFormElem.homephone.value.length != 3)
				{
					alert('Please verify you are entering the home phone number correctly. Do not use dashes or parentheses.');
					gFormElem.homephone.focus();
					return false;
				}
				else if(!areDigits(gFormElem.homephone2.value) || gFormElem.homephone2.value.length != 3)
				{
					alert('Please verify you are entering the home phone number correctly. Do not use dashes or parentheses.');
					gFormElem.homephone2.focus();
					return false;
				}
				else if(!areDigits(gFormElem.homephone3.value) || gFormElem.homephone3.value.length != 4)
				{
					alert('Please verify you are entering the home phone number correctly. Do not use dashes or parentheses.');
					gFormElem.homephone3.focus();
					return false;
				}
			}
			else if(gFormElem.cellphone.value != '' || gFormElem.cellphone2.value != '' || gFormElem.cellphone3.value != '')
			{
				if(!areDigits(gFormElem.cellphone.value) || gFormElem.cellphone.value.length != 3)
				{
					alert('Please enter a complete cell phone number with area code. Do not use dashes or parentheses.');
					gFormElem.cellphone.focus();
					return false;
				}
				else if(!areDigits(gFormElem.cellphone2.value) || gFormElem.cellphone2.value.length != 3)
				{
					alert('Please verify you are entering the cell phone number correctly. Do not use dashes or parentheses.');
					gFormElem.cellphone2.focus();
					return false;
				}
				else if(!areDigits(gFormElem.cellphone3.value) || gFormElem.cellphone3.value.length != 4)
				{
					alert('Please verify you are entering the cell phone number correctly. Do not use dashes or parentheses.');
					gFormElem.cellphone3.focus();
					return false;
				}
			}
			else if(gFormElem.workphone.value != '' || gFormElem.workphone2.value != '' || gFormElem.workphone3.value != '')
			{
				if(!areDigits(gFormElem.workphone.value) || gFormElem.workphone.value.length != 3)
				{
					alert('Please enter a complete work phone number with area code. Do not use dashes or parentheses.');
					gFormElem.workphone.focus();
					return false;
				}
				if(!areDigits(gFormElem.workphone2.value) || gFormElem.workphone2.value.length != 3)
				{
					alert('Please verify you are entering the work phone number correctly. Do not use dashes or parentheses.');
					gFormElem.workphone2.focus();
					return false;
				}
				if(!areDigits(gFormElem.workphone3.value) || gFormElem.workphone3.value.length != 4)
				{
					alert('Please verify you are entering the work phone number correctly. Do not use dashes or parentheses.');
					gFormElem.workphone3.focus();
					return false;
				}
			}
			else // User didn't input a phone number at all
			{
				alert('Please enter at least one phone number you can be reached at.');
				gFormElem.homephone.focus();
				return false;
			}



			// sero - specific LONGFORM fields
			// - contact + qualif

			// contact info


			// Valid email address is required.
			if(gFormElem.email.value == '')
			{
				alert('Please enter your email address');
				gFormElem.email.focus();
				return false;
			}
			else if(gFormElem.email.value.indexOf('@') == -1)
			{
				alert('Your email address isn\'t formatted correctly');
				gFormElem.email.focus();
				return false;
			}
			else if(gFormElem.email.value.indexOf(',') != -1) {
				alert('Your email address is formatted incorrectly');
				gFormElem.email.focus();
				return false;
			}
			else if( gFormElem.email.value.length == ( gFormElem.email.value.indexOf('@') + 1 ) )
			{
				alert('Your email address is formatted incorrectly');
				gFormElem.email.focus();
				return false;
			}

			// street address required.
			if (gFormElem.address.value.length < 2 || gFormElem.address.value == '')
			{
				alert('Please provide street address');
				gFormElem.address.focus();
				return false;
			}

			// city required.
			if (gFormElem.city.value.length < 2 || gFormElem.city.value == '')
			{
				alert('Please enter your city');
				gFormElem.city.focus();
				return false;
			}

			// state required.
			if (gFormElem.state.value.length < 2 || gFormElem.state.value == '')
			{
				alert('Please select your state');
				gFormElem.state.focus();
				return false;
			}

			// state required.
			if (gFormElem.zip.value.length < 5 || gFormElem.zip.value == '')
			{
				alert('Please enter your zip code');
				gFormElem.zip.focus();
				return false;
			}



			// sero qualif


			// drug taken before 12-31-2005 required.
			if (gFormElem.before_05dec31.value.length < 2 || gFormElem.before_05dec31.value == '')
			{
				alert('When did you take Seroquel?');
				gFormElem.before_05dec31.focus();
				return false;
			}

			// diagnosed_diabetes
			if(gFormElem.diagnosed_diabetes.value.length < 2 || gFormElem.diagnosed_diabetes.value == '')
			{
				alert('Was Diabetes diagnosed?');
				gFormElem.diagnosed_diabetes.focus();
				return false;
			}

			// diagnosed_pancreatitis required.
			if (gFormElem.diagnosed_pancreatitis.value.length < 2 || gFormElem.diagnosed_pancreatitis.value == '')
			{
				alert('Was Pancreatitis diagnosed?');
				gFormElem.diagnosed_pancreatitis.focus();
				return false;
			}

		} // end long form sero

		else  // short form
		{
			// First name required.
			if (gFormElem.first_name.value.length < 2 || gFormElem.first_name.value == '')
			{
				alert('Please enter your First Name');
				gFormElem.first_name.focus();
				return false;
			}

			// Valid email address is required.
			if(gFormElem.email.value == '')
			{
				alert('Please enter your email address');
				gFormElem.email.focus();
				return false;
			}
			else if(gFormElem.email.value.indexOf('@') == -1)
			{
				alert('Your email address isn\'t formatted correctly');
				gFormElem.email.focus();
				return false;
			}
			else if(gFormElem.email.value.indexOf(',') != -1) {
				alert('Your email address is formatted incorrectly');
				gFormElem.email.focus();
				return false;
			}
			else if( gFormElem.email.value.length == ( gFormElem.email.value.indexOf('@') + 1 ) )
			{
				alert('Your email address is formatted incorrectly');
				gFormElem.email.focus();
				return false;
			}

			// Phone number is required.
			if(gFormElem.phone.value != '' || gFormElem.phone2.value != '' || gFormElem.phone3.value != '')
			{
				if(!areDigits(gFormElem.phone.value) || gFormElem.phone.value.length != 3)
				{
					alert('Please enter at least one complete phone number with area code. Do not use dashed or parentheses.');
					gFormElem.phone.focus();
					return false;
				}
				if(!areDigits(gFormElem.phone2.value) || gFormElem.phone2.value.length != 3)
				{
					alert('Please verify you are entering the phone number correctly. Do not use dashed or parentheses.');
					gFormElem.phone2.focus();
					return false;
				}
				if(!areDigits(gFormElem.phone3.value) || gFormElem.phone3.value.length != 4)
				{
					alert('Please verify you are entering the phone number correctly. Do not use dashed or parentheses.');
					gFormElem.phone3.focus();
					return false;
				}
			}
			else // User didn't input a phone number at all
			{
				alert('Please enter at least one phone number you can be reached at.');
				gFormElem.phone.focus();
				return false;
			}
		} // end short form


		gFormElem.submit();

		return true;
		});
	}

});



// Checks to see whether x is a string of digits
function areDigits(x)
{
	var i;
	var n;

	for (i = 0, n = x.length; i < n; ++i)
	{
		if (!isInt(x.substr(i, 1)))  {
			return false;
		}
	}

	return true;
}


// Checks to see whether X is an integer.
function isInt(x)
{
	var y = x;

	parseInt(x);
	if (isNaN(y))
	return false;

	return ( x == y && x.toString() == y.toString() );
}


function autotab(fn, fl)
{
	var next = 0;
	var autoComplete;

	for (var i = 0; i < gFormElem.length; ++i)
	{
		if (gFormElem.elements[i].name == fn.name)
		break;
	}

	if (i >= gFormElem.length)
	return;

	next = i + 1;

	if (next >= gFormElem.length)
	next = 0;

	if(gFormElem.elements[i].value.length == fl)
	{
		//autoComplete = gFormElem.elements[next].getAttribute('autocomplete');
		gFormElem.elements[next].setAttribute('autocomplete', 'off');

		gFormElem.elements[next].focus();

		//gFormElem.elements[next].setAttribute('autocomplete', autoComplete);
	}

	return;
}
