// JavaScript Document

var gActiveDoc = new Object();
var gPrevID = 0;
var gCommands = "formatblock,fontname,fontsize,bold,italic,underline,justifyleft,justifyright,justifycenter,justifyfull,inserthorizontalrule,insertorderedlist,insertunorderedlist,outdent,indent,CreateLink,Unlink,hilitecolor,backcolor,InsertImage";
var gStyleTemp = '<link media="all" type="text/css" href="grte.css" rel="stylesheet">';
		
function gCreateEditor(id,fld_value) {
	var geditor = document.getElementById(id);
	geditor.innerHTML = '<div id="gtool.'+id+'" style="display:none">fgfg</div><iframe class="gdocStyle" id="gdoc.'+id+'" name="gdoc.'+id+'" src="blank.htm" width="99.8%" height="100%"></iframe>';
	if(!fld_value) fld_value = "";
	if (document.all) {
		var gdoc = frames["gdoc."+id].document;
		gdoc.open();		
		gdoc.write(gStyleTemp+fld_value);
		gdoc.close();
		gdoc.designMode = "on";	
		gdoc.attachEvent("onclick",
			function(e) {
				if(gPrevID!=id) {
					//alert(id);
					gActiveDoc = gdoc;
					gShowUI(id);				 
				}
			});
	} else {
		var gdoc = document.getElementById("gdoc."+id);	
		gdoc.contentWindow.document.open();
		gdoc.contentWindow.document.write(gStyleTemp+fld_value);
		gdoc.contentWindow.document.close();
		gdoc.contentDocument.designMode = "on";
		gdoc.contentWindow.document.addEventListener("click",
			function(e) { 
				if(gPrevID!=id) {
					//alert(id);
					gActiveDoc = gdoc;
					gShowUI(id);				 
				}
			} , false);
		gdoc.contentWindow.document.addEventListener("keypress", gKB_Handler, true);
	}
	gActiveDoc = gdoc;
	gShowUI(id);
}

function gShowUI(id) {
	var gdeft = document.getElementById("gdef_tool");	
	var gtool = document.getElementById("gtool."+id);	
	if(gPrevID!=0) {
		var ptool = document.getElementById("gtool."+gPrevID);	
		ptool.style.display = "none";
		ptool.innerHTML = ""; 	
	}
	gtool.innerHTML = gdeft.innerHTML;
	gtool.style.display = "";
	gPrevID=id;
}

function gApplyStyle(gstyle) {
	gDoFormat(gActiveDoc, gstyle, '');
}

function gDoFormat(gfield, command, option) {
	if (document.all) {
		gfield.focus();
		gfield.execCommand(command, false, option);
		gfield.focus();
	} else {
		gfield.contentWindow.focus();
	  	gfield.contentWindow.document.execCommand(command, false, option);
		gfield.contentWindow.focus();
	}	
	//alert(command+" -> "+gfield.id);
}

function gGetSelection(gfield) {
	if (document.all) {
		var selection = gfield.selection; 
		if (selection != null) rng = selection.createRange();
		if (typeof(rng.parentElement) != "undefined") {
			rng = rng.text;
		}
	} else {
		var selection = gfield.contentWindow.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
	return rng;
}

function gCreateLink() {
	var gurl = prompt("Insert Hyperlink: \n\r(Leave empty and click OK to remove link)","");
	if(gurl!=null) {
		if(gurl=="") gDoFormat(gActiveDoc, "UnLink");
		else gDoFormat(gActiveDoc, "CreateLink", gurl);
	}
	//var gsel = gSetRange(gActiveDoc);
}

function gInsertImage() {
	var gimg = prompt("Enter Image Path:");
}

function gKB_Handler(evt) {
	if (evt.ctrlKey) {
		var key = String.fromCharCode(evt.charCode).toLowerCase();
		var cmd = '';
		switch (key) {
			case 'b': cmd = "bold"; break;
			case 'i': cmd = "italic"; break;
			case 'u': cmd = "underline"; break;
		};

		if (cmd) {
			gDoFormat(gActiveDoc, cmd, null);
			evt.preventDefault();
			evt.stopPropagation();
		}
 	}
}

function insert_html(html){
	var random_string = "insert_html_" + Math.round(Math.random()*100000000);
	frames.wysiwyg_editor.document.execCommand("insertimage",false, random_string);
	var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
	var current_html = frames.wysiwyg_editor.document.body.innerHTML = frames.wysiwyg_editor.document.body.innerHTML.replace(pat, html);
}

function gPasteHTML() {
	if (document.all) gPasteHTML2();
	else gPopWin('gdialogs/d_pastehtml.php');
}

function gDoPasteHTML() {
	var html = document.getElementById("fld_html").value;	
	if(!html) {
		alert("HTML content required!");
		return;
	}
	//alert(html);
	gWinShow(false);
	var gfield = gActiveDoc;
	if (document.all) {
		gfield.focus();
		gfield.selection.createRange().pasteHTML(html);
		gfield.focus();		
	} else {
		gfield.contentWindow.focus();
		//gfield.contentWindow.document.getElementsByTagName('body')[0].focus();
		//alert(gfield.contentWindow.document.getElementsByTagName('body')[0].innerHTML);
		//gfield.insertHTML(html);
	  	gfield.contentWindow.document.execCommand('insertHTML', false, html);
		gfield.contentWindow.focus();
	}
}

function gPasteHTML2() {
	var html = prompt("Paste HTML:","");
	if(!html) return;
	var gfield = gActiveDoc;
	if (document.all) {
		gfield.focus();
		gfield.selection.createRange().pasteHTML(html);
		gfield.focus();		
	} else {
		gfield.contentWindow.focus();
	  	gfield.contentWindow.document.execCommand('insertHTML', false, html);
		gfield.contentWindow.focus();
	}
}

function gPasteFormat(html) {
	if(!html) return;
	var gfield = gActiveDoc;
	if (document.all) {
		gfield.focus();
		gfield.selection.createRange().pasteHTML(html);
		gfield.focus();		
	} else {
		gfield.contentWindow.focus();
	  	gfield.contentWindow.document.execCommand('insertHTML', false, html);
		gfield.contentWindow.focus();
	}
}

function gFormatBlock(e) {
	if(e.value) {
		var gsel = gGetSelection(gActiveDoc);
		var fmtsel = '<span class="'+e.value+'">'+gsel+'</span>';
		//alert(fmtsel);
		gPasteFormat(fmtsel);
		e.selectedIndex = 0;
	}
}

function gViewSource() {
	alert(gActiveDoc.contentWindow.document.body.innerHTML);
}

