/**
 * Creator: khastie
 * Date: Oct 14, 2008
 * Time: 3:55:32 PM
 * Contents are property of Cappex.com LLC, and strictly confidential.
 */

/**
 * CLASS: FieldFixer
 * DESCR: Capitalized first letter of the field (for name), or all words in field (for city)
 * USAGE:
 * EXMPL:
 */

var FieldFixer = new Class({

	Implements: Options,

	options:{
		isNameFixed : false,
		isCityFixed : false,
		modifiedNames : new Array(3)
	},


	initialize : function (options) {

		this.setOptions(options);

		window.addEvent('domready', function () {
			this.domReady();
		}.bind(this));

	},

	domReady : function () {
		
		$('lastName').addEvent('change', function () {
			if (!this.options.isNameFixed) { // only do this once
				this.fixName($('firstName'));
				this.fixName($('middleInitial'));
				this.fixName($('lastName'));
			}
		}.bind(this));

		$('city').addEvent('change', function () {
			if (!this.options.isCityFixed) { // only do this once
				this.fixCity($('city'));
			}
		}.bind(this));
	},

	/**
	 * Capitalizes the first letter of the field
	 */
	fixName : function (elem) {

		var name = elem.value;

		if (name.length > 0 && !name.contains(' ')) {

			if (name.charAt(0).match('[a-z]')) {
				elem.value = name.charAt(0).toUpperCase() + name.substring(1);
				showTip('nameFixerTip', 'nameFixerTipArrow', 273, 34, 'nameFixerTipText'); // note: showTip is defined in showTips.js for Student Basic, and in quickApplyContent.jsp for QA
				this.options.isNameFixed = true;
			}
		}
	},

	/**
	 * Capitalizes the first letter of the field
	 */
	fixCity : function (elem) {

		var name = elem.value;

		if (name.length > 0) {

			var words = name.split(' ');

			elem.value = '';

			for (var i = 0; i < words.length; i++) {

				if (words[i].charAt(0).match('[a-z]')) {
					words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
					showTip('cityFixerTip', 'cityFixerTipArrow', 273, 34, 'cityFixerTipText'); // note: showTip is defined in showTips.js for Student Basic, and in quickApplyContent.jsp for QA
					this.options.isCityFixed = true;
				}

				elem.value += words[i] + ' ';
			}

			elem.value = elem.value.trim();

		}
	}

});

var fieldFixer = new FieldFixer();
