// JavaScript Document
var MainIndex=0;
var TabCss=new Array(10);
function insertHtml(html)
{
   doc=document.getElementById('editor_maindiv'+MainIndex);
   if(!doc)
   	return;
   doc.focus();
   var sel = document.selection.createRange();
   // don't try to insert HTML into a control selection (ie. image or table)
   if (document.selection.type == 'Control') {
      return;
   }
   sel.pasteHTML(html);
}

function inTag(doc,tag){
   var sel = document.selection.createRange();
	key='!@-i*n%c';
	txt=key+sel.text;
	oldstr=doc.innerHTML;
	sel.pasteHTML(txt);
	str=doc.innerHTML;
	pos=str.indexOf(key);
	alert(pos);
	doc.innerHTML=oldstr;
}

function tagHtml(tag){
   doc=document.getElementById('editor_maindiv'+MainIndex);
   if(!doc)
   	return;
   doc.focus();
   // don't try to insert HTML into a control selection (ie. image or table)
   if (document.selection.type == 'Control') {
      return;
   }
   var sel = document.selection.createRange();
   if(sel.text!=''){
	   //inTag(doc,tag);
		html='<'+tag+'>'+sel.text+'</'+tag+'>&nbsp;';
	   sel.pasteHTML(html);
   }
   else{
	   //html='</'+tag+'>&nbsp;<'+tag+'>';
	   //sel.pasteHTML(html);
	   doc.innerHTML=doc.innerHTML+' ';
   }
}

function KeyPress(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if(charCode==13)//return key- skip line
		return insertBreak();
	/*else
		return false;*/
	
}

function on_paste(evt){
	var data=window.clipboardData.getData('Text');
	insertHtml(data);
	return false;
}

function insertBreak(){
	insertHtml('<br>');
	return false;
}

function get_edit_content(){
	obj_edit=document.getElementById('editor_maindiv'+MainIndex);
	obj_dest=document.getElementById('editor_input'+MainIndex);
	obj_css=document.getElementById('select_css'+MainIndex);
	if((obj_edit)&&(obj_dest)&&(obj_css))
		obj_dest.value=obj_css.value+'</eNdcSs>'+obj_edit.innerHTML;
}

function change_css(value,index){
	if(index){
		TabCss[index]=value;
	}
	else
		TabCss[MainIndex]=value;
	style=document.getElementById('edit_css');
	if(!style)
		return;
	style.href=value;
}

function load_font_edit(select_id,class_prefix){
	obj=document.getElementById(select_id);
	style=document.getElementById('edit_css');
	if((!style)||(!obj))
		return;
	//alert(document.styleSheets[0].rules);
	for(i=0;i<document.styleSheets.length;i++)
		if(document.styleSheets[i].href==style.href)
			style=document.styleSheets[i];
	index=0;
	for(i=0;i<style.rules.length;i++){
		if(style.rules[i].selectorText.indexOf(class_prefix)>=0){
			obj.options[index]=new Option(style.rules[i].selectorText.slice(class_prefix.length+1));
			obj.options[index].value=style.rules[i].selectorText.slice(1);
			index++;
		}
	}
}
function add_edit_font(){
	obj=document.getElementById('select_edit_font'+MainIndex);
	val=document.getElementById('value_edit_font'+MainIndex);
	if((!val)||(!obj))
		return;
	text='<span class="'+obj.value+'">'+val.value+'</span> ';
	insertHtml(text);
}

function add_edit_link(){
	obj=document.getElementById('select_edit_link'+MainIndex);
	val=document.getElementById('value_edit_link'+MainIndex);
	url=document.getElementById('value_edit_url'+MainIndex);
	if((!val)||(!obj)||(!url))
		return;
	text='<a href="'+url.value+'" class="'+obj.value+'">'+val.value+'</a> ';
	insertHtml(text);
}

var selected_image=0;
function select_img_edit(index){
	if(selected_image){
		obj=document.getElementById('edit_img_'+selected_image);
		if(obj)
			obj.className="img_unselect";
	}

	obj=document.getElementById('edit_img_'+index);
	if(obj){
		obj.className="img_select";
		selected_image=index;
	}
}
function add_image(align){
	if(!selected_image)
		return;
	html='<img src="../photos/'+selected_image+'.jpg"';
	if(align)
		html+='align="'+align+'"';
	html+='class="edit_img">';
	insertHtml(html);
}

function put_smili(fname){
   doc=document.getElementById('editor_maindiv0');
   insertHtml('<img src="'+fname+'">');
}

function open_input(evt){
	x=event.screenX;
	y=event.screenY;
	url=showModalDialog("url.html",'',"dialogLeft:"+x+"px;dialogTop="+y+"px; dialogHeight:125px; dialogWidth:325px; help:no");
	return(url);
}
function add_img(evt){
	img=open_input(evt);
	if(img){
		str='<img src="'+img+'">';
		insertHtml(str);
	}
}
function add_url(evt){
	link=open_input(evt);
	if(link){
	   doc=document.getElementById('editor_maindiv'+MainIndex);
	   if(!doc)
   			return;
	   doc.focus();
	   // don't try to insert HTML into a control selection (ie. image or table)
	   if (document.selection.type == 'Control') {
    	  return;
	   }
	   var sel = document.selection.createRange();
	   if(sel.text!=''){
		   html='<a target="_blank" class="f_link_f" href="'+link+'">'+sel.text+'</a>';
		   sel.pasteHTML(html);
	   }
	}
}

function quote(str){
   doc=document.getElementById('editor_maindiv'+MainIndex);
   if(!doc)
		return;
	var regexp = /“/g;
	str=str.replace(regexp,'"');
	doc.innerHTML+='<div class="f_table" contenteditable="false">'+str+'</div>';
	doc.focus();
}

function notIE(){
	if(navigator.appName=='Microsoft Internet Explorer')
		return;
	doc=document.getElementById('editor_maindiv'+MainIndex);
	if(!doc)
		return;
	doc.style.display='none';
	doc=document.getElementById('f_d_area');
	doc.style.display='block';
	doc=document.getElementById('f_area');
	doc.name='f_mesaj';
	doc=document.getElementById('f_mesaj');
	doc.name='f_old';
	doc=document.getElementById('f_panel1');
	doc.style.display='none';
	doc=document.getElementById('f_panel2');
	doc.style.display='none';
}