$(document).ready(function(){
	model = 'Benevole';

	$("#UserPostalCode1, #UserPostalCode2").keypress(function(e) {
		if (
			(e.which == 8 || e.which == 0) || // backspace(8), tab(9)
			(e.which >= 48 && e.which <= 57) || // [0-9]
			(e.which >= 65 && e.which <= 90) || // [A-Z]
			(e.which >= 97 && e.which <= 122))  // [a-z]
		{
			// allow the backspace
			if (e.which == 8 || e.which == 0) { return true; }
			// decide wether to change field focus by comparing the field length with the size field attr
			if (parseInt($(this).attr('value').length) == $(this).attr('size')) {
				// get the field number and add 1 to get the next field on focus
				fieldIndex = parseInt($(this).attr('id').charAt($(this).attr('id').length-1)) + 1;
				//the field name without the index
				fieldName  = $(this).attr('id').substr(0, $(this).attr('id').length-1);
				// set the next field on focus
				$("#"+fieldName+fieldIndex).focus();
				// place the left char in the next field if he is empty
				if ($("#"+fieldName+fieldIndex).attr('value') == '') {
					// IE and Opera put the char on the return true into the focus field, but not moz and safari 
					if ($.browser.mozilla || $.browser.safari) {
						$("#" + fieldName + fieldIndex).attr('value', String.fromCharCode(e.which));
					}
					return true;
				} else {
					return false;
				}
			}
		} else {
			// non-accepted char
			return false;
		}
	});


	$("#"+model+"DobY, #"+model+"DobM, #"+model+"DobD").keypress(function(e) {
		if (
			(e.which == 8 || e.which == 0) || // backspace(8), tab(9)
			(e.which >= 48 && e.which <= 57)) // [0-9]
		{
			// allow the backspace
			if (e.which == 8 || e.which == 0) { return true; }
			// decide wether to change field focus by comparing the field length with the size field attr
			if (parseInt($(this).attr('value').length) == $(this).attr('size')) {
				// get the field number and add 1 to get the next field on focus
				fieldIndex = parseInt($(this).attr('id').charAt($(this).attr('id').length-1)) + 1;
				//the field name without the index
				fieldName  = $(this).attr('id').substr(0, $(this).attr('id').length-1);
				// set the next field on focus
				$("#"+fieldName+fieldIndex).focus();
				// place the left char in the next field if he is empty
				if ($("#"+fieldName+fieldIndex).attr('value') == '') {
					// IE and Opera put the char on the return true into the focus field, but not moz and safari 
					if ($.browser.mozilla || $.browser.safari) {
						$("#" + fieldName + fieldIndex).attr('value', String.fromCharCode(e.which));
					}
					return true;
				} else {
					return false;
				}
			}
		} else {
			// non-accepted char
			return false;
		}
	});

	$("#"+model+"Phone1, #"+model+"Phone2, #"+model+"Phone3").keypress(function(e) {
		if ((e.which == 8 || e.which == 0) || (e.which >= 48 && e.which <= 57)) {
			if (e.which == 8 || e.which == 0) { return true; }
			if (parseInt($(this).attr('value').length) == $(this).attr('size')) {
				fieldIndex = parseInt($(this).attr('id').charAt($(this).attr('id').length-1)) + 1;
				fieldName  = $(this).attr('id').substr(0, $(this).attr('id').length-1);
				$("#"+fieldName+fieldIndex).focus();
				if ($("#"+fieldName+fieldIndex).attr('value') == '') {
					if ($.browser.mozilla || $.browser.safari) {
						$("#" + fieldName + fieldIndex).attr('value', String.fromCharCode(e.which));
					}
					return true;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	});
});