function gotoTourPage() {
	var sel = document.tourform.toursel.options[document.tourform.toursel.selectedIndex].value;
	window.location.replace("/tour/" + sel + ".php");
}

function showNoodleToolsHeader(title, section)
{
	document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	document.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
	document.write("<head>");
	document.write("	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />");
	document.write("	<title>");
	if (title != "") { 
		document.write(title); 
	} else { 
		document.write("NoodleTools : MLA / APA Bibliography Composer, Research Tools");
	}
	document.write("        </title>");
	document.write("	<link rel=\"stylesheet\" href=\"/_css/styles.css\" type=\"text/css\" />");
	document.write("	<!--[if lt IE 7]>");
	document.write("		<link rel=\"stylesheet\" href=\"/_css/ie.css\" type=\"text/css\" />");
	document.write("	<![endif]-->");
	document.write("	<!--[if gte IE 7]>");
	document.write("		<link rel=\"stylesheet\" href=\"/_css/ie7.css\" type=\"text/css\" />");
	document.write("	<![endif]-->");
	document.write("	<meta http-equiv=\"imagetoolbar\" content=\"no\" />");
	document.write("	<meta http-equiv=\"Pragma\" content=\"no-cache\" />");
	document.write("	<meta http-equiv=\"Expires\" content=\"-1\" />");
	document.write("	<meta http-equiv=\"P3P\" content=\"CP='NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV'\" />");
	document.write("	<meta name=\"description\" content=\"NoodleBib: MLA and APA bibliography composer with notecards. Free software to help students with the research process.\" />");
	document.write("	<meta name=\"keywords\" content=\"bibliography, MLA, APA, MLA format, APA format, MLA bibliography, APA bibliography, research, cite, notecards, plagiarism\" />");
	document.write("</head>");

	document.write("<body id=\"home\">");

	showNoodleToolsBody(section);
}

function showNoodleToolsBody(section)
{
	document.write("<div id=\"container\">");
	document.write("	<div id=\"header\">");
	document.write("		<h1><span>NoodleTools</span></h1>");
	document.write("		<ul>");
	document.write("			<li><a href=\"/index.php\" id=\"nav_home\"><span>Home</span></a></li>");
			
	document.write("<li><a href=\"/tools/index.php\" id=\"nav_software_tools\"");
	if (section=="software_tools") { document.write(" class=\"on\""); }
	document.write("><span>Software Tools</span></a></li>\r");
			
	document.write("<li><a href=\"/blog/index.php\" id=\"nav_blog\"");
	if (section=="blog") { document.write(" class=\"on\""); }
	document.write("><span>Blog</span></a></li>\r");
			
	document.write("<li><a href=\"/debbie/\" id=\"nav_teacher_resources\"");
	if (section=="teacher_resources") { document.write(" class=\"on\""); }
	document.write("><span>Teacher Resources</span></a></li>\r");
			
	document.write("<li><a href=\"/about/index.php\" id=\"nav_about_us\"");
	if (section=="about") { document.write(" class=\"on\""); }
	document.write("><span>About Us</span></a></li>\r");
			
	document.write("<li><a href=\"/noodlebib/go.php\" id=\"nav_sign_in\"");
	if (section=="sign_in") { document.write(" class=\"on\""); }
	document.write("><span>Sign In</span></a></li>\r");
			
	document.write("		</ul>");
	document.write("	</div><!-- end header -->");
}

function showNoodleToolsFooter()
{
	document.write("<div id=\"footer\">");
	document.write("	<p>");
	document.write("		Copyright &copy; NoodleTools Inc.");
	document.write("		| <a href=\"/search.php\">Search</a>");
	document.write("		| <a href=\"/privacy.php\">Privacy Policy</a>");
	document.write("		| <a href=\"/tos.php\">Terms of Service</a>");
	document.write("		| <a href=\"/legal.php\">Legal</a>");
	document.write("	</p>");
	document.write("</div><!-- end footer -->");
}

function removeChars2(str, charset) {
    var l = str.length;
    for (var i = 0; i < l; i++) {
    var index = charset.indexOf(str.charAt(i));
    if (index >= 0) {
        str = str.substring(0, i) + str.substring(i+1);
        l--;
        i--;
    }
    }

    return str;
}

function canonicalizeInitials(field) {
    if (field && field.type == "hidden")
      return true;

    var val = field.value;
    var str = removeChars2(val, '. ').toUpperCase();
    field.value = str;

    return true;
}

function checkemail(field)
{
    var s_email = field.value;

    if (s_email.length==0)
    {
        return true;
    }

    if ((s_email.length < 6) ||
       (s_email.indexOf('@',0) < 1) ||
       (s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||
       (s_email.lastIndexOf('@') > (s_email.length - 5)) ||
       (s_email.lastIndexOf('.') > (s_email.length - 3)) ||
       (s_email.lastIndexOf('.') < (s_email.length - 4)) ||
       (s_email.indexOf('..',0) > -1) ||
       (s_email.indexOf('@.',0) > -1) ||
       (s_email.indexOf('.@',0) > -1) ||
       (s_email.indexOf(',',0) > -1))
    {
        alert("Please enter a valid e-mail address (format: name@domain.com).");
	  field.focus();
        return false;
    }
    return true;
}

function ltrim ( s ) {
	return s.replace( /^\s*/, "" )
}

function rtrim ( s ) {
	return s.replace( /\s*$/, "" );
}

function trim ( s ) {
	return rtrim(ltrim(s));
}

function checkphone4(field, msg)
{
    var s_phone4 = field.value;

    if (s_phone4.length==0)
    {
        return true;
    }

    var regexp = /^(\d{4})$/;
    if (!regexp.test(s_phone4))
    {
	if (msg) {
	        alert(msg);
	} else {
	        alert("Please enter the last four digits of your phone number (format: ####).");
	}
	field.focus();
        return false;
    }
    return true;
}

function handleRegUserSubmit() {
	var username = document.regForm.username.value;
	var password = document.regForm.password.value;
	var password2 = document.regForm.password2.value;
	var phone = document.regForm.phone.value;
	var initials = document.regForm.initials.value;

	if (trim(username) == "") { 
		alert("Please select a username."); 
		document.regForm.username.focus();
		return false; 
	}
	if (trim(password) == "") { 
		alert("Please select a password."); 
		document.regForm.password.focus();
		return false; 
	}
	if (trim(password2) == "") { 
		alert("Please retype your password on the second line."); 
		document.regForm.password2.focus();
		return false; 
	}
	if (trim(password) != trim(password2)) { 
		alert("The retyped password does not match your password on the first line. Please retype your passwords."); 
		document.regForm.password.focus();
		return false; 
	}
	if (trim(username).length < 4) { 
		alert("Your username must be 4 or more characters long."); 
		document.regForm.username.focus();
		return false; 
	}
	if (trim(password).length < 4) { 
		alert("Your password must be 4 or more characters long."); 
		document.regForm.password.focus();
		return false; 
	}
	if (trim(password) == trim(username)) { 
		alert("Please select a password that is different than your username."); 
		document.regForm.password.focus();
		return false; 
	}

	if (trim(initials).length < 2) { 
		alert("Please enter the initials of your first and last name."); 
		document.regForm.initials.focus();
		return false; 
	}

	myRegExpPhoneNumber = /\d\d\d\d/
	if (trim(phone).length != 4 || myRegExpPhoneNumber.test(phone) == false) { 
		alert("Please enter the last 4 digits of your phone number."); 
		document.regForm.phone.focus();
		return false; 
	}

	return true;
}

function handleEditRegUserSubmit() {

	var username = document.editregForm.edit_username.value;
	var password = document.editregForm.edit_password.value;
	var password2 = document.editregForm.edit_password2.value;
	var phone = document.editregForm.edit_phone.value;
	var initials = document.editregForm.edit_initials.value;

	if (trim(username) == "") { 
		alert("Please enter a username."); 
		document.editregForm.edit_username.focus();
		return false; 
	}

	if (trim(password) != "" && trim(password2) == "") { 
		alert("Please retype your password on the second line."); 
		document.editregForm.edit_password2.focus();
		return false; 
	}

	if (trim(password) != trim(password2)) { 
		alert("The retyped password does not match your password on the first line. Please retype your passwords."); 
		document.editregForm.edit_password.focus();
		return false; 
	}

	if (trim(username) == trim(password)) { 
		alert("Please select a password that is different than your username."); 
		document.editregForm.edit_password.focus();
		return false; 
	}

	if (trim(username).length < 4) { 
		alert("Your username must be 4 or more characters long."); 
		document.editregForm.edit_username.focus();
		return false; 
	}

	if (trim(password).length < 4 && trim(password).length > 0) { 
		alert("Your password must be 4 or more characters long."); 
		document.editregForm.edit_password.focus();
		return false; 
	}

	if (trim(initials).length < 2) { 
		alert("Please enter the initials of your first and last name."); 
		document.editregForm.edit_initials.focus();
		return false; 
	}

	myRegExpPhoneNumber = /\d\d\d\d/
	if (trim(phone).length != 4 || myRegExpPhoneNumber.test(phone) == false) { 
		alert("Please enter the last 4 digits of your phone number."); 
		document.editregForm.edit_phone.focus();
		return false; 
	}

	document.editregForm.submit();
	return true;
}

function handleUpdateProfileSubmit() {
	var initials = document.updateprofileForm.updateprofile_initials.value;
	var phone = document.updateprofileForm.updateprofile_phone.value;

	if (trim(initials).length < 2) { 
		alert("Please enter the initials of your first and last name."); 
		document.updateprofileForm.updateprofile_initials.focus();
		return false; 
	}

	myRegExpPhoneNumber = /\d\d\d\d/
	if (trim(phone).length != 4 || myRegExpPhoneNumber.test(phone) == false) { 
		alert("Please enter the last 4 digits of the phone number."); 
		document.updateprofileForm.updateprofile_phone.focus();
		return false; 
	}

	document.updateprofileForm.submit();
	return true;
}

function handleEditUserSubmit() {

	var username = document.userForm.edit_username.value;
	var password = document.userForm.edit_password.value;
	var phone = document.userForm.edit_phone.value;

	if (trim(username) == "") { 
		alert("Please enter a personal ID."); 
		document.userForm.edit_username.focus();
		return false; 
	}

	if (trim(username) == trim(password)) { 
		alert("Please select a password that is different than your personal ID."); 
		document.userForm.edit_password.focus();
		return false; 
	}

	if (trim(username).length < 4) { 
		alert("Your personal ID must be 4 or more characters long."); 
		document.userForm.edit_username.focus();
		return false; 
	}

	if (trim(password).length < 4 && trim(password).length > 0) { 
		alert("Your password must be 4 or more characters long."); 
		document.userForm.edit_password.focus();
		return false; 
	}

	myRegExpPhoneNumber = /\d\d\d\d/
	if (trim(phone).length != 4 || myRegExpPhoneNumber.test(phone) == false) { 
		alert("Please enter the last 4 digits of the phone number."); 
		document.userForm.edit_phone.focus();
		return false; 
	}

	return true;
}

function handleConvertNB5Submit() {

	var firstname = document.regForm.convert_firstname.value;
	var lastname = document.regForm.convert_lastname.value;
	var phone = document.regForm.convert_phone.value;
	var oldpassword = document.regForm.convert_oldpassword.value;

	var username = document.regForm.username.value;
	var password = document.regForm.password.value;
	var password2 = document.regForm.password2.value;

	if (trim(firstname) == "") { 
		alert("Please enter your first name."); 
		document.regForm.convert_firstname.focus();
		return false; 
	}

	if (trim(lastname) == "") { 
		alert("Please enter the first intial of your last name."); 
		document.regForm.convert_lastname.focus();
		return false; 
	}

	myRegExpPhoneNumber = /\d\d\d\d/
	if (trim(phone).length != 4 || myRegExpPhoneNumber.test(phone) == false) { 
		alert("Please enter the last 4 digits of your phone number."); 
		document.regForm.convert_phone.focus();
		return false; 
	}

	if (trim(oldpassword) == "") { 
		alert("Please enter your old password."); 
		document.regForm.convert_oldpassword.focus();
		return false; 
	}

	if (trim(username) == "") { 
		alert("Please enter a new username."); 
		document.regForm.username.focus();
		return false; 
	}

	if (trim(password) != "" && trim(password2) == "") { 
		alert("Please retype your new password on the second line."); 
		document.regForm.password2.focus();
		return false; 
	}

	if (trim(password) != trim(password2)) { 
		alert("The retyped password does not match your new password on the first line. Please retype your new passwords."); 
		document.regForm.password.focus();
		return false; 
	}

	if (trim(username) == trim(password)) { 
		alert("Please select a new password that is different than your username."); 
		document.regForm.password.focus();
		return false; 
	}

	if (trim(username).length < 4) { 
		alert("Your new username must be 4 or more characters long."); 
		document.regForm.username.focus();
		return false; 
	}

	if (trim(password).length < 4 && trim(password).length > 0) { 
		alert("Your new password must be 4 or more characters long."); 
		document.regForm.password.focus();
		return false; 
	}

	return true;
}
