function calc_statistic()
{

   var alltext = document.getElementById('textarea_id').value;

   // Remove all symbols \r
   while (alltext != alltext.replace("\r\n", "\n")) alltext = alltext.replace("\r\n", "\n");

   // Number of lines
   lines = alltext.split("\n");
   document.getElementById('txtLines').value = lines.length;

   // Number of characters
   document.getElementById('txtChars').value = alltext.length;

   // Number of words
   words = alltext.split(/[\s.,\W]+/);
   document.getElementById('txtWords').value = words.length;

   // Longest line length
   max_line_length = 0;
   for (var i = 0; i < lines.length; i ++ )
   {
      line_length = lines[i].length
      if (line_length > max_line_length) max_line_length = line_length
   }
   document.getElementById('txtLongest').value = max_line_length;
}

function format_one_pass(text, user_max_line_length)
{
   var newtext = ""
   var part_of_line = ""
   var number_lines

   var temp_symbol

   lines = text.split("\n")
   number_lines = lines.length

   // Cycle for each line
   for (var i = 0; i < number_lines; i ++ )
   {
      if (lines[i].length > user_max_line_length)
      {
         // Get piece of line from 0 to user_max_line_length + 1
         // We need to operate with following situation : when space symbol has position
         // user_max_line_length + 1, so we don't need so add /n immediately after position user_max_line_length,
         // because fist symbol in next line will be a space
         part_of_line = lines[i].substr(0, user_max_line_length)

         // Anylaze this part of line
         if (part_of_line.indexOf(" ") == - 1)
         {
            // If this line has no space symbols - so add hard \n
            // We need to check that symbol after \n will not be an space
            temp_symbol = lines[i].charAt(user_max_line_length)
            if (temp_symbol == " ") lines[i] = lines[i].substring(0, user_max_line_length) + "\n" + lines[i].substring(user_max_line_length + 1)
            else lines[i] = lines[i].substring(0, user_max_line_length) + "\n" + lines[i].substring(user_max_line_length)
         }
         else
         {
            // This part of line has space symbol
            // Search last space symbol
            last_space_position = part_of_line.lastIndexOf(" ")
            lines[i] = lines[i].substring(0, last_space_position) + "\n" + lines[i].substring(last_space_position + 1)
         }
      }

      // Do not add new line symbol to last line
      if (i != number_lines - 1) newtext = newtext + lines[i] + "\n"
      else newtext = newtext + lines[i]
   }

   return newtext
}

function format()
{
   var alltext = document.getElementById('textarea_id').value
   var user_max_line_length = document.getElementById('user_max_line_length').value
   var formated_text

   var previous_symbol

   // Remove all symbols \r
   while (alltext != alltext.replace("\r\n", "\n")) alltext = alltext.replace("\r\n", "\n");

   // We need to remove all \n symbols, but remain groups of \n
   var last_new_line = alltext.lastIndexOf("\n")
   var current_new_line = alltext.indexOf("\n")

   while (current_new_line < last_new_line)
   {
      current_new_line = alltext.indexOf("\n")
      previous_symbol = alltext.charAt(current_new_line - 1)
      next_symbol = alltext.charAt(current_new_line + 1)

      if ((next_symbol != "\n") && (previous_symbol != "\r")) alltext = alltext.substring(0, current_new_line) + " " +  alltext.substring(current_new_line + 1)
      else alltext = alltext.substring(0, current_new_line) + "\r" +  alltext.substring(current_new_line + 1)
   }

   // Replace all \r for \n
   while (alltext != alltext.replace("\r", "\n")) alltext = alltext.replace("\r", "\n");

   // For this moment we can have situation, where string "\n " was transformed to "  " (2 spaces)
   // so replace "  " for " "
   while (alltext != alltext.replace("  ", " ")) alltext = alltext.replace("  ", " ");

   formated_text = alltext
   while (formated_text != format_one_pass(formated_text, user_max_line_length)) formated_text = format_one_pass(formated_text, user_max_line_length);

   document.getElementById('textarea_id').value = formated_text

   // Recalc statistic
   calc_statistic();
}

function show_editor()
{
   var output = ""
   var yourserver = 'http://www.jeboost.com/business/email/'

   output = "<div align=\"center\">"
   output = output + "<a href=\"" + yourserver + "logo.php\" target=\"_blank\">"
   output = output + "<!--LOGO_URL--><img border=\"0\" src=\"http://www.jeboost.com/images/headerachat.jpg\"></a><!--END_LOGO_URL-->"
   output = output + "</div><br>"

	output = output + "<table align=\"center\" border=\"2\"> <tr><td align=\"center\">	<textarea id=\"textarea_id\" cols=\"80\" rows=\"15\" onkeyup=\"calc_statistic()\"></textarea><br><br> Ecrivez ou collez votre email \340 formater ci-dessus.<br><br> <a href=\"javascript:SelectAll()\"> S\351lectionner tout</a> &nbsp;&nbsp;&nbsp; <a href=\"javascript:CopyAll()\">Copier dans le presse papier</a> &nbsp;&nbsp;&nbsp; <a href=\"javascript:ClearAll()\">Supprimer</a> <br><br> Largeur des lignes: <input type=\"text\" id=\"user_max_line_length\" size=\"3\" value=\"65\">	<input type=\"submit\" id=\"format\" value=\"Format\" onclick=\"format()\"><br><br><TABLE id=\"statistic_table\" cellSpacing=\"1\" cellPadding=\"1\" align=\"center\" border=\"1\"><TR>	<TD align=\"center\"><STRONG>Statistiques</STRONG></TD>	<TD align=\"center\" colSpan=\"2\">&nbsp;</TD></TR><TR>	<TD align=\"center\">Nombre de caract\350res (Espaces inclus):</TD>	<TD align=\"center\"><INPUT id=\"txtChars\" style=\"WIDTH: 53px; HEIGHT: 22px; BACKGROUND-COLOR: transparent; TEXT-ALIGN: center\" type=\"text\" size=\"3\" name=\"txtChars\"></TD></TR><TR>	<TD align=\"center\">Nombre de lignes (Espaces inclus): (Blank lines count):</TD>	<TD align=\"center\"><INPUT id=\"txtLines\" style=\"WIDTH: 53px; HEIGHT: 22px; BACKGROUND-COLOR: transparent; TEXT-ALIGN: center\" type=\"text\" size=\"3\" name=\"txtLines\"></TD></TR><TR>	<TD align=\"center\">Nombre de mots:</TD>	<TD align=\"center\"><INPUT id=\"txtWords\" style=\"WIDTH: 53px; HEIGHT: 22px; BACKGROUND-COLOR: transparent; TEXT-ALIGN: center\" type=\"text\" size=\"3\" name=\"txtWords\"></TD></TR><TR>	<TD align=\"center\">Longueur de ligne:</TD>	<TD align=\"center\"><INPUT id=\"txtLongest\" style=\"WIDTH: 53px; HEIGHT: 22px; BACKGROUND-COLOR: transparent; TEXT-ALIGN: center\" type=\"text\" size=\"3\" name=\"txtLongest\"></TD></TR></TABLE> <br><br> </td></tr></table>"  
	
	output = output + "<br><div align=\"center\">"
	output = output + ""
	output = output + ""
	output = output + "</div>"
	document.write(output)
}

function SelectAll()
{
   // Select all text in the textarea
   var alltext = document.getElementById('textarea_id')
   alltext.focus()
   alltext.select()
}

function CopyAll()
{
   var alltext = document.getElementById('textarea_id')
   therange = alltext.createTextRange()
   therange.execCommand("Copy")
   window.status = "Text copied to clipboard"
   setTimeout("window.status=''", 2400);
}

function ClearAll()
{
   var alltext = document.getElementById('textarea_id')
   alltext.value = ""
   calc_statistic()
}

