// JavaScript Document

//Form Validation for Form Name/ID: dataForm/Form ID

function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
	    ((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	    alert("You may only enter numerals in this field.");
		return false;
	}
	return true;
}


function checkFields(form) {
	if(form.sf_gender.value == "none"){
		alert("Please select a Gender.");
		form.sf_gender.focus();
		return false;
    }else if ( isBlank(form.sf_first_nme.value)) {
		// first name
        window.alert('Please enter your first name.');
        form.sf_first_nme.focus();
        return false;
    }else if ( isBlank(form.sf_last_nme.value)) {
		// last name
        window.alert('Please enter your last name.');
        form.sf_last_nme.focus();
        return false;
    }else if ( isBlank(form.sf_address1.value)) {
		// address
        window.alert('Please enter your address.');
        form.sf_address1.focus();
        return false;
    }else if ( isBlank(form.sf_postal_cd.value)) {
		// zip code
        window.alert('Please enter your zip code.');
        form.sf_postal_cd.focus();
        return false;
    }else if ( isBlank(form.sf_email_addr.value)) {
		// email address - blank
        window.alert('Please enter your email address.');
        form.sf_email_addr.focus();
        return false;
    }else if ( !checkEmailAddress(form.sf_email_addr.value) ) {
		// email address - validate
		form.sf_email_addr.value = "";
		alert("Please enter a valid email address.");
		form.sf_email_addr.focus();
		return false;
    }else if (form.sf_email_addr.value != form.email_addr_confirm.value) {
		// email address - reentry compare
		form.email_addr_confirm.value = "";
		alert("Please verify email address.");
		form.email_addr_confirm.focus();
		return false;
	}else if(form.sf_birth_year.value == "none"){
		// birth year - empty
		alert("Please select a Birth Year.");
		form.sf_birth_year.focus();
		return false;
	}else if(form.sf_birth_month.value == "none"){
		// birth month - empty
		alert("Please select a Birth Month.");
		form.sf_birth_month.focus();
		return false;
	}else if(form.sf_birth_day.value == "none"){
		// birth day - empty
		alert("Please select a Birth Day.");
		form.sf_birth_day.focus();
		return false;
	}else if(under18(form)){
		setAgeCookie();
		window.location = form.underageURL.value;
		return false;
	}
	return true;
}

function isBlank(s) {
    for( var i=0;i<s.length;i++ ) {
        var c = s.charAt(i);
        if( (c != ' ') && (c != '\n') && (c != '\t') )
            return false;
        }
        return true;
    }

function checkEmailAddress( emailStr ) {
    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars="\[^\\s" + specialChars + "\]"
    var quotedUser="(\"[^\"]*\")"
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+'
    var word="(" + atom + "|" + quotedUser + ")"
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    var matchArray=emailStr.match(emailPat)
    if (matchArray==null) {
        return false
    }
    var user=matchArray[1]
    var domain=matchArray[2]
    if (user.match(userPat)==null) {
        return false
    }
    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return false
            }
        }
        return true
    }
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        return false
    }
    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
        return false
    }
    // Make sure there's a host name preceding the domain.
    if (len<2) {
        return false
    }
    return true;
}

// Will determine if a user is under 18
function under18(form) {
    var now = new Date();
    var thisMonth = now.getMonth() + 1;
    var thisDay = now.getDate();
    var thisYear = now.getFullYear();
    var birthMonth = form.sf_birth_month.value;
    var birthDay = form.sf_birth_day.value;
    var birthYear = form.sf_birth_year.value;
    var user_age = thisYear - birthYear;
    var minimum_year = 18;
    var sameMonth = 0;
    var sameDay = 0;

    if (user_age < minimum_year) {
        return true;
    }

    if (user_age >= minimum_year) {
        return false;
    }

    if (thisMonth < birthMonth) {
        return true;
    }

    if (thisMonth > birthMonth) {
        return false;
    }

    if ((birthMonth == thisMonth) && (thisDay < birthDay)) {
        return true;  
    }

    if ((birthMonth == thisMonth) && (thisDay >= birthDay)) {
        return false;  
    }
    return false;
}

  

  function setAgeCookie() {
    today = new Date();
    tomorrow = new Date();
    tomorrow.setTime(today.getTime()+(60*60*24*1000));
    document.cookie='EmailAge=true; path=/; expires=' + tomorrow.toGMTString();
  }

function checkAgeCookie() {
	underage = document.cookie;
	if (underage.indexOf('EmailAge') > -1) {
		// Matt Bennett - I added this check to prevent the under 18 page from getting in a loop.
		if (window.location.search != "/offer/under-18") { 
			window.location = "/offer/under-18"; 
		}
	}
	
	checkAgeCookie();
}


