var _EDITDIV = '';
var _EDITON = 0;

function notepadEditor(someDivID) {
	if (someDivID) {
		/* START the notepad */
		if(_EDITON) { alert('Please first complete your other edits'); return ''; }
		_EDITDIV = someDivID;
		if(! document.getElementById(_EDITDIV)) { alert('ERROR: no such div:' + _EDITDIV); return ''; };

		/* show the notepad */
		document.getElementById('notepad').style.display = 'block';
		document.getElementById('toolbar').style.display = 'block';
		document.getElementById('toolbar').style.float = 'right';
		document.getElementById('notepadeditor').style.visibility = 'visible';
		document.getElementById('notepadeditor').style.position = 'absolute';
		/*
		document.getElementById('notepadeditor').style.left = '25%';

		var myTop = _findTop(_EDITDIV);
		document.getElementById('notepadeditor').style.top = myTop;
		*/

		/* Mozilla might not yet know what 'notepad' is */
		if(document.getElementById('notepad').contentDocument) {
			document.getElementById('notepad').contentDocument.designMode = "On";
			notepad = document.getElementById('notepad').contentWindow;
		};

		/* set the content in the notepad */
		// alert(document.getElementById(_EDITDIV).innerHTML);
		// _myDebug(show_props(notepad.document,'notepad.document'));
		if(notepad.document && goodBrowser) {
			notepad.document.body.innerHTML = _myUnescape(document.getElementById(_EDITDIV).innerHTML);
			/* apply some style */
			notepad.document.body.style.border = '0px';
			notepad.document.body.style.fontFamily = 'Verdana';
			notepad.document.body.style.fontSize = '0.8em';
		} else {
			notepad.value = document.getElementById(_EDITDIV).innerHTML;
		};

		_EDITON = 1;

	} else {
		/* END the notepad */
		if(! _EDITDIV) {
			/* noting is being edited currently, so do nothing */
			return '';
		}
		if(notepad.document && goodBrowser) {
			document.getElementById(_EDITDIV).innerHTML = notepad.document.body.innerHTML;
			notepad.document.body.innerHTML = '';
		} else {
			document.getElementById(_EDITDIV).innerHTML = notepad.value;
			notepad.value = '';
		};
		document.getElementById('notepad').style.display= 'none';
		document.getElementById('toolbar').style.display= 'none';
		document.getElementById('notepadeditor').style.position= 'static';
		document.getElementById('notepadeditor').style.visibility= 'hidden';
		var oldEditDiv = _EDITDIV;
		_EDITDIV = '';
		_EDITON = 0;
		/* call some function that we expect to be overridden */
		postNotepadCall(oldEditDiv);
	};
};

function cancelEdit(){
		if(notepad.document && goodBrowser) {
			notepad.document.body.innerHTML = '';
		} else {
			notepad.value = '';
		};
		document.getElementById('notepad').style.display= 'none';
		document.getElementById('toolbar').style.display= 'none';
		document.getElementById('notepadeditor').style.position= 'static';
		document.getElementById('notepadeditor').style.visibility= 'hidden';
		_EDITDIV = '';
		_EDITON = 0;
};

function _myUnescape(someMessage){
	someMessage = unescape(someMessage);
	re = eval('/' + '&lt;' + '/g'); someMessage = someMessage.replace(re,'<');
	re = eval('/' + '&gt;' + '/g'); someMessage = someMessage.replace(re,'>');
	return someMessage;
};

function formatText(command, option) {
	document.getElementById("notepad").contentWindow.focus();
	// stop Mozilla using CSS for every little thing
	try {
		notepad.document.execCommand("useCSS", false, true);
	} catch(er) {
		// alert('error:' + er); /* fail silently - probably on IE */
	};
	notepad.document.execCommand(command,"",option);
	// document.getElementById("notepad").contentWindow.document.execCommand(command, false, option);
	document.getElementById("notepad").contentWindow.focus();
};
function Select(selectname) {
	if (document.getElementById(selectname).selectedIndex != 0) {
		var selected = document.getElementById(selectname).options[document.getElementById(selectname).selectedIndex].value;
		if(selected == 'RemoveFormat'){
			notepad.document.execCommand('RemoveFormat', false,'');
		} else {
			notepad.document.execCommand(selectname, false, selected);
		};
		document.getElementById(selectname).selectedIndex = 0;
	};
	document.getElementById("notepad").contentWindow.focus();
};

function show_props(obj, obj_name) {
	var result = "";
	for (var i in obj) {
		try {
			result += obj_name + "." + i + " = " + obj[i] + "\n";
		} catch(er) {
			alert('error:' + er);
		};
	};
	return result
};

function postNotepadCall(someEditDiv){};

/**
* And all the stuff below is to make it draggable, thanks to
*
* http://www.guyfromchennai.com/?p=35
*/

var agt=navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var dragapproved=false;
var z,x,y;
var maxleft,maxtop,maxright,maxbottom;

function setdragBounds(){
	//you can set the bounds of the draggable area here
	maxleft = 10;
	maxtop = 10;
	maxright = document.body.clientWidth - 10;
	maxbottom = document.body.clientHeight - 100;
};

function move(e) {
	var tmpXpos = (!is_ie)? temp1+e.clientX-x: temp1+event.clientX-x;
	var tmpYpos = (!is_ie)? temp2+e.clientY-y : temp2+event.clientY-y;
	if (dragapproved){
		z.style.left = tmpXpos + "px";
		z.style.top = tmpYpos + "px";
		if(tmpXpos < maxleft)z.style.left = maxleft;
		if(tmpXpos > maxright)z.style.left = maxright;
		if(tmpYpos < maxtop)z.style.top = maxtop;
		if(tmpYpos > maxbottom)z.style.top = maxbottom;
		return false;
	}
}

function drags(e){
	if (!(is_ie)&&!(!is_ie))
		return
	var firedobj=(!is_ie)? e.target : event.srcElement;
	var topelement=(!is_ie)? "HTML" : "BODY";
	if(firedobj.tagName == 'HTML') { return false; };
	while (firedobj.tagName!=topelement
		&& firedobj.className!="drag"
		&& firedobj.tagName!='A'
		&& firedobj.tagName!='BODY'
		&& firedobj.tagName!='SELECT'
		&& firedobj.tagName!='TEXTAREA'
		&& firedobj.tagName!='INPUT'
		&& firedobj.tagName!='IMG') {
			//here you can add the elements that cannot be used for drag . using their class name or id or tag names
			firedobj=(!is_ie)? firedobj.parentNode : firedobj.parentElement;
	};

	if (firedobj.className=="drag"){
		dragapproved=true;
		z=firedobj;
		var tmpheight = z.style.height.split("px");
		maxbottom = (tmpheight[0])?document.body.clientHeight - tmpheight[0]:document.body.clientHeight - 20;
		temp1=parseInt(z.style.left+0);
		temp2=parseInt(z.style.top+0);
		x=(!is_ie)? e.clientX: event.clientX;
		y=(!is_ie)? e.clientY: event.clientY;
		document.onmousemove=move;
		return false;
	};
};
document.onmousedown=drags;
document.onmouseup=new Function("dragapproved=false");


