/*
Mike Cleek
2.25.09
Version 1.0
This form will validate the www.AcademicFreedomPetition.com form.
If any required field is left blank an error will occur
It also has e-mail validation and zip code validation functions: echeck(), vcheck().
*/
function validateForm()
{
    var missingFieldsMsg = "Data invalid: \n\n";
	var missingFieldsCount = 0;	
	
	
    if(document.subscribeForm.first_name.value == null || document.subscribeForm.first_name.value == '')
    {
        missingFieldsMsg += "You must enter a First Name\n";
		missingFieldsCount++;
    }

    if(document.subscribeForm.last_name.value == null || document.subscribeForm.last_name.value == '')
    {
		
		missingFieldsMsg += "You must enter a Last Name\n";
		missingFieldsCount++;
    }

	if(document.subscribeForm.street.value == null || document.subscribeForm.street.value == '')
    {
		
		missingFieldsMsg += "You must enter a valid Address\n";
		missingFieldsCount++;
    }

	if(document.subscribeForm.city.value == null || document.subscribeForm.city.value == '')
    {
		
		missingFieldsMsg += "You must enter a City\n";
		missingFieldsCount++;
    }

	if(document.subscribeForm.state.value == null || document.subscribeForm.state.value == '')
    {
		
		missingFieldsMsg += "You must enter a State\n";
		missingFieldsCount++;
    }

var Zval = vcheck(document.subscribeForm.zip.value);
	if(document.subscribeForm.zip.value == null || document.subscribeForm.zip.value == '' || Zval == false)
    {
		
		missingFieldsMsg += "You must enter a Zip\n";
		missingFieldsCount++;
    }
	
	
		function vcheck(field) {
			var valid = "0123456789";
			
			if (field.length!=5) {
			return false;
			}
			for (var i=0; i < field.length; i++) {
				temp = "" + field.substring(i, i+1);
				if (valid.indexOf(temp) == "-1") {
				return false;
				}
			}
			return true;
		}

	

var Eval = echeck(document.subscribeForm.email.value);
	if(document.subscribeForm.email.value == null || document.subscribeForm.email.value == '' || Eval == false)
    {
		
		missingFieldsMsg += "You must enter a valid email address\n";
		missingFieldsCount++;
    }
		
		
		function echeck(str) {
		
				var at="@"
				var dot="."
				var lat=str.indexOf(at)
				var lstr=str.length
				var ldot=str.indexOf(dot)
				if (str.indexOf(at)==-1){
				   return false
				}
		
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				   return false
				}
		
				if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
					return false
				}
		
				 if (str.indexOf(at,(lat+1))!=-1){
					return false
				 }
		
				 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
					return false
				 }
		
				 if (str.indexOf(dot,(lat+2))==-1){
					return false
				 }
				
				 if (str.indexOf(" ")!=-1){
					return false
				 }
		
				 return true					
			}



	if(missingFieldsCount > 0)
	{
		alert(missingFieldsMsg + "\n Please retry.");	
		return false;
	}
    return true;
}


function submitFunction()
{
	if(validateForm() == true)
		document.subscribeForm.submit();
	
	else	
		return false;
}
