//browser detect
var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var bool_upload = false;
function addBBHelp(elementName, text)
{
	element = document.getElementById(elementName);
	element.firstChild.nodeValue = text;
	//element.Text = text;
	//element.setAttribute('text', text);
}

function regatta_delete(form, name, value)
{
	var form = 	document.getElementById(form);
	var hiddenDelete = document.createElement('input');
	hiddenDelete.setAttribute('type', 'hidden');
	hiddenDelete.setAttribute('name', name);
	hiddenDelete.setAttribute('value', value);
	form.appendChild(hiddenDelete);
}

function updateCharCount(idTextarea, idCounter)
{
	var textarea = document.getElementById('message_text');
	var counter = document.getElementById('charCount');
	counter.innerHTML = textarea.value.length;
}

function addText(elementName, text, text2)
{
	element = document.getElementById(elementName);
	insertAtCursor(element, text, false);
	if (text2)
	{
		insertAtCursor(element, text2, true);
	}
	//element.value += text;
}

function imageLoad()
{
	var image_count = document.getElementById('image_count');
	
	if (image_count)
	{
		imageCount = Number(image_count.value) + 1;
		document.getElementById('add_image').value = '[img=' + imageCount + ']';
		
		var image_allow_upload = document.getElementById('image_allow_upload');
		
		if (image_allow_upload)
		{
			if (image_allow_upload.value == '1')
			{
			bool_upload = true;	
			}
		} //image_allow_upload
		
	} //image_count
	
} //imageLoad


function addImage(elementName, element, imageName)
{
	addText(elementName, '[img=' + imageCount + ']');

	//looks crap in ie
	if ((!is_ie) && (bool_upload))
	{
		//add upload image
		var image_element = document.getElementById(imageName);
	
		var divRow = document.createElement('div');
		divRow.setAttribute('class', 'form_row');
	
		var label = document.createElement('label');
		label.setAttribute('class', 'form_label');
		label.setAttribute('for', '[img=' + imageCount + ']');
		label.appendChild(document.createTextNode('Upload [img=' + imageCount + ']'));
	
		var input = document.createElement('input');
		input.setAttribute('class', 'form_upload');
		input.setAttribute('type', 'file');
		input.setAttribute('id', '[img=' + imageCount + ']');
		input.setAttribute('name', imageCount);
	
		divRow.appendChild(label);
		divRow.appendChild(input);
		image_element.appendChild(divRow);
		//end upload i,age
	}
	
	imageCount++;
	element.value = '[img=' + imageCount + ']';

} //addImage

function setSelRange(inputEl, selStart, selEnd) 
{ 
	if (inputEl.setSelectionRange) 
	{ 
		inputEl.focus(); 
		inputEl.setSelectionRange(selStart, selEnd); 
	} 
	else if (inputEl.createTextRange) 
	{ 
		var range = inputEl.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', selEnd); 
		range.moveStart('character', selStart); 
		range.select(); 
	} 
} //setSelRange


function insertAtCursor(myField, myValue, bool_before) 
{
	//IE support
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
		if (bool_before)
		{
			setSelRange(myField, startPos, endPos);
		}
		else
		{
			setSelRange(myField, startPos + myValue.length, endPos + myValue.length);
		}
	} 
	else //otherwise
	{
		myField.value += myValue;
	}
	
	
} //insertAtCursor
