var win = null;

//*********************************************************
//*										     newWindow	  *
//* 		   Opens a new window center of the screen	  *
//*********************************************************
function newWindow(winLocation,winWidth,winHeight)
{
	var leftPosition,topPosition;
	leftPosition = (screen.width) ? (screen.width-winWidth)/2 : 0;
	topPosition = (screen.height) ? (screen.height-winHeight)/2 : 0;
	winSettings = 'height='+winHeight+',width='+winWidth+',top='+topPosition+',left='+leftPosition+',scrollbars=no';
	win = window.open(winLocation,'',winSettings);
}

//*********************************************************
//*										     hideDive	  *
//* 		   			Toggles a section's visibility 	  *
//*********************************************************
function hideDiv(target_div_id, control_div_id, hidetext, showtext)
{
	var objTargetDiv = document.getElementById(target_div_id);
	var objControlDiv = document.getElementById(control_div_id);
	if (objTargetDiv.style.display == 'none')
	{
		objTargetDiv.style.display = 'block';
		objControlDiv.innerHTML = hidetext;
	}
	else
	{
		objTargetDiv.style.display = 'none';
		objControlDiv.innerHTML = showtext;
	}
}


//*********************************************************
//*										makeBold		  *
//* Wrapper for addTags function -- creates bold text     *
//*********************************************************
function makeBold(element, button)
{
	addTags(element, '*', '*', button);
}


//*********************************************************
//*										makeItalic        *
//* Wrapper for addTags function -- creates italic text	  *
//*********************************************************
function makeItalic(element, button)
{
	addTags(element, '_', '_', button);
}


//*********************************************************
//*											addLink		  *
//* 								Adds a URL link		  *
//*********************************************************
function addLink(element, button)
{
	var objElement = document.getElementById(element);
	objElement.focus();
	
	// IE
	if (document.selection && document.selection.createRange)
	{
		//IE7 or above -- block window.prompt
		if (window.XMLHttpRequest) 
		{
			alert("Enter the link in the following format: \n \"LINKTEXT\":LINKURL \n Where LINKTEXT is the text to be clicked and LINKURL is the site to visit");
		}
		else
		{
			//Text selected, wrap it in delimiters			
			if (document.selection.createRange().text.length != 0)
			{
				var strURL = window.prompt("Please enter the URL to link to","");
				document.selection.createRange().text = "\"" + document.selection.createRange().text + "\":" + strURL;
			}
			//No text selected
			else
			{
				var objButton = document.getElementById(button);
				var strText = window.prompt("Please enter the text that will be linked","");
				var strURL = window.prompt("Please enter the URL to link to","");
				objElement.value = objElement.value + "\"" + strText + "\":" + strURL;
				objElement.focus();
			}
		}
	}


	// MOZILLA/GECKO
	else if (objElement.selectionStart || objElement.selectionStart == "0") 
	{
		//Text selected, wrap it in delimiters
		if (objElement.selectionStart - objElement.selectionEnd != 0)
		{
			var strBefore = objElement.value.substring(0, objElement.selectionStart);
			var strSelected = objElement.value.substring(objElement.selectionStart, objElement.selectionEnd);
			var strAfter = objElement.value.substring(objElement.selectionEnd, objElement.value.length);

			strSelected = "\"" + strSelected + "\":";
		
			var strURL = window.prompt("Please enter the URL to link to");
			objElement.value = strBefore + strSelected + strURL + strAfter; 
		}
		//No text selected
		else
		{
			var objButton = document.getElementById(button);
			var strText = window.prompt("Please enter the text that will be linked","");
			var strURL = window.prompt("Please enter the URL to link to","");
			objElement.value = objElement.value + "\"" + strText + "\":" + strURL;
			objElement.focus();
		}
	}	

}

//*********************************************************
//*										addTags			  *
//* Add formatting tags around a selected piece of text	  *
//*********************************************************
function addTags(element, startdelimiter, enddelimiter, button)
{
	var objElement = document.getElementById(element);
	objElement.focus();	

	// IE
	if (document.selection && document.selection.createRange)
	{
		//Text selected, wrap it in delimiters			
		if (document.selection.createRange().text.length != 0)
		{
			document.selection.createRange().text = startdelimiter + document.selection.createRange().text + enddelimiter;
		}
		//No text selected, so "open" and "close" tags (and change button state)
		else
		{
			var objButton = document.getElementById(button);
			objButton.style.color == "green" ? objElement.value = objElement.value + enddelimiter : objElement.value = objElement.value + startdelimiter
			objButton.style.color == "green" ? objButton.style.color = "black" : objButton.style.color = "green"
			objElement.focus();
		}
	}


	// MOZILLA/GECKO
	else if (objElement.selectionStart || objElement.selectionStart == "0") 
	{
		//Text selected, wrap it in delimiters
		if (objElement.selectionStart - objElement.selectionEnd != 0)
		{
			var strBefore = objElement.value.substring(0, objElement.selectionStart);
			var strSelected = objElement.value.substring(objElement.selectionStart, objElement.selectionEnd);
			var strAfter = objElement.value.substring(objElement.selectionEnd, objElement.value.length);

			strSelected = startdelimiter + strSelected + enddelimiter;

			objElement.value = strBefore + strSelected + strAfter;
		}
		//No text selected, so "open" and "close" tags (and change button state)
		else
		{
			var objButton = document.getElementById(button);
			objButton.style.color == "green" ? objElement.value = objElement.value + enddelimiter : objElement.value = objElement.value + startdelimiter
			objButton.style.color == "green" ? objButton.style.color = "black" : objButton.style.color = "green"
			objElement.focus();
		}
	}
}