// Captures enter key press when a textbox is selected, for Site Search
function doClick(buttonName,e) {
var key;

    if(window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //FF

    if (key == 13) {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) {
            //If we find the button click it
            btn.click();
            event.keyCode = 0;
        }
    }
}

function linkTo(url) {
  openExternalLink(url)
}

function openExternalLink(url) {
	window.open(url);
}

// Provides MaxLength functionality for a TextArea (IE) by preventing further keypresses being 
// registered once the max length is exceeded.
function HandleTextAreaKeyPress(object, maxLength) {
	var charCode = event.keyCode;

	// (Pressing enter will insert two characters)
	if(event.keyCode == 13)
		maxLength -= 1;
		
	if(object.value.length >= maxLength) {
		event.keyCode = 0;
		event.returnValue = false;
	}
}

// Provides MaxLength functionality for a TextArea by preventing the user pasting in text which 
// would exceed the MaxLength. IE
function HandleTextAreaPaste(object, maxLength) {
	// Get length of text in textarea
	var iTextLength = object.value.length;
	
	// Get length of text being pasted
	var iPasteLength = window.clipboardData.getData("Text").length;
	
	// Check if there are any characters selected
	var textRange = document.selection.createRange();
	
	// Get length of text the selected text
	var textSelectedSize = textRange.text.length;

	// If the size of the characters being pasted in is greater than the selected characters
	if(iPasteLength > textSelectedSize)	{
		// and the size of the text would be too big after the paste
		if(((iPasteLength - textSelectedSize) + iTextLength) > maxLength) {
			// then stop the paste and replace the selected text with the number of characters allowed.
			var strValue =  window.clipboardData.getData("Text");
			strValue = strValue.substring(0, textSelectedSize + (maxLength - iTextLength));
			textRange.text = strValue;
			event.returnValue = false;
		}
	}
}

// All browsers.
// Provides MaxLength functionality for a TextArea (not very well)
function calcCharLeft(object,maxLength) {
	if (object.value.length > maxLength) {
		object.value = object.value.substring(0,maxLength);
		charleft = 0;
	}
	else {
		charleft = maxLength - object.value.length;
	}
}

// addEvent function from http://www.scottandrew.com/weblog/articles/cbs-events
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn); 
		return r;
	} else {
		return false;
	}
}

//createElement function from http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}