/**
 * @author Jonny
 */


function text_Editor(id, place_Id, defaultText, width, height){
	
	if(width == null){
		width = "545";
	}
	if(height == null){
		height = "150";
	}
	this.width = width;
	this.height = height;
	this.id = id;
	this.place_Id = place_Id;
	this.defaultText = defaultText;	
	this.onkeyup;
	
	//gets the html
	this.getHTML = function(){
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		text = frame.document.body.innerHTML;
		text = text.replace(/&amp;/gi, "%26");
		text = text.replace(/&/gi, "%26");
		return text;
	}
	
	this.delete_  = function(){
		document.getElementById(this.place_Id).innerHTML = "";
	}
	
	this.replaceWithText = function(){
		text = this.getHTML();
		span = document.createElement("span");
		span.innerHTML = text
		document.getElementById(this.place_Id).replaceChild(span, this.div)
	}
	
	//creates the buttons
	this.create = function(){
		this.div = document.createElement("div");
		this.div.name = this.div.id = this.id + "div";
		
		document.getElementById(this.place_Id).appendChild(this.div)
		
		//creates the select for the font
		this.fontselect = document.createElement("select");
		this.fontselect.name = this.fontselect.id = this.id + "fontselect";
		this.fontselect.onchange = this.fontselectchange;
	
		document.getElementById(this.div.id).appendChild(this.fontselect);
		
		//Arial font
		this.fontarial = document.createElement("option");
		this.fontarial.name = this.fontarial.id = this.id + "fontarial";
		this.fontarial.value = "Arial";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontarial);
		document.getElementById(this.fontarial.id).innerHTML = "Arial";
		
		//Comic font
		this.fontComic = document.createElement("option");
		this.fontComic.name = this.fontComic.id = this.id + "fontComic";
		this.fontComic.value = "Comic Sans MS";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontComic);
		document.getElementById(this.fontComic.id).innerHTML = "Comic Sans MS";
		
		//Courier font
		this.fontCourier = document.createElement("option");
		this.fontCourier.name = this.fontCourier.id = this.id + "fontCourier";
		this.fontCourier.value = "Courier New";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontCourier);
		document.getElementById(this.fontCourier.id).innerHTML = "Courier New";
		
		//Monotype Corsiva font
		this.fontMonotype = document.createElement("option");
		this.fontMonotype.name = this.fontMonotype.id = this.id + "fontMonotype";
		this.fontMonotype.value = "Monotype Corsiva";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontMonotype);
		document.getElementById(this.fontMonotype.id).innerHTML = "Monotype Corsiva";
		
		//Tahoma font
		this.fontTahoma = document.createElement("option");
		this.fontTahoma.name = this.fontTahoma.id = this.id + "fontTahoma";
		this.fontTahoma.value = "Tahoma";
		this.fontTahoma.selected = "selected";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontTahoma);
		document.getElementById(this.fontTahoma.id).innerHTML = "Tahoma";
		
		//Times font
		this.fontTimes = document.createElement("option");
		this.fontTimes.name = this.fontTimes.id = this.id + "fontTimes";
		this.fontTimes.value = "Times";
		
		document.getElementById(this.fontselect.id).appendChild(this.fontTimes);
		document.getElementById(this.fontTimes.id).innerHTML = "Times";
		
		//creates the select for the font size
		this.fontsizeselect = document.createElement("select");
		this.fontsizeselect.name = this.fontsizeselect.id = this.id + "fontsizeselect";
		this.fontsizeselect.onchange = this.fontsizeselectchange;
	
		document.getElementById(this.div.id).appendChild(this.fontsizeselect);
		
		//1 size font
		this.font1size = document.createElement("option");
		this.font1size.name = this.font1size.id = this.id + "font1size";
		this.font1size.value = "1";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font1size);
		document.getElementById(this.font1size.id).innerHTML = "1";
		
		//2 size font
		this.font2size = document.createElement("option");
		this.font2size.name = this.font2size.id = this.id + "font2size";
		this.font2size.value = "2";
		this.font2size.selected = "selected";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font2size);
		document.getElementById(this.font2size.id).innerHTML = "2";
		
		//3 size font
		this.font3size = document.createElement("option");
		this.font3size.name = this.font3size.id = this.id + "font3size";
		this.font3size.value = "3";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font3size);
		document.getElementById(this.font3size.id).innerHTML = "3";
		
		//4 size font
		this.font4size = document.createElement("option");
		this.font4size.name = this.font4size.id = this.id + "font4size";
		this.font4size.value = "4";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font4size);
		document.getElementById(this.font4size.id).innerHTML = "4";
		
		//5 size font
		this.font5size = document.createElement("option");
		this.font5size.name = this.font5size.id = this.id + "font5size";
		this.font5size.value = "5";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font5size);
		document.getElementById(this.font5size.id).innerHTML = "5";
		
		//6 size font
		this.font6size = document.createElement("option");
		this.font6size.name = this.font6size.id = this.id + "font6size";
		this.font6size.value = "6";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font6size);
		document.getElementById(this.font6size.id).innerHTML = "6";
		
		//7 size font
		this.font7size = document.createElement("option");
		this.font7size.name = this.font7size.id = this.id + "font7size";
		this.font7size.value = "7";
		
		document.getElementById(this.fontsizeselect.id).appendChild(this.font7size);
		document.getElementById(this.font7size.id).innerHTML = "7";
		
		//creates a span
		this.span1 = document.createElement("span");
		this.span1.name = this.span1.id = this.id + "span1";
		
		document.getElementById(this.div.id).appendChild(this.span1);
		document.getElementById(this.span1.id).innerHTML = "&nbsp;| ";
		
		//make a bold button
		this.bold = document.createElement("button");
		this.bold.name = this.bold.id = this.id + "bold";
		this.bold.value = "B";
		this.bold.onclick = this.boldclick;
	
		document.getElementById(this.div.id).appendChild(this.bold);
		document.getElementById(this.bold.id).innerHTML = "<b>B</b>";
		
		//makes an italic button
		this.italic = document.createElement("button");
		this.italic.name = this.italic.id = this.id + "italic";
		this.italic.value = "i";
		this.italic.onclick = this.italicclick;
	
		document.getElementById(this.div.id).appendChild(this.italic);
		document.getElementById(this.italic.id).innerHTML = "<i>i</i>";
		
		//makes an italic button
		this.underline = document.createElement("button");
		this.underline.name = this.underline.id = this.id + "underline";
		this.underline.value = "u";
		this.underline.onclick = this.underlineclick;
	
		document.getElementById(this.div.id).appendChild(this.underline);
		document.getElementById(this.underline.id).innerHTML = "<u>u</u>";
		
		//creates a span
		this.span2 = document.createElement("span");
		this.span2.name = this.span2.id = this.id + "span2";
		
		document.getElementById(this.div.id).appendChild(this.span2);
		document.getElementById(this.span2.id).innerHTML = "&nbsp;| ";
		
		//makes an Left Alignment button
		this.alignleft = document.createElement("button");
		this.alignleft.name = this.alignleft.id = this.id + "alignleft";
		this.alignleft.value = "alignleft";
		this.alignleft.onclick = this.alignleftclick;
	
		document.getElementById(this.div.id).appendChild(this.alignleft);
		document.getElementById(this.alignleft.id).innerHTML = "L";
		
		//makes an Center Alignment button
		this.aligncenter = document.createElement("button");
		this.aligncenter.name = this.aligncenter.id = this.id + "aligncenter";
		this.aligncenter.value = "aligncenter";
		this.aligncenter.onclick = this.aligncenterclick;
	
		document.getElementById(this.div.id).appendChild(this.aligncenter);
		document.getElementById(this.aligncenter.id).innerHTML = "C";
		
		//makes an Right Alignment button
		this.alignright = document.createElement("button");
		this.alignright.name = this.alignright.id = this.id + "alignright";
		this.alignright.value = "alignright";
		this.alignright.onclick = this.alignrightclick;
	
		document.getElementById(this.div.id).appendChild(this.alignright);
		document.getElementById(this.alignright.id).innerHTML = "R";
		
		//creates a span
		this.span3 = document.createElement("span");
		this.span3.name = this.span3.id = this.id + "span3";
		
		document.getElementById(this.div.id).appendChild(this.span3);
		document.getElementById(this.span3.id).innerHTML = "&nbsp;| ";
		
		//makes an link button
		this.link = document.createElement("button");
		this.link.name = this.link.id = this.id + "link";
		this.link.onclick = this.linkclick;
	
		document.getElementById(this.div.id).appendChild(this.link);
		document.getElementById(this.link.id).innerHTML = "<span style='color:#0000ff'><u>Link</u></span>";
		
		//makes an image button
		/*
		this.image = document.createElement("button");
		this.image.name = this.image.id = this.id + "image";
		this.image.onclick = this.imageclick;
	
		document.getElementById(this.div.id).appendChild(this.image);
		document.getElementById(this.image.id).innerHTML = "image";
		*/
		//creates a span
		this.span4 = document.createElement("span");
		this.span4.name = this.span4.id = this.id + "span4";
		
		document.getElementById(this.div.id).appendChild(this.span4);
		document.getElementById(this.span4.id).innerHTML = "&nbsp;| ";
		
		//makes an ordered list button
		this.orderedlist = document.createElement("button");
		this.orderedlist.name = this.orderedlist.id = this.id + "orderedlist";
		this.orderedlist.onclick = this.orderedlistclick;
	
		document.getElementById(this.div.id).appendChild(this.orderedlist);
		document.getElementById(this.orderedlist.id).innerHTML = "1";
		
		//makes an unordered list button
		this.unorderedlist = document.createElement("button");
		this.unorderedlist.name = this.unorderedlist.id = this.id + "unorderedlist";
		this.unorderedlist.onclick = this.unorderedlistclick;
	
		document.getElementById(this.div.id).appendChild(this.unorderedlist);
		document.getElementById(this.unorderedlist.id).innerHTML = "&#8226;";
		
		//creates a popup span
		this.pop = document.createElement("span");
		this.pop.name = this.pop.id = this.id + "popup";
		
		document.getElementById(this.div.id).appendChild(this.pop);
		document.getElementById(this.pop.id).innerHTML = "";
		
		this.br = document.createElement("br");
		document.getElementById(this.div.id).appendChild(this.br);
		
		this.def();
	}
	//creates the iframe
	this.def = function()
	{
		this.textarea = document.createElement("iframe");
		this.textarea.name = this.textarea.id = this.id + "textarea";
		this.textarea.width = this.width;
		this.textarea.height = this.height;
		this.onload = this.textarea.onload;

		if (this.textarea.addEventListener){
			this.textarea.addEventListener("load",function(e){this.contentWindow.document.designMode = "on";}, false);
		} else if (this.textarea.attachEvent){
			this.textarea.attachEvent("load", function(e){this.contentWindow.document.designMode = "on";});
		}

		
		document.getElementById(this.div.id).appendChild(this.textarea);
		
		this.frame = this.textarea.contentWindow || this.textarea.contentDocument;
		this.frame.document.designMode="on";
		this.frame.document.open();
		this.frame.document.write('<head><style type="text/css">body{ font-family:Tahoma; font-size:13px; }</style> </head>');
		this.frame.document.close();
		this.frame.document.body.innerHTML = this.defaultText;
		this.frame.focus();

	}
	
	//event listeners
	
	//make font bolb
	this.boldclick = function (e){
		id = this.id.replace("bold", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("bold","","");
		frame.focus();
	}
	
	this.underlineclick = function (e){
		id = this.id.replace("underline", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("underline","","");
		frame.focus();
	}
	
	this.italicclick = function (e){
		id = this.id.replace("italic", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("italic","","");
		frame.focus();
	}
	
	this.alignleftclick = function (e){
		id = this.id.replace("alignleft", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("justifyleft","","");
		frame.focus();
	}
	
	this.aligncenterclick = function (e){
		id = this.id.replace("aligncenter", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("justifycenter","","");
		frame.focus();
	}
	
	this.alignrightclick = function (e){
		id = this.id.replace("alignright", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("justifyright","","");
		frame.focus();
	}
	
	this.linkclick = function (e){
		id = this.id.replace("link", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		if((url = prompt("http://")) != null){
			The_Link = "http://" + url;
			The_Link = "javascript:void window.open('"+The_Link+"');";
			frame.document.execCommand("CreateLink","", The_Link);
		}
		frame.frames[id + "text"].focus();
	}
	
	this.imageclick = function (e){
		id = this.id.replace("image", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		document.getElementById(id+"popup").innerHTML = "<div id='addimgdiv' style='position: absolute; z-index: 1; top: 40%; left: 40%; background-color:#202020; color: #ffffff; width: 20%; height: 12%; border: 15px ridge #efefef;' ></div>";
		document.getElementById("addimgdiv").innerHTML = "<table><tr width='100%'><td id='file' width='100%'></td></tr><tr><td align='right' id='close'></td></tr></table>";
		document.getElementById("file").innerHTML = "<form><input type=\"file\" name=\"file\" /></br> <input type=\"button\" value=\"upload\" onClick=\"fileUpload(this.form,'upload.php','upload', '" + id + "', '" + frame + "'); return false;\" > <div id=\"upload\" style=\" color: #ffffff; \"></div></form>";
		document.getElementById("close").innerHTML = "<button onclick=\"document.getElementById('"+id+"popup').innerHTML = '' \">close</button>";
	}
	
	this.orderedlistclick = function (e){
		id = this.id.replace("orderedlist", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("insertorderedlist", "", "");
		frame.focus();
	}
	
	this.unorderedlistclick = function (e){
		id = this.id.replace("unorderedlist", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand("insertunorderedlist", "", "");
		frame.focus();
	}
	
	this.fontselectchange = function(e){
		id = this.id.replace("fontselect", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand('fontname', "", this[this.selectedIndex].value);
		frame.focus();
	}
	
	this.fontsizeselectchange = function(e){
		id = this.id.replace("fontsizeselect", "");
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
		frame.document.execCommand('fontsize', "", this[this.selectedIndex].value);
		frame.focus();
	}
}

function fileUpload(form, action_url,div_id, id, frame)  
		{  
		frame = document.getElementById(id + "textarea").contentWindow || document.getElementById(id + "textarea").contentDocument;
			// Create the iframe...  
			var iframe = document.createElement("iframe");  
			iframe.setAttribute("id","upload_iframe");  
			iframe.setAttribute("name","upload_iframe");  
			iframe.setAttribute("width","0");  
			iframe.setAttribute("height","0");  
			iframe.setAttribute("border","0");  
			iframe.setAttribute("style","width: 0; height: 0; border: none;");  
   			// Add to document...  
			form.parentNode.appendChild(iframe);  
			window.frames['upload_iframe'].name="upload_iframe";  
  			iframeId = document.getElementById("upload_iframe");
  			// Add event...  
			var eventHandler = function()  
			{  
				if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);  
				else iframeId.removeEventListener("load", eventHandler, false);  
  				// Message from server...  
				if (iframeId.contentDocument) {  
					content = iframeId.contentDocument.body.innerHTML;  
				} else if (iframeId.contentWindow) {  
					content = iframeId.contentWindow.document.body.innerHTML;  
				} else if (iframeId.document) {  
					content = iframeId.document.body.innerHTML;  
				}
				document.getElementById(div_id).innerHTML = content;
   				frame.document.execCommand('InsertImage', "", content);
				frame.focus();
  
				// Del the iframe...  
				setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);  
			}  
   
			if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);  
			if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);  
   
			// Set properties of form...  
			form.setAttribute("target","upload_iframe");  
			form.setAttribute("action", action_url);  
			form.setAttribute("method","post");  
			form.setAttribute("enctype","multipart/form-data");  
			form.setAttribute("encoding","multipart/form-data");  
 			// Submit the form...  
			form.submit();  
  			document.getElementById(div_id).innerHTML = "Uploading...";  
		}



