// Validate eMail address
function ValidateEmail( str ) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
} // end function ValidateEmail(str) 


// Returns true if value is a number or is NULL otherwise returns false 
function CheckNumber( textObj )
{
	var newValue = textObj
	if (newValue.length == 0)
		return true;
// Returns true if value is a number defined as having an optional leading + or -. 
// having at most 1 decimal point.  otherwise containing only the characters 0-9. 
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	//  The first character can be + - .  blank or a digit. 
	check_char = start_format.indexOf(newValue.charAt(0))
	//  Was it a decimal? 
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < newValue.length; i++) {
		check_char = number_format.indexOf(newValue.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)	//  Second decimal 
				return false;
			else
				decimal = true;
		} else if (check_char == 0) {
			if (decimal || digits)
				trailing_blank = true;
		//  ignore leading blanks 

		} else if (trailing_blank)
			return false;
		else
			digits = true;
	}
	//  All tests passed, so... 
	return true
} //  end function CheckNumber() 


// Check integer 
function CheckInteger( textObj ) {
   // Returns true if value is a number or is NULL   otherwise returns false 

	var newValue = textObj ;
	if (newValue.length == 0)
		return true;
	// Returns true if value is an integer defined as   having an optional 
	// leading + or -. otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

	// The first character can be + -  blank or a digit 
	check_char = newValue.indexOf(decimal_format)
	// Was it a decimal? 
	if (check_char < 1)
		return CheckNumber( newValue );
	else
		return false;
} // end function CheckInteger()


// Credit card check
function ValidateCreditCard( textObj ) {
	var newValue = textObj.value ;
	var white_space = " -" ;
	var creditcard_string = "" ;
	var check_char ;

	if (newValue.length == 0) {
		return true;
	}
	
	// squish out the white space 
	for ( var i = 0; i < newValue.length; i++ ) {
		check_char = white_space.indexOf( newValue.charAt(i) )
		if (check_char < 0)
			creditcard_string += newValue.substring(i, (i + 1));
	}	

	// if all white space return error
    if ( creditcard_string.length == 0 )
        return false;
	 	
	// make sure number is a valid integer 
	if ( creditcard_string.charAt(0) == "+" )
        return false;

	if (! CheckInteger(creditcard_string) )
		return false;

    // now check mod10
	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for ( var i = 0; i < creditcard_string.length; i++ ) {
		tempdigit = eval( creditcard_string.charAt(i) )

		if ( doubledigit ) {
			tempdigit *= 2;
			checkdigit += ( tempdigit % 10 );

			if ( (tempdigit / 10) >= 1.0 ) {
				checkdigit++;
			}

			doubledigit = false;
		} else {
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
} //  end function ValidateCreditCard() 



// Check to see if an element is used in form
function getElementByName( frm, formElement ) {
	for(i=0; i<frm.elements.length; i++) {
//	alert( "frm.elements[i].name=" + frm.elements[i].name + "  \nfrm.elements[i].type=" + frm.elements[i].type )
		if (frm.elements[i].name == formElement) {
			return frm.elements[i].type ;
		}
   }
   return "undefined" ;
}



// If an element exists and is in the specified type then check validity
function ValidateForm( frm )
{
	if ( getElementByName(frm, "DojoCho") == "text") {
		if ( frm.DojoCho.value == "" ) {
			alert( "Please enter cheif instructor (dojo cho)" ) ;
			frm.DojoCho.focus() ;
			return false ;
		}
	}
		
	if ( getElementByName( frm, "Dojo") == "select-one") {
		idx = frm.Dojo.selectedIndex ;
		if ( frm.Dojo[idx].value == "" ) {
			alert( "Please choose a dojo" ) ;
			frm.Dojo.focus() ;
			return false ;
		}
	}

	if ( getElementByName(frm, "Dojo") == "text") {
		if ( frm.Dojo.value == "" ) {
			alert( "Please enter the dojo name" ) ;
			frm.Dojo.focus() ;
			return false ;
		}
	}
			
	if ( getElementByName(frm, "FirstName") == "text") {
		if ( frm.FirstName.value == "" ) {
			alert( "Please enter the your first name" ) ;
			frm.FirstName.focus() ;
			return false ;
		}
	}
		
	if ( getElementByName(frm, "LastName") == "text") {
		if ( frm.LastName.value == "" ) {
			alert( "Please enter the your last name" ) ;
			frm.LastName.focus() ;
			return false ;
		}
	}
			
	if ( getElementByName( frm, "Address1") == "text") {
		if ( frm.Address1.value == "" ) {
			alert( "Please enter the your address" ) ;
			frm.Address1.focus() ;
			return false ;
		}
	}

	if ( getElementByName( frm, "City") == "text") {
		if ( frm.City.value == "" ) {
			alert( "Please enter the your city" ) ;
			frm.City.focus() ;
			return false ;
		}
	}

	if ( getElementByName(frm, "State") == "select-one") {
		idx = frm.State.selectedIndex ;
		if ( frm.State[idx].value == "" ) {
			alert( "Please identify your state" ) ;
			frm.State.focus() ;
			return false ;
		}
	}

	if ( getElementByName( frm, "ZIP") == "text") {
		if ( frm.ZIP.value == "" ) {
			alert( "Please enter the your ZIP" ) ;
			frm.ZIP.focus() ;
			return false ;
		}
	}

	if ( getElementByName( frm, "Phone") == "text") {
		if ( frm.Phone.value == "" ) {
			alert( "Please enter the your phone number" ) ;
			frm.Phone.focus() ;
			return false ;
		}
	}

	
	if ( getElementByName(frm, "Email") == "text") {
		if ( frm.Email.value == "" || !ValidateEmail( frm.Email.value ) ) {
			alert( "Please enter your eMail address" ) ;
			frm.Email.focus() ;
			return false ;
		}
	}
	
	if ( getElementByName(frm, "Rank") == "select-one") {
		idx = frm.Rank.selectedIndex ;
		if ( frm.Rank[idx].value == "" ) {
			alert( "Please choose a member rank" ) ;
			frm.Rank.focus() ;
			return false ;
		}
	}
	
	if ( getElementByName(frm, "Attendance") == "radio") {
		for (x=0; x < frm.Attendance.length; x++) {
			if ( frm.Attendance[x].checked )
				break ;
		}
		if ( x >= frm.Attendance.length ) {
			alert( "Please choose which days you will attend" ) ;
			return false ;
		}
	}
	

	if ( getElementByName(frm, "MembershipType") == "radio") {
		for (x=0; x < frm.MembershipType.length; x++) {
			if ( frm.MembershipType[x].checked ) {
				break ;
			}
		}
		if ( x >= frm.MembershipType.length ) {
			alert( "Please choose the type of dojo membership which you are applying" ) ;
			return false ;
		}
	}

	
	if ( getElementByName(frm, "MembershipAction") == "radio") {
		for (x=0; x < frm.MembershipAction.length; x++) {
			if ( frm.MembershipAction[x].checked ) {
				actn = frm.MembershipAction[x].value ;
				break ;
			}
		}
		if ( x >= frm.MembershipAction.length ) {
			alert( "Please choose a membership action (new/renewal/info)" ) ;
			return false ;
		}
	}


	// If there is a membership application radio button and the choice is not "info only" then collect payment	
	if ( ( getElementByName(frm, "MembershipAction") != "radio" ) 
		|| ( getElementByName(frm, "MembershipAction") == "radio" &&  actn != "Info only" ) ) {

		if ( getElementByName(frm, "Payment") == "radio") {
			for (x=0; x < frm.Payment.length; x++) {
				if ( frm.Payment[x].checked ) {
					var pmt = frm.Payment[x].value
					break ;
				}
			}
			if ( x >= frm.Payment.length ) {
				alert( "Please choose a Payment method" ) ;
				return false ;
			}
		
			// Check credit card info if that is payment option
			if ( pmt == "VISA"  || pmt == "MasterCard" || pmt == "Discover" ) {
				if ( frm.CCNumber.value == ""   ||   !ValidateCreditCard( frm.CCNumber ) ) {
					alert( "Please enter your credit card number or choose another payment option" ) ;
					frm.CCNumber.focus() ;
					return false ;
				}
				
				if ( frm.CCExpMM[frm.CCExpMM.selectedIndex].value == "" ) {
					alert( "Please provide credit card expiration month" ) ;
					frm.CCExpMM.focus() ;
					return false ;
				}
			
				if ( frm.CCExpYYYY[frm.CCExpYYYY.selectedIndex].value == "" ) {
					alert( "Please provide credit card expiration year" ) ;
					frm.CCExpYYYY.focus() ;
					return false ;
				}
				if ( frm.CCZIP.value == "" ) {
					alert( "Please enter your credit card billing ZIP code or choose another payment option" ) ;
					frm.CCZIP.focus() ;
					return false ;
				}
				if ( !frm.PaymentAgreement.checked == 1 ) {
					alert( "Please agree to payment terms or choose another payment option" ) ;
					frm.PaymentAgreement.focus() ;
					return false ;
				}
			}
		} // endif ( getElementByName(frm, "Payment") == "radio")
	}
	
	return true ;
}

