/**************************************************************************************************/
/***
/***	TERNBASE (TM) FORM VALIDATION JAVASCRIPT DOCUMENT
/***	-----------------------------------------------------------------------
/***	Written by Matthew Praetzel. Copyright (c) 2007 Matthew Praetzel.
/***	-----------------------------------------------------------------------
/***	All Rights Reserved. Any use of these functions & scripts without written consent is prohibited.
/***
/**************************************************************************************************/

/*                                   **************************                                   */
/************************************   INITIALIZE VARIABLES   ************************************/
/*                                   **************************                                   */
var formStances = [];
/*                                   **************************                                   */
/************************************      FORM FUNCTIONS      ************************************/
/*                                   **************************                                   */
var ternFORM = function (i) {
	var a = formStances,th = this,l = a.length;
	if(window == th) {
		for(var b=0;b<a.length;b++) {
			if(a[b]["node"] == i) {
				return a[b]["object"];
			}
		}
		a[l] = new Array();
		a[l]["node"] = i;
		a[l]["object"] = new ternFORM(i);
		return a[l]["object"];
	}
	if(i) {
		th.tern = th.getNode(i);
	}
};
ternFORM.prototype = {
	vowels : ["a","e","i","o","u"],errors : "",css : "",fn : null,
	submit : 
	function (f) {
		var f = f ? f : this.tern;
		f.submit();
	},
	validateForm :
	function (c,w,f,g) {
		var th = this;
		th.tern.onsubmit = function () { return false; };
		th.efs = new Array();
		ternVENTS.addEvent(th.tern,function () { th.validateAndHandle(c,w,f,g); },"submit",false);
		return th;
	},
	validateAndHandle :
	function(c,w,f,g) {
		var th = this,i = th.tern,er = "",re = "";
		th.fn = f;
		th.css = c;
		th.unlabel(c);
		var req = th.checkRequiredByTitle(false);
		if(th.isObject(i.elements["email"])) {
			var em = th.validateEmail(i.elements["email"]);
		}
		else {
			var em = true;
		}
		var num = th.checkNumericByTitle(false);
		var len = th.checkLengthByTitle(false);
		var cv = th.confirmValuesByTitle(false);
		var oi = th.oneIsChecked();
		if(th.isObject(i.elements["agreement"])) {
			var ag = th.agreement("agreement");
		}
		else {
			var ag = true;
		}
		if(req == true && em && num == true && len == true && cv == true && ag == true && oi) {
			th.fixMultipleSelects();
			th.submit();
			return;
		}
		else {
			if(ternStyle(th.tern).getElementsByClass("errors_cn",false).length) {
				var er = ternStyle(th.tern).getElementsByClass("errors_cn",false)[0];
				if(w == window) {
					var c = ternStyle(er).getCoords()[1];
					var st = ternStyle(window).getScroll()[1];
				}
				else {
					var c = parseInt(ternStyle(er).getCoords()[1]+w.ternStyle(i).getCoords()[1]);
					var st = w.ternStyle(window).getScroll()[1];
				}
			}
			if(st > c) {
				ternScroll(window).scrollSlideTo("medium",false,c);
			}
			var ec = er.getElementsByTagName("div")[0];
			ternStyle(ec).fadeOut("medium",function () { return th.error(g); });
		}
	},
	checkRequiredByTitle :
	function (am,q) {
		var th = this,f = th.tern,a = f.elements,f = [],s = "",e = "";
		for(var i=0;i<a.length;i++) {
			if(a[i].title.toLowerCase().indexOf("required") != -1) {
				var t = th.inputType(a[i]);
				if(((t == "text" || t == "password" || t == "textarea") && a[i].value == "") ||
					((t == "select-one") && a[i].options[a[i].selectedIndex].value == "") ||
					((t == "select-multiple") && !th.multiIsSelected(a[i])) ||
					((t == "checkbox" || t == "radio") && a[i].checked == false)) {
					f[f.length] = a[i].title.substr(0,a[i].title.indexOf("::"));
					s += "\n" + a[i].title.substr(0,a[i].title.indexOf("::"));
					e += "<br />&nbsp;&nbsp;&nbsp;" + a[i].title.substr(0,a[i].title.indexOf("::"));
					th.efs[th.efs.length] = a[i];
				}
			}
		}
		if(s.length > 0) {
			var ht = "The following fields are required:<br />" + e;
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
		}
		if(am == true && s.length > 0) {
			alert("Please fill out the following fields:"+s);
		}
		if(q == "string") {
			return s;
		}
		else if(q == "array" && f.length > 0) {
			return f;
		}
		else if(s.length > 0) {
			return false;
		}
		return true;
	},
	confirmValuesByTitle :
	function (am,q) {
		var th = this,a = th.tern,f = [],s = "",e = "";
		for(var i=0;i<a.length;i++) {
			if(a[i].title.toLowerCase().indexOf("confirm") != -1) {
				var t = th.inputType(a[i]),ti = a[i].title;
				var c = ti.substr(ti.indexOf("confirm:")+8);
				for(var b=0;b<a.length;b++) {
					if(a[b].name == c && th.inputValue(a[b]) != th.inputValue(a[i])) {
						f[f.length] = a[i];
						s += "\n" + a[i].title.substr(0,a[i].title.indexOf("::"));
						e += "<br />&nbsp;&nbsp;&nbsp;" + a[i].title.substr(0,a[i].title.indexOf("::"));
						th.efs[th.efs.length] = a[i];
					}
				}
			}
		}
		if(s.length > 0) {
			var ht = "The following fields do not match:<br />" + e;
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
		}
		if(am == true && s.length > 0) {
			alert("The following fields don't match:"+s);
		}
		if(q == "string") {
			return s;
		}
		else if(q == "array" && f.length > 0) {
			return f;
		}
		else if(s.length > 0) {
			return false;
		}
		return true;
	},
	passwordStrength :
	function (dp,am) {
		var th = this,i = th.tern,v = i.value,s,q = "-",cl = "#FF0000";
		var n = v.match(new RegExp("[0-9]{1}","g"));
		var l = v.match(new RegExp("[a-zA-Z]{1}","g"));
		if(/[^0-9a-zA-Z]/g.exec(v)) {
			i.value = v.substr(0,v.length-1);
			alert("You have typed an incorrect character. Please use only alphanumeric characters.");
			return;
		}
		else {
			if(l && l.length >= 6 && n && n.length >= 2) {
				s = "strong";
				q = "+";
				cl = "#009900";
			}
			else if((l && l.length >= 6 && n && n.length <= 1) || l && l.length >= 6) {
				s = "medium";
				cl = "#FFCC33";
			}
			else {
				s = "weak";
			}
		}
		if(am != undefined && (am == "strong" && s != "strong") || (am == "medium" && (s == "weak"))) {
			alert("Please choose a stronger password. Does your password contain numbers?");
			return false;
		}
		if(dp != true) {
			return s;
		}
		else {
			var p = th.getIndex(i);
			var n = i.parentNode;
			if(document.getElementById(i.name+"Strength")) {
				tn = document.getElementById(i.name+"Strength");
				th.removeChildren(tn);
			}
			else {
				var tn = document.createElement("span");
				tn.setAttribute("id",i.name+"Strength");
				if(c = n.childNodes[p+1]) {
				n.insertBefore(tn,c);
				}
				else {
					n.appendChild(tn);
				}
			}
			tn.appendChild(document.createTextNode("  "+q+" "+s));
			ternStyle(tn).setStyle("color",cl);
			ternStyle(tn).setStyle("font-family","verdana");
			ternStyle(tn).setStyle("font-size","14px");
			ternStyle(tn).setStyle("font-weight","bold");
		}
	},
	validateEmail : 
	function (al,m,i) {
		var th = this,i,er = new RegExp("^[a-zA-Z0-9.-_+]+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$","g"),a = arguments;
		if(a.length == 3) {
			i = th.getNode(i);
		}
		else if(a.length == 0 || al == false) {
			i = th.tern;
		}
		else {
			i = th.getNode(al);
		}
		v = i.value;
		if(er.test(v)) {
			return true;
		}
		else {
			var ht = "Your email address does not seem to be formatted properly.";
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			th.efs[th.efs.length] = i;
		}
		if(al == true) {
			if(m != undefined) {
				alert(m);
			}
			else {
				alert("Your email address does not appear to be formatted properly.");
			}
		}
		return false;
	},
	fieldLength :
	function (eq,l,am) {
		var th = this,i = th.tern,len = i.value.length,cl,vl,an,eq = eq == "=" ? "==" : eq;
		if(eval("len "+eq+" l")) {
			return true;
		}
		else {
			if(eq == ">=") {
				cl = l+" characters or more.";
			}
			else if(eq == "<=") {
				cl = l+" characters or less.";
			}
			else if(eq == "==") {
				cl = l+" characters.";
			}
			var ht = "Please choose "+an+" "+th.tern.title.substr(0,th.tern.title.indexOf("::"))+" of "+cl;
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			th.efs[th.efs.length] = i;
			if(am != undefined && am == true) {
				an = th.indexOf(th.tern.title.substr(0,1).toLowerCase(),th.vowels) != false ? "an" : "a";
				alert("Please choose "+an+" "+th.tern.title.substr(0,th.tern.title.indexOf("::"))+" of "+cl);
			}
			return false;
		}
	},
	checkLengthByTitle :
	function (am,q) {
		var th = this,f = th.tern,a = f.elements,f = [],s = "",e = "";
		for(var i=0;i<a.length;i++) {
			var ps = a[i].title.substr(a[i].title.indexOf("::")+2);
			var fd = a[i].title.substr(0,a[i].title.indexOf("::"));
			if(ps.indexOf("length") != -1) {
				var t = th.inputType(a[i]),eq = ps.substr(ps.indexOf("length")+6,1);
				var st = ps.substr(ps.indexOf("length")+7),l = st.substr(0,1),len = "";
				while(/^[0-9]{1}$/.test(l)) {
					len += "" + parseInt(l);
					st = st.substr(1);
					l = st.substr(0,1);
				}
				var eqa = eq == "=" ? "==" : eq;
				if(eval("a[i].value.length "+eqa+" len")) {
				}
				else {
					if(eq == "=") {
						eq = "equal to";
					}
					else if(eq == ">") {
						eq = "greater than";
					}
					else if(eq == "<") {
						eq = "less than";
					}
					
					if(ps.indexOf("numeric") != -1) {
						var pm = " must be " + eq + ' ' + len + " digits";
					}
					else {
						var pm = " must be " + eq + ' ' + len + " characters";
					}
					f[f.length] = fd;
					s += "\n" + fd + pm;
					e += "<br />&nbsp;&nbsp;&nbsp;" + fd + pm;
					th.efs[th.efs.length] = a[i];
				}
			}
		}
		if(s.length > 0) {
			var ht = "The following fields must be of a certain length:<br />" + e;
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			if(am == true) {
				alert("Please fill out the following fields:"+s);
			}
			if(q == "string") {
				return s;
			}
			else if(q == "array") {
				return f;
			}
			return false;
		}
		return true;
	},
	agreement : 
	function (i,am,m) {
		var th = this,f = th.tern,i = f.elements[i];
		if(i.checked == true) {
			return true;
		}
		else {
			var ht = "You must agree to our Terms of Service to continue.";
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			th.efs[th.efs.length] = i;
			if(am != undefined && am == true) {
				if(m != undefined) {
					alert(m);
				}
				else {
					alert("You must agree to our Terms of Service to continue.");
				}
			}
			return false;
		}
	},
	checkNumericByTitle :
	function (am,q) {
		var th = this,f = th.tern,a = f.elements,f = [],s = "",e = "",nu = /[^0-9]/g;
		for(var i=0;i<a.length;i++) {
			if(a[i].title.toLowerCase().indexOf("numeric") != -1) {
				var t = th.inputType(a[i]);
				if((t == "text" || t == "password" || t == "textarea") && nu.test(a[i].value)) {
					f[f.length] = a[i].title.substr(0,a[i].title.indexOf("::"));
					s += "\n" + a[i].title.substr(0,a[i].title.indexOf("::"));
					e += "<br />&nbsp;&nbsp;&nbsp;" + a[i].title.substr(0,a[i].title.indexOf("::"));
					th.efs[th.efs.length] = a[i];
				}
			}
		}
		if(s.length > 0) {
			var ht = "The following fields must be numeric:<br />" + e;
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			if(am == true) {
				alert("Please fill out the following fields:"+s);
			}
			if(q == "string") {
				return s;
			}
			else if(q == "array") {
				return f;
			}
			return false;
		}
		return true;
	},
	isNumeric :
	function (l,am) {
		var th = this,i = th.tern,v = th.inputValue(i),r = new RegExp("[^0-9]","g");
		if(r.test(v)) {
			if(l != false) {
				var ht = "Please provide a valid "+i.title.substr(0,i.title.indexOf("::"))+". This field needs to be numeric and contain "+l+" characters";
			}
			else {
				var ht = "Please provide a valid "+i.title.substr(0,i.title.indexOf("::"))+". This field needs to be numeric.";
			}
			th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
			th.efs[th.efs.length] = i;
			if(am != undefined && am == true) {
				alert(ht);
			}
			return false;
		}
		return true;
	},
	oneIsChecked :
	function () {
		var th = this,f = th.tern,a = f.elements,no = true,eu = [];
		for(var i=0;i<a.length;i++) {
			var t = th.inputType(a[i]).toLowerCase();
			if(t == "checkbox") {
				var ps = a[i].title.substr(a[i].title.indexOf("::")+2);
				var fd = a[i].title.substr(0,a[i].title.indexOf("::"));
				if(ps.indexOf("one checked") != -1 && th.indexOf(a[i].name,eu) === false && !th.isOneChecked(a[i].name)) {
					var ht = "Please select one for: " + fd;
					th.errors += th.errors.length > 0 ? "<br /><br />" + ht : ht;
					th.efs[th.efs.length] = a[i];
					eu[eu.length] = a[i].name;
					var no = false;
				}
			}
		}
		if(!no) {
			return false;
		}
		return true;
	},
	isOneChecked :
	function (n) {
		var th = this,f = th.tern,n = f.elements[n];
		if(th.isArray(n)) {
			for(var i=0;i<n.length;i++) {
				if(n[i].checked == true) {
					return true;
				}
			}
		}
		else if(n.checked == true) {
			return true;
		}
		return false;
	}
}
ternFORM.prototype.extend = 
function (a,i) {
	var t = i != undefined ? i : this;
	for(k in a) {
		t[k] = a[k];
	}
	return t;
}
ternFORM.prototype.extend({
	resetErrors :
	function() {
		this.errors = "";
		this.efs = new Array();
	},
	error :
	function (g) {
		this.label(this.css);
		this.showErrors(g);
		this.resetErrors();
	},
	showErrors :
	function (g) {
		var th = this,fn = th.fn ? th.fn : function(){};
		var ec = ternStyle(th.tern).getElementsByClass("errors_cn",false)[0];
		var er = ec.getElementsByTagName("div")[0];
		var ed1 = ternStyle(er).getDimensions();
		er.innerHTML = th.errors;
		var ed2 = ternStyle(er).getDimensions();
		ternStyle(ec).setStyle("visibility","visible");
		if(g) {
			for(var i=0;i<g.length;i++) {
				var h = ternStyle(g[i]).getDimensions()[1];
				if(ed2[1] > ed1[1]) {
					ternStyle(g[i]).grow("medium","y",h+ed2[1]);
				}
			}
		}
		ternStyle(ec).grow("medium","y",ed2[1],function () { ternStyle(er).fadeIn("medium");fn(); });
	},
	label :
	function (c,a) {
		var a = a ? a : this.efs;
		for(k in a) {
			var f = a[k];
			if(f) {
				var ls = this.tern.getElementsByTagName("label");
				for(var i=0;i<ls.length;i++) {
					if(ls[i].htmlFor != null && ls[i].htmlFor.indexOf(f.name) != -1) {
						ternStyle(ls[i]).addClass(c);
						break;
					}
				}
			}
		}
	},
	unlabel :
	function (c) {
		var ls = this.tern.getElementsByTagName("label");
		for(var i=0;i<ls.length;i++) {
			ternStyle(ls[i]).removeClass(c);
		}
	}
});
ternFORM.prototype.extend({
	inputValue :
	function () {
		var th = this,a = arguments,i = a[0] ? th.getNode(a[0]) : th.tern,t = i.type;
		if(t == "text" || t == "password") {
			return i.value;
		}
		else if(t == "select-one") {
			return i.options[i.selectedIndex].value;
		}
		else if(t == "checkbox" || t == "radio") {
			return i.checked;
		}
	},
	inputType :
	function (i) {
		var th = this,i = i != undefined ? i : th.tern;
		return i.type;
	},
	getFromPost :
	function (f) {
		var th = this,f = f != undefined ? th.getNode(f) : th.tern;el = f.elements,p = "";
		for(var i=0;i<el.length;i++) {
			var v = el[i].id + "=" + el[i].value;
			p += p.length > 0 ? "&" + v : v;
		}
		return p;
	},
	fixMultipleSelects :
	function(s,f) {
		var th = this,f = f != undefined ? th.getNode(f) : th.tern,el = f.elements,t,o,s = s != undefined ? s : ",";
		for(var i=0;i<el.length;i++) {
			t = th.inputType(el[i]);
			if(t == "select-multiple") {
				o = el[i].options;
				var ops = "";
				for(var a=0;a<o.length;a++) {
					if(o[a].selected == true) {
						ops += ops.length > 0 ? s + o[a].value : o[a].value;
					}
				}
				var tn = th.createTextInput(el[i].name,ops);
				el[i].parentNode.replaceChild(tn,el[i]);
				i--;
			}
		}
	},
	multiIsSelected :
	function (i) {
		var i = i != undefined ? i : this.tern;
		var o = i.options;
		var ops = "";
		for(var a=0;a<o.length;a++) {
			if(o[a].selected == true) {
				return true;
			}
		}
		return false;
	},
	createTextInput :
	function (n,v,s,c) {
		var i = document.createElement('input');
		i.setAttribute('type','text');
		i.setAttribute('name',n);
		i.setAttribute('id',n);
		i.setAttribute('size',s);
		i.value = v;
		if(c) {
			ternStyle(i).addClass(c);
		}
		return i;
	},
	createTextarea :
	function (n,v,c) {
		var i = document.createElement('textarea');
		i.setAttribute('name',n);
		i.setAttribute('id',n);
		i.value = v;
		if(c) {
			ternStyle(i).addClass(c);
		}
		return i;
	},
	createHidden :
	function(n,v) {
		var i = document.createElement('input');
		i.setAttribute('type','hidden');
		i.setAttribute('name',n);
		i.setAttribute('id',n);
		i.value = v;
		return i;
	},
	addHiddenToForm :
	function (n,v) {
		var th = this;f = this.tern;
		var i = th.createHidden(n,v);
		f.elements[0].parentNode.appendChild(i);
	}
});
ternFORM.prototype.extend({
	allowOneChecked :
	function(n) {
		var th = this,f = th.tern,el = f.elements;
		for(var i=0;i<el.length;i++) {
			if(el[i].name == n) {
				ternVENTS.addEvent(el[i],function (e) { th.uncheck(e,n); },"click",false);
			}
		}
		return th;
	},
	uncheck :
	function (e,n) {
		var th = this,f = th.tern,el = f.elements,s = ternVENTS.eventSource(e);
		for(var i=0;i<el.length;i++) {
			if(el[i].name == n && el[i] != s) {
				el[i].checked = false;
			}
		}
	}
});
ternFORM.prototype.extend({
	select :
	function (n) {
		var s = document.createElement('select');
		s.setAttribute('name',n);
		s.setAttribute('id',n);
		return s;
	},
	selectOption :
	function (v,n) {
		var o = document.createElement('option');
		o.value = v;
		o.text = n;
		return o;
	},
	addSelectOption :
	function (f) {
		var o = document.createElement('option');
		o.text = 'Select';
		o.value = '';
		f.options[0] = o;
	},
	addOption :
	function (f,o) {
		f.options[f.options.length] = o;
	},
	selectFn :
	function (f,fn) {
		ternVENTS.addEvent(f,fn,"change",false);
	}
});
ternFORM.prototype.extend({
	getNode :
	function (i) {
		var th = this,a = [];
		if(typeof(i) == "string") {
			return document.getElementById(i);
		}
		else if(th.isArray(i)) {
			for(var b=0;b<i.length;b++) {
				a[b] = typeof(i[b]) == "string" ? document.getElementById(i[b]) : i[b];
			}
		}
		else {
			a = i;
		}
		return a;
	},
	getIndex :
	function (c) {
		var cs = c.parentNode.childNodes;
		for(var i=0;i<cs.length;i++) {
			if(c == cs[i]) {
				return i;
			}
		}
		return false;
	},
	removeChildren : 
	function (i) {
		var th = this,i = i == undefined ? th.tern : th.getNode(i);
		if(i.hasChildNodes) {
			while(i.firstChild) {
				i.removeChild(i.firstChild);
			}
		}
	}
});
ternFORM.prototype.extend({
	isObject :
	function (o) {
		var th = this;
		if((typeof o == "object" && !!o) || th.isFunction(o)) {
			return true;
		}
		return false;
	},
	isIEObject :
	function (o) {
		var th = this;
		if(th.isObject(o) && typeof(o.constructor != "function")) {
			return true;
		}
		return false;
	},
	isFunction : 
	function (f) {
		return typeof f == "function";
	},
	isArray : 
	function (a) {
		var th = this;
		if((th.isObject(a) && a.length > 0 && typeof(a) != "string" && !a.nodeType) || a.constructor == Array) {
			return true;
		}
		else {
			return false;
		}
	},
	indexOf :
	function (v,a) {
		var th = this,a = a ? a : th.tern;
		for(var i=0;i<a.length;i++) {
			if(a[i] === v) {
				return i;
			}
		}
		return false;
	}
});