function $(id) {
	return document.getElementById(id);
}

// Sets up the basic functions needed for 
// each of the DOB field
function setupDOBFields() {
    var mm = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__monthTextBox');
    var dd = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__dayTextBox');
    var yy = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__yearTextBox');
	
	var items = [
		[mm, 'MM'], 
		[dd, 'DD'],
		[yy, 'YYYY']
	];
	hideNotification();
	for (var i=0; i<items.length; i++) {
		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;
	};
}
// Focuses the cursor on the "month" field
function focusDOBMonth() {
    var mm = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__monthTextBox');
	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 = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__monthTextBox').value;
    var dd = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__dayTextBox').value;
    var yy = $('plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneMain_MLFRAVForm__yearTextBox').value;
	
	if (!isValidDate(yy, mm, dd)) {
		showNotification('Please enter a valid date of birth.');
		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.display = "block";
		if (message) {
			n.innerHTML = "<img src='/Images/errorIcon.gif' alt='Error' />" + message;
		}
	}
}

function hideNotification() {
	var n = $('notify');
	if (n) {
		$('notify').style.display = "none";
	}
}
