////////////////////////////////////////////////////////
// UNIVERSAL BULLETIN BOARD CODE INTERFACE v2.1
// Original code By Mark Plachetta (astroboy@zip.com.au)
// Modified 09/09/05
// 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 ubbColor (form_name, form_object, color)
{
  color = '[color=' + color + ']' + getText(form_name, form_object) + '[/color]';
  ubbCode(form_name, form_object, color);
}


//****************
//Code d'insertion
//****************
function ubbCode(form_name, form_object, code)
{
  form = document.forms[form_name];
  
  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 (form_name, form_object, code)
{
  var text = getText(form_name, form_object);
  if (text)
  {
    code = '[' + code + ']' + text + '[/' + code + ']';
    ubbCode(form_name, form_object, 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(form_name, form_object, tag);
  }
}


//Fonction type font
function ubbFont(form_name, form_object, 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(form_name, form_object) + '[/' + attrib + ']';
    ubbCode(form_name, form_object, code);
  }
  resetList(list.name);
}


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

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


//Liste
function ubbList(form_name, form_object, size)
{
  var text = getText(form_name, form_object);
  if (!size && !text)
    ubbBasic(form_name, form_object, 'list');
  else if (!size && text && reSupport)
  {
    var regExp = /\n/g;
    text = text.replace(regExp,'\n[*]');
    var code = '[list]\n[*]' + text + '\n[/list]\n';
    ubbCode(form_name, form_object, code);
  }
  else
  {
    if (text)
      text += '\n';
    var code = text + '[list]\n';
    for (i = 0; i < size; i++)
      code += '[*]\n';
    code += '[/list]\n';
    ubbCode (form_name, form_object, code);
    resetList ("quicklist");
  }
}


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

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

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

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

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

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

  email = prompt('Enter e-mail address:',email) || '';
  if (!isEmail(email)) {
    returnFocus(); return;}

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


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


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

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

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


//Fonction Glow
function ubbGlow(form_name, form_object)
{
  var color = ''; var write = '';
  var text = getText(form_name, form_object);
  if (text && isUrl(text)) { url = text; text = ''; }
  color = prompt('enter color for Glow:',color) || '';
  var code = ((text) ? text + ' ' : '') + '[glow=' + color + ']' + "Your text" + '[/glow]';
  ubbCode(form_name, form_object, code);
}


//Fonction Shadow
function ubbShadow(form_name, form_object)
{
  var color = ''; var write = '';
  var text = getText(form_name, form_object);
  if (text && isUrl(text)) { url = text; text = ''; }
  color = prompt("Enter shadow color:",color) || '';
  var code = ((text) ? text + ' ' : '') + '[shadow=' + color + ']' + "Your text" + '[/shadow]';
  ubbCode(form_name, form_object, code);
}


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


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

function getText(form_name, form_object)
{
  form = document.forms[form_name];
  
  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 ubbWindow(form_name, form_object, action)
{ 
  var url = 'index.php?smallpage=includes/bbcode_window' + '&show=' + action + '&form_name=' + form_name + '&form_object=' + form_object;
  var width = '640';
  var height = '620';
  
  switch(action) 
  {
    case 'smileys':width='640';height='620';break;
	case 'preview':width='560';height='240'; break;
    default:
  }
  
  var options = 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes';
  window.open(url,'bbcode_window',options);
}

function ubbSmile(form_name, form_object)
{
  ubbWindow(form_name,form_object,'smileys');
}

function ubbPictures(form_name, form_object)
{
  form = document.forms[form_name];
  if (!form.pictures_ref) return;
 
  ubbWindow(form_name,form_object,'pictures&ref='+form.pictures_ref.value);
}

function ubbPreview(form_name, form_object)
{
  var show, module, admin, page;
  
  form = document.forms[form_name];
  
  show = form.show.value;
  if (form.module)
  {
    module = form.module.value;
    form.module.value = '';
  }
  if (form.admin)
  {
    admin = form.admin.value;
	form.admin.value = '';
  }
  if (form.page) 
  {
    page = form.page.value;
	form.page.value = '';
  }
  
  /*form.action = 'index.php';*/
  form.target = 'bbcode_window';
  form.elements['form_name'].value = form_name;
  form.elements['form_object'].value = form_object;
  form.smallpage.value='includes/bbcode_window';
  form.show.value='preview';
  ubbWindow(form_name, form_object, 'preview');
  form.submit();
  
  /*form.action = action;*/
  form.smallpage.value='';
  form.show.value=show;
  form.target = '';
  
  if (module) form.module.value = module;
  if (admin) form.admin.value = admin;
  if (page) form.page.value = page;
}

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

/////////////////////////////
// Access Keys
//
var keys = new Array('b','i','u','s','l','k','q','h','e','m','g','o','j');
function checkKey(form_name, form_object) 
{
  if (ie && event.altKey) 
  {
	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 'b':ubbBasic(form_name, form_object, 'b');break;
		case 'i':ubbBasic(form_name, form_object, 'i');break;
		case 'u':ubbBasic(form_name, form_object, 'u');break;
		case 's':ubbBasic(form_name, form_object, 's');break;
		case 'l':ubbLine(form_name, form_object);break;
		case 'k':ubbBasic(form_name, form_object, 'code');break;
		case 'q':ubbBasic(form_name, form_object, 'cite');break;
        case 'h':ubbHref(form_name, form_object);break;              
        case 'e':ubbEmail(form_name, form_object);break;
        case 'm':ubbImage(form_name, form_object);break;
        case 'g':ubbSmile(form_name, form_object);break;
		case 'o':ubbPictures(form_name, form_object);break;
        case 'j':ubbHelp();break;
        default:ubbBasic(form_name, form_object, found);
      }
      this.cancelBubble = true;
      this.returnValue = false;
      return false;
    }
  }
}

function ubbcInit ()
{
  /*if (images) keys[keys.length] = 'p';
  if (flash) keys[keys.length] = 'f';*/
  /*document.onkeydown = checkKey;*/

  if (window.RegExp)
  {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr))
      reSupport = 1;
  }
}
