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;
}

function CheckEmail(t) {
	var pattern;
	var q = t.value;
	// alert(q);
	pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (pattern.exec(q)) {
		// alert(pattern.exec(q));
		return false;
	} else {
		// alert(pattern.exec(q));
		return true;
	}
}

function CheckPword(t) {
	var pattern;
	var q = t.value;
	// alert(q);
	pattern = /[\W-]/;
	if (pattern.exec(q)) {
		// alert(pattern.exec(q));
		return true;
	} else {
		// alert(pattern.exec(q));
		return false;
	}
}

function CheckInjection(t) {
	var pattern;
	var q = t.value;
	// alert(q);
	pattern = /[';]/;
	if (pattern.exec(q)) {
		// alert("true: "+pattern.exec(q));
		return true;
	} else {
		// alert("false: "+pattern.exec(q));
		return false;
	}
}

// Loop through the elements of the form, looking for all text and textarea
// elements. If the field is required check to make sure it is not empty. Empty fields
// are added to a list. Also, check to make sure no fields contain an apostrophe.
// If a field contains an apostrophe it is added to a list. If the submitted information
// contains errors, then display an error message with fields in error.

function chkData(f) 
{
	var e_name = "";
	var e_id = "";
	var e_type = "";
	var empty_fields = "";
	var email_error = "";
	var txt_add = "";
	var msg;
	var put_focus = -1;
	var num_elements = f.length;
	for (i = 0; i < num_elements; i++) {
		var e = f.elements[i];
		if ((e.type == "text") || (e.type == "textarea")) {	
			txt_add = "";
			e_id = "";
			e_name = e.value;	
			e_id = e.name;	// netscape	doesn't recognize the "id" attribute
			// for required fields check if the field is empty
			if ((e_id == "FirstName") || (e_id == "LastName") || (e_id == "Affiliation") || (e_id == "Phone") || (e_id == "Subject_Story")) {
				if ((e_name == null) || (e_name == "") || isblank(e_name))			
				{
					if (e_id == "FirstName") {
						txt_add = "your first name";
					}
					else if (e_id == "LastName") {
						txt_add = "your last name";
					}
					else if (e_id == "Affiliation") {
					  	txt_add = "your affiliation";
					}
					else if (e_id == "Phone") {
					  	txt_add = "your phone number";
					}
					else if (e_id == "Subject_Story") {
					  	txt_add = "your subject of story or purpose of request";
					}
					empty_fields += "	 " + txt_add;
					empty_fields += "\n";
					if (put_focus == -1) {
					put_focus = i;
					}
				}
			}			
		}
	}	
		
	if 	(CheckEmail(f.Email)) {
		email_error += "- The Email Address was either not typed in or was not a valid format. \n";
		if (put_focus == -1) {
			var email_focus = "Yes";
		}
	}
	
	// if they were any errors, display the error message,
	// and return false to prevent form from being submitted.
	// Otherwise return true.
	if (!empty_fields && !email_error) {
		return true;		
	}
	else
	{
		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 information was not filled in:\n"
				    + empty_fields + "\n";
		}
		if (email_error) {
			msg += "\n" + email_error + "\n";
		}
		alert(msg);
		if (empty_fields) {
			f.elements[put_focus].focus();	
		}
		if (put_focus == -1) {
			if (email_focus) {
				f.Email.focus();
			}
		}
		return false;
	}
}	

function chkData_login(f) 
{
	var e_name = "";
	var e_id = "";
	var e_type = "";
	var empty_fields = "";
	var inject_error = "";
	var txt_add = "";
	var msg;
	var put_focus = -1;
	var num_elements = f.length;
	for (var i = 0; i < num_elements; i++) {
		var e = f.elements[i];
		if ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) {	
			txt_add = "";
			e_id = "";
			e_name = e.value;	
			e_id = e.name;	// netscape	doesn't recognize the "id" attribute
			// for required fields check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value))			
			{
				if (e.id == "uname") {
					txt_add = "your username";
				}
				else if (e.id == "pword") {
					txt_add = "your password";
				}
				empty_fields += "	 " + txt_add;
				empty_fields += "\n";
				if (put_focus == -1) {
				put_focus = i;
				}
			}
		}
	}
	if ((f.uname.value != null) && (f.uname.value != "") && !isblank(f.uname.value)) {
		// alert(f.uname.value);		
		if 	(CheckInjection(f.uname)) {
			inject_error += "- A character you entered in the username field is not allowed. \n";
			if (put_focus == -1) {
				var inject_focus = "Yes";
			}
		}
	}
	
	// if they were any errors, display the error message,
	// and return false to prevent form from being submitted.
	// Otherwise return true.
	if (!empty_fields && !inject_error) {
		return true;		
	}
	else
	{
		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 information was not filled in:\n"
				    + empty_fields + "\n";
		}
		if (inject_error) {
			msg += "\n" + inject_error + "\n";
		}
		alert(msg);
		if (empty_fields) {
			f.elements[put_focus].focus();	
		}
		if (put_focus == -1) {
			if (inject_focus) {
				f.uname.focus();
			}
		}
		return false;
	}
}
