﻿	// Script requires Prototype 1.5.1
	// See http://www.prototypejs.org
	//
	// This file is part of the financial healthcheck, and should be syndicated across all partners.
	var FSAFormValidator = Class.create();
	FSAFormValidator.prototype = {
		loaded: false,
		initialize: function() {
			this.load();
		},
		load: function() {
			if((typeof Prototype=='undefined') || 
			   (typeof Element == 'undefined') || 
			   (typeof Element.Methods=='undefined') ||
			   parseFloat(Prototype.Version.split(".")[0] + "." +
						  Prototype.Version.split(".")[1]) < 1.5) {
				this.loaded = false;
				alert("FSA tools require the Prototype JavaScript framework 1.5.0 or later. See http://www.prototypejs.org for more information.\n\nThis form has been disabled - please contact this website's administrator.");
				return false;
			}
			else
			{
				this.loaded = true;
				return true;
			}
		},
		enable: function(id) {
			if (this.loaded) {
				var row = $(id);
				$A(row.getElementsByTagName('*')).each(function(n) { 
					if (n.nodeName.toUpperCase() == "INPUT")
						Form.Element.enable(n);
					if (n.nodeName.toUpperCase() == "DIV")
						Element.removeClassName(n,"disabled");
					return;
				});
			}
		},
		disable: function(id) {
			if (this.loaded) {
				var row = $(id);
				$A(row.getElementsByTagName('*')).each(function(n) { 
					if (n.nodeName.toUpperCase() == "INPUT")
					{
						Form.Element.disable(n);
						n.checked = false;
					}
					if (n.nodeName.toUpperCase() == "DIV")
						Element.addClassName(n,"disabled");
					return;
				});
			}
		},
		changeLabel: function(id,label) {
			if (this.loaded) {
				$(id).innerHTML = label;
			}
		},
		checkid: "",			// set this id to validate radio/checkboxes under this
		checkerrors: 0,			
		checkform: null, 
		check: function(_fm) {
			if (this.loaded) {
				this.checkform = $(_fm);
				if (this.checkid != "") {
					var f = this.checkform.getElements();
					for (var i = 0; i < f.length; i++)
					{
						if (f[i].disabled == false && f[i].type == "radio")
						{	
							if ($A(this.checkform[f[i].name]).all(function(n) { return !n.checked; } ))
								this.checkerrors++;
						}
					}

					if (this.checkerrors > 0) {
						alert("Please answer all the questions");
						this.checkerrors = 0;
						return false;
					}
					else
						return true;
				}
				else
					return true;
			}
			else
				return false;
		}
	}
	var FSAForm = new FSAFormValidator();
