////////////////////////////////////////////////////////
// UNIVERSAL BULLETIN BOARD CODE INTERFACE v2.1
// Original code By Mark Plachetta (astroboy@zip.com.au)
// Modified dimanche 18 avril 2004 13:00:00
// Ti-Gen by Geoffrey Anneheim
////////////////////////////////////////////////////////

//////////////////////////
// Basic browser detection
//////////////////////////
var ie = (document.all) ? 1 : 0;
var nn = (document.layers) ? 1 : 0;
var n6 = (window.sidebar) ? 1 : 0;

function couleur (color)
{
  color = '[color=' + color + ']' + getText() + '[/color]';
  ubbCode(color);
}


//****************
//Code d'insertion
//****************
function ubbCode(code)
{
  if (form[form_object].createTextRange && form[form_object].caretPos)
  {
    var caretPos = form[form_object].caretPos;
    caretPos.text = code;
  }
  else
    form[form_object].value += code;

  form[form_object].focus();
}


//**************************
//Code fonction individuelle
//**************************
var openTags = new Array('');
var closedTags = new Array('dummy','b','i','u','s','code','cite','me','list');

//Fonctions basics
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);
  }
}


//Fonction type font
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);
}


//Fonction d'alignement
function ubbAlign(align)
{
  if (!align) return;

  code = '[align=' + align + ']' + getText() + '[/align]';
  ubbCode(code);
  resetList("talign");
}


//Liste
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[*]');
    var code = '[list]\n[*]' + text + '\n[/list]\n';
    ubbCode(code);
  }
  else
  {
    if (text)
      text += '\n';
    var code = text + '[list]\n';
    for (i = 0; i < size; i++)
      code += '[*]\n';
    code += '[/list]\n';
    ubbCode (code);
    resetList ("quicklist");
  }
}


//Fonction listitems
function ubbListItem()
{
  var code = '[*]' + getText();
  ubbCode(code);
}

//Fonction Line
function ubbLine()
{
  var code = '[line]' + getText();
  ubbCode(code);
}

//Fonction lien
function ubbHref()
{
  var url = 'http://'; var desc = '';
  var text = getText();
  if (text)
  {
    if (isUrl(text))
      url = text;
    else
      desc = text;
  }

  url = prompt("Entrez l'URL:",url) || '';
  desc = prompt('Entrez description:',desc) || url;
  if (!isUrl(url)) {
    returnFocus(); return;}

  var code = '[url=' + url + ']' + desc + '[/url]';
  ubbCode(code);
}

//Fonction @mail
function ubbEmail()
{
  var email = ''; var desc = '';
  var text = getText();
  if (text)
  {
    if (isEmail(text))
      email = text;
    else
      desc = text;
  }

  email = prompt('Entrez adresse email:',email) || '';
  if (!isEmail(email)) {
    returnFocus(); return;}

  var code = '[email=' + email + ']' + email + '[/email]';
  ubbCode(code);
}


//Fonction image
function ubbImage()
{
  var text = getText();
  var url = (text && isUrl) ? text : prompt("\nEntrez l'URL de l'image:","http://") || "";
  if (!url) { return; }
  var code = "[img]" + url + "[/img]";
  ubbCode(code);
}


//Fonction Flash
/*function ubbFlash() {
  var url = 'http://';
  var text = getText();
  if (text && isUrl(text))
    url = text; text = '';

  url = prompt("Entrez l'URL Flash:",url) || '';
  if (!isUrl(url)) { returnFocus(); return; }

  var code = ((text) ? text + ' ' : '') + '[flash]' + url + '[/flash]';
  ubbCode(code);
}*/


//Fonction Glow
function ubbGlow()
{
  var color = ''; var write = '';
  var text = getText();
  if (text && isUrl(text)) { url = text; text = ''; }
  color = prompt('Entrez la couleur pour Glow:',color) || '';
  var code = ((text) ? text + ' ' : '') + '[glow=' + color + ']' + "Votre texte" + '[/glow]';
  ubbCode(code);
}


//Fonction Shadow
function ubbShadow()
{
  var color = ''; var write = '';
  var text = getText();
  if (text && isUrl(text)) { url = text; text = ''; }
  color = prompt("Entrez la couleur de l'ombre:",color) || '';
  var code = ((text) ? text + ' ' : '') + '[shadow=' + color + ']' + "Votre texte" + '[/shadow]';
  ubbCode(code);
}


//Fonction Spoil
function ubbSpoil()
{
  //var code = '[spoiler]' + getText(); + '[/spoiler]';
  ubbBasic('spoiler');
}


//******
//Divers
//******
function storeCaret(el)
{ 
  if (el.createTextRange)
    el.caretPos = document.selection.createRange().duplicate();
}

function getText()
{
  if (ie)
  {
    return ((form[form_object].createTextRange && form[form_object].caretPos) ? form[form_object].caretPos.text : '');
  }
  else
    return '';
}

function ubbHelp()
{
  /*var url = ubbc_dir + '/help/index.htm';
  var options = 'height=350,width=300,scrollbars=yes';
  window.open(url,'ubbc_help',options);*/
}

function ubbSmile()
{
  var url = current_dir + 'others/smileys_lst.php' + '?form=' + form_area + '&form_object=' + form_object;

  //alert (url);
  var options = 'width=680,height=650,scrollbars=yes,resizable=no';
  window.open(url,'ubcc_smilies',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(',');
}

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 returnFocus()
{
  setTimeout('form[form_object].focus()',10);
}

function resetList(list)
{
  setTimeout('form["'+list+'"].options[0].selected = true',10);
}

//Initialisation
var current_dir;
var form;
var form_area;
var form_object;
//writeStyle();
var reSupport = 0;

function ubbcInit (init_dir, textarea, object)
{
  current_dir = init_dir;
  form = document.forms[textarea];
  form_area = textarea;
  form_object = object;

  /*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;
  }
}