// validateForm.js
// Check My CBT registration form to make sure all the inputs are valid.
function passCheck(theForm)
{
	var why = "";
	why += checkField(theForm.FirstName.value, "First Name");
	why += checkField(theForm.LastName.value, "Last Name");
	why += checkField(theForm.Email.value, "Email");
	why += checkField(theForm.Password1.value, "Password");


	if (theForm.Password1.value != theForm.Password2.value)
	{
		why += "Passwords don't match."
	}
	if (theForm.Email.value != theForm.Email2.value)
	{
		why += "Email addresses don't match."
	}

	if (why != "") {
		alert(why);
		return false;
	}
	return true;
}

function checkField (val, field) {
	var error = "";
	if (val == "") {
		error = "You must enter a value in the " + field + " field\n";
	}

	var crud = /["'`]/; // No quotes please!
	if (crud.test(val)) {
		error = "No quotes are allowed in the " + field + " field\n";
	}

	return error;
}

function forgotPass()
{
	emailValue = document.forms[0].email.value; // This is a precarious statement
	if (emailValue)
	{
	    args = "width=500,height=280,scrollbars,resizable";
		url = 'forgotPassword.php?e=' + emailValue;
	    window.open(url,'popup',args);
	} else {
		alert("Please enter your email address, then click the 'Forgot?' link to have a new password emailed to you.");
	}
}

function validateForm(theForm) {
    var why = "";
    if (theForm.realname)
    {
        val = theForm.realname.value;
        if (val == "") {
            why += "Please enter a valid first name.\n";
        }
        if (theForm.lastName) {
            if (theForm.lastName.value == "") {
                why += "Please enter a valid last name.\n";
            } 
/*
		else {
                theForm.realname.value += ' ' + theForm.lastName.value;
            }
*/
        }
    }
    if (theForm.phone)
    {
        val = theForm.phone.value;
        if (val == "") {
            why += "Please enter a valid phone number.\n";
        }
    }
    if (theForm.email)
    {
        why += checkEmail(theForm.email.value, 'from');
    }
    if (theForm.recipient)
    {
        why += checkEmail(theForm.recipient.value, 'to');
    }

    if (why != "") {
        alert(why);
        return false;
    }
    //theForm.realname.value += ' ' + theForm.lastName.value;
    return true;
}
