/////////////////////////////////////////////////
// UNIVERSAL BULLETIN BOARD CODE INTERFACE v2.1
// By Mark Plachetta (astroboy@zip.com.au)
/////////////////////////////////////////////////

/////////////////////////////
// Basic browser detection
//
  var ie = (document.all) ? 1 : 0;
  var nn = (document.layers) ? 1 : 0;
  var n6 = (window.sidebar) ? 1 : 0;


/////////////////////////////
// Stylesheet output
//
  function writeStyle() {
    var html = '<style type="text/css"><!--\n';
       html += '.ibcButton { font-size:11px; font-family:Tahoma, Arial, Helvetica, sans-serif; background-color:#ececec;  }\n';
       html += '.ubbcButton { font-size:11px; font-family:Tahoma, Arial, Helvetica, sans-serif; background-color:#ececec;}\n'; 
       html += '.ibcSelect{ font-size:11px; font-family:Tahoma, Arial, Helvetica, sans-serif; background-color:#ececec;  }\n';
       html += '.ibcButton { font-size:11px; font-family:Tahoma, Arial, Helvetica, sans-serif; background-color:#ececec; }\n';
       html += ' --></style>';

    document.write(html);
  }


/////////////////////////////
// Interface output
//
  function makeInterface(images,flash,graphical) {
    var html = '<table border="0" cellpadding="2" cellspacing="0"><tr><td>' + ((nn) ? '&nbsp;' : '');
       html += ' <select name="fcolor" class="ibcSelect" onchange="ubbFont(this);">';
       html += makeOption('','لون الخط',0);
       html += makeOption('skyblue','سماوي',1);
       html += makeOption('royalblue','أزرق ملكي',1);
       html += makeOption('blue','أزرق',1);
       html += makeOption('darkblue','أزرق داكن',1);
       html += makeOption('orange','برتقالي',1);
       html += makeOption('orangered','ناري',1);
       html += makeOption('crimson','وردي ملكي',1);
       html += makeOption('red','أحمر',1);
       html += makeOption('firebrick','أحمر ناري',1);
       html += makeOption('darkred','أحمر داكن',1);
       html += makeOption('green','أخضر',1);
       html += makeOption('limegreen','أخضر ليموني',1);
       html += makeOption('seagreen','أخضر بحري',1);
       html += makeOption('deeppink','وردي',1);
       html += makeOption('tomato','أحمر طماطي',1);
       html += makeOption('coral','بصلي',1);
       html += makeOption('purple','بنفسجي',1);
       html += makeOption('indigo','بنفسجي داكن',1);
       html += makeOption('burlywood','خشبي',1);
       html += makeOption('sandybrown','بني رملي',1);
       html += makeOption('sienna','بني',1);
       html += makeOption('chocolate','كاكاو',1);
       html += makeOption('teal','تركواز',1);
       html += makeOption('silver','رمادي',1);
       html += '</select> ';
       html += '<select name="fsize" class="ibcSelect" onchange="ubbFont(this);">';
       html += makeOption('','حجم الخط',0);
       html += makeOption('1','صغير جداً',0);
       html += makeOption('2','صغير',0);
       html += makeOption('6','وسط',0);
       html += makeOption('8','كبير',0);
       html += makeOption('10','كبير جداً',0);
       html += '</select> ';
       html += '<select name="ffont" class="ibcSelect" onchange="ubbFont(this);">';
       html += makeOption('','نوع الخط',0);
       html += makeOption('arial','Arial',0);
       html += makeOption('Traditional Arabic','Traditional Arabic',0);
       html += makeOption('Simplified Arabic','Simplified Arabic',0);
       html += makeOption('MS Sans Serif','MS Sans Serif',0);
       html += makeOption('Courier New','Courier New',0);
       html += makeOption('impact','Impact',0);
       html += makeOption('tahoma','Tahoma',0);
       html += makeOption('times','Times',0);
       html += makeOption('verdana','Verdana',0);
       html += '</select> ';
                html += '<select name="talign" class="ibcSelect" onchange="ubbAlign(this.options[this.selectedIndex].value);">';
       html += makeOption('','محاذاة',0);
         html += makeOption('left','يسار',0);
         html += makeOption('center','وسط',0);
         html += makeOption('right','يمين',0);
         html += '</select> ';
       html += '</td></tr><tr><td align=>' + ((nn) ? '&nbsp;' : '');
       html += '<select name="shortc" class="ibcSelect" onchange="shortcut(this.options[this.selectedIndex].value);">';
       html += makeOption('','اختصارات',0);
	html += makeOption('(بسم) \r\n','بسم الله الرحمن الرحيم',0);
       html += makeOption('السلام عليكم ورحمة الله وبركاته،، \r\n','السلام عليكم ورحمة الله وبركاته',0);
       html += makeOption('وعليكم السلام ورحمة الله وبركاته،، \r\n','وعليكم السلام ورحمة الله وبركاته',0);
       html += makeOption('(ص)،، \r\n','صلى الله عليه وسلم',0);
html += makeOption('(ع)\r\n','عليه السلام',0);
html += makeOption('(رض)\r\n','رضى الله عنه',0);
       html += makeOption('[ALIGN=center]بسم الله الرحمن الرحيم[/ALIGN]\r\n\r\nالسلام عليكم ورحمة الله وبركاته،،، \r\n\r\n\r\nوالسلام عليكم ورحمة الله وبركاته','نموذج رسالة',0);
       html += '</select> ';
       html += '</td></tr><tr><td>' + ((nn) ? '&nbsp;' : '');
    if (graphical) {
       html += makeLink("ubbBasic('b');",'سميك') + makeImage('/bold.gif','22','22','سميك') + '</a>';
       html += makeLink("ubbBasic('i');",'مائل') + makeImage('/italics.gif','22','22','مائل') + '</a>';
       html += makeLink("ubbBasic('u');",'تحته خط') + makeImage('/underline.gif','22','22','تحته خط') + '</a>';
       html += makeLink("ubbBasic('s');",'عليه خط') + makeImage('/strikethrough.gif','22','22','عليه خط') + '</a>';
       html += makeLink("ubbBasic('move');",'نص متحرك') + makeImage('/marque.gif','22','22','نص متحرك') + '</a> ';
    } else {
       html += makeButton("ubbBasic('b');",' B ','سميك','b') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbBasic('i');",' I ','مائل','i') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbBasic('u');",' U ','تحته خط','u') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbBasic('s');",' S ','عليه خط','s');
       html += makeButton("ubbBasic('move');",'Marquee','نص متحرك','j') + makeImage('/pixel.gif',1,1,'');

    }

    if (graphical) {
       html += ' ' + makeLink("ubbsound('');",'إدارج صوت') + makeImage('/sound.gif','22','22','إدارج صوت') + '</a>';
       html += makeLink("ubbvideo('');",'إدارج فيديو') + makeImage('/rplayer.gif','22','22','إدارج فيديو') + '</a>';
    } else {
       html += ' ' + makeLink("ubbsound('');",'إدارج صوت') + makeImage('/sound.gif','22','22','إدارج صوت') + '</a>';
       html += makeLink("ubbvideo('');",'إدارج فيديو') + makeImage('/rplayer.gif','22','22','إدارج فيديو') + '</a>';

    }

    if (graphical) {
       html += makeLink("ubbBasic('q');",'قرآن') + makeImage('/quran.gif','22','22','قرآن') + '</a>';
       html += makeLink("ubbList('');",'قائمة') + makeImage('/list.gif','22','22','قائمة') + '</a>';
       html += makeLink("ubbListItem();",'عنصر قائمة') + makeImage('/listitem.gif','22','22','عنصر قائمة') + '</a>';
       html += makeLink("ubbHref();",'إدراج وصلة') + makeImage('/url.gif','22','22','إدراج وصلة') + '</a>';
       html += makeLink("ubbEmail();",'إدراج بريد') + makeImage('/email.gif','22','22','إدراج بريد') + '</a>';
       html += ((images) ? makeLink("ubbImage();",'إدراج صورة') + makeImage('/image.gif','22','22','إدراج صورة') + '</a>' : '');
       html += makeLink("showpoetry();",'تنسيق قصيدة') + makeImage('/poet.gif','23','22','تنسيق قصيدة') + '</a>';
       html +=  makeLink("ubbkeyb();",'الكيبورد العربي') + makeImage('/keyb.gif','23','22','الكيبورد العربي') + '</a>';       

    } else {
       html += makeButton("ubbBasic('q');",' Q ','قرآن','b') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbList('');",'UL','قائمة','l') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbListItem();",'LI','عنصر قائمة','k');
       html += makeButton("ubbHref();",'URL','إدراج وصلة','h') + makeImage('/pixel.gif',1,1,'');
       html += makeButton("ubbEmail();",' @ ','إدراج بريد','e') + makeImage('/pixel.gif',1,1,'');
       html += ((images) ? makeButton("ubbImage();",'IMG','إدراج صورة','p') + makeImage('/pixel.gif',1,1,'') : '');
       html += ((flash) ? makeButton("ubbFlash();",'SWF','إدراج فلاش','f') + makeImage('/pixel.gif',1,1,'') : '');
       html += makeButton("showpoetry();",' ? ','تنسيق قصيدة','');
       html += makeButton("ubbkeyb();",' ? ','الكيبورد العربي','');
       }
          html += '</td></tr></table>';
    document.write(html);
  }


/////////////////////////////
// Code inserter
//
  function ubbCode(code) {
    if (form["Post"].createTextRange && form["Post"].caretPos) {
      var caretPos = form["Post"].caretPos;
      caretPos.text = code;
    } else { form["Post"].value += code; }
    form["Post"].focus();
  }


/////////////////////////////
// HTML shortcuts
//
  function makeButton(onclick,value,title,accesskey) {
    var html = '<input type="button" onclick="' + onclick;
       html += 'return false;" title="' + title;
       html += '" accesskey="' + accesskey + '" class="ubbcButton';
       html += '" value="' + value + '">';
    return html;
  }

  function makeLink(onclick,text) {
    var html = '<a href="#" onclick="' + onclick;
       html += 'return false;" onmouseover="return winStat(\'' + text;
       html += '\');" onmouseout="return winStat(\'\');">';
    return html;
  }

  function makeImage(source,width,height,alt) {
    var html = '<img src="' + ubbc_dir + '/' + source + '" width="' + width;
       html += '" height="' + height + '" border="0" alt="' + alt;
       html += '" align="absmiddle">';
    return html;
  }

  function makeOption(value,text,style) {
    var html = '<option value="' + value;
       html += ((style && ie) ? '" style="color:' + value : '');
       html += '">' + text + '</option>';
    return html;
  }


/////////////////////////////
// Misc utils
//
  function storeCaret(el) { 
    if (el.createTextRange) {
      el.caretPos = document.selection.createRange().duplicate();
    }
  }

  function getText() {
    if (ie) {
      return ((form["Post"].createTextRange && form["Post"].caretPos) ? form["Post"].caretPos.text : '');
    } else { return ''; }
  }

  function isUrl(text) {
    return ((text.indexOf('.') > 7) &&
            ((text.substring(0,7) == 'http://') ||
            (text.substring(0,6) == 'ftp://')));
  }

  function isEmail(str) {
    if (!reSupport) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); }
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
  }

  function winStat(txt) {
    window.status = txt;
    return true;
  }

  function returnFocus() {
    setTimeout('form["Post"].focus()',10);
  }

  function resetList(list) {
    setTimeout('form["'+list+'"].options[0].selected = true',10);
  }

function showpoetry()
{
   var SelText ;
   SelText = getText();
   window.open ("./html/hackpoet.htm",SelText,"help=no, center=yes, status=no, Height=430, Width=480,resizable=yes,scrollbars=yes");    

}

    function ubbkeyb() {
    var url = ubbc_dir + '/keyb/index.html';
    var options = 'height=350,width=500,scrollbars=no';
    window.open(url,'ubbc_keyb',options);
  }


  function ubbHelp() {
    var url = ubbc_dir + '/help/index.htm';
    var options = 'height=350,width=300,scrollbars=yes';
    window.open(url,'ubbc_help',options);
  }


  function removeElement(array,value) {
    array = array.split(',');
    for (i = 0; i < array.length; i++) {
      if (array[i] == value) { var pos = i; break; }
    }
    for (i = pos; i < (array.length-1); i++) {
      array[i] = array[i + 1];
    }
    array.length = array.length - 1;
    return array.join(',');
  }


/////////////////////////////
// Indivdual code types
//
  var openTags = new Array('');
  var closedTags = new Array('dummy','b','i','u','s','code','quote','me','list');
  function ubbBasic(code) {
    var text = getText();
    if (text) {
      code = '[' + code + ']' + text + '[/' + code + ']';
      ubbCode(code);
    } else {
      if (openTags.join(',').indexOf(','+code) != -1) {
        var tag = '[/' + code + ']';
        openTags = removeElement(openTags.join(','),code).split(',');
        closedTags[closedTags.length] = code;
      } else {
        var tag = '[' + code + ']';
        closedTags = removeElement(closedTags.join(','),code).split(',');
        openTags[openTags.length] = code;
      } ubbCode(tag);
    }
  }

  function ubbFont(list) {
    var attrib = list.name.substring(1,list.name.length);
    var value = list.options[list.selectedIndex].value;
    if (value && attrib) {
      var code = '[' + attrib + '=' + value + ']' + getText() + '[/' + attrib + ']';
      ubbCode(code);
    }
    resetList(list.name);
  }

 function ubbAlign(align) {
    if (!align) { return; }
    code = '[align=' + align + ']' + getText() + '[/align]';
    ubbCode(code);
    resetList("talign");
  }

function shortcut(sc) {
    if (!sc) { return; }
    code = sc;
    ubbCode(code);
    resetList("shortc");
  }

  function ubbList(size) {
    var text = getText();
    if (!size && !text) { ubbBasic('list'); }
    else if (!size && text && reSupport) {
      var regExp = /\n/g;
      text = text.replace(regExp,'\n[li]');
      var code = '[list]\n[li]' + text + '\n[/list]\n';
      ubbCode(code);
    } else {
      if (text) { text += '\n'; }
      var code = text + '[list]\n';
      for (i = 0; i < size; i++) { code += '[li]\n'; }
      code += '[/list]\n';
      ubbCode(code);
      resetList("quicklist");
    }
  }

  function ubbListItem() {
    var code = '[li]' + getText();
    ubbCode(code);
  }

  function ubbHref() {
    var url = 'http://'; var desc = '';
    var text = getText();
    if (text) {
      if (isUrl(text)) { url = text; }
      else { desc = text; }
    }
    url = prompt('أدخل ارتباط الموقع:',url) || '';
    desc = prompt('أدخل الوصف:',desc) || url;
    if (!isUrl(url)) { returnFocus(); return; }
    var code = '[url=' + url + ']' + desc + '[/url]';
    ubbCode(code);
  }

  function ubbEmail() {
    var email = ''; var desc = '';
    var text = getText();
    if (text) {
      if (isEmail(text)) { email = text; }
      else { desc = text; }
    }
    email = prompt('أدخل عنوان البريد الإلكتروني:',email) || '';
    desc = prompt('أدخل الوصف:',desc) || email;
    if (!isEmail(email)) { returnFocus(); return; }
    var code = '[email=' + email + ']' + desc + '[/email]';
    ubbCode(code);
  }

  function ubbImage() {
    var url = 'http://'; 
    var text = getText();
    url = prompt("\nأدخل ارتباط الصورة:","http://") || "";
    if (!isUrl(url)) { returnFocus(); return; }
    var code = "[IMG]" + url + "[/IMG]";
    ubbCode(code);
  }



  function ubbsound() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nأدخل ارتباط الصوت:","http://") || "";
    if (!isUrl(url)) { returnFocus(); return; }
    var code = '[ram]' + url + '[/ram]';
    ubbCode(code);
  }

  function ubbvideo() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nأدخل ارتباط الفيديو:","http://") || "";
    if (!isUrl(url)) { returnFocus(); return; }
    var code = '[video]' + url + '[/video]';
    ubbCode(code);
  }

 

/////////////////////////////
// Access Keys
//
  var keys = new Array('b','i','u','s','g','q','m','h','e','l','k');
  function checkKey() {
    if (event.ctrlKey) {
      for (i = 0; i < keys.length; i++) {
        if (String.fromCharCode(event.keyCode) == keys[i].toUpperCase()) {
          var found = keys[i]; break;
        }
      }
      if (found) {
        switch(found) {
          case 'h':ubbHref();break;
          case 'e':ubbEmail();break;
          case 'p':ubbImage();break;
          case 'f':ubbFlash();break;
          case 'l':ubbList();break;
          case 'k':ubbListItem();break;
          case 'g':ubbBasic('code');break;
          case 'q':ubbBasic('quote');break;
          case 'm':ubbBasic('me');break;
          default:ubbBasic(found);
        }
        this.cancelBubble = true;
        this.returnValue = false;
        return false;
      }
    }
  }


/////////////////////////////
// Replacing default iB JS
//
  function emoticon(theSmilie) {
    var text = getText() + ' ';
    var code = text + theSmilie + ' ';
    ubbCode(code);
  }


/////////////////////////////
// Initilization
//
  var form;
  writeStyle();
  var reSupport = 0;
  function ubbcInit(images,flash,graphical) {
    form = document.forms["REPLIER"];

    if (images) { keys[keys.length] = 'p'; }
    if (flash) { keys[keys.length] = 'f'; }
    if (graphical) { document.onkeydown = checkKey; }

    if (window.RegExp) {
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      if (tempReg.test(tempStr)) { reSupport = 1; }
    }
  }

