function isEmail(sEmail)
{
	// Create the RegEx object
	var rgEmail = /([a-z0-9][-._a-z0-9]*)*[a-z0-9]+@?([a-z0-9][-_a-z0-9]*\.)+[a-z]{2,7}/g;
	var aryResult = sEmail.match(rgEmail);
	var bRtn = false;
	
	if (aryResult != null)
		bRtn = ((aryResult[0] == sEmail) ? true : false);
	
	return bRtn;
}

function validateform(frm, sFields)
{
	// csv list of values to check
	fieldlist = sFields.split(",");
	for(element=0; element<fieldlist.length; element++)
	{
		doc = eval('frm.' + fieldlist[element]);
		doc.value = Trim(doc.value);
		if(doc.value == "")
		{
			if (doc.alt)
				alert(doc.alt); 
			else
				alert('You must enter a value for ' + fieldlist[element]); 
			doc.focus();
			return false;
		}
	}
	return true;
}

function isEmailSet(frm, sFields)
{
	// csv list of values to check
	fieldlist = sFields.split(",");
	for(element=0; element<fieldlist.length; element++)
	{
		doc = eval('frm.' + fieldlist[element]);
		doc.value = Trim(doc.value);
		if(!isEmail(doc.value))
		{
			if (doc.alt)
				alert(doc.alt); 
			else
				alert('You must enter a valid email address for ' + fieldlist[element]); 
			doc.focus();
			return false;
		}
	}
	return true;
}

function Trim(sString)
{
	return sString.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function sendcard(frm)
{
	var lstRequired = "txtFromName,txtFromEmail,txtMessage";
	if (validateform(frm, lstRequired) && toEmail(frm))
	{
		return true;
	}
	
	return false;
}

function toEmail(frm)
{
	if (!isEmail(frm.txtMateEmail[0].value))
	{
		alert('You must enter a valid email address for your mate.'); 
		frm.txtMateEmail[0].focus();
		return false;
	}
	
	for (var i=1; i<5; i++)
	{
		if (Trim(frm.txtMateEmail[i].value) != "" && !isEmail(frm.txtMateEmail[i].value))
		{
			alert('You must enter a valid email address for your mate.'); 
			frm.txtMateEmail[i].focus();
			return false;
			break;
		}
	}
	
	return true;
}