﻿function validateControl(field, message)
{
    with (field)
    {
        if (value == "" || value == null)
        {
            alert(message);
            return false;
        }
        else
        {
            return true;
        }
    }
}
function validateEmail(field)
{
	with (field) 
	{
		atPosition = value.indexOf("@");
		lastDot = value.lastIndexOf(".");
		addr = value;
		tLength = addr.length;

		if (atPosition < 1 || lastDot-atPosition < 2 || tLength-lastDot < 3 ) 
		{			
			return false;
		}
		else 
		{
			return true;
		}
	}
}
function checkEmails(field, field2)
{
	pass = 0;
	if (field.value == "" || field.value == null) 
	{
		alert("Email address is blank.");
		pass = 1;
	}
	else
	{
		if (field2.value == "" || field.value == null)
		{
			aleart ("Confirmation email address is blank.");
			pass = 1;
		}
		else
		{
			if (field.value != field2.value)
			{
				alert("Provided emails do not match");
				pass = 1;
			}
			else
			{
				if (validateEmail(field) == false)
				{
					alert ("Email address is malformed.");
					pass = 1;
				}
				else
				{
					if (validateEmail(field2) == false)
					{
						alert ("Confirmation email address is malformed");
						pass = 1;
					}
				}
			}
		}
		if (pass == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
}
function validateForm(control)
{
	with (control)
	{
	    if (validateControl(fname, "Please fill in your first name.") == false) 
		{
	        fname.focus();
	        return false;
	    }
	    else if (validateControl(lname, "Please fill in your last name.") == false) 
		{
	        lname.focus();
	        return false;
	    }
	    else if (checkEmails(email, vEmail) == false) 
		{
			email.focus();
	        return false;
	    }
	    else if (validateControl(subject, "Please fill in a valid subject.") == false) 
		{
	        subject.focus();
	        return false;
	    }
	    else if (validateControl(desc, "If you need help, I need to know what the issue is concerning.\
	    please enter it in the description box.") == false) 
		{
	        desc.focus();
	        return false;
	    }
	}
}
function changeBackground(object, color)
{

	object.style.backgroundColor = color;
}
