function convert_char(rot_algo, range_start, range_end, input_char)
{
  // rot_algo: 5 for ROT5, 13 for ROT13, etc...
  // range_start, range_end: position in ASCII code table
  // input_char: character to be converted
  //
  // 1. Get position of character in a list that contains only the characters
  //    to be converted. Counting starts with 0:
  //    => position_in = input_char.charCodeAt(0) - range_start;
  // 2. Get new position after applying ROT algorithm:
  //    => position_out = (position_in + rot_algo) % (rot_algo * 2);
  // 3. Return converted character:
  //    => return String.fromCharCode(range_start + position_out);

  return String.fromCharCode(range_start + (((input_char.charCodeAt(0) - range_start) + rot_algo) % (rot_algo * 2)));
}


function rot13(input)
{
  var output = '';

  for(var i = 0; i < input.length; i++ )
  {
    /* Letters A-Z (code 65-90) */
    if(input.charCodeAt(i) >= 65 && input.charCodeAt(i) <= 90)
    {
      output = output + convert_char(13, 65, 90, input.charAt(i));
    }

    /* Letters a-z (code 97-122) */
    else if(input.charCodeAt(i) >= 97 && input.charCodeAt(i) <= 122)
    {
      output = output + convert_char(13, 97, 122, input.charAt(i));
    }
    else
    {
      output = output + input.charAt(i);
    }
  }

  return output;
}
