var buttonPressed = '';
var numPollOptions = 2;
var numFileBoxes = 1;

function SubmitForm() 
{
	if (buttonPressed == "addOption") {
		if (numPollOptions < 20) {
			var pollOptions = document.getElementById('pollOptions');

			var newPollOption = '';
			numPollOptions = numPollOptions + 1;
			newPollOption = numPollOptions + '. <input type="text" name="option[' + (numPollOptions - 1) + ']" />';

  		var newdiv = document.createElement('div');			
			newdiv.setAttribute('id', 'pollOption' + numPollOptions);
			newdiv.innerHTML = newPollOption;

			pollOptions.appendChild(newdiv);
		}

		buttonPressed = '';
		return false;
	} else {
		return true;
	}
}

function vbcode(vbcode, prompttext) 
{
	myField = document.form.msg;
	tag_prompt = "Enter the text to be formatted:";	
	
	inserttext = prompt(tag_prompt+"\n["+vbcode+"]xxx[/"+vbcode+"]",prompttext);
	if ((inserttext != null) && (inserttext != "")) {
		myValue = "["+vbcode+"]"+inserttext+"[/"+vbcode+"] ";

		if (document.selection) {
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
		} 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);
		} else {
			myField.value += myValue;
		}
	}
}

function unfold(id,imageid,path)
{
	var	foldimage = 'image' + imageid
	if($('#'+id).css('display') == 'table' || $('#'+id).css('display') == 'block')
	{
	    $('#'+id).slideUp();
		document.images[foldimage].src = path+'explode_plus_b.gif'
		SetCookie(id,'1',356)
	}
	else
	{
		$('#'+id).slideDown(200, function() {
		$('#'+id).css('display','table')});
		
		document.images[foldimage].src = path+'explode_min_b.gif'	
		SetCookie(id,'0',356)
	}
}

function checkfold(id,imageid,path)
{
	var	foldimage = 'image' + imageid
	
	
	if (ReadCookie(id) == 1)
	{
		 $('#'+id).hide();
		document.images[foldimage].src = path+'explode_plus_b.gif'
	}
}

function addmorefiles()
{
	if (numFileBoxes < 3) {
		numFileBoxes++;
		var fileInput = document.createElement('input');
		fileInput.setAttribute('type', 'file');
		fileInput.setAttribute('name', 'files[' + (numFileBoxes - 1) + ']');
		var space = document.createTextNode(numFileBoxes + '. ');
		var breakSpace = document.createElement('br');

		var fileBox = document.getElementById('fileBox');
		fileBox.appendChild(breakSpace);
		fileBox.appendChild(space);
		fileBox.appendChild(fileInput);
	
		if (numFileBoxes == 3) {
			var addFilesButton = document.getElementById('addFilesButton');
			addFilesButton.style.display = "none";
		}
	}
	
	return false;
}

function deleteforumfile(id, topicId, postId, confirmText)
{
	if (confirm(confirmText)) {
		var fileElement = document.getElementById('file' + id);
		fileElement.parentNode.removeChild(fileElement);
		
		$.get('/forum_moderate/delete_file/'+ topicId + '/' + id);
		
		var fileContainer = document.getElementById('topicImages' +  postId);
		if (fileContainer.getElementsByTagName('div').length == 1) {
			$('#topicImages' +  postId).hide();
		}
		
		var fileContainer = document.getElementById('topicFiles' +  postId);
		if (fileContainer) {
			if (fileContainer.getElementsByTagName('div').length == 1) {
				$('#topicFiles' +  postId).hide();
			}		
		}
	}

	return(false);
}
