/**
* This Javascript file is used by Wells Fargo funds - form management
* Initial javascript copied from VAP default form manager
* It works on forms where form name is 'main'
*/
	var wf_fsman;

	function WF_FormState(formObj) {
		this.formObj = formObj;
		this.formObj.onclick = wf_fsmanScan;
		// not sure all these checks require scan

		this.formObj.onkeyup = wf_fsmanScan;
		//this.formObj.onfocus = wf_fsmanScan;

		this.formObj.onchange = wf_fsmanScan;
		this.registeredButtons = new Array();
		this.allElements = new Array(formObj.elements.length);
		// max all elements can be required
		this.requiredElements = new Array(formObj.elements.length);

	}

	WF_FormState.prototype.initialize = function () {
		// should be called after registerRequiredFields
		var elementObj;
		for (var j = 0; j < this.requiredElements.length
					&& (this.requiredElements[j] != null) ; j++) {
			elementObj = this.requiredElements[j];
			var elementValue = "";
			var noscan = elementObj.getAttribute("noscan");
			if (noscan != null && noscan.length > 1) {
				this.allElements[j] = "noscan";
			} else {
				switch (elementObj.type) {

					case ("text"):
					case ("textarea"):
					case ("hidden"):
					case ("password"):
						elementValue = elementObj.value;
						break;
					case ("radio"):
					case ("checkbox"):
						elementValue = elementObj.defaultChecked;
						break;
					case ("select-one"):
						elementValue = elementObj.selectedIndex;
						break;
					case ("select-multiple"):
						for (var k = 0; k < elementObj.options.length; k++) {
							// the value is a string representing the selected state of each option
							elementValue += elementObj.options[k].defaultSelected.toString();
						}
						break;
				}
				this.allElements[j] = elementValue;
			}
		}
	}

        WF_FormState.prototype.registerRequiredField = function(requiredField) {
		// register required field
		for (var i = 0; i < this.requiredElements.length; i++) {
			if ( this.requiredElements[i] == null ) {
				// assign empty slot to this required field
				this.requiredElements[i] = requiredField;
				return;
			}
		}
	}

	WF_FormState.prototype.hasChanged = function () {
		var elementObj;
		var formState;
		var elementValue;
		var elemHasChanged = false;
		for (var i = 0; i < this.requiredElements.length
				&& (this.requiredElements[i] != null); i++) {
			elementObj = this.requiredElements[i];
			elementValue = this.allElements[i];
			var noscan = elementObj.getAttribute("noscan");
			if (noscan != null && noscan.length > 1) {
				elemHasChanged = false;
			} else {
				switch (elementObj.type) {
					case ("text"):
					case ("textarea"):
					case ("hidden"):
					case ("password"):
						elemHasChanged = (elementValue != elementObj.value);
						break;
					case ("radio"):
					case ("checkbox"):
						if ((elementValue == false && elementObj.checked) || (elementValue == true && !elementObj.checked)) {
							elemHasChanged = true;
						}
						break;
					case ("select-one"):
						elemHasChanged = (elementValue != elementObj.selectedIndex);
						break;
					case ("select-multiple"):
						var multiValueString = "";
						for (var k = 0; k < elementObj.options.length; k++) {
							multiValueString += elementObj.options[k].selected.toString();
						}
						elemHasChanged = (elementValue != multiValueString);

				}
			}
			if (!elemHasChanged) {
				return false;
			}
		}
		return elemHasChanged;
	}

	WF_FormState.prototype.scan = function () {
		if (this.hasChanged()) {
			this.fa_unlockButtons();
			return true;
		} else {
			this.fa_lockButtons();
			return false;
		}
	}

	WF_FormState.prototype.register = function (buttonObj) {
		if (buttonObj != null) {
			buttonObj.disabled = true;
			this.registeredButtons[this.registeredButtons.length] = buttonObj;
		}
	}

	WF_FormState.prototype.fa_unlockButtons = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			this.registeredButtons[i].disabled = false;
		}
	}

	WF_FormState.prototype.fa_lockButtons = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			this.registeredButtons[i].disabled = true;
		}
	}


	function WF_FormStateManager(documentString, sff) {
		this.initialized = false;
		this.allForms = null;
		this.doc = null;
		this.docString = documentString;
		this.registeredButtons = new Array();
		this.selectFirstField = (sff) ? true : false;
		this.forceApplyButtonLockFlag = false;
		this.noscan = false;
		this.initialize();
		// to hold main form
		this.wfMainForm = null;
	}

	WF_FormStateManager.prototype.initialize = function () {
		this.initialized = false;
		this.forceApplyButtonLockFlag = false;
		this.allForms = new Array();


		if (this.docString != null) {
			this.doc = eval(this.docString);
			if (this.doc != null && this.doc.forms.length > 0) {
				this.allForms = new Array();
				var formStateObj;
				for (var i = 0; i < this.doc.forms.length; i++) {
					var formStateObj = new WF_FormState(this.doc.forms[i]);
					this.registerFormState(formStateObj);
					if (i == 0) {
						this.focusFirst(this.doc.forms[i]);
					}
					if ( this.doc.forms[i].name == "main") {
						wfMainForm = formStateObj;
					}
				}
				this.doc.onunload = wf_fsmanInitialize;
			} else {
				setTimeout("wf_fsmanInitialize()", 500);
				return;
			}
		}
	}

	WF_FormStateManager.prototype.registerFormState = function(formStateObj) {
		if (formStateObj != null) {
			this.allForms[this.allForms.length] = formStateObj;
			this.initialized = true;
		}
	}

	WF_FormStateManager.prototype.focusFirst = function (formObj) {
		var elementObj;
		for (var e = 0; e < formObj.elements.length; e++) {
			elementObj = formObj.elements[e];
			if (elementObj.type == "text" || elementObj.type == "textarea") {
				if (this.selectFirstField) {
					elementObj.select();
				} else {
					elementObj.focus();
				}
				break;
			}
		}
	}

	WF_FormStateManager.prototype.hasChanged = function () {
		if (!this.initialized) {
			this.initialize();
			return false;
		}
		for (var i = 0; i < this.allForms.length; i++) {
			formStateObj = this.allForms[i];
			if (formStateObj.hasChanged()) {
				return true;
			}
		}
		return false;
	}

	// Return Main Wf Form State object
	WF_FormStateManager.prototype.getMainForm = function () {
		return wfMainForm;
	}

	WF_FormStateManager.prototype.scan = function () {
		if (this.noscan) return;
		var childFSHasChanged = false;
		var fsObj;
		for (var i = 0; i < this.allForms.length; i++) {
			fsObj = this.allForms[i];
			if (fsObj.scan()) {
				childFSHasChanged = true;
			}
		}

		if (childFSHasChanged) {
			this.fa_unlockButtons();
		} else {
			this.fa_lockButtons();
		}
	}

	WF_FormStateManager.prototype.register = function (buttonObj) {
		if (buttonObj != null) {
			buttonObj.disabled = true;
			this.registeredButtons[this.registeredButtons.length] = buttonObj;
		}
	}

	WF_FormStateManager.prototype.fa_unlockButtons = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			button = this.registeredButtons[i];
			if (button.name == "mainSubmit") {
				if (this.forceApplyButtonLockFlag) continue;
			}
			button.disabled = false;
		}
	}

	WF_FormStateManager.prototype.fa_lockButtons = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			this.registeredButtons[i].disabled = true;
		}
	}

	WF_FormStateManager.prototype.fa_lockApplyButton = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			button = this.registeredButtons[i];

			if (button.name == "mainSubmit") {
				button.disabled = true;
			}

		}
	}

	WF_FormStateManager.prototype.fa_unlockApplyButton = function () {
		// Check bypass lock
		if (this.forceApplyButtonLockFlag) return;

		for (var i = 0; i < this.registeredButtons.length; i++) {
			button = this.registeredButtons[i];

			if (button.name == "mainSubmit") {
				button.disabled = false;
			}

		}
	}

	WF_FormStateManager.prototype.fa_unlockResetButton = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			button = this.registeredButtons[i];
			if (button.name == "mainReset") {
				button.disabled = false;
			}

		}
	}

	WF_FormStateManager.prototype.fa_lockResetButton = function () {
		for (var i = 0; i < this.registeredButtons.length; i++) {
			button = this.registeredButtons[i];

			if (button.name == "mainReset") {
				button.disabled = true;
			}

		}
	}


	WF_FormStateManager.prototype.fa_forceApplyButtonLock = function () {
		this.forceApplyButtonLockFlag = true;
	}

	WF_FormStateManager.prototype.fa_clearApplyButtonLock = function () {
		this.forceApplyButtonLockFlag = false;
	}

	WF_FormStateManager.prototype.disableScan = function () {
		this.noscan = true;
	}

	WF_FormStateManager.prototype.enableScan = function () {
		this.noscan = false;
	}

	function fa_unlockButtons() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_unlockButtons()", 50);
		}
	}

	function fa_lockButtons() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_lockButtons()", 50);
		}
	}

	function fa_unlockApplyButton() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_unlockApplyButton()", 50);
		}
	}

	function fa_lockApplyButton() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_lockApplyButton()", 50);
		}
	}

	function fa_unlockResetButton() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_unlockResetButton()", 50);
		}
	}

	function fa_lockResetButton() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_lockResetButton()", 50);
		}
	}

	function fa_forceApplyButtonLock() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_forceApplyButtonLock()", 50);
		}
	}

	function fa_clearApplyButtonLock() {
		if (wf_fsman != null) {
			setTimeout("wf_fsman.fa_clearApplyButtonLock()", 50);
		}
	}

	function wf_fsmanScan() {
		setTimeout("wf_fsman.scan()", 50);
	}
	function wf_fsmanInitialize() {
		wf_fsman.initialize();
	}
