/**********************************************************************************
 * Classes definition section
 **********************************************************************************/

 // Class for symbol replacement
function ExpRep(regExp, strRep)
{
  this.m_regExp = regExp;   // Regular expression to be replaced
  this.m_strRep = strRep;   // Replacement string
}

// Class to define an Icon in a list above comment edit field
function CommentIcon(urlIcon, strBeg, strEnd)
{
  this.m_urlIcon = urlIcon; // url of the Icon to display
  this.m_strBeg  = strBeg;  // String that is inserted before the selection (or at the cursor position)
  this.m_strEnd  = strEnd;  // String that is inserted after the selection
}

// Class for tags couple check
function TagCouple(tagOpenName, tagCloseName, tagOpenReg, tagCloseReg)
{
  this.m_tagOpenName = tagOpenName;     // Basic name of the opening tag
  this.m_tagCloseName = tagCloseName;   // Basic name of the closing tag
  this.m_tagOpenReg = tagOpenReg;       // Regular expression defining the opening tag
  this.m_tagCloseReg = tagCloseReg;     // Regular expression defining the closing tag
}

/**********************************************************************************
 * Data definition section
 **********************************************************************************/

// Definition of symbols (defined by regular expression) to be replaced by HTML tags
var i = 0;
expRepArray = new Array();
expRepArray[i++]  = new ExpRep("(:\\))|(:-\\))|(:o\\))",
                             "<img alt=\":)\" src=\"http://img71.imageshack.us/img71/9065/smile10.gif\" />");

expRepArray[i++]  = new ExpRep("(:D)|(;D)|(:-D)|(;-D)|(:oD)|(;oD)",
                             "<img alt=\";D\" src=\"http://img71.imageshack.us/img71/4954/lol7.gif\" />");

expRepArray[i++]  = new ExpRep(":love:",
                             "<img alt=\":love:\" src=\"http://img71.imageshack.us/img71/5016/love3.gif\" />");

expRepArray[i++]  = new ExpRep(":shock:",
                             "<img alt=\":shock:\" src=\"http://img71.imageshack.us/img71/355/surpris.gif\" />");

expRepArray[i++]  = new ExpRep(":clin:",
                             "<img alt=\":clin:\" src=\"http://img71.imageshack.us/img71/8492/clin.gif\" />");

expRepArray[i++]  = new ExpRep(":lick:",
                             "<img alt=\":lick:\" src=\"http://img71.imageshack.us/img71/456/langue.gif\" />");

expRepArray[i++]  = new ExpRep(":cry:",
                             "<img alt=\":cry:\" src=\"http://img71.imageshack.us/img71/7752/cry5.gif\" />");

expRepArray[i++]  = new ExpRep(":sad:",
                             "<img alt=\":sad:\" src=\"http://img71.imageshack.us/img71/496/sad8.gif\" />");

expRepArray[i++]  = new ExpRep(":eek:",
                             "<img alt=\":eek:\" src=\"http://img71.imageshack.us/img71/8930/eek5.gif\" />");

expRepArray[i++]  = new ExpRep(":question:",
                             "<img alt=\":question:\" src=\"http://img71.imageshack.us/img71/1160/icon_question.gif\" />");

expRepArray[i++]  = new ExpRep(":oops:",
                             "<img alt=\":oops:\" src=\"http://img71.imageshack.us/img71/7170/oops2.gif\" />");

expRepArray[i++]  = new ExpRep(":shy:",
                             "<img alt=\":shy:\" src=\"http://img71.imageshack.us/img71/6018/rougir.gif\" />");

expRepArray[i++]  = new ExpRep(":confus:",
                             "<img alt=\":confus:\" src=\"http://img71.imageshack.us/img71/8338/confus.gif\" />");

expRepArray[i++]  = new ExpRep(":euh:",
                             "<img alt=\":euh:\" src=\"http://img71.imageshack.us/img71/1336/euh.gif\" />");

expRepArray[i++]  = new ExpRep(":neutre:",
                             "<img alt=\":neutre:\" src=\"http://img71.imageshack.us/img71/6664/neutre.gif\" />");

expRepArray[i++]  = new ExpRep(":berk:",
                             "<img alt=\":berk:\" src=\"http://img71.imageshack.us/img71/3808/mrgreen.gif\" />");

expRepArray[i++]  = new ExpRep(":angry:",
                             "<img alt=\":angry:\" src=\"http://img71.imageshack.us/img71/6505/angry6.gif\" />");

expRepArray[i++]  = new ExpRep(":exclam:",
                             "<img alt=\":exclam:\" src=\"http://img71.imageshack.us/img71/6027/exclam.gif\" />");

expRepArray[i++]  = new ExpRep("\\[b\\]",
                             "<strong>");
expRepArray[i++]  = new ExpRep("\\[/b\\]",
                             "</strong>");
expRepArray[i++]  = new ExpRep("\\[i\\]",
                             "<em>");
expRepArray[i++]  = new ExpRep("\\[/i\\]",
                             "</em>");
expRepArray[i++]  = new ExpRep("\\[quote(\\s+([^\\];]+)(;\\s*([0-9:]+)\\s*)?(;\\s*([^\\]]+))?)?\\s*\\]",
                             "<div class=\"commentQuoteHeader\"><a href=\"#c$4\">Citation : $2 $6</a></div><div class=\"commentQuoteBody\">");
expRepArray[i++]  = new ExpRep("\\[/quote\\]",
                             "</div>");


// Definition of icons : the image and the symbol text that is inserted around the selection
i = 0;
iconsArray = new Array();
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/9065/smile10.gif",
                                   ":)", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/8492/clin.gif",
                                   ":clin:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/4954/lol7.gif",
                                   ":D", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/456/langue.gif",
                                   ":lick:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/5016/love3.gif",
                                   ":love:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/7752/cry5.gif",
                                   ":cry:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/496/sad8.gif",
                                   ":sad:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/8930/eek5.gif",
                                   ":eek:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/355/surpris.gif",
                                   ":shock:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/1160/icon_question.gif",
                                   ":question:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/7170/oops2.gif",
                                   ":oops:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/6018/rougir.gif",
                                   ":shy:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/8338/confus.gif",
                                   ":confus:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/1336/euh.gif",
                                   ":euh:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/6664/neutre.gif",
                                   ":neutre:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/3808/mrgreen.gif",
                                   ":berk:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/6505/angry6.gif",
                                   ":angry:", "");
iconsArray[i++]  = new CommentIcon("http://img71.imageshack.us/img71/6027/exclam.gif",
                                   ":exclam:", "");



// Definition of tag-couples to be checked (to avoid HTML tag corruption)
i = 0;
tagCoupleArray = new Array();
tagCoupleArray[i++] = new TagCouple("[b]", "[/b]",
                                    "\\[b\\]", "\\[/b\\]");
tagCoupleArray[i++] = new TagCouple("[i]", "[/i]",
                                    "\\[i\\]", "\\[/i\\]");
tagCoupleArray[i++] = new TagCouple("[quote]", "[/quote]",
                                    "\\[quote[^\\]]*\\]", "\\[/quote\\]");

/**********************************************************************************
 * Function definition section
 * REGULAR FUNCTIONS
 **********************************************************************************/


/**********************************************************************************
 * FUNCTION : formatComment()
 **********************************************************************************
 * Function called to display a comment body by replacing symbols defined in
 * expRepArray by their corresponding HTML tags.
 * INPUT :  p_str   String containing the comment body
 **********************************************************************************/
function formatComment(p_str)
{
  var v_str = p_str;
  for(i in expRepArray)
  {
    var v_exp = new RegExp(expRepArray[i].m_regExp, "gi");
    v_str = v_str.replace(v_exp, expRepArray[i].m_strRep);
    delete v_exp;
  }
  document.write(v_str);
}

/**********************************************************************************
 * FUNCTION : displayCommentIcons()
 **********************************************************************************
 * Display the list of icons defined in iconsArray.
 **********************************************************************************/
function displayCommentIcons()
{
  for(i in iconsArray)
  {
    document.write("<img class=\"comIconImg\" alt=\"" + iconsArray[i].m_strBeg + iconsArray[i].m_strEnd +
                   "\" src=\"" + iconsArray[i].m_urlIcon +
                   "\" onclick=\"insertIconTag(" + i + ")\">");
  }
}

/**********************************************************************************
 * FUNCTION : insertTextBegEnd()
 **********************************************************************************
 * Insert in the textarea commentBody of the comment form of the blog a couple
 * of strings around the selection.
 **********************************************************************************/
function insertTextBegEnd(p_strBeg, p_strEnd)
{
  var input = document.forms['frmComment'].elements['commentBody'];
  input.focus();
  // For IE
  if(typeof document.selection != 'undefined') {
    // Format code insertion
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = p_strBeg + insText + p_strEnd;
    // Adjust cursor position
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -p_strEnd.length);
    } else {
      range.moveStart('character', p_strBeg.length + insText.length + p_strEnd.length);
    }
    range.select();
  }
  // For Gecko based browsers
  else if(typeof input.selectionStart != 'undefined')
  {
    // Format code insertion
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + p_strBeg + insText + p_strEnd + input.value.substr(end);
    // Adjust cursor position
    var pos;
    if (insText.length == 0) {
      pos = start + p_strBeg.length;
    } else {
      pos = start + p_strBeg.length + insText.length + p_strEnd.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  // For the other browsers
  else
  {
    var pos = input.value.length;
    // Format code insertion at the end
    input.value = input.value + p_strBeg + p_strEnd;
  }
}

/**********************************************************************************
 * FUNCTION : insertIconTag()
 **********************************************************************************
 * Insert a comment tag in the comment edit field.
 * INPUT :  p_i    Index of icon tag to insert in the comment field.
 **********************************************************************************/
function insertIconTag(p_i)
{
  insertTextBegEnd(iconsArray[p_i].m_strBeg, iconsArray[p_i].m_strEnd);
}

/**********************************************************************************
 * COUPLE TAG FUNCTIONS
 **********************************************************************************/

/**********************************************************************************
 * FUNCTION : checkCommentAndSubmit()
 **********************************************************************************
 * Check comment tag-couples and display an error if a tag is single.
 * Also call handleSubmit() of Canalblog.
 * INPUT :  p_form  Handler of the form (this) used by handleSubmit()
 **********************************************************************************/
function checkCommentAndSubmit(p_form)
{
  var v_str = document.forms['frmComment'].elements['commentBody'].value;

  if(v_str == "")
  {
    document.getElementById("comErrorMessage").innerHTML = "Commentaire vide";
    return false;
  }

  for(i in tagCoupleArray)
  {
    
    var v_exp = new RegExp(tagCoupleArray[i].m_tagOpenReg, "gi");
    var v_tagsArray = v_str.match(v_exp);
    if(v_tagsArray == null)
      nbOpen = 0;
    else
      nbOpen = v_tagsArray.length;
    delete v_exp;

    var v_exp = new RegExp(tagCoupleArray[i].m_tagCloseReg, "gi");
    v_tagsArray = v_str.match(v_exp);
    if(v_tagsArray == null)
      nbClose = 0;
    else
      nbClose = v_tagsArray.length;
    delete v_exp;

    if(nbOpen < nbClose)
    {
      document.getElementById("comErrorMessage").innerHTML = "Il manque " + (nbClose - nbOpen) + " " + tagCoupleArray[i].m_tagOpenName;
      return false;
    }
    else if(nbOpen > nbClose)
    {
      document.getElementById("comErrorMessage").innerHTML = "Il manque " + (nbOpen - nbClose) + " " + tagCoupleArray[i].m_tagCloseName;
      return false;
    }
  }

  handleSubmit(p_form);

  return true;
}

/**********************************************************************************
 * QUOTES FUNCTIONS
 **********************************************************************************/

/**********************************************************************************
 * FUNCTION : insertQuote()
 **********************************************************************************
 * Insert a comment quote in the comment edit field.
 * INPUT :  p_id   Id of the comment that has to be quoted.
 **********************************************************************************/
function insertQuote(p_id)
{
  v_comment = document.getElementById("commentStr" + p_id).value;
  v_quote = document.getElementById("commentQuoteStr" + p_id).value;

  v_quote = v_quote + v_comment;

  var v_exp = new RegExp("<br\\s*/*>", "gi");
  v_quote = v_quote.replace(v_exp, "\n");
  delete v_exp;

  var v_exp = new RegExp("<[^>]+>", "gi");
  v_quote = v_quote.replace(v_exp, "");
  delete v_exp;

  insertTextBegEnd(v_quote + "[/quote]", "");
}
