//JavaScript

var whitespace = " \t\n\r";

function isEmpty(theField)
{
	// Check for nothing entered
	if ((theField == null || theField.length == 0 || theField == ""))
		return true;
}
function hasWhitespace(theField)
{
	var x;
	// check that there are non-white space characters entered.
	for (x=0; x<theField.length; x++)
	{
		var y = theField.charAt(x);
		if (whitespace.indexOf(y) == -1)
			return false;
	}
	// if we get here we must have white space
	return true;
}
function emailCheck(emailAsEntered)
{
	var atSymbol = "@";
	var dot = ".";
	for (z=0; z<emailAsEntered.length; z++)
	{
		var c = emailAsEntered.charAt(z);
		var x = atSymbol.indexOf&copy;;
		if (x ==! -1)
		{
			for (y=0; y<emailAsEntered.length; y++)
			{
				var p = emailAsEntered.charAt(y);
				var q = dot.indexOf(p);
				if (q !== -1)
				{
					return true;
				}
			}
		}
	}
	return false; // if it reaches then at least one of the essential characters was missing
}

function valid_email(email){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(email);
	return returnval;
}

function contentPresent()
{
	if (isEmpty(document.frm_training.name.value))
	{
		document.frm_training.name.focus();
		alert ("Please complete the Name field");
		return false;
	}
	/*
	if (isEmpty(document.frm_training.organization.value))
	{
		document.frm_training.organization.focus();
		alert ("Please complete the Organization field");
		return false;
	}
	*/
	if (document.frm_training.expertise.value != "")
	{
		comment = document.frm_training.expertise.value;
		http = comment.split("http");
		www = comment.split("www");
		if (http.length > 1 || www.length > 1){
			alert("Please don't put your website address here");
			document.frm_training.expertise.focus();
			return false;
		}
	}
	
	if (isEmpty(document.frm_training.participate.value))
	{
		document.frm_training.participate.focus();
		alert ("Please complete the Participate field");
		return false;
	}else{
		comment = document.frm_training.participate.value;
		http = comment.split("http");
		www = comment.split("www");
		if (http.length > 1 || www.length > 1){
			alert("Please don't put your website address here");
			document.frm_training.participate.focus();
			return false;
		}
	}
	/*
	if (isEmpty(document.frm_training.address.value))
	{
		document.frm_training.address.focus();
		alert ("Please complete the Address field");
		return false;
	}
	if (isEmpty(document.frm_training.phone.value))
	{
		document.frm_training.phone.focus();
		alert ("Please complete the Phone field");
		return false;
	}
	*/
	if (isEmpty(document.frm_training.email.value))
	{
		document.frm_training.email.focus();
		alert ("Please complete the Email field");
		return false;
	}
	if (!emailCheck(document.frm_training.email.value) || hasWhitespace(document.frm_training.email.value))
	{
		alert ("The email address appears to be invalid - please check and re-enter");
		document.frm_training.email.focus();
		return false;
	}
	
}
function contact_information(){
	//cc hcw scgem sec cah bcca mtcc msc
	if (isEmpty(document.sendForm.sfgod.value) && isEmpty(document.sendForm.ccnc.value) && isEmpty(document.sendForm.scgem.value) && isEmpty(document.sendForm.sec.value) && isEmpty(document.sendForm.cah.value) && isEmpty(document.sendForm.hcw.value) && isEmpty(document.sendForm.mtcc.value) && isEmpty(document.sendForm.msc.value)){
		document.sendForm.sfgod.focus();
		alert ("Please complete the Qty of Children in Crisis: A New Commitment field");
		return false;
	}
	if (isEmpty(document.sendForm.name.value))
	{
		document.sendForm.name.focus();
		alert ("Please complete the Name field");
		return false;
	}
	if (isEmpty(document.sendForm.mailing.value))
	{
		document.sendForm.mailing.focus();
		alert ("Please complete the Mailing Address field");
		return false;
	}
	if (isEmpty(document.sendForm.email.value))
	{
		document.sendForm.email.focus();
		alert ("Please complete the Email field");
		return false;
	}
	if (valid_email(document.sendForm.email.value) == false || hasWhitespace(document.sendForm.email.value))
	{
		alert ("The email address appears to be invalid - please check and re-enter");
		document.sendForm.email.focus();
		return false;
	}
	if (isEmpty(document.sendForm.verify.value))
	{
		document.sendForm.verify.focus();
		alert ("Please complete the Verify Email field");
		return false;
	}
	if (valid_email(document.sendForm.verify.value) == false || hasWhitespace(document.sendForm.verify.value))
	{
		alert ("The email address appears to be invalid - please check and re-enter");
		document.sendForm.verify.focus();
		return false;
	}
	if (document.sendForm.email.value != document.sendForm.verify.value){
		alert ("The email address is not the same - please check and re-enter");
		document.sendForm.verify.focus();
		return false;
	}
	if (document.sendForm.commends.value != ""){
		comment = document.sendForm.commends.value;
		http = comment.split("http");
		www = comment.split("www");
		if (http.length > 1 || www.length > 1){
			alert("Please don't put your website address here");
			return false;
		}
	}
	//alert(document.sendForm.email.value);
	document.sendForm.submit();
}
