/*
* Check email using regexes
*/
function check_email(email)
{
 	if (typeof(email) != "string")
        return false;

    //characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @
	var re = /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/;
    if (email.search(re) == -1)
       return false;

    //must start or end with alpha or num
    re = /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/;
    if (email.search(re) != -1)
        return false;

    //name must end with alpha or num
    re = /([0-9a-zA-Z_]{1})\@./;
    if (email.search(re) == -1)
        return false;

    //host must start with alpha or num
    re = /.\@([0-9a-zA-Z_]{1})/;
    if (email.search(re) == -1)
        return false;

    //pair .- or -. or -- or .. not allowed
	re = /.\.\-.|.\-\..|.\.\..|.\-\-./;
    if (email.search(re) != -1)
        return false;

    //pair ._ or -_ or _. or _- or __ not allowed
    re = /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./;
    if (email.search(re) != -1)
        return false;

    //host must end with '.' plus 2-5 alpha for TopLevelDomain
    re = /\.([a-zA-Z]{2,5})$/;
    if (email.search(re) == -1)
        return false;

    return true;
}

function submit_form(form)
{
	var email = form['em'].value;
	if (!check_email(email))
	{
		alert("Invalid email address!!!\n\nPlease enter your email address.");
		form['em'].focus();
		return false;
	}
	var pw = form['pw'].value;
	if (pw.length == 0)
	{
		alert("Please enter your password.");
		form['pw'].focus();
		return false;
	} 
	else if (pw.length > 32) 
	{
		alert("Your password must be no greater than 32 characters.");
		form['pw'].value = "";
		form['pw'].focus();
		return false;
	}
	else if (pw.length < 6) 
	{
		alert("Your password must be at least 6 characters.");
		form['pw'].value = "";
		form['pw'].focus();
		return false;
	}
	var pwHash = hex_md5(pw);
	var challenge = form['ch'].value;
	var hash = hex_md5(pwHash + challenge);
	form['sid'].value = hash;
	form['pw'].value = "";
	form['ch'].value = "";

	// prevent from running this again.
	form.onsubmit = null;

	return true;
}
