// JavaScript Document


/* *****************************************************************************


                          A'CODE BBCODE EDITOR V 1.0


              Copyright 2006 (C) Ansugo ( http://ansugo.zaxaz.com )





    Feel free ( free in free beer, free in freedom or freedom projects on iraq,


    doesn't matter for me what you understand from free ) to use on any type of 


    projects. You can change codes, to whatever you want. Just remember who 


    writes this code first. Rest is yours.


    


              Special Thanks To :


              Peter-Paul Koch (Some Code)     http://www.quirksmode.org/


              Alex King       (A routine)     http://www.alexking.org/


              Mark James      (Silk buttons)  http://www.famfamfam.com/


              Türker Yıldırım (Tests)         http://www.turker.biz/


              -----------------------------------------------------


                          A'CODE BBCODE EDITOR V 1.0


            Telif Hakkı 2006 (C) Ansugo ( http://ansugo.zaxaz.com )





    İstediğiniz yerde özgürce (özgür'ü ifade etmek türkçe de ne kolay!) kullana-


    bilirsiniz. Kodları istediğiniz gibi değiştirebilirsiniz. Sadece ilk kimin


    yazdığını hatırlayın. Gerisi size kalmış.





              Teşekkürler :


              Peter-Paul Koch (Kod parçaları)  http://www.quirksmode.org/


              Alex King       (Bir rutin)      http://www.alexking.org/


              Mark James      (Düğmeler)       http://www.famfamfam.com/


              Türker Yıldırım (Denemeler)      http://www.turker.biz/


   ***************************************************************************** */








/* *****************************************************************************


   myTags array is an array which holds the bbcodes. The format is like this :


   A unique button id which starts with idubb_


   A unique button name


   A button title (to show when user hover on )


   A start tag


   An end tag


   ----------------------------


   myTags dizisi bb kodlarını tutan bir dizidir. Dizi düzeni şöyledir :


   idubb_ ile başlayan benzersiz bir button tanımlama numarası


   Benzersiz bir buton adı


   Bir buton başlığı ( kullanıcı buton üzerine gelince gösterilecek )


   Bir başlangıc tagı


   Bir bitiş tagı


   ***************************************************************************** */








var myTags = new Array();

myTags[myTags.length] = new Array('idubb_strong','strong','KALIN','[B]','[/B]');
myTags[myTags.length] = new Array('idubb_italic','italic','ITALIK','[I]','[/I]');
myTags[myTags.length] = new Array('idubb_underline','underline','ALT CIZGILI','[U]','[/U]');

//myTags[myTags.length] = new Array('idubb_del','strike','STRIKED TEXT','[S]','[/S]');
//myTags[myTags.length] = new Array('idubb_h1','h1','HEADING 1','[H1]','[/H1]');
//myTags[myTags.length] = new Array('idubb_h2','h2','HEADING 2','[H2]','[/H2]');
//myTags[myTags.length] = new Array('idubb_h3','h3','HEADING 3','[H3]','[/H3]');
//myTags[myTags.length] = new Array('idubb_h4','h4','HEADING 4','[H4]','[/H4]');
//myTags[myTags.length] = new Array('idubb_h5','h5','HEADING 5','[H5]','[/H5]');
//myTags[myTags.length] = new Array('idubb_h6','h6','HEADING 6','[H6]','[/H6]');
//myTags[myTags.length] = new Array('idubb_sub','sub','SUBER SCRIPT','[SUB]','[/SUB]');
//myTags[myTags.length] = new Array('idubb_sup','sup','SUPER SCRIPT','[SUP]','[/SUP]');
myTags[myTags.length] = new Array('idubb_code','code','CODE BLOCK','[CODE]','[/CODE]');
//myTags[myTags.length] = new Array('idubb_pre','pre','PREDEFINED TEXT','[PRE]','[/PRE]');
myTags[myTags.length] = new Array('idubb_quote','quote','QUOTED TEXT','[QUOTE]','[/QUOTE]');
//myTags[myTags.length] = new Array('idubb_left','left','ALIGN LEFT','[LEFT]','[/LEFT]');
//myTags[myTags.length] = new Array('idubb_center','center','ALIGN CENTER','[CENTER]','[/CENTER]');
//myTags[myTags.length] = new Array('idubb_right','right','ALIGN RIGHT','[RIGHT]','[/RIGHT]');
//myTags[myTags.length] = new Array('idubb_justify','justify','JUSTIFIED TEXT','[JUSTIFY]','[/JUSTIFY]');
//myTags[myTags.length] = new Array('idubb_list','list','NORMAL LIST','[LIST]','[/LIST]');
//myTags[myTags.length] = new Array('idubb_listnum','listnum','NUMBERED LIST','[LIST1]','[/LIST1]');
myTags[myTags.length] = new Array('idubb_url','url','LINK','[URL]','[/URL]');
myTags[myTags.length] = new Array('idubb_img','img','RESIM','[IMG]','[/IMG]');
//myTags[myTags.length] = new Array('idubb_color','color','TEXT COLOR','[COLOR]','[/COLOR]');
//myTags[myTags.length] = new Array('idubb_email','email','EMAIL ADRESS','[EMAIL]','[/EMAIL]');
//myTags[myTags.length] = new Array('idubb_tab','tab','INSERT TAB','[T]','[/T]');
myTags[myTags.length] = new Array('idubb_br','br','INSERT NEWLINE','[BR]','[/BR]');
/* *****************************************************************************


   ShowAfterRegex function is a gateway to tell regex engine to format the text,


   and writes the formatted text to layer defined.


   ----------------------------


   ShowAfterRegex fonksiyonu düzenli ifade motoruna girilen yazıyı düzenlemesini


   söyler ve düzenlenmiş çıktıyı belirtilmiş olan katmana yazar.


   ***************************************************************************** */








function ShowAfterRegex (myText,myDiv) {


  myText = document.getElementById(myText);


  myDiv  = document.getElementById(myDiv);


  //alert(myDiv.scroll);


  myDiv.innerHTML = myText.value;


  myDiv.innerHTML = RegExpian(myText);


  


  if ( document.getElementById(myText.name + 'html' ) ) {


    document.getElementById(myText.name + 'html').value = myDiv.innerHTML;


  }


  


  myDiv.scrollTop = 20000;


}








/* *****************************************************************************


   RegExpian function is a function to format the bbcoded text to html format.


   ----------------------------


   RegExpian fonksiyonu bb kodları ile verilen yazıyı html biçimine çevirir.


   ***************************************************************************** */








function RegExpian (input) {


  inputreg = input.value;





  inputreg = inputreg.replace(/&/ig,"&amp;");


  inputreg = inputreg.replace(/>/ig,"&gt;");


  inputreg = inputreg.replace(/</ig,"&lt;");


  //inputreg = inputreg.replace(/\"/ig,"&quot;");


  //inputreg = inputreg.replace(/\[/ig,"&#91;");


  //inputreg = inputreg.replace(/\]/ig,"&#93;");





  inputreg = inputreg.replace(/\r\n/ig,"\n");


  inputreg = inputreg.replace(/\r/ig,"\n");


  


  inputreg = inputreg.replace(/\[B\]/ig,"<strong>");


  inputreg = inputreg.replace(/\[\/B\]/ig,"</strong>");


  inputreg = inputreg.replace(/\[I\]/ig,"<em>");


  inputreg = inputreg.replace(/\[\/I\]/ig,"</em>");


  inputreg = inputreg.replace(/\[U\]/ig,"<u>");


  inputreg = inputreg.replace(/\[\/U\]/ig,"</u>");


  inputreg = inputreg.replace(/\[S\]/ig,"<strike>");


  inputreg = inputreg.replace(/\[\/S\]/ig,"</strike>");


  inputreg = inputreg.replace(/\[H1\]/ig,"<h1>");


  inputreg = inputreg.replace(/\[\/H1\]/ig,"</h1>");


  inputreg = inputreg.replace(/\[H2\]/ig,"<h2>");


  inputreg = inputreg.replace(/\[\/H2\]/ig,"</h2>");


  inputreg = inputreg.replace(/\[H3\]/ig,"<h3>");


  inputreg = inputreg.replace(/\[\/H3\]/ig,"</h3>");


  inputreg = inputreg.replace(/\[H4\]/ig,"<h4>");


  inputreg = inputreg.replace(/\[\/H4\]/ig,"</h4>");


  inputreg = inputreg.replace(/\[H5\]/ig,"<h5>");


  inputreg = inputreg.replace(/\[\/H5\]/ig,"</h5>");


  inputreg = inputreg.replace(/\[H6\]/ig,"<h6>");


  inputreg = inputreg.replace(/\[\/H6\]/ig,"</h6>");


  inputreg = inputreg.replace(/\[SUB\]/ig,"<sub>");


  inputreg = inputreg.replace(/\[\/SUB\]/ig,"</sub>");


  inputreg = inputreg.replace(/\[SUP\]/ig,"<sup>");


  inputreg = inputreg.replace(/\[\/SUP\]/ig,"</sup>");


  inputreg = inputreg.replace(/\[CODE\]/ig,"<code>");


  inputreg = inputreg.replace(/\[\/CODE\]/ig,"</code>");


  inputreg = inputreg.replace(/\[PRE\]/ig,"<pre>");


  inputreg = inputreg.replace(/\[\/PRE\]/ig,"</pre>");


  inputreg = inputreg.replace(/\[QUOTE\]/ig,"<blockquote>");


  inputreg = inputreg.replace(/\[\/QUOTE\]/ig,"</blockquote>");


  inputreg = inputreg.replace(/\[LEFT\]/ig,"<div align=\"left\">");


  inputreg = inputreg.replace(/\[\/LEFT\]/ig,"</div>");


  inputreg = inputreg.replace(/\[CENTER\]/ig,"<div align=\"center\">");


  inputreg = inputreg.replace(/\[\/CENTER\]/ig,"</div>");


  inputreg = inputreg.replace(/\[RIGHT\]/ig,"<div align=\"right\">");


  inputreg = inputreg.replace(/\[\/RIGHT\]/ig,"</div>");


  inputreg = inputreg.replace(/\[JUSTIFY\]/ig,"<div align=\"justify\">");


  inputreg = inputreg.replace(/\[\/JUSTIFY\]/ig,"</div>");


  inputreg = inputreg.replace(/\[LIST\]/ig,"<ul>");


  inputreg = inputreg.replace(/\[\/LIST\]/ig,"</ul>");


  inputreg = inputreg.replace(/\[LIST1\]/ig,"<ol>");


  inputreg = inputreg.replace(/\[\/LIST1\]/ig,"</ol>");


  inputreg = inputreg.replace(/\[\*\](.*)(\n|\r|\r\n)/ig,"<li>$1</li>"); // A fix for list lines - Liste satırları için düzeltme


  inputreg = inputreg.replace(/\[\*\](.*)/ig,"<li>$1</li>"); // A fix for list lines - Liste satırları için düzeltme


  


  inputreg = inputreg.replace(/\[URL\]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\" title=\"$1\">$1</a>");


  inputreg = inputreg.replace(/\[URL HREF=\"([^\"]*)\" TITLE=\"([^\"]*)\"]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\" title=\"$2\">$3</a>");


  inputreg = inputreg.replace(/\[EMAIL\]([^\[]*)\[\/EMAIL\]/ig,"<a href=\"mailto:$1\" title=\"$1\">$1</a>");


  inputreg = inputreg.replace(/\[EMAIL HREF=\"([^\"]*)\"](.*)\[\/EMAIL\]/ig,"<a href=\"mailto:$1\">$2</a>");


  inputreg = inputreg.replace(/\[IMG\]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" />");


  inputreg = inputreg.replace(/\[IMG SRC=\"([^\"]*)\" ALIGN=\"([^\"]*)\"]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" alt=\"$3\" align=\"$2\" />");


  inputreg = inputreg.replace(/\[IMG SRC=\"([^\"]*)\"]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" alt=\"$2\" />");


  inputreg = inputreg.replace(/\[COLOR NUM=\"([^\"]*)\"]([^\[]*)\[\/COLOR\]/ig,"<font color=\"$1\">$2</font>");


  inputreg = inputreg.replace(/\[BR\]/ig,"<br />");





  inputreg = inputreg.replace(/\n/ig,"<br />");


  inputreg = inputreg.replace(/\t/ig,"&nbsp;&nbsp;&nbsp;&nbsp;");


  return inputreg;


}








/* *****************************************************************************


   WriteTagButtons function is function which writes the buttons to area where


   it called from.


   ----------------------------


   WriteTagButtons fonksiyonu fonksiyonun çağrıldığı yere düğmeleri yazan bir 


   fonksiyondur.


   ***************************************************************************** */








function WriteTagButtons (textareatemp,layertemp) {

var menus;

menus = '<span style=" width:350px; float:left; margin:0 0 2px 0;  ">';

for ( i = 0; i < myTags.length; i++ ) {
menus = menus + '<input type="button" id="' + myTags[i][0] + '" style="background:url(./images/' + myTags[i][1] + '.jpg) no-repeat  center ; margin:4px 0 0 7px ; width:23px;height:22px;padding:0px;border:1px solid #8698ac;" name="' + myTags[i][1] + '" onClick="WriteOption(\'' + myTags[i][0] + '\',\''+textareatemp+'\',\''+ layertemp + '\',\'' + myTags[i][3] + '\',\'' + myTags[i][4] + '\');" title="' + myTags[i][2] + '" />';
  	}

menus = menus + '</span><span style="float:right;clear:rigt; margin:4px 0 0 7px ; width:22px;height:22px;padding:0px;"><input type="button" id="heig" onClick="gonom(\''+textareatemp+'\');"  style="height:10px;font-size:8px;width:18px;border:1px solid #ccc;background:url(./images/normal.jpg) no-repeat  center "/><input type="button" id="heig" onClick="goup(\''+textareatemp+'\');" style="height:10px;font-size:8px;width:18px;border:1px solid #ccc;background:url(./images/up.jpg) no-repeat  center " /></span>';
	
	document.getElementById('WriteTagButtons').innerHTML = menus;

}

function  goup(obj){
			  
			 document.getElementById(obj).style.height=document.getElementById(obj).offsetHeight+100+'px';
			  }
			  
function  gonom(obj){
	  
document.getElementById(obj).style.height=100+'px';
  }






/* *****************************************************************************


    WriteOption is a function which gets button id,textarea, write layer, open


    tag and close tag from buttons' onClick event and writes the proper text to 


    textarea, after that writes the formatted output to layer.


    ----------------------------


    WriteOption fonksiyonu düğmelerden birine basıldığında (onClick) düğme tanım 


    numarasını, yazı alanını, yazılakcak katmanı, açılış tagını ve kapanış tagını 


    alır, yazılması gereken yazıyı yazı alanına yazar ve bundan sonra düzenlenmiş 


    html çıktıyı ilgili katmana yazar.


   ***************************************************************************** */





function WriteOption (myButId,myText,myLayer,tagOpen,tagClose) {


  myText = document.getElementById(myText);


  myLayer= document.getElementById(myLayer);








  mySelection = '';


  


  if ( window.getSelection || document.getSelection ) {


    mySelection = myText.value.substring(myText.selectionStart,myText.selectionEnd);


  }


  else {


    mySelection = iestr = document.selection.createRange().text;


  }





    


  Content = '';








  myButSwitch = myButId.substring(6,myButId.length);





  // -- LIST


  if ( myButSwitch == 'list' || myButSwitch == 'listnum' ) {


    Content = tagOpen + "\n";


    


		do {


		  tmpval = prompt("Please enter an item:","");


		  if ( tmpval != undefined && tmpval != '') {


		    Content += "[*]" + tmpval + "\n";


		  }


		} while (tmpval != null && tmpval != "" && tmpval != undefined);


		


		Content = Content + tagClose + "\n";


  }


  // -- URL


  else if ( myButSwitch == 'url') {


    tagOpen = tagOpen.substring(0,tagOpen.length-1);


    Content  = tagOpen;


    tmphref  = prompt("Pointing Location:","http://");


    //tmptitle = prompt("Location Title:","");


    tmptext  = '';


    


    if ( mySelection == '' ) {


      tmptext  = prompt("Location Text:","");


    }


    else {


      tmptext = mySelection;


    }


    


    if ( tmphref == null || tmphref == "" || tmphref == undefined ) { Content = Content + ' ' + 'HREF="#"'; }


    else                                                            { Content = Content + ' ' + 'HREF="' + tmphref + '"'; }


    


  //  if ( tmptitle == null || tmptitle == "" || tmptitle == undefined ) { Content = Content + ' ' + 'TITLE="' + '"'; }


   // else                                                               { Content = Content + ' ' + 'TITLE="' + tmptitle + '"'; }


    


    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {


      if ( tmphref != undefined && tmphref != '' ) {


        Content = Content + ']' + tmphref;


      }


      else {


        Content = Content + ']';


      }


    }


    else {


     Content = Content + ']' + '' + tmptext;


    }


    


    Content = Content + tagClose;


  }


  // -- IMG


  else if ( myButSwitch == 'img') {


    tagOpen = tagOpen.substring(0,tagOpen.length-1);


    Content  = tagOpen;


    tmphref  = prompt("Image Source:","");


    tmptext  = '';


    //if ( mySelection == '' ) {


    // tmptext  = prompt("Image Alt Text:","");    


	//  }


    //else {


     // tmptext = mySelection;


    //}


    tmpalign = '';


   // tmpalign = prompt("Where to Align? :","");


    


    


    if ( tmphref == null || tmphref == "" || tmphref == undefined ) {


      Content = Content + ' ' + 'SRC="#"';


    }


    else {


     Content = Content + ' ' + 'SRC="' + tmphref + '"';


    }


    


    if ( tmpalign == null || tmpalign == "" || tmpalign == undefined ) {


      Content = Content + ' ALIGN="CENTER"';


    }


    else {


      Content = Content + ' ALIGN="' + tmpalign + '"';


    }





    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {


      if ( tmphref != undefined && tmphref != '' ) {


        Content = Content + ']' + tmphref;


      }


      else {


        Content = Content + ']';


      }


    }


    else {


     Content = Content + ']' + '' + tmptext;


    }


    


    Content = Content + tagClose;


  }


  // -- COLOR


  else if ( myButSwitch == 'color') {


    tagOpen = tagOpen.substring(0,tagOpen.length-1);





    Content  = tagOpen;


    tmpcolor = prompt("Enter Color Type like #FFFFFF:","");


    tmptext  = '';


    if ( mySelection == '' ) {


      tmptext  = prompt("Enter Text","");


    }


    else {


      tmptext = mySelection;


    }


    


    if ( tmpcolor == null || tmpcolor == "" || tmpcolor == undefined ) {


      Content = Content + ' ' + 'NUM="#"]';


    }


    else {


     Content = Content + ' ' + 'NUM="' + tmpcolor + '"]';


    }





    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {


    }


    else {


     Content = Content + tmptext;


    }


    


    Content = Content + tagClose;


  }


  // -- EMAIL


  else if ( myButSwitch == 'email') {


    tagOpen = tagOpen.substring(0,tagOpen.length-1);





    Content  = tagOpen;


    tmpemail = prompt("Enter Email Address","");


    tmptext  = '';


    if ( mySelection == '' ) {


      tmptext  = prompt("Enter Email Link Text","");


    }


    else {


      tmptext = mySelection;


    }





    if ( tmpemail == null || tmpemail == "" || tmpemail == undefined ) {


      Content = Content + ' ' + 'HREF="#"]';


    }


    else {


     Content = Content + ' ' + 'HREF="' + tmpemail + '"]';


    }





    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {


      Content = Content + tmpemail;


    }


    else {


     Content = Content + tmptext;


    }


    


    Content = Content + tagClose;


  }


  else if ( myButSwitch == "br" ) {


    Content = "[BR]";


  }


  else if ( myButSwitch == 'newline' ) {


    Content = '\n';


  }


  else if ( myButSwitch == 'tab' ) {


    Content = '\t';


  }


  


		var startPos = myText.selectionStart;


		var endPos = myText.selectionEnd;


		var cursorPos = endPos;


		var scrollTop = myText.scrollTop;


  


  if ( window.getSelection || document.getSelection ) {


    // Almost Mozilla or Opera


    if ( myText.selectionStart != myText.selectionEnd ) {


      if ( Content != '' ) {


        myText.value = 


          myText.value.substring(0,myText.selectionStart)


        + Content


        + myText.value.substring(myText.selectionEnd,myText.value.length);


        


        cursorPos += Content.length - mySelection.length;


      }


      else {


       myText.value = 


        myText.value.substring(0,myText.selectionStart)


        + tagOpen


        + myText.value.substring(myText.selectionStart,myText.selectionEnd)


        + tagClose


        + myText.value.substring(myText.selectionEnd,myText.value.length);


        


        cursorPos += tagOpen.length + tagClose.length;


        


      }


    }


    else {


      if ( Content != '' ) {


        myText.value = 


          myText.value.substring(0,myText.selectionStart)


        + Content


        + myText.value.substring(myText.selectionEnd,myText.value.length);


        


        cursorPos += Content.length;


      }


      else {


      myText.value = 


        myText.value.substring(0,myText.selectionStart)


      + tagOpen


      + myText.value.substring(myText.selectionStart,myText.selectionEnd)


      + tagClose


      + myText.value.substring(myText.selectionEnd,myText.value.length);


      


      cursorPos += tagOpen.length;


      


      }


    }


    


		myText.focus();


		myText.selectionStart = cursorPos;


		myText.selectionEnd = cursorPos;


		myText.scrollTop = scrollTop;


  }


  else if (document.selection) {


    // Almost suckzors


    myText.focus();


    iestr = document.selection.createRange();


    


    if ( iestr.text.length > 0 ) {


      if ( Content != '' ) {


        iestr.text = Content;


      }


      else {


        iestr.text = tagOpen + iestr.text + tagClose;


      }


    }


    else {


      if ( Content != '' ) {


        iestr.text = Content;


      }


      else {


        iestr.text = tagOpen + tagClose;


      }


    }


    myText.focus();


  }


  ShowAfterRegex(myText.id,myLayer.id);


}


