var myTags = new Array();
myTags[myTags.length] = new Array('idubb_strong','strong','BOLD TEXT','<B>','</B>');
myTags[myTags.length] = new Array('idubb_italic','italic','ITALIC TEXT','<I>','</I>');
myTags[myTags.length] = new Array('idubb_underline','underline','UNDERLINED TEXT','<U>','</U>');
myTags[myTags.length] = new Array('idubb_del','strike','STRIKED TEXT','<del>','</del>');
myTags[myTags.length] = new Array('idubb_h1','h1','HEADING 1','<div class=head>','</div>');
myTags[myTags.length] = new Array('idubb_h2','h2','HEADING 2','<div class=smallhead>','</div>');
myTags[myTags.length] = new Array('idubb_quote','quote','QUOTED TEXT','<blockquote>','</blockquote>');
myTags[myTags.length] = new Array('idubb_center','center','ALIGN CENTER','<div align=center>','</div>');
myTags[myTags.length] = new Array('idubb_right','right','ALIGN RIGHT','<div align=right>','</div>');
myTags[myTags.length] = new Array('idubb_url','url','URL','<A>','</A>');
myTags[myTags.length] = new Array('idubb_img','img','IMAGE','<IMG>','');
myTags[myTags.length] = new Array('idubb_strong','img','LEFT IMAGE','<div class=ibl>','</div>');
myTags[myTags.length] = new Array('idubb_strong','img','RIGHT IMAGE','<div class=ibr>','</div>');
myTags[myTags.length] = new Array('idubb_color','color','TEXT COLOR','<SPAN>','</SPAN>');
myTags[myTags.length] = new Array('idubb_email','email','EMAIL ADRESS','<A>','</A>');



function WriteTagButtons (textareatemp) {
  for ( i = 0; i < myTags.length; i++ ) {
    document.write('<input type="button" id="' + myTags[i][0] + '" style="margin: 0px;background: #ffffff url(./pic/bbc/' + myTags[i][1] + '.gif) no-repeat;width:22px;height:22px;padding:2px;" name="' + myTags[i][1] + '" onClick="WriteOption(\'' + myTags[i][0] + '\',\''+textareatemp+'\',\'' + myTags[i][3] + '\',\'' + myTags[i][4] + '\');" title="' + myTags[i][2] + '" />');
  }
document.write('<br>');
}


function WriteOption (myButId,myText,tagOpen,tagClose) {
  myText = document.getElementById(myText);


  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 += "<li>" + tmpval + "</li> \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;
    }
    else {
      Content = Content + ' ALIGN="' + tmpalign + '"';
    }

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

        Content = Content + '>';

    }
    else {
     Content = Content + ' ALT="' + 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 or 'red' :","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Enter Text","");
    }
    else {
      tmptext = mySelection;
    }
    
    if ( tmpcolor == null || tmpcolor == "" || tmpcolor == undefined ) {
      Content = Content + ' ' + 'style="color: #">';
    }
    else {
     Content = Content + ' ' + 'style="color:' + 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="mailto:#">';
    }
    else {
     Content = Content + ' ' + 'HREF="mailto:' + tmpemail + '">';
    }

    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
      Content = Content + tmpemail;
    }
    else {
     Content = Content + tmptext;
    }
    
    Content = Content + tagClose;
  }

  
		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();
  }

}