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

	if (page==1)
	{
		if (theForm.req_amount.value != '')
		{
			if (!isCurrency(theForm.req_amount.value))
			{
				alert('Invalid format for Amount');
				theForm.req_amount.value='';
				theForm.req_amount.style.backgroundColor='#b2bb1c';
				theForm.req_amount.focus();
				return false;
			}
		}

		if (theForm.member_number.value != '')
		{
			if (!isNumeric(theForm.member_number.value))
			{
				alert('Invalid format for Member Number');
				theForm.member_number.value='';
				theForm.member_number.style.backgroundColor='#b2bb1c';
				theForm.member_number.focus();
				return false;
			}
		}

		if (theForm.req_zip.value != '')
		{
			if (!isZip(theForm.req_zip.value))
			{
				alert('Invalid format for Zip\n\nValid format is 12345 or 12345-6789');
				theForm.req_zip.value='';
				theForm.req_zip.style.backgroundColor='#b2bb1c';
				theForm.req_zip.focus();
				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 != '')
		{
			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.dob.value != '')
		{
			if (!isDatePast(theForm.dob.value))
			{
				alert('Invalid format for Date of Birth\n\nValid format is mm/dd/yyyy');
				theForm.dob.value='';
				theForm.dob.style.backgroundColor='#b2bb1c';
				theForm.dob.focus();
				return false;
			}
		}

		if (theForm.ssn.value != '')
		{
			if (!isSSN(theForm.ssn.value))
			{
				alert('Invalid format for Social Security Number\n\nValid format is 123-45-6789');
				theForm.ssn.value='';
				theForm.ssn.style.backgroundColor='#b2bb1c';
				theForm.ssn.focus();
				return false;
			}
		}

		if (theForm.dependents.value != '')
		{
			if (!isNumeric(theForm.dependents.value))
			{
				alert('Invalid format for # of Dependents');
				theForm.dependents.value='';
				theForm.dependents.style.backgroundColor='#b2bb1c';
				theForm.dependents.focus();
				return false;
			}
		}

		if (theForm.doh.value != '')
		{
			if (!isDatePast(theForm.doh.value))
			{
				alert('Invalid format for Date of Hire\n\nValid format is mm\\dd\\yyyy');
				theForm.doh.value='';
				theForm.doh.style.backgroundColor='#b2bb1c';
				theForm.doh.focus();
				return false;
			}
		}

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

		if (theForm.employer_zip.value != '')
		{
			if (!isZip(theForm.employer_zip.value))
			{
				alert('Invalid format for Employer Zip\n\nValid format is 12345 or 12345-6789');
				theForm.employer_zip.value='';
				theForm.employer_zip.style.backgroundColor='#b2bb1c';
				theForm.employer_zip.focus();
				return false;
			}
		}

		if (theForm.salary.value != '')
		{
			if (!isCurrency(theForm.salary.value))
			{
				alert('Invalid format for Salary');
				theForm.salary.value='';
				theForm.salary.style.backgroundColor='#b2bb1c';
				theForm.salary.focus();
				return false;
			}
		}

		if (theForm.rent_mortgage.value != '')
		{
			if (!isCurrency(theForm.rent_mortgage.value))
			{
				alert('Invalid format for Rent / Mortgage');
				theForm.rent_mortgage.value='';
				theForm.rent_mortgage.style.backgroundColor='#b2bb1c';
				theForm.rent_mortgage.focus();
				return false;
			}
		}

		if (theForm.other_income.value != '')
		{
			if (!isCurrency(theForm.other_income.value))
			{
				alert('Invalid format for Other Income');
				theForm.other_income.value='';
				theForm.other_income.style.backgroundColor='#b2bb1c';
				theForm.other_income.focus();
				return false;
			}
		}

		if (theForm.total_payments.value != '')
		{
			if (!isCurrency(theForm.total_payments.value))
			{
				alert('Invalid format for Total Payments');
				theForm.total_payments.value='';
				theForm.total_payments.style.backgroundColor='#b2bb1c';
				theForm.total_payments.focus();
				return false;
			}
		}

		if (theForm.zip0.value != '')
		{
			if (!isZip(theForm.zip0.value))
			{
				alert('Invalid format for Coap Zip\n\nValid format is 12345 or 12345-6789');
				theForm.zip0.value='';
				theForm.zip0.style.backgroundColor='#b2bb1c';
				theForm.zip0.focus();
				return false;
			}
		}

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

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

		if (theForm.dob0.value != '')
		{
			if (!isDatePast(theForm.dob0.value))
			{
				alert('Invalid format for Coap Date of Birth\n\nValid format is mm\\dd\\yyyy');
				theForm.dob0.value='';
				theForm.dob0.style.backgroundColor='#b2bb1c';
				theForm.dob0.focus();
				return false;
			}
		}

		if (theForm.ssn0.value != '')
		{
			if (!isSSN(theForm.ssn0.value))
			{
				alert('Invalid format for Coap Social Security Number');
				theForm.ssn0.value='';
				theForm.ssn0.style.backgroundColor='#b2bb1c';
				theForm.ssn0.focus();
				return false;
			}
		}

		if (theForm.dependents0.value != '')
		{
			if (!isNumeric(theForm.dependents0.value))
			{
				alert('Invalid format for Coap # of Dependents');
				theForm.dependents0.value='';
				theForm.dependents0.style.backgroundColor='#b2bb1c';
				theForm.dependents0.focus();
				return false;
			}
		}

		if (theForm.doh0.value != '')
		{
			if (!isDatePast(theForm.doh0.value))
			{
				alert('Invalid format for Coap Date of Hire\n\nValid format is mm\\dd\\yyyy');
				theForm.doh0.value='';
				theForm.doh0.style.backgroundColor='#b2bb1c';
				theForm.doh0.focus();
				return false;
			}
		}

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

		if (theForm.employer_zip0.value != '')
		{
			if (!isZip(theForm.employer_zip0.value))
			{
				alert('Invalid format for Coap Employer Zip\n\nValid format is 12345 or 12345-6789');
				theForm.employer_zip0.value='';
				theForm.employer_zip0.style.backgroundColor='#b2bb1c';
				theForm.employer_zip0.focus();
				return false;
			}
		}

		if (theForm.salary0.value != '')
		{
			if (!isCurrency(theForm.salary0.value))
			{
				alert('Invalid format for Coap Salary');
				theForm.salary0.value='';
				theForm.salary0.style.backgroundColor='#b2bb1c';
				theForm.salary0.focus();
				return false;
			}
		}

		if (theForm.rent_mortgage0.value != '')
		{
			if (!isCurrency(theForm.rent_mortgage0.value))
			{
				alert('Invalid format for Coap Rent / Mortgage');
				theForm.rent_mortgage0.value='';
				theForm.rent_mortgage0.style.backgroundColor='#b2bb1c';
				theForm.rent_mortgage0.focus();
				return false;
			}
		}

		if (theForm.other_income0.value != '')
		{
			if (!isCurrency(theForm.other_income0.value))
			{
				alert('Invalid format for Coap Other Income');
				theForm.other_income0.value='';
				theForm.other_income0.style.backgroundColor='#b2bb1c';
				theForm.other_income0.focus();
				return false;
			}
		}

		if (theForm.total_payments0.value != '')
		{
			if (!isCurrency(theForm.total_payments0.value))
			{
				alert('Invalid format for Coap Total Payments');
				theForm.total_payments0.value='';
				theForm.total_payments0.style.backgroundColor='#b2bb1c';
				theForm.total_payments0.focus();
				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.frmLoan_app.req_zip.value='xxxxx-xxxx';
		document.frmLoan_app.req_phone.value='(xxx) xxx-xxxx';
	}

	return true;
}

