// JavaScript Document
function winopen(theURL,winname,features) { 
    window.open(theURL,winname,features)
}

function validateZip(){
	var zip = document.Nutellastorelocator.zip.value;
	if (isBlank(zip) || zip.length < 5 || !isNumber(zip)) {
	    alert("You must enter a valid zip code.");
		//window.location = "storeLocator.htm";
	    return false;
	} else {
		document.Nutellastorelocator.submit();
		return true;
	}
}
//Breakfast Builder Age Verify
   function verifyAndSubmit4() {
	var theForm = document.agecheck;
	
	var month = theForm.month.value;
	if (isBlank(month) || !isNumber(month) || month >12 || month < 1) {
	    alert("You must enter your birth month: (01 - 12)");
		window.location = "index.htm";
	    return false;
	}
	
	var day = theForm.day.value;
	if (isBlank(day) || !isNumber(day) || day >31 || day <1) {
	    alert("You must enter your birth day: (01 - 31)");
		window.location = "index.htm";
	    return false;
	}
	
	var year = theForm.year.value;
	if (isBlank(year) || year.length < 4 || !isNumber(year)) {
	    alert("You must enter your complete birth year.");
		window.location = "index.htm";
	    return false;
	}
	return true
   }


// Tell a friend functions
   function verifyAndSubmit3() {
	var theForm = document.eMailer;
	
	var month = theForm.month.value;
	if (isBlank(month) || !isNumber(month) || month >12 || month < 1) {
	    alert("You must enter your birth month: (01 - 12)");
		window.location = "tell.htm";
	    return false;
	}
	
	var day = theForm.day.value;
	if (isBlank(day) || !isNumber(day) || day >31 || day <1) {
	    alert("You must enter your birth day: (01 - 31)");
		window.location = "tell.htm";
	    return false;
	}
	
	var year = theForm.year.value;
	if (isBlank(year) || year.length < 4 || !isNumber(year)) {
	    alert("You must enter your birth year.");
		window.location = "tell.htm";
	    return false;
	} else {
		return checkAge3();
	}
	return true
   }
   
function checkAge3() {
			/* the minumum age you want to allow in */
			var min_age = 18;

			var year = parseInt(document.forms["eMailer"]["year"].value);
			var month = parseInt(document.forms["eMailer"]["month"].value) - 1;
			var day = parseInt(document.forms["eMailer"]["day"].value);

			var theirDate = new Date((year + min_age), month, day);
			var today = new Date;

			if ( (today.getTime() - theirDate.getTime()) < 0) {
				//alert("You must be 18 years or older to use this feature.");
				document.eMailer.reset();
				window.location = "http://www.nutellausa.com/"
				return false;
			}
			else {
				return mailThisUrl();
			}
		}

var initialsubj="Nutella... it's Spreadably Delicious!TM"
var initialmsg="Hi, I want to Spread the Word to you about Nutella(R), the Original, Creamy Chocolaty Hazelnut SpreadTM. Nutella is a delicious hazelnut spread with quality ingredients that moms can serve as part of a balanced breakfast. To find out where you can buy it, get one-of-a-kind Nutella(R) brand merchandise, or enter to win a free case of Nutella(R) spread, click here: http://www.nutellausa.com -"
var secondmsg="Ferrero U.S.A., Inc. only transmits this message. The sender's name and e-mail address were not verified. Please delete if you do not wish to receive this message. (c)Ferrero."
var good; 

function checkEmailAddress(field) {
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\..{2,2}))$)\b/gi);
if (goodEmail) {
good = true;
window.location = "tell.htm";
}
else {
alert('Please enter a valid address.');
window.location = "tell.htm";
good = false;
   }
}

function mailThisUrl() {
good = false
checkEmailAddress(document.eMailer.email);
if (good) {
window.location = "mailto:"+document.eMailer.email.value+"?subject="+initialsubj+"&body="+initialmsg+"%20%20%20%20"+secondmsg
   }
   return true
}

// Verify got Case a month sweeps
   function verifyAndSubmit2() {
	var theForm = document.entryform;
	
	var month = theForm.month.value;
	if (isBlank(month) || !isNumber(month) || month >12 || month < 1) {
	    alert("You must enter your birth month: (01 - 12)");
	    return false;
	}
	
	var day = theForm.day.value;
	if (isBlank(day) || !isNumber(day) || day >31 || day <1) {
	    alert("You must enter your birth day: (01 - 31)");
	    return false;
	}
	
	var year = theForm.year.value;
	if (isBlank(year) || year.length < 4 || !isNumber(year)) {
	    alert("You must enter your birth year.");
	    return false;
	}
	
	if (isBlank(theForm.first.value)) {
	    alert("You must enter your first name.");
	    return false;
	}

	if (isBlank(theForm.last.value)) {
	    alert("You must enter your last name.");
	    return false;
	}

	if (isBlank(theForm.email.value) || !(validateEmail(theForm.email.value))) {
	    alert("You must enter a valid email address.");
	    return false;
	}

	if (isBlank(theForm.street1.value)) {
	    alert("You must enter your street address.");
	    return false;
	}

	if (isBlank(theForm.city.value)) {
	    alert("You must enter your city.");
	    return false;
	}

	if (isBlank(theForm.state.value)) {
	    alert("You must enter your state.");
	    return false;
	}

	var zip = theForm.zip.value;
	if (isBlank(zip) || zip.length < 5 || !isNumber(zip)) {
	    alert("You must enter a valid zip code.");
	    return false;
	}

	//check the question 2 response
	if (theForm.question2.value == "Other" || theForm.question2.value == "Select One") {
		if (theForm.question2a.value.length <=0) {
			alert("You must enter how you heard about Nutella.");
			return false;
		} 
	} else {
		clearQuestion2a();
	}
	
	// check question 3 response
	if (theForm.question3.value == "Select One") {
	alert("You must answer Question 3.");
			return false;
	}
	
	// check question 4 response
	if (theForm.question4.value == "Select One") {
	alert("You must answer Question 4.");
			return false;
	}

	// check question 5 response
	if (theForm.question5.value == "Select One") {
	alert("You must answer Question 5.");
			return false;
	}
	
	if (theForm.rulesCheckBox.checked == false) {
	    alert("You must read the contest rules.");
	    return false;
	} else {
	return checkAge2()
	}

	return true;
    }

	function clearQuestion2a() {
		document.entryform.question2a.value = "";
	}
	
	function checkAge2() {
			/* the minumum age you want to allow in */
			var min_age = 18;

			var year = parseInt(document.forms["entryform"]["year"].value);
			var month = parseInt(document.forms["entryform"]["month"].value) - 1;
			var day = parseInt(document.forms["entryform"]["day"].value);

			var theirDate = new Date((year + min_age), month, day);
			var today = new Date;

			if ( (today.getTime() - theirDate.getTime()) < 0) {
				//alert("You must be 18 years or older to enter");
				document.entryform.reset();
				window.location = "http://www.nutellausa.com/"
				return false;
			}
			else {
				return true;
			}
		}



// Verify for Despereaux sweeps
    function verifyAndSubmit() {
	var theForm = document.sweeps;
	if (isBlank(theForm.fname.value)) {
	    alert("You must enter your first name.");
	    return false;
	}

	if (isBlank(theForm.lname.value)) {
	    alert("You must enter your last name.");
	    return false;
	}

	if (isBlank(theForm.email.value) || !(validateEmail(theForm.email.value))) {
	    alert("You must enter a valid email address.");
	    return false;
	}

	var month = theForm.month.value;
	if (isBlank(month) || !isNumber(month)) {
	    alert("You must enter your birth month.");
	    return false;
	}
	
	var day = theForm.day.value;
	if (isBlank(day) || !isNumber(day)) {
	    alert("You must enter your birth day.");
	    return false;
	}
	
	var year = theForm.year.value;
	if (isBlank(year) || year.length < 4 || !isNumber(year)) {
	    alert("You must enter your birth year.");
	    return false;
	}

//ALERT IF USER IS UNDER 18 YEARS OLD
	//if (year > 1990){
	//	alert("You must be 18 or older to enter")
	//	window.location = "http://www.nutellausa.com/"
	//	return false;
	//}
	
	if (isBlank(theForm.street.value)) {
	    alert("You must enter your street address.");
	    return false;
	}

	if (isBlank(theForm.city.value)) {
	    alert("You must enter your city.");
	    return false;
	}

	//var state = theForm.state.value;
	//if (state = "--") {
	    //alert("You must choose your state.");
	    //return false;
	//}

	var zip = theForm.zip.value;
	if (isBlank(zip) || zip.length < 5 || !isNumber(zip)) {
	    alert("You must enter a valid zip code.");
	    return false;
	}

	if (theForm.rulesCheckBox.checked == false) {
	    alert("You must read the contest rules.");
	    return false;
	}
	else {
	return checkAge()
	}

	return true;
    }


    function isBlank(s) {
	for(var i = 0 ; i < s.length ; i++ ) {
	    var c = s.substr(i, 1); 
	    if((c != " " ) && (c != "\n" ) && (c != "\t" )) 
		return false;
	    }
	return true;
    }

    function isNumber(s) {
	for(var i = 0 ; i < s.length ; i++ ) {
	    var c = s.substr(i, 1); 
	    if ((c != "1") &&
		(c != "2") &&
		(c != "3") &&
		(c != "4") &&
		(c != "5") &&
		(c != "6") &&
		(c != "7") &&
		(c != "8") &&
		(c != "9") &&
		(c != "0"))
		return false;
	    }
	return true;
    }

    function validateEmail(em) {
	var atIdx = em.indexOf("@");
	var dotIdx = em.lastIndexOf(".");
	if (atIdx < 0 || dotIdx < 0)
	    return false;
	var chunk1 = em.slice(0, atIdx);
	var chunk2 = em.slice(atIdx + 1, dotIdx);
	var chunk3 = em.slice(dotIdx + 1);
	if (chunk1.length < 2 || chunk2.length < 2 || chunk3.length < 2)
	    return false;
	return true;
    }
	
	function checkAge() {
			/* the minumum age you want to allow in */
			var min_age = 18;

			/* change "age_form" to whatever your form has for a name="..." */
			var year = parseInt(document.forms["sweeps"]["year"].value);
			var month = parseInt(document.forms["sweeps"]["month"].value) - 1;
			var day = parseInt(document.forms["sweeps"]["day"].value);

			var theirDate = new Date((year + min_age), month, day);
			var today = new Date;

			if ( (today.getTime() - theirDate.getTime()) < 0) {
				alert("You must be 18 years or older to enter");
				window.location = "http://www.nutellausa.com/"
				return false;
			}
			else {
				return true;
			}
		}
		
		
function disclaimer(passedWebsite)
{
	if(confirm('You are about to leave www.nutellausa.com. Ferrero U.S.A. does not operate the destination website and different terms of use and privacy policy will apply'))
	{
		window.open(passedWebsite,'NewWin','toolbar,status,resizable,location,scrollbars,menubar')
	}
}

function confirmation1() {
	var answer = confirm("You are about to leave www.nutellausa.com. Ferrero U.S.A.,Inc. does not operate the destination website and different terms of use and privacy policy will apply.")
	if (answer){
	window.open('http://www.health.gov/dietaryguidelines/dga2005/report','new');
	}
}

function confirmation2() {
	var answer = confirm("You are about to leave www.nutellausa.com. Ferrero U.S.A.,Inc. does not operate the destination website and different terms of use and privacy policy will apply.")
	if (answer){
	window.open('http://www.mypyramid.gov','new');
	}
}