
/*
	--------------------------------------------------------------------------------------------------------------
	@package:  Validation Tools
	@author:     Michael Walsh
	@date:        13-08-2006
	@version:    1.0
	--------------------------------------------------------------------------------------------------------------
	MSIE 5.5+
	Firefox 1.07+
	Netscape 7.0+
	Opera 8.0+
	--------------------------------------------------------------------------------------------------------------
*/

/*
	@method:   Form validation.
	@version:   1.0
*/
function validateForm(o)
{
	var oDiv = document.getElementById(o.hud.element), oForm = document[o.form.name], aErrors = [];
	for (var i=0; i < o.schema.length; i++)
	{
		var required = o.schema[i].required, value, re;
		switch (o.schema[i].type)
		{
			case "input":
			value = oForm[o.schema[i].field].value; break;
			case "radio":
			value = (oForm[o.schema[i].field].value!=undefined) ? oForm[o.schema[i].field].value : ''; break;
			case "checkbox":
			value = (oForm[o.schema[i].field].value!=undefined) ? oForm[o.schema[i].field].value : ''; break;
			case "select":
			value = Forms.Select.getSelectedValues(oForm[o.schema[i].field]); break;
			case "range":
			value = []; for (var j=o.schema[i].range[0]; j <= o.schema[i].range[1]; j++) { var name = o.schema[i].field+j; value[j-1] = oForm[name].value; } break;
			case "password":
			value = oForm[o.schema[i].field].value; break;
		}
		if (required&&!value.length) aErrors[aErrors.length] = [o.schema[i].field,o.schema[i].message.required];
		else if(o.schema[i].validation=='custom')
		{
			if (o.schema[i].range!=undefined)
			{
				for (var j=o.schema[i].range[0]; j <= o.schema[i].range[1]; j++)
				{
					var name = o.schema[i].field+j;
					var output = o.schema[i].custom.apply(this,[{id:name,index:j}]);
					if (!output.valid) for (var k=0; k < output.errors.length; k++) aErrors[aErrors.length] = output.errors[k];
				}
			} else { var output = o.schema[i].custom.apply(this,[{id:o.schema[i].field}]); if (!output.valid) for (var l=0; l < output.errors.length; l++) aErrors[aErrors.length] = output.errors[l]; }
		}
		else if(value.length)
		{
			var bFail=false;
			switch (o.schema[i].validation)
			{
				case "string":
				bRe=true;re = /./;
				break;
				case "email":
				bRe=true;re = /^[a-z\d._%-]+@[a-z\d.-_]+(\.[a-z]{2,4})$/;
				break;
				case "numeric":
				bRe=true;re = /^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/;
				break;
				case "password":
				bRe=true;re = /./;
				break;
				case "date":
				var aDate=value.split("/"); var date = new Date(aDate[2],aDate[1]-1,aDate[0]); var bFail=(parseInt(aDate[2])!=date.getFullYear()||parseInt(aDate[1]-1)!=date.getMonth()||parseInt(aDate[0])!=date.getDate());
				break;
			}
			if ((bRe&&!re.exec(value.toLowerCase()))||bFail) aErrors[aErrors.length] = [o.schema[i].field,o.schema[i].message.validation];
			else if (o.schema[i].maxlength&&(value.length>o.schema[i].maxlength)) aErrors[aErrors.length] = [o.schema[i].field,o.schema[i].message.maxlength];
		}
		if (o.schema[i].validation!='custom') if (o.form.classes.valid!=null) oForm[o.schema[i].field].className = o.form.classes.valid;
	}
	if (aErrors.length) { var message = o.hud.message; for (var i=0; i < aErrors.length; i++) { message += aErrors[i][1]; if (aErrors[i][0]!=null) if (o.form.classes.error!=null) oForm[aErrors[i][0]].className = o.form.classes.error; } if (aErrors[0][0]!=null) oForm[aErrors[0][0]].focus(); oDiv.className = o.hud.classes.error; oDiv.innerHTML = message; if (typeof o.hud.onError == 'function') o.hud.onError.apply(this,[aErrors]); return false; }
	else { oDiv.className = o.hud.classes.valid; if (typeof o.hud.onValid == 'function') { eval(o.hud.onValid)(); } return true; }//o.hud.onValid.call();
};

/*
	--------------------------------------------------------------------------------------------------------------
	@package:  Form Toolkit
	@author:     Michael Walsh
	@date:        29-06-2006
	@version:    1.0
	--------------------------------------------------------------------------------------------------------------
	MSIE 5.0+
	Firefox 1.07+
	Netscape 6.0+
	Opera 7.0+
	--------------------------------------------------------------------------------------------------------------
*/

/*
	@method:   Form toolkit.
	@version:   1.0
*/
Forms = 
{
	_version:1.0,
	confirmAction:function(s,url)
	{
		if (confirm(s)) window.location = url; else return false;
	},
	confirmCheck:function(o,s)
	{
		if (!o.checked) return false;
		if (!confirm(s)) o.checked = false;
	},
	confirmUncheck:function(o,s)
	{
		if (o.checked) return false;
		if (!confirm(s)) o.checked = true;
	},
	collateForm:function(oForm)
	{
		var o = [], i = 0;
		do
		{
			var item = oForm[i], add = true;
			switch (item.type)
			{
				case 'checkbox': if (!item.checked) add = false; break;
				case 'radio': if (!item.checked) add = false; break;
				case 'select-multiple': o[o.length] = {id:oForm[i].id,value:Forms.urlEncode(Forms.Select.getSelectedValues(oForm[i].id))}; add = false; break;
			}
			if (add) o[o.length] = {id:oForm[i].id,value:Forms.urlEncode(oForm[i].value)}; i++;
		}
		while (oForm[i]);
		return o;
	},
	submitToUrl:function(f,url)
	{
		var oForm = document.getElementById(f);
		oForm.action = url; oForm.submit();
	},
	submitWithValue:function(f,a,val)
	{
		var oForm = _DOM.get(f), oField = oForm[a];
		oField.value = val; oForm.submit();
	},
	urlEncode:function(s)
	{
		return escape(s).replace(/\+/g,"%2B").replace(/(\/)/g,"%2F").replace(/%26/g,"@AMP@");
	},
	wordsRemaining:function(id,text,count)
	{
		var oDest = get(id), oSource = get(text), remaining = (count-oSource.value.length);
		if (remaining<0) { oSource.value = oSource.value.substring(0, count); remaining = 0; }
		oDest.value = remaining;
	},
	Select:
	{
		_version:1.0,
		get:function(sId)
		{
			return (document.getElementById) ? document.getElementById(sId) : document.all[sId];
		},
		getCount:function(sId)
		{
			var oSelect = this.get(sId);
			return oSelect.options.length;
		},
		getSelected:function(sId)
		{
			var oSelect = this.get(sId), aIndexs = [], j = 0;
			for (var i = 0; i < oSelect.options.length; i++) { if (oSelect.options[i].selected) { aIndexs[j] = i; j++; } }
			return aIndexs;
		},
		getSelectedValues:function(sId)
		{
			var oSelect = this.get(sId), aIndexs = [], j = 0;
			for (var i = 0; i < oSelect.options.length; i++) { if (oSelect.options[i].selected) { aIndexs[j] = oSelect.options[i].value; j++; } }
			return aIndexs;
		},
		getSelectedText:function(sId)
		{
			var oSelect = this.get(sId), aIndexs = [], j = 0;
			for (var i = 0; i < oSelect.options.length; i++) { if (oSelect.options[i].selected) { aIndexs[j] = oSelect.options[i].text; j++; } }
			return aIndexs;
		},
		jumpUrlWithAttributes:function(o)
		{
			var qstring = '?';
			if (typeof o.query!='undefined') for (var i=0;i<o.query.length;i++) { if (typeof getQueryVariable(o.query[i])!='undefined') qstring += ((i==0)?'':'&')+o.query[i]+'='+getQueryVariable(o.query[i]); }
			if (typeof o.attributes!='undefined') for (var i=0;i<o.attributes.length;i++) qstring += ((i==0&&!o.query.length)?'':'&')+o.attributes[i];
			window.location = window.location.pathname+qstring;
		},
		selectAt:function(sId,n)
		{
			var oSelect = this.get(sId);
			if (oSelect == null || oSelect.options == null) return false;
			oSelect.options[n].selected = true;
		},
		selectAll:function(sId)
		{
			var oSelect = this.get(sId);
			if (oSelect == null || oSelect.options == null) return false;
			for (var i = 0; i < oSelect.options.length; i++) oSelect.options[i].selected = true;
		},
		selectByValue:function(sId,sValue)
		{
			var oSelect = this.get(sId);
			if (oSelect == null || oSelect.options == null) return false;
			for (var i = 0; i < oSelect.options.length; i++) if (oSelect.options[i].value == sValue) oSelect.options[i].selected = true;
		},
		deselectAll:function(sId)
		{
			var oSelect = this.get(sId);
			if (oSelect == null || oSelect.options == null) return false;
			for (var i = 0; i < oSelect.options.length; i++) oSelect.options[i].selected = false;
		},
		add:function(sId,sName,sValue)
		{
			var oSelect = this.get(sId), oOption = document.createElement("option");
			oOption.appendChild(document.createTextNode(sName,sValue));
			if (arguments.length == 3) oOption.setAttribute("value",sValue);
			oSelect.appendChild(oOption);
		},
		remove:function(sId,nIndex)
		{
			var oSelect = this.get(sId);
			oSelect.remove(nIndex);
		},
		purge:function(sId)
		{
			var oSelect = this.get(sId);
			for (var i = oSelect.options.length-1; i >= 0; i--) this.remove(sId,i);
		},
		copy:function(sId,sTarget,oConfig)
		{
			if (oConfig == null) var oConfig = {};
			var bMove = (oConfig.move) ? oConfig.move : false;
			var bTrim = (oConfig.trim) ? oConfig.trim : false;
			var oSelect = this.get(sId), oTarget = this.get(sTarget), nIndex = this.getSelected(sId)
			for (var i = 0; i < nIndex.length; i++) { var element = oSelect.options[nIndex[i]]; if (bMove) { oTarget.appendChild(element) } else { this.add(sTarget,  (bTrim) ? element.text.trim() : element.text, element.value); } } // element.disabled = true; element.selected = false;
		},
		extract:function(sId)
		{
			var oSelect = this.get(sId), nIndex = this.getSelected(sId);
			for (var i = nIndex.length-1; i >= 0; i--) { this.remove(sId,nIndex[i]); }
		},
		populate:function(sId,aOptions,oConfig)
		{
			if (oConfig == null) var oConfig = {};
			var bClear = (oConfig.purge) ? oConfig.purge : false;
			var bTrim = (oConfig.trim) ? oConfig.trim : false;
			var oSelect = this.get(sId);
			if (bClear) this.purge(sId);
			for (var i = 0; i < aOptions.length; i++) { if (typeof aOptions[i]!='string') this.add(sId, (bTrim) ? aOptions[i][1].trim() : aOptions[i][1], aOptions[i][0]); else this.add(sId, (bTrim) ? aOptions[i].trim() : aOptions[i], aOptions[i]); }
		},
		shiftUp:function (sId)
		{
			var oSelect = this.get(sId), nIndex = this.getSelected(sId);
			if (nIndex[0] == 0 || nIndex.length == 0 || nIndex.length > 1) return;
			var oOption = oSelect.options[nIndex[0]], oShiftOption = oSelect.options[nIndex[0]-1];
			oSelect.insertBefore(oOption, oShiftOption);
		},
		shiftDown:function (sId)
		{
			var oSelect = this.get(sId), nIndex = this.getSelected(sId);
			if (nIndex[0] == oSelect.options.length-1 || nIndex.length == 0 || nIndex.length > 1) return;
			var oOption = oSelect.options[nIndex[0]], oShiftOption = oSelect.options[nIndex[0]+1];
			oSelect.insertBefore(oShiftOption, oOption);
		}
	}
};