

function TextAreaObject(name) {

	this.name = name;
	this.lastKey = 0;
	this.maxChar = 0;
	this.charCount = 0;
	this.entryField = "";
	this.counterField = "";
	this.hasReturnKey = false;
	this.value = "";

	this.init = TextAreaObject_init;
	this.keyDown = TextAreaObject_keyDown;
	this.keyUp = TextAreaObject_keyUp;

}

function TextAreaObject_keyDown(event) {
	
	isKeyDownHandled = true;
	keyDownReturnValue = true;

	object = eval(this.name + "_validator");
	
	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;
	status = "key pressed " + lastKey;
		
	if (lastKey == 17) {
		isKeyDownHandled = false;
		isCtrlKeyPressed = true;
		return true;		
	} 
	if (lastKey == 16) {
		isKeyDownHandled = false;
		isShiftKeyPressed = true;
		return true;		
	} 

	object.lastKey = lastKey;
	
	if (object.lastKey >= 112 && object.lastKey <= 123) {			
		isKeyDownHandled = false;
		return true;
	}
	
	if (object.lastKey == 9 ||
		object.lastKey == 18 ||
		object.lastKey == 20 ||
		object.lastKey == 27 ||
		object.lastKey == 34 ||
		object.lastKey == 35 ||
		object.lastKey == 36 ||
		object.lastKey == 37 || 
		object.lastKey == 38 || 
		object.lastKey == 39 || 
		object.lastKey == 40 ||
		object.lastKey == 45) {
			
		return true;
	}


	if (!object.hasReturnKey && object.lastKey == 13) {
		keyDownReturnValue = false;
		return false;
	}

	if (isCtrlKeyPressed && object.lastKey == 86) {
		return true;
		        
	}
		
	if (object.lastKey == 8 || object.lastKey == 46) {
		if (this.value.length > 0) object.charCount = this.value.length - 1;
		if (object.counterField != null) object.counterField.value = object.maxChar + "/" + object.charCount;
		return true;
	}	
	if (object.charCount == object.maxChar) {
		keyDownReturnValue = false;
		return false;	
	} 

	object.charCount = this.value.length + 1;
	if (object.counterField != null) object.counterField.value = object.maxChar + "/" + object.charCount;
		
	return true;
}

function TextAreaObject_keyUp(event) {

	isKeyUpHandled = true;
	keyUpReturnValue = true;

	object = eval(this.name + "_validator");

	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;

	if (lastKey == 17 ) {
		isKeyUpHandled = false;
		isCtrlKeyPressed = false;
		return true;
	} 
	if (lastKey == 16 ) {
		isKeyUpHandled = false;
		isShiftKeyPressed = false;
		return true;
	} 

	if (object.maxChar < this.value.length) { 
		this.value = object.value;
		return;
	}

    object.value = this.value;
	object.charCount = this.value.length;
	if (object.counterField != null) object.counterField.value = object.maxChar + "/" + object.charCount;

}

function TextAreaObject_init(anEntryField, aCounterField, aMaxChar, hasReturnKey) {

	this.lastKey = 0;
	this.maxChar = aMaxChar;
	this.charCount = 0;
	this.entryField = anEntryField;
	this.counterField = aCounterField;
	this.hasReturnKey = hasReturnKey;
	if (this.hasReturnKey == null) this.hasReturnKey = false;
	
	if (anEntryField != null) {
		anEntryField.onkeydown = this.keyDown;
		anEntryField.onkeyup = this.keyUp;
		registerObject(anEntryField.name);
	} else {
		if (aCounterField != null) aCounterField.value = "";
	}


}


function NumberObject(name) {

	this.name = name;
	this.lastKey = 0;
	this.size = 0;
	this.scale = 0;
	this.maxValue = null;
	this.entryField = "";

	this.init = NumberObject_init;
	this.keyDown = NumberObject_keyDown;
	this.keyUp = NumberObject_keyUp;

}

function NumberObject_keyDown(event) {
	
	isKeyDownHandled = true;
	keyDownReturnValue = true;

	object = eval(this.name + "_validator");
	object.lastValue = this.value;
	
	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;
	status = "key pressed " + lastKey;
		
	if (lastKey == 16 || lastKey == 17) {
		isKeyDownHandled = false;
		return true;		
	} 

	object.lastKey = lastKey;

	if (isShiftKeyPressed && object.hasSign) {
		if (object.lastKey != 49) {
			keyDownReturnValue = false;
			return false;
		}
	}
	
	if (object.lastKey >= 112 && object.lastKey <= 123) {			
		isKeyDownHandled = false;
		return true;
	}

	if (object.lastKey == 9) return true;
	if (object.hasSign && ((isShiftKeyPressed && object.lastKey == 49) || object.lastKey == 109 || object.lastKey == 189)) {
		if (this.value.charAt(0) == '-' || this.value.charAt(0) == '+') {
			keyDownReturnValue = false;
			return false;
		}
		if (this.value.length > 0 && getSelectionStart(this) > 0) {
			keyDownReturnValue = false;
			return false;
		}
		return true;
	}
	if (object.lastKey == 8 || object.lastKey == 46) return true;
	if (object.lastKey == 37 || object.lastKey == 39) return true;

	if ((object.lastKey > 47 && object.lastKey < 58) || (object.lastKey > 95 && object.lastKey < 106)) {
		maxLength = object.size;
		if (object.scale > 0) maxLength += 1;
					
		if (object.hasSign && (this.value.charAt(0) == '-' || this.value.charAt(0) == '+')) {
			temp = this.value.substr(1, this.value.length -1);
		} else {
			temp = this.value;
		}
		if (temp.length >= maxLength && getSelectionStart(this) < maxLength) {
			this.value = "";
			return true;
		}
		
		temp = temp.split(".");
		if (object.scale > 0 && temp[0].length == (object.size - object.scale) && getSelectionStart(this) < (object.size - object.scale)) {
			this.value = "";
			return true;
		}
		
		if (temp.length == 1) {
			if (temp[0].length < (object.size - object.scale)) return true;			
		} else if (temp.length == 2) {
			if ((temp[0].length + temp[1].length) < object.size) return true;	
		}

		keyDownReturnValue = false;
		return false;

	} 
	if (object.scale > 0 && object.lastKey == 190) {
		if (object.hasSign && this.value.charAt(0) == '-') 
			temp = this.value.substr(1, this.value.length -1);
		else
			temp = this.value;
		if (temp.length <= (object.size - object.scale) && this.value.indexOf(".") == "-1") return true
	}

	keyDownReturnValue = false;
	return false;
		

}

function NumberObject_keyUp(event) {

	isKeyUpHandled = true;
	keyUpReturnValue = true;

	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;

	if (lastKey == 16 || lastKey == 17) {
		isKeyUpHandled = false;
		return true;		
	} 

	object = eval(this.name + "_validator");
	temp = this.value.split(".");
	if (temp.length == 2) {
		if (temp[0].length > (object.size - object.scale)) this.value = object.lastValue;	
		if (temp[1].length > object.scale) this.value = object.lastValue;	
	}

}


function NumberObject_init(anEntryField, aSize, aScale, aMaxValue, hasSign) {

	this.lastValue = "";
	this.lastKey = 0;
	this.hasSign = hasSign;
	this.size = aSize;
	this.scale = aScale;
	this.maxValue = aMaxValue;
	this.entryField = anEntryField;
	
	if (this.hasSign == null) this.hasSign = false;

	if (anEntryField != null) {
		anEntryField.onkeydown = this.keyDown;
		anEntryField.onkeyup = this.keyUp;
	
		registerObject(anEntryField.name);
	}

}



function TimeObject(name) {

	this.name = name;
	this.lastKey = 0;
	this.entryField = "";

	this.init = TimeObject_init;
	this.keyDown = TimeObject_keyDown;
	this.keyUp = TimeObject_keyUp;

}

function TimeObject_keyDown(event) {
	
	isKeyDownHandled = true;
	keyDownReturnValue = true;

	object = eval(this.name + "_validator");
	object.lastValue = this.value;
	
	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;
	status = "key pressed " + lastKey;
		
	if (lastKey == 16 || lastKey == 17) {
		isKeyDownHandled = false;
		return true;		
	} 

	object.lastKey = lastKey;

	if (isShiftKeyPressed && object.lastKey == 190) {
		if (this.value.length == 3) {
			return false;
		}
		return true;
	}
	
	if (object.lastKey >= 112 && object.lastKey <= 123) {			
		return true;
	}

	if (object.lastKey == 9) return true;
	if (object.lastKey == 8 || object.lastKey == 46) return true;
	if (object.lastKey == 37 || object.lastKey == 39) return true;

	if ((object.lastKey > 47 && object.lastKey < 58) || (object.lastKey > 95 && object.lastKey < 106)) {
		if (this.value.length == 0 && ((object.lastKey > 47 && object.lastKey < 51) || (object.lastKey > 95 && object.lastKey < 99))) return true;

		if (this.value.length == 0 && ((object.lastKey > 47 && object.lastKey < 58) || (object.lastKey > 95 && object.lastKey < 58))) {
			this.value = "0";
			return true;
		}

		if (this.value.length == 1) {
			if ((this.value.charAt(0) == "0" || this.value.charAt(0) == "1") && ((object.lastKey > 47 && object.lastKey < 58) || (object.lastKey > 95 && object.lastKey < 106))) return true
			if (this.value.charAt(0) == "2" && ((object.lastKey > 47 && object.lastKey < 52) || (object.lastKey > 95 && object.lastKey < 100))) return true
		}
		if (this.value.length == 2 || this.value.length == 3) {
			if (this.value.length == 2 && this.value.split(":").length == 1) this.value = this.value + ":";
			if ((object.lastKey > 47 && object.lastKey < 54) || (object.lastKey > 95 && object.lastKey < 102)) return true
		}
		if (this.value.length == 4) {
			return true;
		}
		if (this.value.length == 5 && getSelectionStart(this) < 5) {
			if (((object.lastKey > 47 && object.lastKey < 51) || (object.lastKey > 95 && object.lastKey < 99))) {
				this.value = "";
				return true;
			}
			if (((object.lastKey > 47 && object.lastKey < 58) || (object.lastKey > 95 && object.lastKey < 58))) {
				this.value = "0";
				return true;
			}
		}
	} 


	keyDownReturnValue = false;
	return false;
		

}


function TimeObject_keyUp(event) {

	isKeyUpHandled = true;
	keyUpReturnValue = true;

	if (isNav) lastKey = event.which;
	if (isIE) lastKey = window.event.keyCode;

	if (lastKey == 16 || lastKey == 17) {
		isKeyUpHandled = false;
		return true;		
	} 
	if (lastKey == 8) return true;
	
	object = eval(this.name + "_validator");
	
	if (this.value.length == 2 && lastKey != 190) this.value = this.value + ":";
	
	temp = this.value;
	temp = temp.split(":");
	if (temp.length == 1) {
		if (parseInt(temp[0], 10) > 23) this.value = object.lastValue;		
	} else if (temp.length == 2) {
		if (parseInt(temp[0], 10) > 23) this.value = object.lastValue;		
		if (parseInt(temp[1], 10) > 59) this.value = object.lastValue;		
	} else if (temp[1].length > 2) {
		this.value = object.lastValue;
	} else if (temp[2].length > 2) {
		this.value = object.lastValue;
	} else if (temp.length > 2 ) {
		this.value = object.lastValue;		
	}
	
	
}

function TimeObject_init(anEntryField) {

	this.lastValue = "";
	this.lastKey = 0;
	this.entryField = anEntryField;
	
	if (anEntryField != null) {
		anEntryField.onkeydown = this.keyDown;
		anEntryField.onkeyup = this.keyUp;
	
		registerObject(anEntryField.name);
	}

}


function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else return o.selectionStart
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.value.length)
		return r.text.length
	} else return o.selectionEnd
}
