function totalcalc(theForm) 
{
	var plantype = theForm.memtype.value;

	if ((plantype != "none")) 
	{
		var planinfo = plantype.match(/\$\d+\.\d+/i);
		planinfo = planinfo.toString();

		var pprice = planinfo.substring(1);
	
		if (!isNaN(parseFloat(pprice))) 
		{
			theForm.totalprice.value = planinfo;
		}
	}

}



function fbn(buttonname) {
    button = buttonname;
}

function validRequired(formField,fieldLabel)
{
    var result = true;
    
    if (formField.value == "")
    {
        alert('Please enter a value for the "' + fieldLabel +'" field.');
        formField.focus();
        result = false;
    }
    
    return result;
}

function allDigits(str)
{
    return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
    var result = true;

    // Note: doesn't use regular expressions to avoid early Mac browser bugs    
    for (var i=0;i<str.length;i++)
        if (charset.indexOf(str.substr(i,1))<0)
        {
            result = false;
            break;
        }
    
    return result;
}

function validNum(formField,fieldLabel,required)
{
    var result = true;

    if (required && !validRequired(formField,fieldLabel))
        result = false;
  
     if (result)
     {
         if (!allDigits(formField.value))
         {
            alert('Please enter a number for the "' + fieldLabel +'" field.');
            formField.focus();        
            result = false;
        }
    } 
    
    return result;
}


function Form1_Validator(theForm)
{

var alertsay = "";

	// check if a enrolltype has been selected
	if (theForm.memtype.value == "none")
	{
	alert("Please select a Plan Type.");
	theForm.memtype.focus();
	return (false);
	}

	// check if an activation period has been selected
/*	if (theForm.actdate.value == "none")
	{
	alert("Please select a Plan Activation Period.");
	theForm.actdate.focus();
	return(false);
	}
*/	
	// check to see if FirstName is blank
	if (theForm.FirstName.value == "")
	{
	alert("You must enter a First Name.");
	theForm.FirstName.focus();
	return (false);
	}

	// check to see if LastName is blank
	if (theForm.LastName.value == "")
	{
	alert("You must enter a Last Name.");
	theForm.LastName.focus();
	return (false);
	}


	// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = theForm.email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
		  if (EmailAt && EmailPeriod)
			break;
		  if (j == checkEmail.length)
			break;
	}
		// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
			EmailValid = true
			break;
	}
	}


	if (!EmailValid)
	{
	alert("Please enter a valid email address");
	theForm.email.focus();
	return (false);
	}
	
	// check to see if SSN is not number
	//if (theForm.ssn.value == "") 
	//{
	//alert("You must enter your SSN.");
	//theForm.ssn.focus();
	//return (false);
	//}
	
	if ((theForm.ssn.value == ""))
    {
    alert('You must enter a SSN.');
    theForm.ssn.focus();        
    return(false);
    }
	//if (!validNum(theForm.ssn,"Social Security Number",true)) {
    //    return (false);
	//}
	
	// check to see if PhoneNUmber is blank
	if (theForm.homephone.value == "")
	{
	alert("You must enter a Phone Number.");
	theForm.homephone.focus();
	return (false);
	}



	// check to see if StreetAddress1 is blank
	if (theForm.StreetAddress1.value == "")
	{
	alert("You must enter an Address.");
	theForm.StreetAddress1.focus();
	return (false);
	}

	
	// check to see if City is blank
	if (theForm.City.value == "")
	{
	alert("You must enter a City.");
	theForm.City.focus();
	return (false);
	}


	// check to see if State is blank
	if (theForm.State.value == "99")
	{
	alert("Please select a State");
	theForm.State.focus();
	return (false);
	}


	// check to see if PostalCode is blank
	if (theForm.PostalCode.value == "")
	{
	alert("You must enter a Zip Code.");
	theForm.PostalCode.focus();
	return (false);
	}
	


function typeOfCard(number) {
	/* 
	//	Card Prefixes
	//
	//	Mastercard	51-55
	//	Visa		4
	//	AmEx		34,37
	//	Discover	6011
	*/

	var firstNumber = number.substring(0,1);
	var firstThreeNumbers = number.substring(0,3);

	if (firstNumber == 4) {
		return "Visa";
	} 

	var firstTwoNumbers = number.substring(0,2);
	if (firstTwoNumbers > 50 && firstTwoNumbers < 56) {
		return "MasterCard";
	}

	if (firstTwoNumbers == 34 || firstTwoNumbers == 37) {
		return "AMEX";
	}

	var firstFourNumbers = number.substring(0,4);
	if (firstFourNumbers == 6011) {
		return "DISCOVER";
	}
}

// Function that determines whether a credit card number is valid
// Please note that a valid credit card number is not essentially a
// credit card in good standing.
function isValidCreditCard(number) {
	var total = 0;
	var flag = 0;
	for (var i=(number.length - 1);i>=0; i--) {
		if (flag == 1) {
			var digits = number.charAt(i) * 2;
			if (digits > 9) digits -= 9;
			total += digits;
//			var reminder = digits % 10;
//			var quotient = (digits - reminder) / 10;
//			total = total + parseInt(reminder);
//			total = total + parseInt(quotient);
			flag = 0;
		} else {
			total = total + parseInt(number.charAt(i));
			flag = 1;
		}
	}
	if ((total%10) == 0) {
		return true;
	} else {
		return false;
	}
}
	var msg1   = new String("");
	var ccType = new String(theForm.cc_type.value);
	var ccNum  = new Number(theForm.cc_num.value.length);
	var ccExp  = new Number(theForm.cc_exp.value.length);
	// verify payment type and values entered
 	if (button == "creditcard" && ccType != "Visa" && ccType != "MasterCard")
		 {
		 	
		 		alert("The credit card type you have entered is invalid.");		
				theForm.cc_type.focus();
				return(false);
			
		}
//
	// verify exp month selected if CC pressed
	if (button == "creditcard" && ccNum < 13 | ccNum > 20)
		 {
		msg1 += "The credit card number must be 13 to 20 ";
		msg1 += "characters in length, depending on the card\'s type."
		alert(msg1);
		theForm.cc_num.focus();
		return(false);
		}
	
	if (button == "creditcard" && !isValidCreditCard(theForm.cc_num.value)) {
		alert("The credit card number you have entered is invalid.");
		theForm.cc_num.focus();
		return(false);
	}
	if (button == "creditcard" && typeOfCard(theForm.cc_num.value) != ccType) {
		msg1 += "The credit card number you have entered does not\n correspond to the card's type.";
		msg1 += "\nCC Type by number: " + typeOfCard(theForm.cc_num.value);
		msg1 += "\nCC Type you entered: " + ccType;
		alert(msg1);
		theForm.cc_num.focus();
		return(false);
	}
	// verify exp year selected if CC pressed
	if (button == "creditcard" && ccExp != 7) {
		msg1 += "The credit card date must be in \"mm\/yyyy\" format.";
		alert(msg1);
		frmUser.cc_exp.focus();
		return(false);
		}

	// verify routing number if check pressed
	if (button == "instantcheck" && theForm.CheckRoutingNumber.value == "")
	{
	alert("Please Enter Your Bank's Routing Number.");
	theForm.CheckRoutingNumber.focus();
	return (false);
	}

	// verify check acct instant check pressed
	if (button == "instantcheck" && theForm.CheckAccountNumber.value == "")
	{
	alert("Please Enter Your Checking Account Number.");
	theForm.CheckAccountNumber.focus();
	return (false);
	}

	// verify initials box
	/*if (!theForm.agree.checked)
	{
	alert("You should agree to the terms and conditions.\n Please mark the checkbox.");
	theForm.agree.focus();
	return (false);
	}*/
	
	
	theForm.ptype.value = button;
}
