function error(errorMsg){
	alert(errorMsg);
}

function isLowerCase(letter){
// Returns true if the one letter string is a letter A-Z or a-z
	// Check to make sure the string is one character
	if (letter.length != 1){
		error("isLowerCase: String passed is not one letter in length");
		return new String;
	}
	// String is one character in length

	return ((letter.charCodeAt() >= "a".charCodeAt()) && (letter.charCodeAt() <= "z".charCodeAt()))
}

function isUpperCase(letter){
// Returns true if the one letter string is a letter A-Z or a-z
	// Check to make sure the string is one character
	if (letter.length != 1){
		error("isUpperCase: String passed is not one letter in length");
		return new String;
	}
	// String is one character in length

	return ((letter.charCodeAt() >= "A".charCodeAt()) && (letter.charCodeAt() <= "Z".charCodeAt()))
}

function isLetter(letter){
// Returns true if the one letter string is a letter A-Z or a-z
	// Check to make sure the string is one character
	if (letter.length != 1){
		error("isLetter: String passed is not one letter in length");
		return new String;
	}
	// String is one character in length

	return isLowerCase(letter) || isUpperCase(letter);
}

function letterShift(letter, shift){
// Take a one character string and shifts the letter. Returns a one character string.
	// Check to make sure the string is one character
	if (letter.length != 1){
		error("letterShift: String passed is not one letter in length");
		return new String;
	}
	// String is one character in length

	// Check to see if it is a character we must shift (a letter)
	if (!isLetter(letter)){
		// Not a letter we have to worry about
		return letter;
	}

	// We want to work with a letterCode that is between 0 and 25 so we subtract baseLetterCode (either the code for A or a)
	if (isUpperCase(letter)){
		baseLetterCode = "A".charCodeAt();
	}
	else{
		baseLetterCode = "a".charCodeAt();
	}
	letterCode = letter.charCodeAt() - baseLetterCode;

	// Do the shift
	letterCode = letterCode + shift;
	letterCode = letterCode % 26;

	// The above works for all shifts right (+) but not left (-) so we must check
	// Example the letter a with a shift of 4 to the left would leave you with a -4 as the letterCode when it should be 21 (w)
	if (letterCode < 0){
		letterCode = 25 + letterCode + 1;
	}

	// The shift is complete. Create the proper ASCII/UniCode code
	letterCode = letterCode + baseLetterCode

	// Return the letter
	return String.fromCharCode(letterCode);
}	

function codeString(plainText, shift){
	codedText = "";
	for(i = 0; i < plainText.length; i++){
		codedText += letterShift(plainText.charAt(i),shift);
	}	
	// codedText is the coded string
	return codedText;
}

function formatText(string){
// Formats the string into groups of five letters per line and disregards all non-letter characters.
	newstring = "";
	count = 0;

	for(i = 0; i < string.length; i++){
		character = string.charAt(i);
		if( isLetter(character) || character == "\n" ){
			if (isLetter(character)){
				// This character is a letter
				if(count > 4){
					newstring += " ";
					// Why 1? Because their is a letter in the queue to be printed
					count = 1;
				}
				else{
					count++;
				}				
			}
			else{
				// The character is a return
				count = 0;
			}
			newstring += character;
		}
	}
	return newstring
}


function processForm(form){
	// First we must check to make sure the data is valid. Most things are defaulted so the only things we need to check are: 1) That the user entered text and 2) That the Shift is a whole number

	// Checking for text
	plainTextID = document.getElementById("plainText");
	if (plainTextID.value == ""){
		error("You must enter some text to code.");
		return new String;
	}

	// Checking for a shift number
	shiftNumberID = document.getElementById("shift");
	if (shiftNumberID.value == ""){
		error("You must enter a number to shift.");
		return new String;
	}
	else{
		// Checking to make sure the number entered is a number
		if (isNaN(parseInt(shiftNumberID.value))){
			error("The shift has to be a number.")
			return new String;
		}
		else{
			// Checking to make sure the shift number is positive
			if ( parseInt(shiftNumberID.value) < 0){
				error("Your shift must be a positive number.");
				return new String;
			}
		}
	}

	// Testing is complete. Collecting the information.
	// The plain text
	plainText = plainTextID.value;

	// The direction
	directionLeft = document.getElementById("directionLeft").checked;

	// Format
	format = document.getElementById("formatYes").checked;

	// Uppercase
	uppercase = document.getElementById("uppercaseYes").checked;

	// If the direction is left, then the shift value must be change to negative
	if (directionLeft){
		shift = parseInt("-" + shiftNumberID.value);
	}
	else{
		shift = parseInt(shiftNumberID.value);

	}

	// Code the text
	codedText = codeString(plainText, shift);

	// If the format is selected, we will group the text in sections of five characters
	if(format){
		codedText = formatText(codedText);
	}

	// If the uppercase is selected we will uppercase the text
	if(uppercase){
		codedText = codedText.toUpperCase();
	}

	// Display the coded text
	codedTextID = document.getElementById("codedText");
	codedTextID.value = codedText;	
}
