/*
 *	File Name : linktous.js
 *	Author : 				Domenic Polsoni - (FABUmarketing.com)
 *	
 *	Date Created :			April 1, 2009
 *	Date Modified :			April 1, 2009	- file created [dp]
 *							
 *												
 *     
 *	Various functions that are to be performed on forms
 */	
window.onload = function()
{
	if (document.getElementsByTagName)
	{
		var getSelectAll = document.getElementsByTagName("div");	// Get an array of all div tag elements to filter out "Select All"
		var getCode = document.getElementsByTagName("code");		// Get an array of all code tag elements
		var codeArray = new Array();								// Create a new array to store those elements that we want to work on
		
		// First loop: displays all "Select All" elements because their 
		// default is set to display:none
		for (var i = 0; i < getSelectAll.length; i++)
		{	
			// Run through the array and find the 'select all' divs
			if (getSelectAll[i].className == "selectAll")
			{	
				if (!window.opera)
					getSelectAll[i].innerHTML = "<a href=\"#\">Select All Code</a>";
				else
				{
					getSelectAll[i].style.width = "258px";
					getSelectAll[i].innerHTML = "Please click on the textarea to select the code.";
				}
				// Display them
				getSelectAll[i].style.display = "block";
				// Those 'Select All' elements will then be added to an
				// array to be accessed later
				codeArray.push(getSelectAll[i]);
			}
			
		}
		
		// Loop through elements searching for 'this' element
		if (!window.opera)
		{	
			for (var i = 0; i < codeArray.length; i++)
			{
				// When "Select All' is clicked, highlight text
				codeArray[i].onclick = function(e)
				{
					if (!e)
					{
						var e = window.event;
						e.returnValue = false;
					}
					else
						e.preventDefault();
						
					// Firefox DOM does not ignore whitespace so it treats the nextsibling
					// as text.  This needs to be checked for.
					var elem = this.nextSibling;
					while (elem.nodeType != 1)
						elem = elem.nextSibling;
					highLight(elem);
				}
			}
		}
		
		// Loop through elements searching for 'this' element
		// Also, Opera has a problem with the "Select All" functionality
		// this will allow the user to click directly on the text to select an item
		if (window.opera)		
		{	
			for (var i = 0; i < getCode.length; i++)
			{
				// When the actual textbox code is clicked, highlight text
				getCode[i].onclick = function(e)
				{	
					highLight(this);
				}
			}
		}
	}
}

/* 
	Selects text within called element.
*/
function highLight(elem)
{
	// For all browsers but IE.
	if (window.getSelection) 
	{
		var sel = window.getSelection();	
		if (sel.rangeCount > 0)
			sel.removeAllRanges();			// Webkit behaves strangely if this isn't called immediately.
		var range = document.createRange();
		range.selectNode(elem);
		sel.addRange(range);
	}
	// For IE
	else if (document.selection) 
	{
		var range = document.body.createTextRange();
		range.moveToElementText(elem);
		range.select();
	}
}