/**
 *	Changes 01: October 08, swapped focus() for select() so that subsequent keypress is focused into
 *				the correct input rather than forcing jump onwards..
 */

function fx_dtd( event ){
	var element = Event.element(event);
	var boxId 	= element.id.substr(element.id.length-1);
	var boxStem = element.id.substr(0,element.id.length-1);
	if ( element.value == 'DD' || element.value == 'MM' || element.value == 'YYY' ){
		element.value = '';
	}
	if ( _fx_dtdValidKey(event.keyCode) ) 													// <<-- Keypad Numbers.. (excluding 0,1)
			{
			// ~~ Valid character...  establish where the keypress should go..  
			if ( boxId < 3 ){ // <<-- one of the first, on to the next..
				if ( element.value.length < 3 ) {
					// ~~ all good..
				} else {
					nextId = (parseFloat(boxId)*10 + 10)/10; // <<-- Grrring string concatenation!?
					fx_dtd_debug('Move: Keypress');
					$(boxStem+nextId).select(); // used in place of focus() - see above Change[01];.. 
				}
			} else {
				if ( element.value.length < 5 ) {
					// ~~ all good..
				} else {
					Event.stop(event);
				}
			}
	} else if ( event.keyCode == Event.KEY_BACKSPACE ) {
		if ( boxId > 1 && element.value.length == 0 ){
			prevId = (parseFloat(boxId)*10 - 10)/10; 	// <<-- Grrring string concatenation!?
			_fx_dtd_selfAfflicted = 1; 					// <<-- Stop TAB keypresses jumping from box to box.. 
			_fx_dtd_Blanker($(boxStem+nextId));
			fx_dtd_debug('Move: BACKSPACE');
			$(boxStem+prevId).select(); // used in place of focus() - see above Change[01];.. 
		}
	} else {
		// Other characters.. 
		switch ( event.keyCode ){
			case Event.KEY_BACKSPACE 	: // <<-- Ok, backspace is kinda handy!
			case Event.KEY_LEFT 		: // <<-- Ok, backspace is kinda handy!
			case Event.KEY_RIGHT 		: // <<-- Ok, backspace is kinda handy!
			case Event.KEY_DELETE 		: 
			case Event.KEY_TAB			: break;
			default						: Event.stop(event);
		}
	}
	fx_dtdFull(boxStem);
}

function fx_dtdKeyUp ( event ){	
	var element = Event.element(event);
	if( element.value ){	
		var boxId 	= element.id.substr(element.id.length-1);
		var boxStem = element.id.substr(0,element.id.length-1);
		if ( boxId < 3 ){ // <<-- one of the first, on to the next..
			if ( element.value.length == 2 ) {
				if ( _fx_dtdValidKey(event.keyCode) ) {
					nextId = (parseFloat(boxId)*10 + 10)/10; // <<-- Grrring string concatenation!?
					fx_dtd_debug('Move: KeyUp');
					$(boxStem+nextId).select(); // used in place of focus() - see above Change[01];.. 
					_fx_dtd_Blanker($(boxStem+nextId));
				}
			} 
		} else {
			if ( element.value.length > 4 ) {
				element.value = element.value.substring(0,4);
			}
		}
		fx_dtdFull(boxStem);
	}
}

function fx_dtdFull(sStem){
	//if ( $(sStem+1).value.length > 2 ) { $(sStem+1).value = $(sStem+1).value.substr(0,2); }
	//if ( $(sStem+2).value.length > 2 ) { $(sStem+2).value = $(sStem+2).value.substr(0,2); }
	//if ( $(sStem+3).value.length > 4 ) { $(sStem+3).value = $(sStem+3).value.substr(0,4); }
	if ( $(sStem+1).value > 31 ) { $(sStem+1).value = 31; }
	if ( $(sStem+2).value > 12 ) { $(sStem+2).value = 12; }
	if ( 	$(sStem+1).value.length == 2 && $(sStem+1).value != 'DD' && 
			$(sStem+2).value.length == 2 && $(sStem+2).value != 'MM' && 
			$(sStem+3).value.length == 4 && $(sStem+3).value != 'YYY'
		){
		$(sStem+'full').value  = $(sStem+3).value+'-'+$(sStem+2).value+'-'+$(sStem+1).value;
		$(sStem+'icon').src 	= '/r/i/iface/tooltip-green.gif';
		$(sStem+'icon').title 	= "Date Format is OK.";
		if ( block = $(sStem+1).up('.f-missing') ) {
			block.removeClassName('f-missing');
		}
	} else {
		$(sStem+'full').value  = '';
		if ( $(sStem+'icon').src.substr($(sStem+'icon').src.length-7) != 'pix.gif' ) {
			$(sStem+'icon').src 	= '/r/i/iface/tooltip-yellow.gif';
			$(sStem+'icon').title 	= "Please ensure you've entered the date in DD/MM/YYYY format";
		}
	}
}


function _fx_dtd_Blanker(ele){
	if( ele.value == 'MM' || ele.value == 'DD' || ele.value == 'YYYY' ){	
		ele.value = '';
		$(ele).removeClassName('new');
	} else {
		ele.select();
	}
}
function _fx_dtd_Filler(ele){
	if( ele.value == '' ) {
		var boxId 	= ele.id.substr(ele.id.length-1);
		var fills 	= ['','DD','MM','YYYY'];
		ele.value 	= fills[boxId]; 
		ele.addClassName('new');
	} else if ( ele.value > 0 && ele.value < 10 && ele.length < 2 ) {
		ele.value 	= 0 + ele.value; 
	}
}

function fx_dtd_debug(s){
	//console.log(s);
}



/**
 *	Is this a key we can accept as input?
 */
function _fx_dtdValidKey( keyCode ){
	if ( 
			( keyCode >= 48 && keyCode <= 57 ) || 										// <<-- Normal Number
			( keyCode >= 96 && keyCode <= 105 ) 										// <<-- Keypad Numbers.. (excluding 0,1)
		){
		return true;
	} else {
		return false;
	}
}
