/*

	Allows alteration of code being pasted before paste occures
	
	>> Jason Taylor : 7 - 2 - 2003 <<

	example syntax:

			function onPasteEvent(text){
				return text.toLowerCase()
			}

			window.onload = function(){
				capturePasteEvent(onPasteEvent)
			}

*/



// Delcare Global Varibles
var _hidden_paste_div = false
var _paste_lock = false
var _paste_range = null
var _paste_text = ''
var _paste_function = null

// capture paste event
function capturePasteEvent(f1){
	// Assign code to run before pasting text
	_paste_function = f1 || function(text){ return text }
	// Assign function to event
	document.body.onpaste = function(){
		// Check to see if paste is locked
		if(!_paste_lock){
			// Check to see if hidden paste textarea exists
			if(!_hidden_paste_div){
				// Create a textarea and hide it off screen
				_hidden_paste_div = document.createElement('TEXTAREA')
				_hidden_paste_div.style.position = 'absolute'
				_hidden_paste_div.style.top = -100
				_hidden_paste_div.style.left = -100
				document.body.appendChild(_hidden_paste_div)
			}
			// Save current selection
			_paste_range = document.selection.createRange();
			// Lock paste event
			_paste_lock = true
			// Ini textarea to empty string
			_hidden_paste_div.value = ''
			// Set focus to hidden textarea
			_hidden_paste_div.focus()
			// Paste clipboard contents into hidden text area
			document.execCommand("paste")
			// Process text -  function
			_hidden_paste_div.value = _paste_function(_hidden_paste_div.value)
			// Select the altered text
			_hidden_paste_div.select()
			// Create a temp range
			var trange = document.selection.createRange();
			// Copy altered text back to clipboard
			trange.execCommand("Copy")
			// Destory pointer
			trange = null
			// Set focus back to original position
			setTimeout('_paste_range.select();',1)
			// Unlock paste
			_paste_lock = false
			return true
		}
		return true
	}
}


