
function findObj ( n )
{
	return document.getElementById ( n );
}

// string			R
// e-mail			RisEmail
// number			RisNum
// number range		RisRange<min>:<max>
// selection		RisSelect<min>:<max>
// password			RisPassword<fieldname>

function validateForm ()
{
	var i, p, q, nm, test, num, min, max, errors = "", args = validateForm.arguments, dname, f2;
	
	for ( i = 0; i < ( args.length - 2 ); i += 3 )
	{
		test = args [i + 2];
		val = findObj ( args [i] );
		dname = args [i + 1];
		if ( val )
		{
			nm = val.name;

			if ( ( val = val.value ) != "" )
			{
				if ( test.substr ( 0, 8 ) == "RisEmail" )
				{
					var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
					
					if (!(filter.test(val)))
					{
						errors += "- E-mail address is typed incorrectly.\n";
					}
				} else
				if ( test.substr ( 0, 9 ) == "RisSelect" )
				{
					num = parseFloat ( val );
					p = test.indexOf ( ":" );
					min = test.substring ( 9, p );
					max = test.substring ( p + 1 );
					if ( num < min || max < num )
						errors += "- " + dname + " should be selected.\n";
				} else
				if ( test.substr ( 0, 11 ) == "RisPassword" )
				{
					num = test.substring ( 11 );
					if ( num != null )
					{
						f2 = findObj ( num );
						if ( val != f2.value ) errors += "- Passwords do not match.\n";
					}
				} else
				if ( test != "R" )
				{
					num = parseFloat ( val );
					if ( isNaN ( val ) )
						errors += "- " + dname + " must contain a number.\n";
						if ( test.indexOf ( "inRange" ) != -1 )
						{
							p = test.indexOf ( ":" );
							min = test.substring ( 8, p );
							max = test.substring ( p + 1 );
							if ( num < min || max < num )
								errors += "- " + dname + " must contain a number between " + min + " and " + max + ".\n";
						}
				}
			} else
			if ( test.charAt ( 0 ) == "R" )
				errors += "- " + dname + " is required.\n"; 
		}
	}
	
	if ( errors ) alert ( "The following error(s) occurred:\n" + errors );
	return ( errors == "" );
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function isDate(dtStr){
	var dtCh= "/";
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (strMonth.length<1){
		alert("Please enter a valid month")
		return false
	}
	
	if ( strDay.length<1 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYr.length<1){
		alert("Please enter a year")
		return false
	}
	
return true
}
function dateVal()
{
	
	var date =  document.getElementById("dob_month").value + "/" + document.getElementById("dob_day").value + "/" + 
	document.getElementById("dob_year").value;
	
	if (isDate(date)==false){
		
		return false
	}
    return true
    
}


