// THIS FUNCTION VALIDATES THE DATA IN THE FORM
function checkData() {

        if(!validate(document.myform.element)) {
			error_mess("element");
			document.myform.element.focus();
			return false;
	}	
	
	if(!validate(document.myform.first)) {
			error_mess("first");
			document.myform.first.focus();
			return false;
	}

	if(!validate(document.myform.last)) {
			error_mess("last");
			document.myform.last.focus();
			return false;
	}
	
	if (isEmail(document.myform.email.value) == false) {
			alert("Please enter a valid email address.");
			document.myform.email.focus();
			return false;
	}
	
	if(!validate(document.myform.dateofbirth)) {
			error_mess("dateofbirth");
			document.myform.dateofbirth.focus();
			return false;
	}
	
	if(!Agechecker()) {
			return false;
	}
	
	if(!validate(document.myform.phone)) {
				error_mess("phone");
				document.myform.phone.focus();
				return false;
	}	
	
	if(!validate(document.myform.country)) {
			error_mess("country");
			document.myform.country.focus();
			return false;
	}	
	
	
	
		return true;
	
	}




// THIS FUNCTION DISPLAYS ERROR MESSAGES BASED ON THE VALIDATION
function error_mess(field) {

	switch(field) { 

		case "element": { 
			alert("Please select an answer");
			break;             
		}
		
		case "first": { 
			alert("Please enter a first name");
			break;             
		}

		case "last": { 
			alert("Please enter a last name");
			break;             
		}

		case "address": { 
			alert("Please enter a street address");
			break;             
		}

		case "phone": { 
			alert("Please enter a phone number");
			break;             
		}
		case "country": { 
			alert("Please enter a Country");
			break;             
		}

		case "phone": { 
			alert("Please enter a phone number");
			break;             
		}

		case "state": { 
			alert("Please enter a state");
			break;             
		}

		case "zip": { 
			alert("Please enter a zip");
			break;             
		}

		case "email": { 
			alert("Please enter a valid email");
			break;             
		}

		case "dateofbirth": { 
			alert("Please enter a valid date of birth");
			break;             
		}

		case "gender": { 
			alert("Please select a Gender");
			break;             
		}

	}

}



// THIS FUNCTION SETS A COOKIE
function setCookie(name, value, hours) {
    if (!hours) hours = 24;
    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(!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; }
}

// NOT SURE WHAT THIS DOES
function onSubmitAction() {
	gsSetOptionalCheckbox("optings")
	if(!gsRadioIsAnyItemSelected(document.myform.gender)) {
		document.myform.gender[0].checked = true;
		document.myform.gender[0].value = "NOT SELECTED";
	}
}

// NOT SURE WHAT THIS DOES
function gsRadioIsAnyItemSelected(oRadioButton) {
	// determine if any items are selected in a radio group
	for(var i = 0; i < oRadioButton.length; i++)
		if (oRadioButton[i].checked) return true;
	        return false;
}

// NOT SURE WHAT THIS DOES
function gsSetOptionalCheckbox(sCheckBoxName) {
	if (!document.myform[sCheckBoxName].checked) {
		document.myform[sCheckBoxName].checked = true;
		document.myform[sCheckBoxName].value = "NOT SELECTED";
	}
}

//////////////////////////////
// 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) {
 var min_age = 16;
 var year = parseInt(dob_array["year"]);
 var month = parseInt(dob_array["month"]) - 1;
 var day = parseInt(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() {
	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);
				if(difference == false) {
					alert("You are too young");
	    		    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;
	}
}