function validate(theForm,page)
{
	if (!checkRequired(theForm))
		return false;

	if (page==1)
	{
		if (theForm.req_fname.value=='')
		{
			theForm.req_fname.focus();
			theForm.req_fname.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for First name');
			return false;
		}

		if (theForm.req_lname.value=='')
		{
			theForm.req_lname.focus();
			theForm.req_lname.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Last name');
			return false;
		}

		if (theForm.req_address.value=='')
		{
			theForm.req_address.focus();
			theForm.req_address.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Address');
			return false;
		}

		if (theForm.req_city.value=='')
		{
			theForm.req_city.focus();
			theForm.req_city.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for City');
			return false;
		}

		if (theForm.req_state.value=='')
		{
			theForm.req_state.focus();
			theForm.req_state.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for State');
			return false;
		}

		if (theForm.req_zip.value=='')
		{
			theForm.req_zip.focus();
			theForm.req_zip.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Zip');
			return false;
		}

		if (theForm.req_zip.value != '')
		{
			if (!isZip(theForm.req_zip.value))
			{
				alert('Invalid format for Zip');
				theForm.req_zip.value='';
				theForm.req_zip.style.backgroundcolor='#b2bb1c';
				theForm.req_zip.focus();
				return false;
			}
		}

		if (theForm.req_phone.value=='')
		{
			theForm.req_phone.focus();
			theForm.req_phone.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Phone');
			return false;
		}

		if (theForm.req_phone.value != '')
		{
			if (!isPhone(theForm.req_phone.value))
			{
				alert('Invalid format for Phone');
				theForm.req_phone.value='';
				theForm.req_phone.style.backgroundcolor='#b2bb1c';
				theForm.req_phone.focus();
				return false;
			}
		}

		if (theForm.req_email.value=='')
		{
			theForm.req_email.focus();
			theForm.req_email.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Email');
			return false;
		}

		if (theForm.req_email.value != '')
		{
			if (!isEmail(theForm.req_email.value))
			{
				alert('Invalid format for Email');
				theForm.req_email.value='';
				theForm.req_email.style.backgroundcolor='#b2bb1c';
				theForm.req_email.focus();
				return false;
			}
		}

		if (theForm.req_ssn.value=='')
		{
			theForm.req_ssn.focus();
			theForm.req_ssn.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for SSN');
			return false;
		}

		if (theForm.req_ssn.value != '')
		{
			if (!isSSN(theForm.req_ssn.value))
			{
				alert('Invalid format for SSN');
				theForm.req_ssn.value='';
				theForm.req_ssn.style.backgroundcolor='#b2bb1c';
				theForm.req_ssn.focus();
				return false;
			}
		}

		if (theForm.req_dob.value=='')
		{
			theForm.req_dob.focus();
			theForm.req_dob.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Date of Birth');
			return false;
		}

		if (theForm.req_dob.value != '')
		{
			if (!isDatePast(theForm.req_dob.value))
			{
				alert('Invalid format for Date of Birth');
				theForm.req_dob.value='';
				theForm.req_dob.style.backgroundcolor='#b2bb1c';
				theForm.req_dob.focus();
				return false;
			}
		}

		if (theForm.req_maiden_name.value=='')
		{
			theForm.req_maiden_name.focus();
			theForm.req_maiden_name.style.backgroundcolor='#b2bb1c';
			alert('Please enter a value for Mother\'s Maiden Name');
			return false;
		}

	}

	return true;
}

function checkRequired(theForm)
{
	var children = theForm.elements;
	var req_test = /^(req_)[a-zA-Z0-9_]*/i;
	var i;
	var name;
	var id;
	var out = "";
	for( i = 0; i < children.length; i++)
	{
		name = children[i].name;
		id   = children[i].id;
		if(req_test.test(name))
		{
			if(!valueTest(children[i].value))
			{
				if (id != '')
					document.getElementById(id).style.backgroundColor = '#b2bb1c';
				else if (name != '')
					eval('theForm.'+name+'.style.backgroundColor = \'#b2bb1c\'');					
				var empty = true;
			}
		}
	}

	if (empty)
	{
		alert('Please enter all required information');
		return false;
	}
	else
	{
		return true;
	}
}

function valueTest(val)
{
	var defaults = new Array("mm/dd/yyyy","xxx-xx-xxxx","(xxx)xxx-xxxx", "xxxxx-xxxx");
	val = val.replace(' ', '');
	if ((val == null || val.length == 0) || in_array(val,defaults))
		return false;
	else
		return true;
}

function in_array(needle, haystack)
{
    var found = false, key;
 
    for (key in haystack)
    {
        if (haystack[key] === needle)
        {
            found = true;
            break;
        }
    }
 
    return found;
}

function isNumeric(input)
{
	var ValidChars = "0123456789";
	
	for (i=0; i<input.length; i++) 
	{ 
		if (ValidChars.indexOf(input.charAt(i)) == -1) 
		{
			return false;
		}
	}
	return true;
}

function isZip(input)
{
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (input.length!=5 && input.length!=10)
		return false;
	
	for (var i=0; i < input.length; i++)
	{
		temp = "" + input.substring(i, i+1);
		if (temp == "-")
			hyphencount++;
			
		if (valid.indexOf(temp) == "-1")
			return false;
		if ((hyphencount > 1) || ((input.length==10) && ""+input.charAt(5)!="-"))
			return false;
	}
	return true;
}

function isCurrency(input) 
{ 
	var Chars = "0123456789.,$"; 
	for (var i = 0; i < input.length; i++) 
	{ 
		if (Chars.indexOf(input.charAt(i)) == -1) 
			return false; 
	} 
	return true;
}

function isSSN(input)
{
	var matchArr 	= input.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes 	= input.split('-').length - 1;
	if (matchArr == null || numDashes == 1)
		return false;
	else if (parseInt(matchArr[1],10)==0)
		return false;

	return true;
}

function isEmail(input)
{
	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=input.match(emailPat);
	
	if (matchArray==null)
		return false;

	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++)
	{
		if (user.charCodeAt(i)>127)
			return false;
	}
	
	for (i=0; i<domain.length; i++)
	{
		if (domain.charCodeAt(i)>127)
			return false;
	}
	
	// See if "user" is valid 
	if (user.match(userPat)==null)
		return false;
	
	/* If the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
				return false;
		}
	}
	
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++)
	{
		if (domArr[i].search(atomPat)==-1)
			return false;
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	if (domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
		return false;
	
	// Make sure there's a host name preceding the domain.
	if (len<2)
		return false;
	
	// If we've gotten this far, everything's valid!
	return true;
}

function isPhone(input)
{
	var stripped = input.replace(/[\(\)\.\-\ ]/g, ''); 
	
	if (isNaN(parseInt(stripped)))
		return false;
	else if (!(stripped.length == 10))
		return false;

	return true;
}

//Generic date function
function isDate(input)
{
	var datePat 	= /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray 	= input.match(datePat); // is the format ok?
	
	if (matchArray == null)
		return false;
	
	month 	= matchArray[1]; 
	day 	= matchArray[3];
	year 	= matchArray[5];
	
	if (month < 1 || month > 12)
		return false;
	
	if (day < 1 || day > 31)
		return false;
	
	//Thirty days hath September, April, June and November...
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
		return false;
	
	//All the rest have thirty-one, except for February...
	if (month == 2)
	{ 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
			return false;
	}
	
	return true;
}

//Future date function (expiration dates, date due, etc.)
function isDateFuture(input)
{
	var datePat 	= /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray 	= input.match(datePat); // is the format ok?
	
	if (matchArray == null)
		return false;
	
	month 	= matchArray[1]; 
	day 	= matchArray[3];
	year 	= matchArray[5];
	
	if (month < 1 || month > 12)
		return false;
	
	if (day < 1 || day > 31)
		return false;
	
	//Thirty days hath September, April, June and November...
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
		return false;
	
	//All the rest have thirty-one, except for February...
	if (month == 2)
	{ 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
			return false;
	}
	
	//Get today's date
	var today 		= new Date();
	var thisYear 	= today.getFullYear();
	var thisMonth 	= today.getMonth();
	var thisDay 	= today.getDate();
	
	if (year<thisYear)
		return false;
	else if (year==thisYear && month<thisMonth)
		return false;
	else if (year==thisYear && month==thisMonth && day<thisDay)
		return false;
	
	return true;
	
	return true;
}

//Past date function (birthday, etc.)
function isDatePast(input)
{
	var datePat 	= /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray 	= input.match(datePat); // is the format ok?
	
	if (matchArray == null)
		return false;
	
	month 	= matchArray[1]; 
	day 	= matchArray[3];
	year 	= matchArray[5];
	
	if (month < 1 || month > 12)
		return false;
	
	if (day < 1 || day > 31)
		return false;
	
	//Thirty days hath September, April, June and November...
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
		return false;
	
	//All the rest have thirty-one, except for February...
	if (month == 2)
	{ 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
			return false;
	}
	
	//Get today's date
	var today 		= new Date();
	var thisYear 	= today.getFullYear();
	var thisMonth 	= today.getMonth();
	var thisDay 	= today.getDate();
	
	if (year>thisYear)
		return false;
	else if (year==thisYear && month>thisMonth)
		return false;
	else if (year==thisYear && month==thisMonth && day>thisDay)
		return false;	
	
	return true;
}

function checkRadioButton(input,name)
{
	var checked = false;
	
	for (var i=0; i<input.length; i++)
	{ 
		if (input[i].checked)
			checked = true;
	} 
	
	if (checked == false)
	{
		alert('Please make a selection for '+name);
		return false;
	}
	else
		return true;
}

function checkCheckBox(input,name)
{
	var checked = false;
	
	if (input.checked == false)
	{
		alert('Please make a selection for '+name);
		return false;
	}
	else
		return true;
}

function isCreditCard(input)
{
	/*this function uses the Luhn Algorithm*/

	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < input.length; i++)
	{
		x = input.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7)
		return false;

	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++)
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	
	for (i=0; i<k+m; i++) 
		c += w.charAt(i*2+1-m) * 1;

	if (c%10==0)
		return true;
	else
		return false;
}

function setDefault(page)
{
	if (page==1)
	{
		document.frmIndex.req_zip.value='xxxxx-xxxx';
		document.frmIndex.req_phone.value='(xxx) xxx-xxxx';
		document.frmIndex.req_ssn.value='xxx-xx-xxxx';
		document.frmIndex.req_dob.value='mm/dd/yyyy';
	}

	return true;
}

