var cur_strt=0;
var cur_end=0;
var edit_ary=new Array();

function browser(){
	switch (navigator.appName){
		case "Microsoft Internet Explorer": return 'IE';
		case "Opera": return 'Opera';
		case "Netscape": return 'Netscape';
	}
	return false;
}
function call_float(html,e,w){
	float_txt=return_by_id('float');
	float_txt.innerHTML=html+"\n<p><a href='#close' onClick=\"collapse('float','none');\">close</a></p>";
	if (e!=null) {
		mouse=find_mouse(e);
		float_txt.style.top=mouse['y']+10+"px";
		float_txt.style.left=mouse['x']-60+"px";
	}
	if (w==null) w=200;
	float_txt.style.width=w+"px";
	collapse('float','');
}
function collapse(obj,state){
	if (!obj.style) obj=return_by_id(obj);
	obj.style.display=state;
}
function find_mouse(e) {
	var posx=0;
	var posy=0;
	var pos_id="";
	e=e||window.event;
	if (e.pageX || e.pageY) 	{
		posx=e.pageX;
		posy=e.pageY;
		pos_id='page';
	}
	else if (e.clientX || e.clientY) 	{
		posx=e.clientX+document.documentElement.scrollLeft;
		posy=e.clientY+document.documentElement.scrollTop;
		pos_id='client';
		if (browser()=='IE'){
			posx+=document.body.scrollLeft;
			posy+=document.body.scrollTop;
			pos_id+='IE';
		}
	}
	tmp=new Array();
	tmp['x']=posx;
	tmp['y']=posy;
	tmp['type']=pos_id;
	return tmp;
}
function form_append(frm,to){
	frm=frm.elements;
	total=frm.length;
	for(i=0;i<total;i+=1) to.appendChild(frm[0]);
	return to;
}
function form_check(id){
	data_type=return_by_id("form_"+id).sub.value;
	if (data_type.indexOf('_')) data_type=data_type.substring(0,data_type.indexOf('_'));
	if (data_type=="article") {
		//info on multiple forms from http://www.codetoad.com/forum/15_24387.asp
		f=document.createElement("form");
		with(f) {
			method="post";
			action="";
			name="f_"+id;
			id="f"+id;
			style.display="none";
		}
		form_append(return_by_id("form_"+id),f);
		form_append(return_by_id("link_form"),f);
		f.submit();
	} else {
		return_by_id("form_"+id).submit();
	}
}
function htmlencode(txt) {//modified from http://www.yuki-onna.co.uk/html/encode.html
     encodedHtml=escape(txt);
     encodedHtml=encodedHtml.replace(/\//g,"%2F");
     encodedHtml=encodedHtml.replace(/\?/g,"%3F");
     encodedHtml=encodedHtml.replace(/=/g,"%3D");
     encodedHtml=encodedHtml.replace(/&/g,"%26");
     encodedHtml=encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
 }
function in_array(ary,srch){
	if (ary==null) return false;
	for (i=0;i<ary.length;i++){
		if (ary[i]==srch){
			return i;
		}
	}
	return false;
}
function mark_cursor(obj){
	if (browser()=='IE'){
		if (document.selection){
			//from: http://the-stickman.com
			// The current selection
			var range=document.selection.createRange();
			// We'll use this as a 'dummy'
			var stored_range=range.duplicate();
			// Select all text
			stored_range.moveToElementText(obj);
			// Now move 'dummy' end point to end point of original range
			stored_range.setEndPoint('EndToEnd',range);
			// Now we can calculate start and end points
			obj.selectionStart=stored_range.text.length-range.text.length;
			obj.selectionEnd=obj.selectionStart+range.text.length;
		}
	}
	cur_strt=obj.selectionStart;
	cur_end=obj.selectionEnd;
}
function move_order(id,dir,prefix){
	if (prefix==null) prefix='';
	tmp=Array();
	tmp['name']=return_by_id('name_'+prefix+id).innerHTML;
	tmp['val']=return_by_id(prefix+id+'_id').value;
	old=id;
	switch(dir){
		case 'up':
		if (id!=0) {
			id--;
		}
		break;
		case 'down':
		if (id!=return_by_id(prefix+'number')) id++;
		break;
	}
	if (id!=old){
		return_by_id('name_'+prefix+old).innerHTML=return_by_id('name_'+prefix+id).innerHTML;
		return_by_id(prefix+old+'_id').value=return_by_id(prefix+id+'_id').value;
		return_by_id('name_'+prefix+id).innerHTML=tmp['name'];
		return_by_id(prefix+id+'_id').value=tmp['val'];
	}
}
function msg(type,txt){
	switch(type){
		case 'alert':
		alert(txt); return true;
		break;
		case 'prompt':
		return prompt(txt);
		break;
	}
	return false;
}
function return_by_id(id) { 
	if (document.getElementById) 
		var return_var = document.getElementById(id); 
	else if (document.all) 
		var return_var = document.all[id]; 
	else if (document.layers) 
		var return_var = document.layers[id]; 
	else return false;
	return return_var; 
}
function return_xmlhttp(){
	if (window.XMLHttpRequest) {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  alert("Your browser does not support XMLHTTP!");
	}
	return xmlhttp;
}
function return_xmlDoc(){
	if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}
function switch_id(id,val){
	obj=return_by_id(id+'_id');
	old_val=obj.value;
	obj.value=val;
	obj=return_by_id(id+'_'+val);
	obj.style.margin="1px";
	obj.style.border="solid #fff 1px";
	if (old_val!=null) {
		obj=return_by_id(id+'_'+old_val);
		obj.style.margin="2px";
		obj.style.border="none";
	}
}
function switch_input(id,to){
	var xmlhttp;
	switch(to){
		case 'visual':
		xmlhttp=return_xmlhttp();
		param="text="+htmlencode(return_by_id(id+'_text').value)+"&external=true";
		xmlhttp.open("POST","inc/convert_text.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", param.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4) {
				return_by_id('v_input_'+id).innerHTML=xmlhttp.responseText;
			}
		}
		xmlhttp.send(param);
		collapse('form_'+id,'none');
		collapse('visual_'+id,'');
		break;
		case 'code':
		collapse('form_'+id,'');
		collapse('visual_'+id,'none');
		break;
	}
}
function text_action(id,act,e){
	obj=return_by_id(id+"_text");
	switch(act){
		case 'bold':
		symbstrt=symbend="b";
		break;
		case 'code':
		html="<select name='code_input' id='code_input' onChange=\"text_action("+id+",'code_get')\">\n";
		code=Array('null','php','java');
		for (i=0;i<code.length;i++){
			html+="<option value='"+code[i]+"' ";
			if (return_by_id(id+'_code').value==code[i]) {html+="selected=selected "; }
			html+=">"+code[i]+"</option>\n";
		}
		html+="</select>\n";
		call_float(html,e);
		return true;
		break;
		case 'code_get':
		sel=return_by_id('code_input');
		return_by_id(id+'_code').value=sel.options[sel.selectedIndex].value;
		return true;
		break;
		case 'underline':
		if (cur_strt==cur_end) {
			uline=msg('prompt','underlined');
			if (uline==null) return false;
		} else uline=obj.value.substring(cur_strt,cur_end);
		obj.value=obj.value.substring(0,cur_strt)+"[u,"+uline+"]"+obj.value.substring(cur_end,obj.value.length);
		return true;
		break;
		case 'file':
		xmlhttp=return_xmlhttp();
		xmlhttp.open("GET","inc/get_file.php",true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4) {	
				html="<form id='fl_"+id+"'>\n";
				html+="<p style='float:right'><a href='#file_"+id+"' onClick=\"call_float(add)\">add new</a></p>\n";
				html+="<p>id:<input type='text' readonly='readonly' size='2' id='file_id' />\n";
				html+="<p>"+xmlhttp.responseText+"</p>\n";
				html+="<input type='button' value='select file' onClick=\"text_action('"+id+"','file_get')\" />\n</form>\n";
				call_float(html,e);
			}
		}
		add="<p style='float:right'><a href='#file_"+id+"' onClick=\"text_action(id,'file')\">back</a></p>\n";
		add+="<form id='fl_add_"+id+"' method='post' action='inc/save_file.php' enctype='multipart/form-data' target='upload_target'>\n";
		add+="<p><input type='file' name='url' id='url' style='width:170px;' /></p>\n";
		add+="<p><input type='text' name='name' id='name' /></p>\n";
		add+="<p><input type='hidden' name='type' id='type' value='file' />\n";
		add+="<input type='submit' value='add' onClick=\"return_by_id('upload_target').style.display='';\" />";
		add+="</form>\n";
		add+="<iframe id='upload_target' style='display:none;'></div>\n";
		return true;
		break;
		case 'file_get':
		txt=obj.value.substring(0,cur_strt);
		txt+="[file,"+return_by_id('file_id').value+"]";
		txt+=obj.value.substring(cur_end,obj.value.length);
		obj.value=txt;
		collapse('float','none');
		return true;
		break;
		case 'image':
		xmlhttp=return_xmlhttp();
		xmlhttp.open("GET","inc/get_image.php",true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4) {
				html="<form id='image_"+id+"'>\n";
				html+="<p style='float:right'><a href='#image_"+id+"' onClick=\"call_float(add)\">add new</a></p>\n";
				html+="<p>id:<input type='text' readonly='readonly' size='2' id='img_id' />\n";
				html+="<p>"+xmlhttp.responseText+"</p>\n";
				html+="<input type='hidden' id='align_id' name='align_id' />";
				html+="<p style='text-align:center;padding:5px;'><a href='#null' onClick=\"switch_id('align','left')\">\n";
				html+="<img src='img/al_lft.gif' alt='left-align' title='left-align' style='padding:0px;margin:2px;' id='align_left' />\n</a>\n";
				html+="<a href='#null' onClick=\"switch_id('align','centre')\">\n";
				html+="<img src='img/al_cen.gif' alt='centre-align' title='centre-align' style='padding:0px;margin:2px;' id='align_centre' />\n</a>\n";
				html+="<a href='#null' onClick=\"switch_id('align','right')\">\n";
				html+="<img src='img/al_rgt.gif' alt='right-align' title='right-align' style='padding:0px;margin:2px;' id='align_right' />\n</a>\n</p>\n";
				html+="<p style='float:left;margin:0em 15px 0em 0%'>width: <input type='text' size='3' id='width_id' value='' /></p>\n";
				html+="<p style='position:relative;bottom:5px;'>square<br />image: <input type='checkbox' id='square_id' /></p>\n";
				html+="<input type='button' value='select image' onClick=\"text_action('"+id+"','image_get')\" />\n</form>\n";
				if (e==null) call_float(html);
				else call_float(html,e);
			}
		}
		add="<p style='float:right'><a href='#image_"+id+"' onClick=\"text_action(id,'image')\">back</a></p>\n";
		add+="<form id='fl_add_"+id+"' method='post' action='inc/save_file.php' enctype='multipart/form-data' target='upload_target'>\n";
		add+="<p><input type='file' name='url' id='url' style='width:170px;' /></p>\n";
		add+="<p><input type='text' name='name' id='name' /></p>\n";
		add+="<p><input type='hidden' name='type' id='type' value='image' />\n";
		add+="<input type='submit' value='add' onClick=\"return_by_id('upload_target').style.display='';\" />";
		add+="</form>\n";
		add+="<iframe id='upload_target' style='display:none;'></div>\n";
		return true;
		break;
		case 'image_get':
		txt=obj.value.substring(0,cur_strt);
		txt+="[img,"+return_by_id('img_id').value+",";
		if (return_by_id('align_id').value!="") txt+=return_by_id('align_id').value+",";
		else txt+="centre,";
		if (return_by_id('width_id').value!="") txt+=return_by_id('width_id').value;
		else txt+="0";
		if (return_by_id('square_id').checked==true) txt+=",sq";
		txt+="]";
		txt+=obj.value.substring(cur_end,obj.value.length);
		obj.value=txt;
		collapse('float','none');
		return true;
		break;
		case 'italic':
		symbstrt=symbend="i";
		break;
		case 'link':
		url=msg('prompt','url - leave blank for selection');
		if (url==null&&cur_strt==cur_end) return false;
		else if (url==null||url=="") url=obj.value.substring(cur_strt,cur_end);
		if (cur_strt==cur_end) {
			con=msg('prompt','text - leave blank for url');
		} else {
			con=msg('prompt','text - leave blank for selection');
			if (con==null||con=="") con=obj.value.substring(cur_strt,cur_end);
		}
		txt=obj.value.substring(0,cur_strt)+"[a,"+url;
		if (con!=null) txt=txt+","+con;
		obj.value=txt+"]"+obj.value.substring(cur_end,obj.value.length);
		return true;
		break;
		case 'tag':
		html="<input type='text' name='tag_input' id='tag_input' value='"+return_by_id(id+'_tag').value+"' onBlur=\"text_action("+id+",'tag_get')\" onKeyUp=\"text_action("+id+",'tag_get')\" />"
		call_float(html,e);
		return true;
		break;
		case 'tag_get':
		return_by_id(id+'_tag').value=return_by_id('tag_input').value;
		return true;
		break;
		case 'underline':
		if (cur_strt==cur_end) {
			uline=msg('prompt','underlined');
			if (uline==null) return false;
		} else uline=obj.value.substring(cur_strt,cur_end);
		obj.value=obj.value.substring(0,cur_strt)+"[u,"+uline+"]"+obj.value.substring(cur_end,obj.value.length);
		return true;
		break;
		case 'title':
		html="<input type='text' name='title_input' id='title_input' value='"+return_by_id(id+'_name').value+"' onBlur=\"text_action('"+id+"','title_get')\" onKeyUp=\"text_action('"+id+"','title_get')\" />"
		call_float(html,e);
		return true;
		break;
		case 'title_get':
		return_by_id(id+'_name').value=return_by_id('title_input').value;
		return true;
		break;
		case 'underline':
		if (cur_strt==cur_end) {
			uline=msg('prompt','underlined');
			if (uline==null) return false;
		} else uline=obj.value.substring(cur_strt,cur_end);
		obj.value=obj.value.substring(0,cur_strt)+"[u,"+uline+"]"+obj.value.substring(cur_end,obj.value.length);
		return true;
		break;
	}
	if (cur_strt!=cur_end) {
		bold_select=obj.value.substring(cur_strt,cur_end);
		txt=obj.value.substring(0,cur_strt)+"["+symbstrt+"]"+bold_select+"[/"+symbend+"]"+obj.value.substring(cur_end,obj.value.length);
	} else {
		txt=obj.value.substring(0,cur_strt);
		if (in_array(edit_ary,act)!==false) {
			txt=txt+"[/"+symbend+"]";
			edit_ary[in_array(edit_ary,act)]=null;
		} else {
			txt=txt+"["+symbstrt+"]";
			edit_ary[edit_ary.length]=act;
		}
		txt=txt+obj.value.substring(cur_end,obj.value.length);
	}
	obj.value=txt;
	focus=obj;
}
function xmlValue(xmlDoc,id){
	return xmlDoc.getElementByTagName(id)[0].childNode[0].nodeValue;
}