// JavaScript Document

// registration form validation function
// ---- utility function checking for whitespace characters
function isblank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if((c !=' ') && (c != '\n') && (c != '')) return false;
	};
	return true;
};

// ---- perform form validation
function validateRegistration(f) {
	var msg;
	var empty_fields = "";
	var errors = "";
	
	// validate all required text-based fields
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		
		if(((e.type == "text") || (e.type == "textarea")) && !e.optional) {
			// email validation
			if(e.name == "email" || e.name == "senderemail" || e.name == "recipientemail") {
				if(e.value.indexOf('@', 0) < 1) {
					empty_fields += "\n              required @(at) not in email address";
					continue;
				};
				
				if(e.value.indexOf('.', 0) < 1) {
					empty_fields += "\n              required .(dot) not in email address";
					continue;
				};
			};
			
			// url validation
			if(e.name == "url") {
				if(e.value == "http://") {
					empty_fields += "\n              " + e.name;
					continue;
				};
			};
			
			// other validation
			if((e.value == null) || (e.value == "") || isblank(e.value)) {
				empty_fields += "\n              " + e.name;
				continue;
			};
			
			// check for numeric fields
			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 contain only numbers. ";
					
					if(e.name == "phone")
						errors += "Phone number\nmust be formatted as 4105554545";
					if(e.name == "zip")
						errors += "Postal/Zip must\ncontain 5 numbers";
					//else if(e.name == "numberusers")
						//errors += "Number of users must contain only numbers";
					//errors += ".\n";					
				};
			};
		};
	};
	
	// format and display any erros
	if(!empty_fields && !errors) return true;
	
	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;
};