//Form Validation for Heart, Heart Lite, Heart Online

function ValidateFields() {
	var strng = document.dlDemo.telephone.value;
	var illegalChars = /[^ ]\W_/;
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');

	if (document.dlDemo.email.value.length > 0) {
		if (!checkEmail(document.dlDemo.email.value)) {
			return;
		}
	}

	if ((document.dlDemo.orgname.value != "") && !(illegalChars.test(document.dlDemo.orgname.value)) && (document.dlDemo.orgname.value.length != 1)) { //Organization Name
		if ((document.dlDemo.address.value != "") && !(illegalChars.test(document.dlDemo.address.value)) && (document.dlDemo.address.value.length != 1)) { //Address
			if ((document.dlDemo.city.value != "") && !(illegalChars.test(document.dlDemo.city.value)) && (document.dlDemo.city.value.length != 1)) { //City
				if (document.dlDemo.state.value != "" && (document.dlDemo.state.value.length != 1)) { //State
					if (document.dlDemo.zip.value != "" && (document.dlDemo.zip.value.length != 1) && !(isNaN(document.dlDemo.zip.value))) { //ZipCode
						if (!isNaN(parseInt(stripped)) || !(stripped.length < 10)) { //Telephone
							if (document.dlDemo.email.value != "" && document.dlDemo.email.value.length > 0) {
								if ((document.dlDemo.contact.value != "") && !(illegalChars.test(document.dlDemo.contact.value))) //Contact Name
								{
									document.dlDemo.submit();
								}
								else
								{
									alert("You have not properly filled in all of the required fields.\nContact cannot contain symbols (ex: ,-+=)")
									document.dlDemo.contact.focus()
								}
							}
							else {
								alert("You have not properly filled in all required fields.");
								document.dlDemo.email.focus();
							}
						}
						else
						{                                                           
							alert("You have not properly filled in all of the required fields.\nPhone number requires at least 10 digits (no characters)")
							document.dlDemo.telephone.focus()
						}
					}
					else
					{
						alert("You have not properly filled in all of the required fields.  Zip code cannot contain letters")
						document.dlDemo.zip.focus()
					}
				}
				else
				{
					alert("You have not properly filled in all of the required fields.")
					document.dlDemo.state.focus()
				}
			}
			else
			{
				alert("You have not properly filled in all of the required fields.\nCity cannot contain symbols (ex: ,-+=)")
				document.dlDemo.city.focus()
			}
		}
		else
		{
			alert("You have not properly filled in all of the required fields.\nAddress cannot contain symbols (ex: ,-+=)")
			document.dlDemo.address.focus()
		}
	}
	else
	{
		alert("You have not properly filled in all of the required fields.\nOrganization Name cannot contain symbols (ex: ,-+=)")
		document.dlDemo.orgname.focus()
	}
}


function ValidateFieldsHFSLiveDemo() {
	var strng = document.dlDemo.telephone.value;
	var illegalChars = /[^ ]\W_/;
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');

	if (document.dlDemo.email.value.length > 0) {
		if (!checkEmail(document.dlDemo.email.value)) {
			return;
		}
	}
					
	if (!isNaN(parseInt(stripped)) || !(stripped.length < 10)) { //Telephone
		if (document.dlDemo.email.value != "" && document.dlDemo.email.value.length > 0) {
			if ((document.dlDemo.contact.value != "") && !(illegalChars.test(document.dlDemo.contact.value))) //Contact Name
			{
				document.dlDemo.submit();
			}
			else
			{
				alert("You have not properly filled in all of the required fields.\nContact cannot contain symbols (ex: ,-+=)")
				document.dlDemo.contact.focus()
			}
		}
		else {
			alert("You have not properly filled in all required fields.");
			document.dlDemo.email.focus();
		}
	}
	else
	{                                                           
		alert("You have not properly filled in all of the required fields.\nPhone number requires at least 10 digits (no characters)")
		document.dlDemo.telephone.focus()
	}			
}



function checkEmail(strng) {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
		alert("Please enter a valid email address.\n");
		document.dlDemo.email.focus();
		return false;
    }
    else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
			alert("Please enter a valid email address.\n");
			document.dlDemo.email.focus();
			return false;
		}
		else {
			return true;
		}
	}
}

////phone
//var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
////strip out acceptable non-numeric characters
//if (isNaN(parseInt(stripped))) {
//   error = "The phone number contains illegal characters.";
//}