﻿// define the namespace if necessary
if ( OPSF == undefined )
	var OPSF = { };

OPSF.StudentAidApp = { 
	// classes in this namespace
	FormManager: function() { this.initialize(); },
	WordCounter: function(textareaId, labelId, wordLimit) { this.initialize(textareaId, labelId, wordLimit); },

	initialize: function() {
		var formManager = new OPSF.StudentAidApp.FormManager();
	}
};


OPSF.StudentAidApp.FormManager.prototype = {
	// public properties

	// private properties
	_wc01 : null,
	_wc02 : null,
	_wc03 : null,
	_wc04 : null,
	_wc05 : null,
	_wc06 : null,
	_wc07 : null,
	_wc08 : null,
	_wc09 : null,
	_wc10 : null,
	_wc11 : null,
	
	// constructor
	initialize: function() {
	
		if ( this.doesIdExist("lblSubjectsMostWordCount") )
			this._wc01 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtSubjectsMost", "lblSubjectsMostWordCount", 250 );
			
		if ( this.doesIdExist("lblSubjectsLeastWordCount") )
			this._wc02 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtSubjectsLeast", "lblSubjectsLeastWordCount", 250 );

		if ( this.doesIdExist("lblOrganizationsWordCount") )
			this._wc03 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtOrganizations", "lblOrganizationsWordCount", 250 );

		if ( this.doesIdExist("lblEmploymentWordCount") )
			this._wc04 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtEmployment", "lblEmploymentWordCount", 250 );

		if ( this.doesIdExist("lblCallingWordCount") )
			this._wc05 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtCalling", "lblCallingWordCount", 250 );

		if ( this.doesIdExist("lblLeadershipSkillsWordCount") )
			this._wc06 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtLeadershipSkills", "lblLeadershipSkillsWordCount", 250 );

		if ( this.doesIdExist("lblProblemsInChurchWordCount") )
			this._wc07 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtProblemsInChurch", "lblProblemsInChurchWordCount", 250 );

		if ( this.doesIdExist("lblDevotionalLifeWordCount") )
			this._wc08 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtDevotionalLife", "lblDevotionalLifeWordCount", 250 );

		if ( this.doesIdExist("lblActiveInChurchWordCount") )
			this._wc09 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtActiveInChurch", "lblActiveInChurchWordCount", 250 );

		if ( this.doesIdExist("lblSpiritualPilgrimageWordCount") )
			this._wc10 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtSpiritualPilgrimage", "lblSpiritualPilgrimageWordCount", 250 );

		if ( this.doesIdExist("lblDescribeYourselfWordCount") )
			this._wc11 = new OPSF.StudentAidApp.WordCounter( "ctl00_ContentPlaceHolder1_txtDescribeYourself", "lblDescribeYourselfWordCount", 500 );
	},
	
	doesIdExist: function(strid) {
		var obj = document.getElementById(strid);
		return (obj != null);
	}
};

OPSF.StudentAidApp.WordCounter.prototype = {

	// public properties

	// private properties
	_textarea       : null,
	_wordCountLabel : null,
	_wordLimit      : -1,
	
	// constructor
	initialize: function(textareaId, labelId, wordLimit) {
		this._textarea = document.getElementById(textareaId);
		this._wordCountLabel = document.getElementById(labelId);
		this._wordLimit = wordLimit;
		
		var textNode = document.createTextNode( "0" );
		this._wordCountLabel.appendChild(textNode);

		YAHOO.util.Event.addListener(this._textarea, "keyup", this.onKeyUp, this, true);
		YAHOO.util.Event.addListener("aspnetForm", "submit", this.onSubmit, this, true);
	},
	
	onKeyUp: function(e, me) {
		me.UpdateWordCountLabel();
	},
	
	onSubmit: function(e, me) {
		me.EnforceWordLimit();
	},
	
	GetWordCount: function(str) {
		// replace all instances of one-or-more spaces with a single space
		var text = str.replace(/\s+/g, ' ');

		if ( text.length == 0 )
			return 0;

		// trim leading and tailing spaces
		while( text.substring(0,1) == ' ' )
		{
			text = text.substring(1);
			if ( text.length == 0 )
				return 0;
		}
		while( text.substring(text.length-1, text.length) == ' ' )
			text = text.substring(0, text.length - 1);
		
		var arr = text.split(' ');
		return arr.length;
	},

	GetCutoffText: function(str) {
		var previoustext = "";
		var currenttext = "";
		
		for ( var i = 0; i < str.length; i++ )
		{
			currenttext += str[i];
			if ( this.GetWordCount(currenttext) > this._wordLimit )
				return previoustext;
				
			previoustext = currenttext;			
		}
	},

	EnforceWordLimit: function() {
		if ( this.GetWordCount(this._textarea.value) > this._wordLimit )
			this._textarea.value = this.GetCutoffText(this._textarea.value);
	},

	UpdateWordCountLabel: function() {
		while ( this._wordCountLabel.childNodes.length > 0 )
			this._wordCountLabel.removeChild( this._wordCountLabel.childNodes[0] );

		var wordCount = this.GetWordCount( this._textarea.value );
		var textNode = document.createTextNode(wordCount);
		this._wordCountLabel.appendChild(textNode);
	}

};


YAHOO.util.Event.onAvailable("ctl00_ContentPlaceHolder1_lblBottom", OPSF.StudentAidApp.initialize, this);
