// ######################################
// ## Form Validation Script ############
// ######################################
// biocomplexity.indiana.edu 
// ©copyright 2003
//#######################################

// log in form #################
function checkLoginForm() {
var uname = document.form.username;
var pass = document.form.password;
var unameV = document.form.username.value;
var passV = document.form.password.value;

if (unameV == "") {
alert('Please enter a username.');
uname.focus();
return false;
}
else {
		//!@$#\*_-\.
		// alphanumeric containing . - _ ! $ # * @ but nothing else and not beginning with a dash
		re = /^[\w!@\$#\.\*]+([!@#\$\.\*-]?\w+)*[\w!@\$#\.\*]+$/;
        if (re.test(unameV) == true) {
			// continue
			}
        else
        {
            alert('The username contains invalid characters or is improperly formatted.');
			uname.focus();
			uname.select();
			return false;
        }
}

if (passV == "") {
alert('Please enter a password.');
pass.focus();
return false;
}
else {
		//!@$#\*_-\.
		// alphanumeric containing . - _ ! $ # * @ but nothing else and not beginning with a dash
		re = /^[\w!@\$#\.\*-]+$/;
        if (re.test(passV) == true) {
			// continue
			}
        else
        {
            alert('The password contains invalid characters or is improperly formatted.');
			pass.focus();
			pass.select();
			return false;
        }
}


}
// ##########################

function checkPostForm(tflag) {

var np = document.newPost;

if (tflag == 1) {
if (np.subject.value == "") {
alert('Please enter a subject.');
np.subject.focus();
return false;
	} 

if (np.subject.length > 60) {
alert('The subject you have entered is too long.  Please edit it such that it is 60 characters or less.');
np.subject.focus();
return false;
	}
}

if (np.message.value == "") {
alert('Please enter a message.');
np.message.focus();
return false;
	} 

if (np.message.length > 65535) {
alert('The message you have entered is too long.  Please edit it such that it is 65535 characters or less.');
np.message.focus();
return false;
	}


}

// ##########################

function checkPaperForm() {

var pf = document.paper;

if (pf.PaperTitle.value == "") {
alert('Please enter a paper title.');
pf.PaperTitle.focus();
return false;
} 

if (pf.Citation.value == "") {
alert('Please enter a citation.');
pf.Citation.focus();
return false;
}

}

// check the search form

function checkSearchForm() {

var sf = document.searchForm;

if (sf.q.value == '') {
alert('Please enter a query.');
sf.q.focus();
return false;
}
else {
		//!@$#\*_-\.
		// alphanumeric containing . - _ ! $ # * @ but nothing else and not beginning with a dash
		re = /^\w*(\w+\s?)*\w+$/;
        if (re.test(sf.q.value) == true) {
			// continue
			}
        else
        {
            alert('The query contains invalid characters.');
			sf.q.focus();
			sf.q.select();
			return false;
        }
	}

}


// ##############################
// disabling / enabling form fields
// courtesy http://www.xs4all.nl/~ppk/js/index.html?/~ppk/js/disabled.html
// #############################
function extracheck(obj)
{
	return !obj.disabled;
}

function toggleLink(no) {
	var pf = document.paper;

	for (var i=no+1; i<= no+3; i++) {
	var disable = pf.elements[i].disabled;
	  pf.elements[i].disabled = !disable;
	}

}

// user add form ##############
function checkUserForm() {
var usf = document.userProfile;

if (usf.fullname.value == "") {
// continue - not required
}
else {
	var re;
	re = /^(\w+|(\w{1,3}\.)+)(\s?\w+)*(\s?\w+\s)*(\s?\w{1,3}\.\s)*(\w+|(\w{1}\.)+)$/;
	if (re.test(usf.fullname.value) == true) {
	//continue
	}
	else {
	alert('Improperly formatted name entered.  Please remove excess whitespace.');
	usf.fullname.focus();
	usf.fullname.select();
	return false;
	}
}

if (usf.email.value == "") {
alert('Please enter an e-mail address.  [Enter a fake one if you do not wish to enter one.]');
usf.email.focus();
return false;
}
else {
		var re;
        re = /^\w([\.-]?\w+)*@\w+([\.-]?\w+)*(\.[a-z]{2,})+$/;
        if (re.test(usf.email.value) == true) {
			//continue
			}
        else
        {
            alert('The e-mail address entered is invalid.');
			usf.email.focus();
			usf.email.select();
			return false;
        }

}

if (usf.pchangeFlag.checked) {

if (usf.password.value == "" || usf.passwordConfirm.value == "") {
alert('A password is required.  Please enter one.');
usf.password.focus();
return false;
}

if (usf.password.value.length < 6) {
alert('The password must at least be six characters in length.');
usf.password.focus();
usf.password.select();
return false;
}

if (usf.password.value != usf.passwordConfirm.value) {
alert('The two passwords do not match.  Please re-enter them such that they match.');
usf.password.focus();
usf.password.select();
return false;

if (usf.password.value == usf.username.value) {
alert('The password may not be identical to the username.');
usf.password.focus();
usf.password.select();
return false;
}

}

else {
		//!@$#\*_-\.
		// alphanumeric containing .  _ ! $ # * @ but nothing else and not beginning with a dash
		re = /^[\w!@\$#\.\*-]+$/;
        if (re.test(usf.password.value) == true) {
			// continue
			}
        else
        {
            alert('The password contains invalid characters or is improperly formatted.');
			usf.password.focus();
			usf.password.select();
			return false;
        }
}

}


}
// ##########################