
/*
	--------------------------------------------------------------------------------------------------------------
	@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(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()||aDate[0]!=date.getDate());
				break;
			}
			if ((bRe&&!re.exec(value.toString().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]].id!=undefined) 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();
};

/*
	@method:   Global div display helpers.
	@version:   1.0
*/
function showMessage()
{
	_DOM.setStyle(oValidationConstruct.hud.element,"display","block");
	var a = (arguments.length) ? ((typeof arguments[0]=='number')? arguments[0] : arguments[0].length) : [''];
	var o = { height: { to: ((_browser=='MSIE')?((a*18)+35):((a*15)+10)) }, opacity: { to: 1 } }; // (a*((_browser=='MSIE')?25:15))+
	var tween = new YAHOO.util.Anim(oValidationConstruct.hud.element, o, 1, YAHOO.util.Easing.backOut);
	window.location.hash="pagetop";
	tween.animate(); 
};
function hideMessage()
{
	var o = { height: { to: 0 }, opacity: { to: 0 } };  
	var tween = new YAHOO.util.Anim(oValidationConstruct.hud.element, o, ((_browser=='MSIE')?1:0.5), YAHOO.util.Easing.backIn);
	tween.animate();
	tween.onComplete.subscribe(function(){_DOM.setStyle(oValidationConstruct.hud.element,"display","none");})
};

/*
	--------------------------------------------------------------------------------------------------------------
	@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)
	{
        var ascii = [/%u25CF/g,/%u2013/g,/%u2014/g,/%u2122/g,/%u203A/g,/%u2039/g,/%u2030/g,/%u2021/g,/%u2020/g,/%u201E/g,/%u201D/g,/%u201C/g,/%u201A/g,/%u2019/g,/%u2018/g,/%C6/g,/%C1/g,/%C2/g,/%C0/g,/%C5/g,/%C3/g,/%C4/g,/%C7/g,/%D0/g,/%C9/g,/%CA/g,/%C8/g,/%CB/g,/%CD/g,/%CE/g,/%CC/g,/%CF/g,/%D1/g,/%D3/g,/%D4/g,/%D2/g,/%D8/g,/%D5/g,/%D6/g,/%DE/g,/%DA/g,/%DB/g,/%D9/g,/%DC/g,/%DD/g,/%E1/g,/%E2/g,/%E6/g,/%E0/g,/%E5/g,/%E3/g,/%E4/g,/%E7/g,/%E9/g,/%EA/g,/%E8/g,/%F0/g,/%EB/g,/%ED/g,/%EE/g,/%EC/g,/%EF/g,/%F1/g,/%F3/g,/%F4/g,/%F2/g,/%F8/g,/%F5/g,/%F6/g,/%DF/g,/%FE/g,/%FA/g,/%FB/g,/%F9/g,/%FC/g,/%FD/g,/%FF/g,/%A1/g,/%A3/g,/%A4/g,/%A5/g,/%A6/g,/%A7/g,/%A8/g,/%A9/g,/%AA/g,/%AB/g,/%AC/g,/%AD/g,/%AE/g,/%AF/g,/%B0/g,/%B1/g,/%B2/g,/%B3/g,/%B4/g,/%B5/g,/%B6/g,/%B7/g,/%B8/g,/%B9/g,/%BA/g,/%BB/g,/%BC/g,/%BD/g,/%BE/g,/%BF/g,/%D7/g,/%F7/g,/%A2/g,/%3E/g,/%3C/g,/(\/)/g,/\+/g,/%26/g,/\&/g];
        var html = ["@AMP@#9679;","&ndash;","&mdash;","&trade;","&rsaquo;","&lsaquo;","&permil;","&Dagger;","&dagger;","&bdquo;","&rdquo;","&ldquo;","&sbquo;","&rsquo;","&lsquo;","&AElig;","&Aacute;","&Acirc;","&Agrave;","&Aring;","&Atilde;","&Auml;","&Ccedil;","&ETH;","&Eacute;","&Ecirc;","&Egrave;","&Euml;","&Iacute;","&Icirc;","&Igrave;","&Iuml;","&Ntilde;","&Oacute;","&Ocirc;","&Ograve;","&Oslash;","&Otilde;","&Ouml;","&THORN;","&Uacute;","&Ucirc;","&Ugrave;","&Uuml;","&Yacute;","&aacute;","&acirc;","&aelig;","&agrave;","&aring;","&atilde;","&auml;","&ccedil;","&eacute;","&ecirc;","&egrave;","&eth;","&euml;","&iacute;","&icirc;","&igrave;","&iuml;","&ntilde;","&oacute;","&ocirc;","&ograve;","&oslash;","&otilde;","&ouml;","&szlig;","&thorn;","&uacute;","&ucirc;","&ugrave;","&uuml;","&yacute;","&yuml;","&iexcl;","&pound;","&curren;","&yen;","&brvbar;","&sect;","&uml;","&copy;","&ordf;","&laquo;","&not;","&shy;","&reg;","&macr;","&deg;","&plusmn;","&sup2;","&sup3;","&acute;","&micro;","&para;","&middot;","&cedil;","&sup1;","&ordm;","&raquo;","&frac14;","&frac12;","&frac34;","&iquest;","&times;","&divide;","&cent;","@GT@","@LT@","%2F","%2B","@AMP@","@AMP@"];
        var escaped = escape(s);
        for (var i=0; i<ascii.length; i++) escaped = escaped.replace(ascii[i],html[i]);
        return escaped;
	},
	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);
		}
	}
};