/*******************************
	CMS STUFF
*******************************/

//initialize cms
addEvent(window,"load",cmsInit);

function cmsInit() {
	//alert("Initializing CMS Front-end.");
	
	//add tranformations to content
	var content = document.getElementById("contentform"); //content container
	if(content) {
		var cItems = content.childNodes;
		for(var i = 0;i < cItems.length;i++) {
			if(cItems.item(i).nodeType == 1) {
				var thisItem = cItems.item(i);
				var myId = thisItem.id;
				//alert(myId);
				var idInfo = myId.split('_');
				
				if(idInfo.length >= 3) {
					switch(idInfo[2]) {
						case "text":	makeEditable(thisItem,replaceTextWithForm);
								break;
								
						case "heading":	makeEditable(thisItem,replaceHeadingWithForm);
								break;
								
						case "image":	makeEditable(thisItem,replaceImgWithForm);
								break;
								
						case "flash":	makeEditable(thisItem,replaceFlashWithForm);
								break;
								
						case "director":	makeEditable(thisItem,replaceDirectorWithForm);
								break;
								
						default:	//default
					}
				}
				
				//links for inserting content
				if(thisItem.nodeName.toLowerCase() == "ul" && thisItem.className == "insertionpoint") {
					theLinks = thisItem.getElementsByTagName("a");
					for(var j = 0;j < theLinks.length;j++) {
						addEvent(theLinks[j],"mouseup",insertNewContent);
						//var contentNum = String(theLinks[j].getAttribute("href")).substring(1);
						//theLinks[j].setAttribute("href","javascript: insertNewContent('" + theLinks[j].firstChild.nodeValue + "'," + contentNum + ");");
					}
				}
			}
		}
	}
	
	//add transformation to news
	addEvent(document.getElementById("addnews"),"click",insertNewNews);
	var theNews = document.getElementById("newsform"); //news container
	if(theNews) {
	}
}



function insertNewContent(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	//make sure this is a link or get the link if this happens to refer to the textnode
	while(targ.nodeType != 1 || String(targ.nodeName).toLowerCase() != "a") {
		do {
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
	}

	var newType = targ.firstChild.nodeValue;
	var appliesTo = targ.parentNode.parentNode;
	while(appliesTo.nodeType != 1 || !(appliesTo.id.length > 7 && String(appliesTo.id).substring(0,7) == "content")) {
		appliesTo = appliesTo.nextSibling;
		
		if(appliesTo.nodeType == 1 && String(appliesTo.nodeName).toLowerCase() == "div" && appliesTo.className == "editable") {
			appliesTo = appliesTo.firstChild;
		}
	}
	
	applyInfo = appliesTo.id.split("_");
	var newId = applyInfo[0] + "_" + applyInfo[1] + "_" + newType;
	
	if(newType == "text") {
		insertTextForm(targ.parentNode.parentNode,newId);
	} else
	if(newType == "heading") {
		insertHeadingForm(targ.parentNode.parentNode,newId);
	} else
	if(newType == "image") {
		insertImgForm(targ.parentNode.parentNode,newId);
	} else
	if(newType == "flash") {
		insertFlashForm(targ.parentNode.parentNode,newId);
	} else
	if(newType == "director") {
		insertDirectorForm(targ.parentNode.parentNode,newId);
	}
}



function replaceTextWithForm(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	targ = findMyContent(targ);
	var aParaId = targ.id;	
	
	//Get info from paragraph id
	var paraInfo = aParaId.split('_');
	//Replace description with a textbox containing its content
	var paraText = document.getElementById(aParaId).innerHTML;
	paraText = paraText.replace("<span class=\"highlightsentance\">","");
	paraText = paraText.replace("<SPAN class=\"highlightsentance\">","");
	paraText = paraText.replace("</span>","");
	paraText = paraText.replace("</SPAN>","");	

	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('textarea');
	thisElem.id = aParaId;
	thisElem.setAttribute("name",aParaId + "_content_split");
	thisElem.setAttribute("cols",48);
	thisElem.setAttribute("rows",7);
	newForm.appendChild(thisElem);
	newForm.childNodes[0].value = paraText;
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	newForm.appendChild(thisElem);
	newForm.childNodes[1].value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + paraInfo[1]);
	newForm.appendChild(thisElem);
	newForm.childNodes[2].value = "Delete";
	
	newForm = document.getElementById(aParaId).parentNode.replaceChild(newForm,document.getElementById(aParaId));
}

function insertTextForm(targ,newId) {
	
	//Get info from paragraph id
	var paraInfo = newId.split('_');
	//Replace description with a textbox containing its content
	var paraText = "";

	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('textarea');
	thisElem.id = newId;
	thisElem.setAttribute("name",newId + "_content_insert_split");
	thisElem.setAttribute("cols",48);
	thisElem.setAttribute("rows",7);
	newForm.appendChild(thisElem);
	newForm.childNodes[0].value = paraText;
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	newForm.appendChild(thisElem);
	newForm.childNodes[1].value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + paraInfo[1]);
	newForm.appendChild(thisElem);
	newForm.childNodes[2].value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}

function replaceHeadingWithForm(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	targ = findMyContent(targ);
	
	//Get info from paragraph id
	aParaId = targ.id;
	var paraInfo = aParaId.split('_');
	//Replace description with a textbox containing its content
	var paraText = document.getElementById(aParaId).innerHTML;
	paraText = paraText.replace("<span class=\"highlightsentance\">","");
	paraText = paraText.replace("<SPAN class=\"highlightsentance\">","");
	paraText = paraText.replace("</span>","");
	paraText = paraText.replace("</SPAN>","");	

	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",aParaId + "_content");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Heading: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = aParaId + "_content";
	thisElem.setAttribute("name",aParaId + "_content");
	thisElem.setAttribute("type","text");
	thisElem.setAttribute("size",48);
	newForm.appendChild(thisElem);
	newForm.childNodes[1].value = paraText;
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	newForm.appendChild(thisElem);
	newForm.childNodes[3].value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + paraInfo[1]);
	newForm.appendChild(thisElem);
	newForm.childNodes[4].value = "Delete";
	
	newForm = document.getElementById(aParaId).parentNode.replaceChild(newForm,document.getElementById(aParaId));
}

function insertHeadingForm(targ,newId) {
	
	//Get info from paragraph id
	var paraInfo = newId.split('_');
	//Replace description with a textbox containing its content
	var paraText = "";

	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",newId + "_content_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Heading: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = newId + "_content_insert";
	thisElem.setAttribute("name",newId + "_content_insert");
	thisElem.setAttribute("type","text");
	thisElem.setAttribute("size",48);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = paraText;
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + paraInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}



function replaceImgWithForm(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	while(targ.id == undefined || targ.id == "") {
		do {
			if(targ.previousSibling.id != undefined && targ.previousSibling.id != "") {
				targ = targ.previousSibling;
				break;
			}
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
	}
	var targId = targ.id;	
	
	//Get info from paragraph id
	var targInfo = targId.split('_');
	//Get attribute infos
	var imgSrc = targ.getAttribute("src");
	var imgAlt = targ.getAttribute("alt");
	
	//Clean up img src string if it has http://blahblahblah in it
	var rootPath = String(window.location);
	var rootSlash = rootPath.lastIndexOf("/");
	if(rootSlash > 0 && rootSlash <= imgSrc.length) {
		rootPath = String(window.location).substring(0,rootSlash);
		if(rootPath == imgSrc.substring(0,rootSlash)) {
			imgSrc = imgSrc.substring(rootSlash + 1);
		}
	}
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('img');
	thisElem.id = targId + "_newsrcimg";
	thisElem.setAttribute("src",imgSrc);
	thisElem.setAttribute("alt",imgAlt);
	newForm.appendChild(thisElem);
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_newsrc");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Image file: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_newsrc";
	thisElem.setAttribute("name",targId + "_src");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = imgSrc;
	addEvent(thisElem,"keyup",updateImg);
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_caption");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Caption: "));
	
	var thisElem = document.createElement('textarea');
	thisElem.id = targId + "_caption";
	thisElem.setAttribute("name",targId + "_caption");
	thisElem.setAttribute("cols",48);
	thisElem.setAttribute("rows",7);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = imgAlt;
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}

function insertImgForm(targ,newId) {
	//Get info from paragraph id
	var targInfo = newId.split('_');
	//Get attribute infos
	var imgSrc = "";
	var imgAlt = "";
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('img');
	thisElem.id = newId + "_newsrc_insertimg";
	thisElem.setAttribute("src",imgSrc);
	thisElem.setAttribute("alt",imgAlt);
	newForm.appendChild(thisElem);
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",newId + "_newsrc_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Image file: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = newId + "_newsrc_insert";
	thisElem.setAttribute("name",newId + "_src_insert");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = imgSrc;
	addEvent(thisElem,"keyup",updateImg);
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",newId + "_caption_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Caption: "));
	
	var thisElem = document.createElement('textarea');
	thisElem.id = newId + "_caption_insert";
	thisElem.setAttribute("name",newId + "_caption_insert");
	thisElem.setAttribute("cols",48);
	thisElem.setAttribute("rows",7);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = imgAlt;
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}



function replaceFlashWithForm(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	while(targ.id == undefined || targ.id == "") {
		do {
			if(targ.previousSibling.id != undefined && targ.previousSibling.id != "") {
				targ = targ.previousSibling;
				break;
			}
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
	}
	var targId = targ.id;
	
	//Get info from paragraph id
	var targInfo = targId.split('_');
	//Get attribute infos
	var swfSrc = targ.getAttribute("data");
	var swfWidth = targ.getAttribute("width");
	var swfHeight = targ.getAttribute("height");
	var swfWMode = "transparent";
	
	//Clean up swfSrc string if it has http://blahblahblah in it
	var rootPath = String(window.location);
	var rootSlash = rootPath.lastIndexOf("/");
	if(rootSlash > 0 && rootSlash <= swfSrc.length) {
		rootPath = String(window.location).substring(0,rootSlash);
		if(rootPath == swfSrc.substring(0,rootSlash)) {
			swfSrc = swfSrc.substring(rootSlash + 1);
		}
	}
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('object');
	thisElem.id = targId;
	thisElem.setAttribute("type","application/x-shockwave-flash");
	thisElem.setAttribute("data",swfSrc);
	thisElem.setAttribute("width",swfWidth);
	thisElem.setAttribute("height",swfHeight);
	thisElem = newForm.appendChild(thisElem);
		var subElem = document.createElement('param');
		subElem.setAttribute("name","movie");
		subElem.setAttribute("value",swfSrc);
		thisElem.appendChild(subElem);
		subElem = document.createElement('param');
		subElem.setAttribute("name","wmode");
		subElem.setAttribute("value",swfWMode);
		thisElem.appendChild(subElem);
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_movie");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Flash file: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_movie";
	thisElem.setAttribute("name",targId + "_movie");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfSrc;
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_width");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Width: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_width";
	thisElem.setAttribute("name",targId + "_width");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfWidth;
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_height");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Height: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_height";
	thisElem.setAttribute("name",targId + "_height");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfHeight;
	
	var thisElem = document.createElement('input');
	thisElem.setAttribute("name",targId + "_wmode");
	thisElem.setAttribute("type","hidden");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfWMode;
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.parentNode.replaceChild(newForm,targ.parentNode);
}

function insertFlashForm(targ,newId) {
	var targId = newId;
	
	//Get info from paragraph id
	var targInfo = targId.split('_');
	//Get attribute infos
	var swfSrc = "";
	var swfWidth = "";
	var swfHeight = "";
	var swfWMode = "transparent";
	
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_movie_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Flash file: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_movie_insert";
	thisElem.setAttribute("name",targId + "_movie_insert");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfSrc;
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_width_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Width: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_width_insert";
	thisElem.setAttribute("name",targId + "_width_insert");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfWidth;
	
	newForm.appendChild(document.createElement('br'));
	
	var thisElem = document.createElement('label');
	thisElem.setAttribute("for",targId + "_height_insert");
	thisElem = newForm.appendChild(thisElem);
	thisElem.appendChild(document.createTextNode("Height: "));
	
	var thisElem = document.createElement('input');
	thisElem.id = targId + "_height_insert";
	thisElem.setAttribute("name",targId + "_height_insert");
	thisElem.setAttribute("type","text");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfHeight;
	
	var thisElem = document.createElement('input');
	thisElem.setAttribute("name",targId + "_wmode_insert");
	thisElem.setAttribute("type","hidden");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = swfWMode;
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}



function replaceDirectorWithForm(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	while(targ.id == undefined || targ.id == "") {
		do {
			if(targ.previousSibling.id != undefined && targ.previousSibling.id != "") {
				targ = targ.previousSibling;
				break;
			}
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
	}
	var targId = targ.id;
	
	//Get info from paragraph id
	var targInfo = targId.split('_');
	//Get attribute infos
	var dcrProps = new Array();
	var objKids = targ.getElementsByTagName("param");
	for(var i = 0;i < objKids.length;i++) {
		var varName = String(objKids[i].getAttribute("name"));
		var varVal = String(objKids[i].getAttribute("value"));
		
		if(varName == "src" || (varName.length > 2 && varName.substring(0,2) == "sw")) {
			//Clean up swfSrc string if it has http://blahblahblah in it
			var rootPath = String(window.location);
			var rootSlash = rootPath.lastIndexOf("/");
			if(rootSlash > 0 && rootSlash <= varVal.length) {
				rootPath = String(window.location).substring(0,rootSlash);
				if(rootPath == varVal.substring(0,rootSlash)) {
					varVal = varVal.substring(rootSlash + 1);
				}
			}
		}
		
		dcrProps.push([varName,varVal]);
	}
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	var thisElem = targ.cloneNode(true);
	newForm.appendChild(thisElem);
	
	for(var i = 0;i < dcrProps.length;i++) {
		newForm.appendChild(document.createElement('br'));
	
		var thisElem = document.createElement('label');
		thisElem.setAttribute("for",targId + "_" + dcrProps[i][0]);
		thisElem = newForm.appendChild(thisElem);
		thisElem.appendChild(document.createTextNode(dcrProps[i][0] + ": "));

		var thisElem = document.createElement('input');
		thisElem.id = targId + "_" + dcrProps[i][0];
		thisElem.setAttribute("name",targId + "_" + dcrProps[i][0]);
		thisElem.setAttribute("type","text");
		thisElem = newForm.appendChild(thisElem);
		thisElem.value = dcrProps[i][1];
	}
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.parentNode.replaceChild(newForm,targ.parentNode);
}

function insertDirectorForm(targ,newId) {
	var targId = newId;
	
	//Get info from paragraph id
	var targInfo = targId.split('_');
	//Get attribute infos
	var dcrProps = new Array();
	dcrProps.push(["src",""]);
	dcrProps.push(["sw1",""]);
	dcrProps.push(["sw2",""]);
	dcrProps.push(["width",""]);
	dcrProps.push(["height",""]);
	dcrProps.push(["progress","false"]);
	dcrProps.push(["swRemote","swSaveEnabled='true' swVolume='false' swRestart='false' swPausePlay='false' swFastForward='false' swContextMenu='false'"]);
	
	//write new elements
	var newForm = document.createElement('fieldset');
	
	for(var i = 0;i < dcrProps.length;i++) {
		newForm.appendChild(document.createElement('br'));
	
		var thisElem = document.createElement('label');
		thisElem.setAttribute("for",targId + "_" + dcrProps[i][0] + "_insert");
		thisElem = newForm.appendChild(thisElem);
		thisElem.appendChild(document.createTextNode(dcrProps[i][0] + ": "));

		var thisElem = document.createElement('input');
		thisElem.id = targId + "_" + dcrProps[i][0] + "_insert";
		thisElem.setAttribute("name",targId + "_" + dcrProps[i][0] + "_insert");
		thisElem.setAttribute("type","text");
		thisElem = newForm.appendChild(thisElem);
		thisElem.value = dcrProps[i][1];
	}
	
	newForm.appendChild(document.createElement('br'));
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","update");
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Update";
	
	thisElem = document.createElement('input');
	thisElem.setAttribute("type","submit");
	thisElem.setAttribute("name","delete_" + targInfo[1]);
	thisElem = newForm.appendChild(thisElem);
	thisElem.value = "Delete";
	
	newForm = targ.parentNode.replaceChild(newForm,targ);
}


function insertfirstForm() {
	var newForm = document.createElement('fieldset');
	newForm.appendChild(document.createElement('textarea'));
	newForm.childNodes[0].id = "insertfirstcontent";
	newForm.childNodes[0].setAttribute("name","insertfirstcontent");
	newForm.childNodes[0].setAttribute("cols",48);
	newForm.childNodes[0].setAttribute("rows",7);
	newForm.appendChild(document.createElement('input'));
	newForm.childNodes[1].setAttribute("type","submit");
	newForm.childNodes[1].setAttribute("name","insertfirst");
	newForm.childNodes[1].value = "Insert new paragraph";

	newForm = document.getElementById("insertfirstbutton").parentNode.replaceChild(newForm,document.getElementById("insertfirstbutton"));
}


function insertNewNews(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	
	//make sure this is a link or get the link if this happens to refer to the textnode
	while(targ.nodeType != 1 || String(targ.nodeName).toLowerCase() != "a") {
		do {
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
	}
	
	
}


function navLinksForm(aListId) {
	var newForm = document.createElement('form');
	newForm.setAttribute("id","navform");
	newForm.setAttribute("method","POST");
	newForm.setAttribute("action",window.location);
	
	var listItems = document.getElementById(aListId).childNodes;
	for(var i = 0;i < listItems.length;i++) {
		if(listItems[i].nodeName == "a") {
			var itemInfo = new Array();
			//Node name
			itemInfo["name_english"] = listItems[i].getElementsByTagName("a")[0].firstChild.nodeValue;
			
			//
		}
	}
	
	document.getElementsByTagName("body")[0].appendChild(newForm);
}


function makeEditBox(theElem,theFunc) {
	theZone = document.createElement('div');
	theZone.appendChild(theElem.cloneNode(true));
	
	theLink = document.createElement('a');
	theLink.setAttribute("href","javascript:void(0);");
	theLink.appendChild(document.createTextNode("Edit"));
	//addEvent(theLink,"click",theFunc);
	theLink.className = "editbutton";
	theZone.appendChild(theLink);
	
	theLink = document.createElement('a');
	theLink.setAttribute("href","javascript:void(0);");
	theLink.appendChild(document.createTextNode("Delete this Content"));
	theLink.className = "deletebutton";
	theZone.appendChild(theLink);
	
	theZone.className = "editablebox";
	
	theElem.parentNode.replaceChild(theZone,theElem);
	addEvent(theZone,"mouseover",hoverStyles);
	addEvent(theZone,"mouseout",hoverStyles);
	return theZone;
}


//Adds behaviors and buttons to a piece of content
//Args: 
function makeEditable(theElem,theFunc)	{
	//make containing box and add the content to it
	container = document.createElement('div');
	container.className = "editable";
		oldContent = theElem.cloneNode(true);
		addEvent(oldContent,"click",theFunc);
		addEvent(oldContent,"mouseover",hoverStyles);
		addEvent(oldContent,"mouseout",hoverStyles);
	container.appendChild(oldContent);
	
	//Edit button
	aButton = document.createElement('a');
	aButton.setAttribute("href","javascript:void(0);");
	aButton.appendChild(document.createTextNode("Edit this Content"));
	aButton.className = "editbutton";
	addEvent(aButton,"click",theFunc);
	addEvent(aButton,"mouseover",hoverStyles);
	addEvent(aButton,"mouseout",hoverStyles);
	container.appendChild(aButton);
	
	//Delete button
	aButton = document.createElement('a');
	aButton.setAttribute("href","javascript:void(0);");
	aButton.appendChild(document.createTextNode("Delete this Content"));
	aButton.className = "deletebutton";
	addEvent(aButton,"click",deleteContentButton);
	addEvent(aButton,"mouseover",hoverStyles);
	addEvent(aButton,"mouseout",hoverStyles);
	container.appendChild(aButton);
	
	//Replace
	theElem.parentNode.replaceChild(container,theElem);
	return container
}

//Button behavior to delete a piece of content
function deleteContentButton(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }

	if((targ = findMyContent(targ)) == false) {
		alert("error");
		return false;
	}

	var targInfo = targ.id.split("_");

	
	//Confirm Delete
	var goAhead = confirm("Are you sure you want to delete \"" + stripHTML(targ.innerHTML.substring(0,60)) + "...?\"");
	if(goAhead) deleteContent(targInfo[1]);
}

//Deletes a specified peice of content
function deleteContent(contentNum) {
	//Add element that serves as delete command
	delInput = document.createElement('input');
	delInput.setAttribute("type","hidden");
	delInput.setAttribute("name","delete_" + contentNum);
	delInput.setAttribute("value","Delete");
	theForm = document.getElementById("contentform");
	theForm.appendChild(delInput);
	
	//submit form
	theForm.submit();
}

//Finds content related to a target by searching siblings and then parents recursively
//Arguments: the element from which to start the search
function findMyContent(srcElem) {
	var targ = srcElem;
	
	//Start search
	while(targ.id.length < 8 || targ.id.substring(0,8) != "content_") {
		//search siblings
		var mySibs = targ.parentNode.childNodes;
		for(var i = 0;i < mySibs.length;i++) {
			if(mySibs[i].nodeType == 1) {
				theId = mySibs[i].id;
				if(theId.length > 7 && theId.substring(0,8) == "content_") return mySibs[i];
			}
		}
		
		if(targ.parentNode.id == "colleft") {
			//move up a level
			targ = targ.parentNode;
		} else {
			//don't go up any farther; we've failed
			return false;
		}
	}
	
	//good to start with
	return targ;
}


function updateImg(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	var targId = targ.id;
	theImg = document.getElementById(targId + "img");
	theImg.src = targ.value;
}


//hover highlighting (since IE won't do this with css)
function hoverStyles(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	/*
	while(targ.nodeType != 1 || targ.className == undefined || targ.className == "") {
		do {
			targ = targ.parentNode;
		} while(targ.nodeType != 1);
		//alert(targ);
	}
	*/
	targ = findMyContent(targ).parentNode;
	
	baseStyle = targ.className.split("_")[0];

	targ.className = (targ.className == baseStyle) ? baseStyle + "_hover" : baseStyle;
	//alert("target: " + targ + "\nid: " + targ.id + "\nclass: " + targ.className);
}


//Check for multiparagraph insertion
function checkMultiples(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }
	var targId = targ.id;
	
	alert("checking multiples on " + targId);
	var texts = document.getElementsByTagName("textarea");
	for(var i = 0;i < texts.length;i++) {
		var idInfo = texts[i].id.split("_");
		if(idInfo.length >= 3 && idInfo[2] == "text") {
			
		}
	}
	var elem = document.createElement('input');
	elem.setAttribute("name","testerize");
	elem.setAttribute("type","hidden");
	elem.setAttribute("value","blah this is a test");
	targ.appendChild(elem);
	//return false;
}


function stripHTML(theString) {
	while((startTag = theString.indexOf("<")) >= 0) {
		if((endTag = theString.indexOf(">")) > startTag) {
			theString = theString.substring(0,startTag) + theString.substring(endTag + 1);
		} else {
			break;
		}
	}
	return theString;
}


//------------------------- ADDITIONAL UTILITIES -------------------------------------
function addEvent(a_elementRef,a_eventName,a_funcRef) {
/*
	var elemList, funcList;
	if(typeof(a_elementRef) != "object") {
		elemList = [a_elementRef];
	} else {
		elemList = a_elementRef;
	}
	if(typeof(a_funcRef) != "object") {
		funcList = [a_funcRef];
	} else {
		funcList = a_funcRef;
	}
	
	for(theElem in a_elementRef) {
		for(theFunc in a_funcRef) {
			//add event
			if(a_elementRef[theElem].addEventListener) {
				a_elementRef[theElem].addEventListener(a_eventName,a_funcRef[theFunc],false);
			} else if(a_elementRef[theElem].attachEvent) {
				a_elementRef[theElem].attachEvent("on" + a_eventName,a_funcRef[theFunc]);
			}
		}
	}
	*/
	if(a_elementRef.addEventListener) {
		a_elementRef.addEventListener(a_eventName,a_funcRef,false);
	} else if(a_elementRef.attachEvent) {
		a_elementRef.attachEvent("on" + a_eventName,a_funcRef);
	}
}

function removeEvent(a_elementRef,a_eventName,a_funcRef) {
	if(a_elementRef.removeEventListener) {
		a_elementRef.removeEventListener(a_eventName,a_funcRef,false);
	} else if(a_elementRef.detachEvent) {
		a_elementRef.detachEvent("on" + a_eventName,a_funcRef);
	}
}