

function setAllCheckboxes(formname, fieldvalue, fieldname){
	var myform = document.forms[formname];
	for(var i=0; i<myform.elements.length; i++){
		if (myform.elements[i].type == "checkbox"){
			if(!fieldname || myform.elements[i].name.indexOf( fieldname ) === 0){
				myform.elements[i].checked = fieldvalue;
			}
		}
	}
}

function updateParentTitle(newtitle){
	parent.document.title = newtitle;
}


/**
 * Replaces the current selected text if any, or adds the selected text at the cursor
 * Original code: http://www.ficgs.com/How-to-insert-BBcode-Textarea-forums_1881.html
 */
function addText(obj, Text) {
	obj.focus();
	
	if (document.selection && document.selection.createRange){  // Internet Explorer
		sel = document.selection.createRange();
		if (sel.parentElement() == obj)
			sel.text = Text;
	} else if (obj != "undefined"){  // Firefox
		var longueur = parseInt(obj.textLength);
		var selStart = obj.selectionStart;
		var selEnd = obj.selectionEnd;

		obj.value = obj.value.substring(0,selStart) + Text + obj.value.substring(selEnd,longueur);
	} else {
		obj.value += Text;
	}
	obj.focus();
}

/**
 * Wraps the selected text in the textarea with Tag and fTag. If there is no text selected, it will be inserted at the cursors position
 */
function addTags(field, preTag, postTag) {

	if (document.selection){					//IE

		field.focus();
		var selected = document.selection.createRange().text;
		var ins = preTag + selected + postTag;
		var selected2 = document.selection.createRange();
		var sel = document.selection.createRange();
		selected2.moveStart ('character', -field.value.length);
		sel.text = preTag + selected + postTag;
		sel.moveStart('character', selected2.text.length + ins.length - selected.length);

	} else if (field.selectionStart || field.selectionStart == 0){	//OTHER BROWSERS

		//source PHPBB - their source: http://www.massless.org/mozedit/
		var selLength = field.textLength;
		var selStart = field.selectionStart;
		var selEnd = field.selectionEnd;
		var scrollTop = field.scrollTop;

		if (selEnd == 1 || selEnd == 2)
			selEnd = selLength;

		var s1 = (field.value).substring(0,selStart);
		var s2 = (field.value).substring(selStart, selEnd)
		var s3 = (field.value).substring(selEnd, selLength);

		field.value = s1 + preTag + s2 + postTag + s3;
		field.selectionStart = selEnd + preTag.length + postTag.length;
		field.selectionEnd = field.selectionStart;
		field.focus();
		field.scrollTop = scrollTop;
	}

} 
