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

function winclose(){
	window.opener='x';
	window.close();
}


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;
    }


// 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;
	}

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

	return true;
    }

	
	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/"
				window.close();
				return false;
			}
			else {
				return true;
			}
		}