// short_and_long_validation.js
// Validates both generic 'short forms' and 'long forms'.
//
// Copyright 2007 Complete Online Marketing Solutions
// Authors: Matt Szubrycht <matts@comoms.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();
	
	// 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;
	}

	if (gFormType == FORMTYPE_LONG)
	{	
		// 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;		
		}
	}
	else
	{
		// 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;		
		}
	}
	
	// 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;
	}
	else if(gFormType == FORMTYPE_LONG && gFormElem.email2 && gFormElem.email2.value != gFormElem.email.value)
	{
	    alert('Please confirm your email address.');
	    gFormElem.email2.focus();
		return false;
	}
	
	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;
}