// JavaScript Document
var MainIndex=0;
var TabCss=new Array(10);
var InIe=true;
var bold=false,italic=false,underline=false;
function insertHtml(html)
{
	if(InIe){
	   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);
	}
	else{
	   doc=document.getElementById('f_area');
	   if(!doc)return;	   	
	   doc.focus();
	   doc.innerHTML+=html;
	   doc.value+=html;
	}
}


function tagHtml(tag){
	if(InIe){
	   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='<'+tag+'>'+sel.text+'</'+tag+'>&nbsp;';
		   sel.pasteHTML(html);
	   }
	   else{
		   doc.innerHTML=doc.innerHTML+' ';
	   }
	}
	else{
	   doc=document.getElementById('f_area');
	   if(!doc)return;
	   switch(tag){
		   case('b'):v=bold;bold=!bold;break;
		   case('em'):v=italic;italic=!italic;break;
		   case('u'):v=underline;underline=!underline;break;
	   }
	   if(v)slash='/';else slash='';
	   html='<'+slash+tag+'>';
	   doc.innerHTML+=html;
	   doc.value+=html;
	   doc.focus();
	}
}

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){
	if(InIe){
	   doc=document.getElementById('editor_maindiv0');
	   insertHtml('<img src="'+fname+'">');
	}
	else{
		while(fname.indexOf('/')>0){
			fname=fname.substr(fname.indexOf('/')+1);
		}
		fname=fname.slice(0,-4);
		insertHtml('['+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,message){
	if(!InIe){
		alert(message);return;
	}
	img=open_input(evt);
	if(img){
		str='<img src="'+img+'">';
		insertHtml(str);
	}
}
function add_url(evt,message){
	if(!InIe){
		alert(message);return;
	}
	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" href="'+link+'">'+sel.text+'</a>';
		   sel.pasteHTML(html);
	   }
	}
}

function quote(str){
	if(InIe)
	   doc=document.getElementById('editor_maindiv'+MainIndex);
	 else
	   doc=document.getElementById('f_area');
   if(!doc)return;
	var regexp = /“/g;
	str=str.replace(regexp,'"');
	if(InIe)
		doc.innerHTML+='<div class="f_tables_h" contenteditable="false">'+str+'</div>';
	else{
		doc.innerHTML+='<quote>'+str+'</quote>';
		doc.value+='<quote>'+str+'</quote>';
	}
	doc.focus();
}

function notIE(){
	if(navigator.appName=='Microsoft Internet Explorer')
		return;
	InIe=false;
	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';*/
}