



function submitSpellcheck(f) {
  f.doSpellCheck.value = 'true';
  f.submit();
}
function openEmoticonWin(formName) {
    window.open("/emoticons.jsp?formName="+formName, "emoticons", "menubar=no,location=no,personalbar=no,scrollbars=no,width=30,height=330");
}
function preview(f) {
  var content = f.body.value;
  var left = screen.availWidth-450;
  var w = window.open("","popup","width=450,height=350,directories=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes,screenY=0,top=0,screenX=" + left + ",left=" + left);
  w.document.writeln("<html><title>Preview</title><body>");
  w.document.writeln(content);
  w.document.writeln('<hr><form><center><input type="submit" value="Close Window" onClick="window.close();return false;"></center></form>');
  w.document.writeln("</body></html>");
  w.document.close();
}

function insertAtCursor(myField, myValue) {
	//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);
	} else {
		myField.value += myValue;
	}
}

function placeImage(imgName, body) {
	insertAtCursor(body, '[img src="'+imgName+'"]');
}

function placeAudio(fileName, body) {
	insertAtCursor(body, '[audio src="'+fileName+'"]');
}

function embedPhoto(imgURL, body) {
	insertAtCursor(body, '<img src="'+imgURL+'">');
}

function embedPhotoGui(imgURL, body) {
	var inst = tinyMCE.selectedInstance;
	inst.getWin().focus();
	tinyMCE.execCommand('mceInsertContent',false,'<img src="' + imgURL + '" \/>');
	//tinyMCE.themes['advanced']._insertImage(imgURL, "", 0, "", "", null, null, "");
}

function clearText(field, otherName) {
	if (field.value != null && field.value.length > 0)
		document.attachForm[otherName].value = "";
}

function clearFile(field, fileID) {
	if (field.value != null && field.value.length > 0) {
		document.getElementById(fileID).innerHTML = 
			document.getElementById(fileID).innerHTML;
	}
}

function addBlogCategory(form) {
	// requires /scripts/scriptaculous/lib/prototype.js
	var forumID = form.forumID.value;
	var category = form.blogCategory.value;
	var url = '/blogcategory!add.jspa?forumID='+forumID+'&blogCategory='+escape(category);
	new Ajax.Request(url, 
		{ 
			onSuccess: function(transport) {
				if (transport.responseText.indexOf('blog category mode=success') > 0) {
					var catCount = form.blogCategories.value;
					form.blogCategories.value = eval(catCount)+1;
					/*
						We have to insert the new category in alphabetical order, otherwise the selected categories will be off
						when the form is submitted (in LWPostAction.loadBlogCategories(), the category list used to compare against the
						blogCategoryMap is sorted alphabetically)
					
						If there is at least one category, loop through the 
						checkboxes & insert the new category so list is alphabetical
					*/
					// this is the html to generate
					var catTmplHTML = new Template('<div class="lw-text" id="blogcat-div-#{newID}"><input type="checkbox" id="blogcat-input-#{newID}" name="blogCategoryMap[#{newID}]" value="true"><span>#{newCat}</span></div>');
					// category object
					var insertCat;
					// newly created element from category template 
					var catTmpl;
					if(catCount > 0) {
						for(var i = 0; i < catCount; i++) {
							var nextI = parseInt(i) + 1;
							var parentId = 'blogcat-div-' + i;
							var nextParentId = 'blogcat-div-' + nextI;
							var parent = $(parentId);
							var nextParent  = $(nextParentId);
							var currentCat;
							// 2nd descendant is a span, innerHTML is category name
							if(parent) {
								parent.cleanWhitespace();
								currentCat = parent.down(1).innerHTML;
							}										
							// if there is a div underneath the current one (it would be nextParent), compare the categories 
							// to to see where to insert the new one
							if(nextParent) {
								nextParent.cleanWhitespace();
								var nextCat = nextParent.down(1).innerHTML;
								var newCatElement;
								var modSibs = 0;
								if(category > currentCat && category < nextCat) {
									// it's somewhere in the middle of the list
									insertCat = {newID: nextI, newCat: category};
									catTmpl = catTmplHTML.evaluate(insertCat);
									newCatElement = parent.insert({after:catTmpl});
									modSibs = 1;
								} else if(category < currentCat) {
									// set as first category in the list
									insertCat = {newID: i, newCat: category};
									catTmpl = catTmplHTML.evaluate(insertCat);
									newCatElement = parent.insert({before:catTmpl});
									newCatElement = newCatElement.previous();
									modSibs = 1;
								} else if(category > nextCat && catCount == (i + 2)) {
									// it's the last category in the list
									insertCat = {newID: nextI + 1, newCat: category};
									catTmpl = catTmplHTML.evaluate(insertCat);
									var newCatElement = nextParent.insert({after:catTmpl});
									break;
								}
								
								// increment index of all categories below
								// get collection of all the new elements siblings
								if(modSibs == 1) {
									var sibs = newCatElement.nextSiblings();
									for(var j = 0; j < sibs.length; j++) {
										// get div id attribute & increment
										var nextBlogId = 'blogcat-div-' + nextI;
										sibs[j].writeAttribute('id', nextBlogId);
										// get input id & name attributes & increment
										var chkbox = sibs[j].firstDescendant();
										var nextInputId = 'blogcat-input-' + nextI;
										var nextInputName = 'blogCategoryMap[' + nextI + ']';
										chkbox.writeAttribute({id: nextInputId, name: nextInputName});
										nextI++;	
									}
									// we're done
									break;
								}
								
							} else {
								// there's currently only one category 
								if(category > currentCat) {
									// add it to the last position
									insertCat = {newID: nextI, newCat: category};
									catTmpl = catTmplHTML.evaluate(insertCat);
									var newCatElement = parent.insert({after:catTmpl});
									break;
		
								} else {
									// add it to this position
									insertCat = {newID: i, newCat: category};
									catTmpl = catTmplHTML.evaluate(insertCat);
									var newCatElement = parent.insert({before:catTmpl});
									
									// increment currentCatParent id
									var nextBlogId = 'blogcat-div-' + nextI;
									parent.writeAttribute('id', nextBlogId);
									var chkbox = parent.firstDescendant();
									var nextInputId = 'blogcat-input-' + nextI;
									var nextInputName = 'blogCategoryMap[' + nextI + ']';
									chkbox.writeAttribute({id: nextInputId, name: nextInputName});
									break;
								}	
							}
						}
					} else {
						// no categories yet, so just add the category
						insertCat = {newID: catCount, newCat: category};
						$('blog-category-list').innerHTML += catTmplHTML.evaluate(insertCat);
					}
					// clear the category input
					form.blogCategory.value = "";
				}
			}
		});
}
