/*
	***** Selection Routines *****
	
	A set of functions for manipulating selected text in 
	textboxes and textareas

	17/11/2005	CM	Added Checkbox functions
*/

// This javascript function will either replace the currently selected text, or if no text is selected, 
// will insert text at the current cursor position of a TextArea or Text Input control:
function SetSelectedText( elementID, text ){
	var obj = document.getElementById( elementID );
	if ( !obj ) throw "Element " + elementID + " not found!";
	// Netscape, Mozilla, Firefox
	if( typeof(obj.selectionStart) != "undefined" ){
		obj.focus();
		var start_selection = obj.selectionStart;
		var end_selection = obj.selectionEnd;
		if (end_selection < start_selection) {
			var temp = end_selection; 
			end_selection = start_selection;
			start_selection = temp;
		}
		// Sad, but true: You actually have to replace *all* of the text in
		// the text input to replace the selection in Mozilla browsers!
		var startText = (obj.value).substring(0, start_selection);
		var endText = (obj.value).substring(end_selection, obj.textLength);
		obj.value = startText + text + endText;
		obj.selectionStart = start_selection;
		obj.selectionEnd = start_selection + text.length;
	}
	// Internet Explorer
	if( obj.createTextRange ){
		obj.focus( obj.caretPos );
		obj.caretPos = document.selection.createRange().duplicate();
		obj.caretPos.text = text;
		obj.caretPos.moveStart( "character", text.length * -1 );
		obj.caretPos.select();
	}
	obj.focus();
}

//This javascript function will select a range of text in a TextArea or Text Input control:
function SelectRange( elementID, startOffset, length ){
	var obj = document.getElementById( elementID );
	if( !obj ) throw "Element " + elementID + " not found!";
	// Netscape, Mozilla, Firefox
	if( typeof(obj.selectionStart) != "undefined" ){
		obj.selectionStart = startOffset;
		obj.selectionEnd = startOffset + length;
		obj.focus();return;
	}
	// Internet Explorer
	if( obj.createTextRange ){
		obj.focus( obj.caretPos );
		var caretPos = document.selection.createRange().duplicate();
		caretPos.moveToElementText(obj);
		caretPos.collapse();
		caretPos.moveStart( "character", startOffset );
		caretPos.moveEnd( "character", length );
		caretPos.select();
	}
}

//This javascript function will return the text that is currently selected in a TextArea or Text Input control:
function GetSelectedText( elementID ){
	var obj = document.getElementById( elementID );
	if( !obj )throw "Element " + elementID + " not found!";
	// Netscape, Mozilla, Firefox
	if( typeof(obj.selectionStart) != "undefined" ){
		obj.focus();
		var start_selection = obj.selectionStart;
		var end_selection = obj.selectionEnd;
		if (end_selection < start_selection){
			var temp = end_selection;
			end_selection = start_selection;
			start_selection = temp;
		}
		return (obj.value).substring(start_selection, end_selection);
	}
	// Internet Explorer
	if( obj.createTextRange ){
		obj.focus( obj.caretPos );
		obj.caretPos = document.selection.createRange().duplicate();
		return obj.caretPos.text;
	}
}

// Select or deselect all checkboxes regardless of current 
function checkboxSetAll(curform, newvalue) {
	var inputs = curform.getElementsByTagName('INPUT');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].getAttribute('type')=='checkbox' && (arguments.length==2 || inputs[i].name==arguments[2])) { inputs[i].checked=newvalue; }
	}
}

// Switch selections - check unchecked, uncheck checked
function checkboxInvertSelection(curform) {
	var inputs = curform.getElementsByTagName('INPUT');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].getAttribute('type')=='checkbox' && (arguments.length==1 || inputs[i].name==arguments[1])) { 
			inputs[i].checked=!inputs[i].checked;
		}
	}
}

// Select ranges - check two spaced checkboxes and all between will be checked
function checkboxSelectRange(curform) {
	var inputs = curform.getElementsByTagName('INPUT');
	var inrange=false;
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].getAttribute('type')=='checkbox' && (arguments.length==1 || inputs[i].name==arguments[1])) { 
			if (inputs[i].checked) {
				inrange = !inrange;
			} else {
				inputs[i].checked=inrange; 
			}
		}
	}
}
		
