﻿var ref_mm, ref_dd, ref_yy;

function $$(id) {
	return document.getElementById(id);
}

// Sets up the basic functions needed for 
// each of the DOB field
function setupDOBFields(monthID, dayID, yearID) {
	var mm = $$(monthID);
	var dd = $$(dayID);
	var yy = $$(yearID);

	var items = [
		[mm, 'MM'],
		[dd, 'DD'],
		[yy, 'YYYY']
	];

	for (var i = 0; i < items.length; i++) {
		items[i][0].value = items[i][1];
		items[i][0].defaultValue = items[i][1];
		items[i][0].onfocus = function(event) {
			clearOnFocus(this, this.defaultValue);
		};
		items[i][0].onblur = function(event) {
			resetOnBlur(this, this.defaultValue);
		};
		items[i][0].onkeypress = function(event) {
			return restrictToNumeric(event);
		};
	}

	// Setup an auto-tabbing ability for the fields
	mm.onkeyup = function(event) {
		if (!isControlKey(getKeyCode(event)) && this.value.match(/^[0-9]{2}$/)) {
			dd.focus();
		}
		return true;
	};
	dd.onkeyup = function(event) {
		if (!isControlKey(getKeyCode(event)) && this.value.match(/^[0-9]{2}$/)) {
			yy.focus();
		}
		return true;
	};
	yy.onkeyup = function(event) {
		if (!isControlKey(getKeyCode(event)) && this.value.match(/^[0-9]{4}$/)) {

		}
		return true;
	};

	focusDOBMonth(monthID);

	// Store the id references for later use
	ref_mm = monthID;
	ref_dd = dayID;
	ref_yy = yearID;
}

// Focuses the cursor on the "month" field
function focusDOBMonth(monthID) {
	var mm = $$(monthID);
	if (mm) {
		// Focus the box directly which clears the text
		mm.focus();

		// The first time we hit the page we should repopulate
		// the text to MM because the hinting on focus clears it
		mm.value = mm.defaultValue;

		// Select the text in the box so the first typing overwrites
		mm.select();
	}
}


// this function will submit the av form
function validateDOBForm() {

	var mm = $$(ref_mm).value;
	var dd = $$(ref_dd).value;
	var yy = $$(ref_yy).value;

	if (!isValidDate(yy, mm, dd)) {
		showNotification('Please enter a valid birth date.');
		return false;
	}

	if (yy.length < 4) {
		showNotification('Please enter a 4-digit year.');
		return false;
	}

	return true;
}

function isValidDate(year, month, day) {
	var yy = year.toString();
	var mm = month.toString();
	var dd = day.toString();

	switch (yy.length) {
		case 3:
			yy = yy.substring(1, 3);
			// Allow dropcasing here to let the 2-digit
			// code apply to the chomped 3-digit

		case 2:
			if (yy.charAt(0) == '0') {
				yy = '20' + year;
			} else {
				yy = '19' + year;
			}
			break;

		case 1:
			yy = '200' + yy;
			break;
	}

	// Parse the integers out ensuring base 10 to prevent octal parsing
	yy = parseInt(yy, 10);
	mm = parseInt(mm, 10) - 1;
	dd = parseInt(dd, 10);

	var input = new Date(yy, mm, dd);
	var today = new Date();
	var years = today.getFullYear() - input.getFullYear();

	// Ensure that the date given is actually a real date, is equal to or
	// less than today, and that the person is less than or equal to 120 
	// years old.
	return (input.getMonth() == mm && input.getFullYear() == yy)
			&& (input <= today)
			&& (years <= 120);
}

function restrictToNumeric(event) {
	var keyCode = getKeyCode(event);
	return isNumeric(keyCode) || isControlKey(keyCode);
}

function getKeyCode(event) {
	var keyCode = -1;
	if (window.event) {								// IE
		keyCode = window.event.keyCode;
	} else if (event.which && event.which != 0) {	// Netscape/Opera/Mozilla
		keyCode = event.which;
	} else {										// All Others
		if (event.charCode == 0) {					// Firefox
			keyCode = event.keyCode;
		} else {
			keyCode = event.charCode;
		}
	}
	return keyCode;
}

function isNumeric(keyCode) {
	return keyCode >= 48 && keyCode <= 57;
}

function isControlKey(keyCode) {
	return (keyCode > 0 && keyCode <= 13) || keyCode == 37 || keyCode == 39;
}

function clearOnFocus(input, defaultValue) {
	if (input.value == defaultValue) {
		//input.value = '';
	}

	// Select the text in the box so the first typing overwrites
	input.select();

	// Hide any notifications
	hideNotification();
}

function resetOnBlur(input, defaultValue) {
	if (input.value == '') {
		input.value = defaultValue;
	}
}

function showNotification(message) {
	var n = $$('Notify');
	if (n) {
		$$('Notify').style.visibility = "visible";
		if (message) {
			n.innerHTML = message;
		}
	}
}

function hideNotification() {
	var n = $$('Notify');
	if (n) {
		$('Notify').style.visibility = "hidden";
		//n.innerHTML = "Enter your birth date to experience the Original Pilsner";
	}
}