/* -----------------------------------------------------------------------------
      NAME: checkName(string)
   PURPOSE: Varifies if a name contains valid characters.
        IN: str - the string we want to check.
       OUT: TRUE - if name is valid.
	        FALSE - if name is invalid.
----------------------------------------------------------------------------- */

function checkName(name)
{
	var pattern  = /^[a-zA-Z\s]*$/;
	
	if(pattern.test(name))
		return true; // name if OK
	else
		return false; // name contained eligal characters
}

/* -----------------------------------------------------------------------------
      NAME: validateEmail(string)
   PURPOSE: Varifies if a E-mail address is of the correct format.
        IN: str - the E-mail address we want to check.
       OUT: TRUE - if E-mail address is valid.
	        FALSE - if E-mail address is invalid.
----------------------------------------------------------------------------- */

function validateEmail(emailAddress)
{
	var pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(pattern.test(emailAddress))
		return true; // E-mail address format is OK
	else
		return false; // E-mail address is invalid
}

/* -----------------------------------------------------------------------------
      NAME: chkForm()
   PURPOSE: Check form fields are completed properly.
        IN: n/a
       OUT: TRUE - if form passed all the checks.
	        FALSE - if form contained errors.
----------------------------------------------------------------------------- */

function chkForm()
{
	var mpForm = document.frmMPMailer; // get ref to the form
	var errMsg = "";
	var errCnt = 0;
	
	// check recipient's details form fields
	if(mpForm.txtRName.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Recipient's name is missing.\n";
	}
	else
	{
		if(checkName(mpForm.txtRName.value) == false)
		{
			errCnt++;
			errMsg += errCnt + ". Recipient's name is invalid.\n";
		}
	}
	
	if(mpForm.txtREmail.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Recipient's E-mail address is missing.\n";
	}
	else
	{
		if(validateEmail(mpForm.txtREmail.value) == false)
		{
			errCnt++;
			errMsg += errCnt + ". Recipient's E-mail address does not appear to be a valid E-mail address.\n";
		}
	}
	
	/*
	if(mpForm.txtRAddress.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Recipient's address is missing.\n";
	}
	*/
	
	// check recipient's details form fields
	if(mpForm.txtSName.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Your name is missing.\n";
	}
	else
	{
		if(checkName(mpForm.txtSName.value) == false)
		{
			errCnt++;
			errMsg += errCnt + ". Your name is invalid.\n";
		}
	}
	
	if(mpForm.txtSEmail.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Your E-mail address is missing.\n";
	}
	else
	{
		if(validateEmail(mpForm.txtSEmail.value) == false)
		{
			errCnt++;
			errMsg += errCnt + ". Your E-mail address does not appear to be a valid E-mail address.\n";
		}
	}
	
	/*
	if(mpForm.txtSAddress.value == "")
	{
		errCnt++;
		errMsg += errCnt + ". Your address is missing.\n";
	}
	*/
	
	// if we got errors show them to the user
	if(errCnt > 0)
	{
		if(errCnt == 1){
			alert("The following problem was encountered:\n\n" + errMsg);
		}else{
			alert("The following " + errCnt + " problems were encountered:\n\n" + errMsg);
		}
		
		return false;
	}
	
	return true; // the form entries are all valid
}
