/*Alumni Address Form JavaScript Validation*/

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)
        {
            invalidLabel(thisTag.parentNode);
            thisTag.focus(); // puts cursor in field
            if (thisTag.nodeName == "INPUT")
            {
                thisTag.focus(); // selects value
            }
            return false;
        }
        return true;
        
        function validBasedOnClass(thisClass)
        {
            var classBack = "";
            
            switch(thisClass)
            {
                case "":
                case "invalid":
                    break;
                case "reqd":
                    if (thisTag.value == "")
                    {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "post":
                    if (!validPost(thisTag.value))
                    {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "phone":                    
                    if (!validPhone(thisTag.value))
                    {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "email":
                    if (!validEmail(thisTag.value))
                    {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
				default:
					classBack += thisClass;
			}
			return classBack;
		}
        
        function validPost(post)
        {
            var postUSRegExp = /^\d{5}$/;
            var postPORegExp = /^\d{5}-\d{4}$/;
            var postCANRegExp = /^[A-Z]\d[0-9A-Z]\s\d[0-9A-Z]\d$/;
            if (!post.match(postUSRegExp) &&
                !post.match(postPORegExp) &&
                !post.match(postCANRegExp))
            {
                return false;
            }
            return true;
        }
        
        function validPhone(phone) 
        {
            if (phone == "") {
                return true;
            }
            else
            {
                var phoneUSRegExp = /^\d{3}-\d{3}-\d{4}$/;
                var phoneIntlRegExp = /^\d{1,3}-\d{2,3}-\d{9,12}$/;
                
                if (!phone.match(phoneUSRegExp) &&
                    !phone.match(phoneIntlRegExp))
                {
                    return false;
                }
             
                return true;
            }
        }
        
        function validEmail(email) 
        {
            var invalidChars = " /&^[\:,;%']()$";
        
            if (email == "") {
                return true;
            }
            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 invalidLabel(parentTag) 
        {
            if(parentTag.parentNode.nodeName == "LABEL")
            {
                parentTag.parentNode.className += " invalid";
            }
        }
    
    }
}


function autoYear() 
{
    var time = new Date();
    var year = time.getYear();
 
    if (year < 1900) 
    {
        year = year + 1900;
    }

    var date = year - 75; /*change the '25' to the number of years in the past you want to show */
    var future = year + 0; /*change the '0' to the number of years in the future you want to show */ 

    document.write("<select class=\"reqd\" size=\"1\" name=\"year\" id=\"year\"><option value=\"\" selected=\"selected\"></option>");
    
    do 
    {
        date++;
        document.write("<option value=\""+date+"\">"+date+"</option>");
    }
    while (date < future)
    document.write("</select>");
}



