/*  Copyright 2008  Christian Huellmann  (email : welcome@huellmann.com)

	Author: Christian Huellmann
	Version: 1.0
	Author URI: http://www.huellmann.com/

	Based on the "JavaScript-Umwandler für ROT5/ROT13/ROT18/ROT47" from Netzreport (netzreport.googlepages.com). Thanks!
	Website: http://netzreport.googlepages.com/online_umwandler_rot_5_13_18_47.html


    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/



/**
 * Encrypts a given string with a rot-algorithm.
 *
 * @param	string		input			The string to encrypt.
 * @param	string		rot_type		Your choosen rog-algorithm rot5, rot13, rot18 or rot47
 * @return	string						Encrypted string. On failure a empty string.
 **/

function rot_encrypt( input, rot_type )
{
	// Convert a single character
	var convert_char = function ( 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)) );
		}
	
	var output = "";
	
	// Convert according to chosen ROT algorithm:
	switch( rot_type )
	{
		case "rot5":
			for( var i = 0; i < input.length; i++ )
			{
				if( input.charCodeAt(i) >= 48 && input.charCodeAt(i) <= 57 )			// Numbers 0-9 (code 48-57):
					output += convert_char( 5, 48, 57, input.charAt( i ) );
				else
					output += input.charAt( i );
			}
			break;
			
		case "rot13":
			for (var i = 0; i < input.length; i++ )
			{
				if( input.charCodeAt(i) >= 65 && input.charCodeAt(i) <= 90 )			// Letters A-Z (code 65-90)
					output += convert_char( 13, 65, 90, input.charAt(i) );
				else if ( input.charCodeAt(i) >= 97 && input.charCodeAt(i) <= 122 )		// Letters a-z (code 97-122)
					output += convert_char( 13, 97, 122, input.charAt(i) );
				else
					output += input.charAt(i);
			}
			break;
			
		case "rot18":
			for (var i = 0; i < input.length; i++ )
			{
				if ( input.charCodeAt(i) >= 65 && input.charCodeAt(i) <= 90 )			// Letters A-Z (code 65-90)
					output += convert_char( 13, 65, 90, input.charAt(i) );
				else if ( input.charCodeAt(i) >= 97 && input.charCodeAt(i) <= 122 )		// Letters a-z (code 97-122)
					output += convert_char( 13, 97, 122, input.charAt(i) );
				else if ( input.charCodeAt(i) >= 48 && input.charCodeAt(i) <= 57 )		// Numbers 0-9 (code 48-57)
					output += convert_char( 5, 48, 57, input.charAt(i) );
				else
					output += input.charAt(i);
			}
			break;
			
		case "rot47":
			for (var i = 0; i < input.length; i++ )
			{		
				if ( input.charCodeAt(i) >= 33 && input.charCodeAt(i) <= 126 )			// Characters "!"-"~" (code 33-126):
					output = output + convert_char( 47, 33, 126, input.charAt(i) );
				else
					output = output + input.charAt(i);
			}
			break;
		default:
	}
	
	return output;
}
