// THIS FUNCTION VALIDATES THE DATA IN THE FORM
function checkData(age) {

	if(!age) { age = 18; }

	if(document.myform.first) {
		if(!validate(document.myform.first)) {
			error_mess("first");
			document.myform.first.focus();
			return false;
		}
	}

	if(document.myform.last) {
		if(!validate(document.myform.last)) {
			error_mess("last");
			document.myform.last.focus();
			return false;
		}
	}

	if(document.myform.address) {
		if(!validate(document.myform.address)) {
			error_mess("address");
			document.myform.address.focus();
			return false;
		}
	}

	if(document.myform.city) {
		if(!validate(document.myform.city)) {
			error_mess("city");
			document.myform.city.focus();
			return false;
		}
	}

	if(document.myform.state) {
		if(!validate(document.myform.state)) {
			error_mess("state");
			document.myform.state.focus();
			return false;
		}
	}

	if(document.myform.zip) {
		if(!validate(document.myform.zip)) {
			error_mess("zip");
			document.myform.zip.focus();
			return false;
		}
	}

	if(document.myform.gender) {
		if(document.myform.gender.value || document.myform.gender.value === ""){
			if(!validate(document.myform.gender)) {
				error_mess("gender");
				document.myform.gender.focus();
				return false;
			}
		}else{
			var genderSet = false;
			for (i = document.myform.gender.length; i > 0; i--){
				if(document.myform.gender[i-1].checked){
					genderSet = true;
				}
			}
			if(!genderSet){
				error_mess("gender");
				return false;
			}
		}
	}

	if(document.myform.email) {
		if(!validate(document.myform.email)) {
			error_mess("email");
			document.myform.email.focus();
			return false;
		}

		if (isEmail(document.myform.email.value) == false) {
			alert("Please enter a valid email address.");
			document.myform.email.focus();
			return false;
		}
	}

	if(document.myform.dateofbirth) {
		if(!validate(document.myform.dateofbirth)) {
			error_mess("dateofbirth");
			document.myform.dateofbirth.focus();
			return false;
		}

		if(!Agechecker(age)) {
			return false;
		}
	}

	// IF WE HAVE THE ZIPCODE IN THE FORM VALIDATE IT AS WELL
	if (document.myform.zip) {
		if(!validate(document.myform.zip)) {
			error_mess("zip");
			document.myform.zip.focus();
			return false;
		}
	}

	if(document.myform.iagree) {
		if (!document.myform.iagree.checked) {
			alert("In order to continue, you must check the box labeled I Agree.");
			document.myform.iagree.focus();
			return false;
		}
	}

	return true;

}

// THIS FUNCTION DISPLAYS ERROR MESSAGES BASED ON THE VALIDATION
function error_mess(field) {

	switch(field) {

		case "first": {
			alert("Please enter your first name.");
			break;
		}

		case "last": {
			alert("Please enter your last name.");
			break;
		}

		case "address": {
			alert("Please enter your street address.");
			break;
		}

		case "phone": {
			alert("Please enter your phone number.");
			break;
		}

		case "city": {
			alert("Please enter your city.");
			break;
		}

		case "state": {
			alert("Please enter your state.");
			break;
		}

		case "zip": {
			alert("Please enter your zip.");
			break;
		}

		case "email": {
			alert("Please enter your email address.");
			break;
		}

		case "dateofbirth": {
			alert("Please enter your date of birth.");
			break;
		}

		case "gender": {
			alert("Please select your Gender.");
			break;
		}

	}

}


// THIS FUNCTION SETS A COOKIE
function setCookie(name, value, hours) {
    if (!hours) hours = 168;
    var expdate = new Date();
    expdate.setTime(expdate.getTime() + hours*60*60*1000);
    document.cookie = name + "=" + escape(value) + "; expires=" + expdate.toGMTString();
    document.cookie = "jsdtest=123";
}

// THIS validate FUNCTION JUST TEST TO SEE IF A FIELD EXISTS
function validate(item) {
	if(item) {
		if(!exists(item)) { return 0; }
		else { return 1; }
	}
}

// THIS FUNCTION CHECKS A VALUE TO SEE IF IT IS EMPTY OR NOT
function exists(item) {
	if(item.value!="") { return true; }
	else { return false; }
}



//////////////////////////////
// THIS IS ALL THE AGE STUFF//
//////////////////////////////

// FIX THE YEAR IF IT'S NOT 2000
function getYear(d) { return (d < 1000) ? d + 1900 : d; }

// FUNCTION WHICH VALIDATES A DATE ENTERED AS REAL/EXISTS
function isDate (year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1;  // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ( (getYear(tempDate.getYear()) == year) &&
		(month == tempDate.getMonth()) &&
		(day == tempDate.getDate()) )
		return true;
	else
		return false;
}

// FUNCTION THAT GETS TODAY'S DATE
function getToday() {
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
	var m = date.getMonth() + 1;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	if (document.all) { var today = day + "/" + month + "/" + year;	}
	else { var today = month + "/" + day + "/" + year; }
	return today;
}

// FUNCTION THAT FIGURES OUT THE DIFFERNCE IN YEARS BETWEEN TWO DATES
function dateDiff(dob_array,now,min_age) {
 var year = parseFloat(dob_array["year"]);
 var month = parseFloat(dob_array["month"]) - 1;
 var day = parseFloat(dob_array["day"]);
 var userBday = new Date((year + min_age), month, day);
 var today = new Date;

 if ( (today.getTime() - userBday.getTime()) < 0) {
  return false;
 } else {
  return true;
 }
}


// MAIN FUNCTION: THIS WILL GET TODAY'S DATE, VALIDATE THE DATE
// ENTERED IN THE FORM, AND THEN COMPARE THE TWO TO MAKE SURE
// THE AMOUNT OF YEARS IN BETWEEN IS 13 OR OVER
function Agechecker(age) {
	var today = getToday();
	var dob_array = new Array();
	if (document.myform.dateofbirth.value == "") {
		alert("Please enter a value");
		document.myform.dateofbirth.focus();
		return false;
	}
	else {
		var dateString = document.myform.dateofbirth.value;
		var year = dateString.substring(6,10);
		var month = dateString.substring(0,2);
		var day = dateString.substring(3,5);
		var test1 = dateString.substring(2,3);
		var test2 = dateString.substring(5,6);
		dob_array["year"]=year;
		dob_array["month"]=month;
		dob_array["day"]=day;

		if((test1=="/")&&(test2=="/")) {
			if (isDate(year,month,day)) {
				var difference = dateDiff(dob_array,today,age);
				if(difference == false) {
					if (document.myform.battlename.value == "ssbb") {
						setCookie('ssbb_sweeps', 'underage');
						alert("We're sorry, but you are not eligible for this sweepstakes. Your personal information has not been saved. You can visit www.nintendo.com for more information on Nintendo products.");
						window.location.replace('/pages/features/character-battle/index.php');
						window.location.reload();
					} else {
						alert("You are too young");
					}
					return false;
	    		    if(age != 13)
	    		    {
						setCookie('gs_minor', 'invalid', 1);
    	    			window.location="/index.html";
	    		    }
			        return false;
				}
			}
			else {
				alert("That date does not exist");
				document.myform.dateofbirth.focus();
				document.myform.dateofbirth.value="";
				return false;
			}
			return true;
		}
		else {
			alert("That date is not in the proper format: mm/dd/yyyy");
			document.myform.dateofbirth.focus();
			document.myform.dateofbirth.value="";
			return false;
		}
	}
}

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function isProper(string) {
    if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
}

function isan(string) {
    if (string.length == 0)
        return false;
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;

    return true;
}

function charCount(string,num,field) {
	if (string.length != num) {
		alert("You must have "+num+" characters in the "+field+" field");
		return false;
	}
	else {
		return true;
	}
}
