var oCookie = {
	createCookie: function (name,value,days) {
		
		if(days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}
		
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie: function (name) {
		
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}

		return false;
	},
	eraseCookie: function (name) {
		this.createCookie(name,"",-1);
	}
};

function checkAccess(sBirthday) {
	if((sCookie = oCookie.readCookie(sCookieName)) != false) {
		callBack(sCookie);
	} else {

		// check if date format is valid
		var isValid = isValidDate(sBirthday);
		if(isValid != false) {

			// check if the person is major
			var isMajor = isMajorPerson(sBirthday);
			if(isMajor) {
				sValue = "1";
			} else {
				sValue = "-1";
			}

			// create the cookie
			oCookie.createCookie(
				sCookieName,
				sValue
			);
			callBack(sValue);
		} else {
			alert(sInvalidMsg);
		}

	}
}

function isValidDate(sDate) {

	// check if the date is valid
	var sDateFormat = /^\d{2}(\-|\/|\.)\d{2}\1\d{4}$/;
	var isValid = sDateFormat.test(sDate);

	// check if date format is valid
	if (isValid) {

		var day = parseInt(sDate.substr(0,2), 10);
		var month = parseInt(sDate.substr(3,5), 10);
		var year = parseInt(sDate.substr(6,9), 10);

		// check if the day is valid
		if (day > 0 && day <= 31) {

			// check if the month is valid
			if (month > 0 && month <= 12) {
				isValid = true;
			} else {
				isValid = false;
			}
		} else {
			isValid = false;
		}

	} else {
		isValid = false;
	}

	return isValid;
}

function isMajorPerson(sDate) {

	var day = parseInt(sDate.substr(0,2));
	var month = parseInt(sDate.substr(3,5));
	var year = parseInt(sDate.substr(6,10));

	// get the date when the person will be major
	var dDate = new Date();
	dDate.setYear(year + 18);
	dDate.setMonth(month - 1);
	dDate.setDate(day);

	// check if the person became major until today (inclusively)
	var today = new Date();
	if (dDate <= today) {
		return true;
	} else {
		return false;
	}
		
}