//v2.0.2		|||||||||||||||||||   ©2006 Irondev LLC, Joel O'Neill   http://www.irondev.com  |||||||||||||||||||
//11/22/07	changes to InsertTagFromString



//globals +--------------------------------------------------------------------------------------------------
//globals +--------------------------------------------------------------------------------------------------

//whether the form on the page has ever been modified
var glbFormModified = false;


//globals +--------------------------------------------------------------------------------------------------
//globals +--------------------------------------------------------------------------------------------------



//inserts tags into a provided textarea/textbox
//toElement		the form element being modified
//start			the starting or opening tag
//end			the end or closing tag
function InsertTag(toElement, start, end)
{
	if(typeof(toElement) != 'object'){toElement = document.getElementById(toElement);}
	toElement.focus();
	
	//IE +------------------------------------
	if (document.selection)
	{
		var txt;
		var objRange = document.selection.createRange();
		txt = objRange.text;
		objRange.text = start + txt + end;
		//alert(end);
	}
	
	//all others +----------------------------
	else if(toElement.selectionStart)
	{
		var sStart, sEnd, txt;
		//get start and end points
		sStart = toElement.selectionStart;
		sEnd = toElement.selectionEnd;
		//get range
		txt = toElement.value.substring(sStart, sEnd);
		txt = start + txt + end;
		//set new text and set selection
		toElement.value = toElement.value.substring(0, sStart) + txt + toElement.value.substr(sEnd);
		toElement.selectionStart = sStart;
		toElement.selectionEnd = sEnd + (txt.length - (sEnd - sStart));
	}
	toElement.focus();
}



//inserts tags into a provided textarea/textbox when coming from a single line, such as a form element value
//form element values among other things, cannot have quotes or provide a starting and ending tag so for this
//function the special characters are used: "|" for the separator between tags, "^" for quotes
//toElement		the form element being modified
//				the string to be parsed
function InsertTagFromString(toElement, str)
{
	var arrTags = str.split('|');
	for(i=0;i<arrTags.length;i++){arrTags[i] = arrTags[i].replace(/\^/g, '"');}
	if(arrTags.length == 1){InsertTag(toElement, '', arrTags[0]);}
	else if(arrTags.length == 2){InsertTag(toElement, arrTags[0], arrTags[1]);}
	else{InsertTag(toElement, '', str.replace(/\|/g, '').replace(/\^/g, '"'));}
}




//for when a button or dropdown is used to insert text into a textarea/textbox
//it pushes the fromElement's value/selectedValue to the InsertTagFromString function
function InsertTagFromForm(toElement, fromElement)
{
	if(typeof(fromElement) != 'object'){fromElement = document.getElementById(fromElement);}
	if(fromElement.type == 'select-one')
	{
		InsertTagFromString(toElement, Selected(fromElement).value);
		fromElement.selectedIndex = 0;
	}
	else{InsertTagFromString(toElement, fromElement.value);}
}



//highlights modified text for the given object
function Modified(obj)
{
	if(typeof(obj) != 'object'){obj = document.getElementById(obj);}
	if(obj != null)
	{
		obj.style.backgroundColor = '#ffff88';
		for(i=0; i<obj.form.elements[obj.name].length; i++)
		{
			obj.form.elements[obj.name][i].style.backgroundColor = '#ffff88';
		}
		glbFormModified = true;
	}
}



//if Modified has ever been called then this can be used in any onclick
//to confirm leaving the page with a form that hasn't been updated
function FormModified(msg)
{
    if(msg == '') {msg = 'The form has been modified but not updated, exit this page?';}
	if(!glbFormModified) {return true;}
	else {return confirm(msg);}
}

