// JavaScript Document
/*function initForms(){
    for (var i = 0; i < document.forms.length; i++) {
        document.forms[i].onsubmit = function(){
            return validForm();
            
        }
    }
}*/


function validContactForm()
{
    var words;
    var result = true;

    with( document.contactForm)
    {
        words = txtName.value.split(" ");
        if( result == true &&
            (txtName.value == "" || words.length < 2) )
        {
            alert("Please enter your full name.");
            txtName.focus();
            txtName.select();
            result = false;
			
        }

       txtEmail.value.replace(/ /, "");
        txtPh1.value.replace(/ /, "");
        txtPh2.value.replace(/ /, "");
        txtPh3.value.replace(/ /, "");
        if( result == true )
        {
			if( txtEmail.value == "" &&
                (txtPh1.value == "" &&
                 txtPh2.value == "" &&
                 txtPh3.value == "") )
			{
                alert("Please enter a valid email address or phone number.");
                txtEmail.focus();
                result = false;
            }
        }

		if( result == true )
		{
			if( marketingSource.value == "" )
			{
				alert("Please enter a marketing source.");
				marketingSource.focus();
                result = false;
			}
		}

        if( result == true )
        {
            if( txtEmail.value != "" )
            {
                match = txtEmail.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
                if( match == null )
                {
                    alert("Please enter a valid email address.");
                    txtEmail.focus();
                    txtEmail.select();
                    result = false;
                }
            }
        }

        if( result == true &&
            (txtEmail.value == "" || txtPhone1.value != "" || txtPhone2.value != "" || txtPhone3.value != "") )
        {
            if( txtPhone1.value.match(/^[2-9][0-9][0-9]$/) == null )
            {
                alert("Please enter a valid area code.");
                txtPhone1.focus();
                txtPhone1.select();
                result = false;
            }

            if( result == true &&
                txtPhone2.value.match(/^\d\d\d$/) == null )
            {
                alert("Please enter a valid phone number prefix.");
                txtPhone2.focus();
                txtPhone2.select();
                result = false;
            }

            if( result == true &&
                txtPhone3.value.match(/^\d\d\d\d$/) == null )
            {
                alert("Please enter a valid phone number suffix.");
                txtPhone3.focus();
                txtPhone3.select();
                result = false;
            }
        }

    }
	
    return result;
}





function validForm(){
    var allGood = true;
    var allTags = document.getElementsByTagName("*");
    for (var i = 0; i < allTags.length; i++) {
        if (!validTag(allTags[i])) {
            allGood = false;
        }
    }
    return allGood;
    function validTag(thisTag){
        var outClass = "";
        var allClasses = thisTag.className.split(" ");
        for (var j = 0; j < allClasses.length; j++) {
            outClass += validBasedOnClass(allClasses[j]) + " ";
        }
        thisTag.className = outClass;
        if (outClass.indexOf("invalid") > -1) {
            thisTag.focus();
            window.alert("Please fill out the required field");
            if (thisTag.nodeName == "INPUT") {
                thisTag.select();
            }
            return false;
        }
        return true;
        function validBasedOnClass(thisClass){
            var classBack = "";
            switch (thisClass) {
                case "":
                case "invalid":
                    break;
                case "reqd":
                    if (allGood && thisTag.value == "") {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
                case "radio":
                    if (allGood && !radioPicked(thisTag.name)) {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                default:
                    classBack += thisClass;
            }
            return classBack;
        }
        
		function validEmail(email) {
			var invalidChars = " /:,;";
		
			if (email == "") {
				return false;
			}
			for (var k=0; k<invalidChars.length; k++) {
				var badChar = invalidChars.charAt(k);
				if (email.indexOf(badChar) > -1) {
					return false;
				}
			}
			var atPos = email.indexOf("@",1);
			if (atPos == -1) {
				return false;
			}
			if (email.indexOf("@",atPos+1) != -1) {
				return false;
			}
			var periodPos = email.indexOf(".",atPos);
			if (periodPos == -1) {	
				return false;
			}
			if (periodPos+3 > email.length)	{
				return false;
			}
			return true;
		}
		
		
        function radioPicked(radioName){
            var radioSet = "";
            
            for (var k = 0; k < document.forms.length; k++) {
                if (!radioSet) {
                    radioSet = document.forms[k][radioName];
                }
            }
            if (!radioSet) 
                return false;
            for (k = 0; k < radioSet.length; k++) {
                if (radioSet[k].checked) {
                    return true;
                }
            }
            return false;
        }
    }
}


/* Main contact form validation */


