// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification without anything for cc verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verifynocc(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        
        if ((e.type == "checkbox")&& !e.optional && !e.checked ) {
        	errors += "- The field " + e.name + " must be checked."
        }
        
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        }
    }
    //Now a little specific error checking
	
    if (f.studentname.value.length < 4) {
    	errors += "\nStudent name must be 4 characters or longer.";
    }
    	
    if (f.studentpassword.value.length < 4) {
    	errors += "\nPassword must be 4 characters or longer.";
    }
    	
	

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) {
    	//FORM IS GOOD, display some info on form and let the user say OK
    	msg  = "______________________________________________________\n\n";
	msg += "You have submitted the following subscription application.\n\n";
	
//	for(var i = 0; i < f.length; i++) {
//        	var e = f.elements[i];
//        	msg += e.name + " : " + e.value + "\n";
//        }

	msg += 	 f.firstname.value + " " + f.middlename.value + " " + f.lastname.value + "\n";
	msg +=   f.address.value  + "\n";
	if (f.address2.value  != "") {
		msg +=   f.address2.value  + "\n";
	}
	msg +=   f.city.value  + " " + f.state.value  + " " + f.zip.value  + " " + f.country.value  + "\n\n";
	msg +=   "Your email address \n ";
	msg +=   f.email.value  + "\n\n";
	msg +=   "Your home phone \n ";
	msg +=   f.phone.value  + "\n\n";
	msg +=   "Your Student Name and Password information\n ";
	msg +=   f.studentname.value  + "\n ";
	msg +=   f.studentpassword.value  + "\n ";
	msg +=   f.passwordquestion.value  + "\n ";
	msg +=   f.passwordanswer.value  + "\n\n";
	mydate = new Date();

	



	msg += "\n\n";
	msg += "Press OK to continue with purchase.\n"; 
    	msg += "______________________________________________________";
	if (confirm(msg)) {
		return true;
	}
	else {
		return false;
	}

		

	}
	else { //form has errors, inform the user.
	    msg  = "______________________________________________________\n\n"
	    msg += "The form was not submitted because of the following error(s).\n";
	    msg += "Please correct these error(s) and re-submit.\n";
	    msg += "______________________________________________________\n\n"
	
	    if (empty_fields) {
	        msg += "- The following required field(s) are empty:" 
	                + empty_fields + "\n";
	        if (errors) msg += "\n";
	    }
	    msg += errors;
	    alert(msg);
	    return false;
	}
}


// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        
        if ((e.type == "checkbox")&& !e.optional && !e.checked ) {
        	errors += "- The field " + e.name + " must be checked."
        }
        
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        }
    }
    //Now a little specific error checking
    if (f.creditcardtype.selectedIndex == 0) {
    	errors += "\nCredit card type is missing.";
    }
    
    if (f.ccexmonth.selectedIndex == 0) {
    	errors += "\nCredit card expiration month is missing.";
    }

    if (f.ccexyear.selectedIndex == 0) {
    	errors += "\nCredit card expiration year is missing.";
    }

    if (f.studentname.value.length < 4) {
    	errors += "\nStudent name must be 4 characters or longer.";
    }
    	
    if (f.studentpassword.value.length < 4) {
    	errors += "\nPassword must be 4 characters or longer.";
    }
    	
		

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) {
    	//FORM IS GOOD, display some info on form and let the user say OK
    	msg  = "______________________________________________________\n\n";
	msg += "You have submitted the following subscription application.\n\n";
	
//	for(var i = 0; i < f.length; i++) {
//        	var e = f.elements[i];
//        	msg += e.name + " : " + e.value + "\n";
//        }

	msg += 	 f.firstname.value + " " + f.middlename.value + " " + f.lastname.value + "\n";
	msg +=   f.address.value  + "\n";
	if (f.address2.value  != "") {
		msg +=   f.address2.value  + "\n";
	}
	msg +=   f.city.value  + " " + f.state.value  + " " + f.zip.value  + " " + f.country.value  + "\n\n";
	msg +=   "Your email address \n ";
	msg +=   f.email.value  + "\n\n";
	msg +=   "Your Student Name and Password information\n ";
	msg +=   f.studentname.value  + "\n ";
	msg +=   f.studentpassword.value  + "\n ";
	msg +=   f.passwordquestion.value  + "\n ";
	msg +=   f.passwordanswer.value  + "\n\n";
	msg +=   "Your Credit Card information\n ";
//	msg +=   f.creditcardtype[f.creditcardtype.selectedIndex].value  + " " + f.creditcardnum.value  + " Expires " + f.creditcardexpirationmonth.value + "/" + f.creditcardexpirationyear.value + "\n\n";
	msg +=   f.creditcardtype.options[f.creditcardtype.selectedIndex].text  + " " + f.creditcardnum.value  + " Expires " + f.ccexmonth.options[f.ccexmonth.selectedIndex].text + "/" + f.ccexyear.options[f.ccexyear.selectedIndex].text + "\n\n";
	mydate = new Date();
	msg +=   "Your Membership Plan\n ";
	if(f.plan.length>1) {
	
		if (f.plan[0].checked) {
			msg += "Start Monthly plan on " + mydate.toLocaleDateString() + ".\n ";
			msg += "Receive 1 lesson per month.\n";
		}
	
		if (f.plan[1].checked) {
			msg += "Start Quarterly plan on " + mydate.toLocaleDateString() + ".\n ";
			msg += "Receive 3 lessons every 3 months.\n";
		}
	
		if (f.plan[2].checked) {
			msg += "Start Yearly plan on " + mydate.toLocaleDateString() + ".\n ";
			msg += "Receive 12 lessons per year.\n";
		}
	}
	else if(f.months.length>0) {
		if (f.months[0].checked) {
			msg += "Add a month of lessons.\n ";
		}
	
		if (f.months[1].checked) {
			msg += "Add 2 months of lessons.\n ";
		}
	
		if (f.months[2].checked) {
			msg += "Add 3 months of lessons.\n ";
		}
	}
	else {
		msg += "Promotional Plan\n";
	}		
		
	



	msg += "\n\n";
	msg += "Press OK to continue with purchase.\n"; 
    	msg += "______________________________________________________";
	if (confirm(msg)) {
		return true;
	}
	else {
		return false;
	}

		

	}
	else { //form has errors, inform the user.
	    msg  = "______________________________________________________\n\n"
	    msg += "The form was not submitted because of the following error(s).\n";
	    msg += "Please correct these error(s) and re-submit.\n";
	    msg += "______________________________________________________\n\n"
	
	    if (empty_fields) {
	        msg += "- The following required field(s) are empty:" 
	                + empty_fields + "\n";
	        if (errors) msg += "\n";
	    }
	    msg += errors;
	    alert(msg);
	    return false;
	}
}
