function e(val){
	return document.getElementById(val);
}

function show(divid){
	e(divid).style.display="";
}

function hide(divid){
	e(divid).style.display="none";
}

function toggleshow(divid){
	if (e(divid).style.display=="none"){
		show(divid);	
	}else{
		hide(divid);
	}
}

function GetXmlHttpObject() {
    var objXMLHttp=null
    if (window.XMLHttpRequest) {
        objXMLHttp=new XMLHttpRequest()
        }
    else if (window.ActiveXObject) {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
    return objXMLHttp
}

function changeTemplate(pageid,templateid) {
	location.href="index.php?changetemplate&pageid=" + pageid + "&templateid=" + templateid;
}

function openUploadWindow(divid){
	window.open("upload.php?pd=" + divid,"remote","width=400,height=100");
}

function updateContentWithImage(divid,imagename){
	//var imgcontent = "<img src='http://tipis-tepees-teepees.com/images/" + imagename + "' border=0>";
	//e("freeRTE_content").value = imgcontent + e("freeRTE_content").value;
//	alert(divid);
//	alert(e(divid + ".freeRTE_content"));
//	e("freeRTE_content").value = "hi there";
//	document.getElementById("freeRTE_content").value = "wowzerz";
}









function handleLoadTemplateEditor(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		ret = PHP_Unserialize(xmlHttp.responseText);
		if (ret["successful"]){
			// successful login. SESSION has already been created.
			divid = "template_edit";
			div = e(divid);
			div.style.zIndex = 999999;
			var thehtml;
			thehtml = "<form method='post'><input type='hidden' name='savetemplate' value='1'><input type='hidden' name='templateid' value='" + ret["id"] + "'>";
			thehtml += "<textarea name=\"content\" style=\"width:800px;height:600px\" id=\"tinymce_area_"+ret["id"]+"\">"+ret["content"]+"</textarea>";
			thehtml += "<div align=right>&nbsp;<BR><input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
			thehtml += "</form>\n";
			thehtml += "<script language=\"javascript\">function "+ret["id"]+"_editArea(areaid){document.getElementById(\"div_content_\"+areaid).style.display='none';						document.getElementById(\"div_tinymce_\"+areaid).style.display='';					}					function "+ret["id"]+"_closeEditArea(areaid){						document.getElementById(\"div_content_\"+areaid).style.display='';						document.getElementById(\"div_tinymce_\"+areaid).style.display='none';					}					function "+ret["id"]+"_cancelUpdate(areaid){						if (confirm(\"Cancel changes?\")){							closeEditArea(areaid);							}					}				</script>";
//			thehtml = "<form method='post' name='rteform' id='rteform'><input type='hidden' name='savetemplate' value='1'><input type='hidden' name='templateid' value='" + ret["id"] + "'>";
//			thehtml += initRTEtovar(ret["content"],'admin/example/example.css');
//			thehtml += "<div align=right>&nbsp;<BR><input type='button' value='Upload Image' onclick='openUploadWindow(\"" + divid + "\");' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
			//div.innerHTML = "<textarea>" + thehtml + "</textarea>";

			div.innerHTML = thehtml;
			show(divid);

			e("tinymce_area_"+ret["id"]).value = ret["content"];
/*			tinyMCE.init({
				mode : "exact",
				plugins : "images,advimagescale,table",
				elements : "tinymce_area_"+ret["id"],
				theme : "advanced",
advimagescale_loading_callback: function(imgNode) {
alert(imgNode.src + " is loading");
},
advimagescale_loaded_callback: function(imgNode) {
alert(imgNode.src + " is loaded");
},
advimagescale_resize_callback: function(editorInstance, imgNode) {
alert("resized to " + imgNode.width + "x" + imgNode.height);
},
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,formatselect,forecolor,|,justifyleft,justifycenter,justifyright,justifyfull",
				theme_advanced_buttons2 : "tablecontrols,|,images,image,bullist,numlist,outdent,indent,|,undo,redo,|,link,unlink,|,code",
				theme_advanced_buttons3 : ""
			});
*/

//			function reportElementNesting(elm){
//				var	h=document.documentElement;
//				var	a=[];
//				do{
//					a.unshift(elm.tagName + "=" + elm.id);
//				}while((elm=elm.parentNode)!=h);
//				return a.join(' > ');
//			}
			//	alert(reportElementNesting(document.getElementById('hereitshouldgo')));
			//startRTE(ret["content"]);
			//menuBuilder();
		}else{
			alert(ret["message"]);
		}
	}
}

function loadTemplateEditor(id){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	var str="gettemplate=1&templateid=" + id;
	xmlHttp.onreadystatechange=handleLoadTemplateEditor;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str)		
}
















function handleLoadEditable(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		ret = PHP_Unserialize(xmlHttp.responseText);
//		alert(xmlHttp.responseText);
		if (ret["successful"]){
			// successful login. SESSION has already been created.
			divid = ret["id"] + "_edit";
			div = e(divid);
			div.style.zIndex = 999999;
			var thehtml;
			thehtml = "<form method='post'><input type='hidden' name='savecontent' value='1'><input type='hidden' name='areaid' value='" + ret["id"] + "'>";
			thehtml += "<textarea name=\"content\" style=\"width:100%;height:300px\" id=\"tinymce_area_"+ret["id"]+"\">"+ret["content"]+"</textarea>";
			thehtml += "<div align=right>&nbsp;<BR><input type='button' value='Insert Contact Form' onclick='insertContactForm(\"" + ret["id"] + "\");' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
			thehtml += "</form>\n";
			thehtml += "<script language=\"javascript\">function "+ret["id"]+"_editArea(areaid){document.getElementById(\"div_content_\"+areaid).style.display='none';						document.getElementById(\"div_tinymce_\"+areaid).style.display='';					}					function "+ret["id"]+"_closeEditArea(areaid){						document.getElementById(\"div_content_\"+areaid).style.display='';						document.getElementById(\"div_tinymce_\"+areaid).style.display='none';					}					function "+ret["id"]+"_cancelUpdate(areaid){						if (confirm(\"Cancel changes?\")){							closeEditArea(areaid);							}					}				</script>";
			//thehtml += initRTEtovar(ret["content"],'admin/example/example.css');
			//thehtml += "<div align=right>&nbsp;<BR><input type='button' value='Insert Contact Form' onclick='insertContactForm(\"" + divid + "\");' class='adminbutton'>&nbsp;&nbsp;<input type='button' value='Upload Image' onclick='openUploadWindow(\"" + divid + "\");' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
//			alert(thehtml);
			
			div.innerHTML = thehtml;
			show(divid);

			e("tinymce_area_"+ret["id"]).text = ret["content"];
			tinyMCE.init({
				mode : "exact",
				plugins : "images,advimagescale,table",
				entity_encoding : "raw",
				elements : "tinymce_area_"+ret["id"],
				theme : "advanced",
advimagescale_loading_callback: function(imgNode) {
alert(imgNode.src + " is loading");
},
advimagescale_loaded_callback: function(imgNode) {
alert(imgNode.src + " is loaded");
},
advimagescale_resize_callback: function(editorInstance, imgNode) {
alert("resized to " + imgNode.width + "x" + imgNode.height);
},
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,formatselect,forecolor,|,justifyleft,justifycenter,justifyright,justifyfull",
				theme_advanced_buttons2 : "tablecontrols,|,images,image,bullist,numlist,outdent,indent,|,undo,redo,|,link,unlink,|,code",
				theme_advanced_buttons3 : ""
			});
					
					
			
			//startRTE(ret["content"]);
			//menuBuilder();
		}else{
			alert(ret["message"]);
		}
	}
}

function loadEditable(id){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	var str="getarea=1&area=" + id;
	xmlHttp.onreadystatechange=handleLoadEditable
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-length",str.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(str);
}







function loadAddToList(id){	
	divid = "add" + id + "item";
	div = e(divid);
	var thehtml;
	thehtml = "<form method='post'><input type='hidden' name='addlistcontent' value='1'><input type='hidden' name='listid' value='" + id + "'>";
	thehtml += initRTEtovar('','admin/example/example.css');
	thehtml += "<div align=right>&nbsp;<BR><input type='submit' name='cancel' value='Cancel' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save & Add Item' class='adminbutton'>&nbsp;&nbsp;</div></form>";
	div.innerHTML = thehtml;
	show(divid);
	//startRTE('');
	//menuBuilder();
}











function handleLoadEditableList(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		ret = PHP_Unserialize(xmlHttp.responseText);
		if (ret["successful"]){
			// successful login. SESSION has already been created.
			divid = ret["listid"] + "_" + ret["id"] + "_edit";
			div = e(divid);
			var thehtml;
			thehtml = "<form method='post'><input type='hidden' name='savelistcontent' value='1'><input type='hidden' name='listid' value='" + ret["id"] + "'>";
			thehtml += initRTEtovar(ret["content"],'admin/example/example.css');
			thehtml += "<div align=right>&nbsp;<BR><input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
			div.innerHTML = thehtml;
			show(divid);
			//startRTE(ret["content"]);
			//menuBuilder();
		}else{
			alert(ret["message"]);
		}
	}
}

function loadEditableList(id){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	var str="getlist=1&itemid=" + id;
	xmlHttp.onreadystatechange=handleLoadEditableList
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str)		
}










function handleRemoveListItem(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		ret = PHP_Unserialize(xmlHttp.responseText);
		if (ret["successful"]){
			// hide the editable div
			divid = ret["listid"] + "_" + ret["id"];
			e(divid).style.display="none";
		}
	}
}

function removeListItem(id){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	var str="removelistitem=1&itemid=" + id;
	xmlHttp.onreadystatechange=handleRemoveListItem
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str)	
}













function handleLoadEditableGallery(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		ret = PHP_Unserialize(xmlHttp.responseText);
		if (ret["successful"]){
			// successful login. SESSION has already been created.
			divid = "gallery" + ret["id"] + "_" + ret["field"] + "_edit";
			div = e(divid);
			var thehtml;
			thehtml = "<form method='post'><input type='hidden' name='savegallerycontent' value='1'><input type='hidden' name='galleryid' value='" + ret["id"] + "'><input type='hidden' name='field' value='" + ret["field"] + "'>";
			thehtml += initRTEtovar(ret["content"],'admin/example/example.css');
			thehtml += "<div align=right>&nbsp;<BR><input type='submit' name='cancel' value='Cancel Changes' class='adminbutton'>&nbsp;&nbsp;<input type='submit' name='save' value='Save Changes' class='adminbutton'>&nbsp;&nbsp;</div></form>";
			div.innerHTML = thehtml;
			show(divid);
			//startRTE(ret["content"]);
			//menuBuilder();
		}else{
			alert(ret["message"]);
		}
	}
}

function loadEditableGallery(galleryid,field){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	var str="getgalleryarea=1&galleryid=" + galleryid + "&field=" + field;
	xmlHttp.onreadystatechange=handleLoadEditableGallery
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str)		
}