/*
** Validator
** Idea and basics by greyMagic Software.
*/
var Validator={
	templates:{
		user:[ /^[\w\-\., ]+$/,"letters, numbers, spaces, dashes, underscores and periods" ],
		email:[ /^[\w\-\.]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/,"a valid e-mail address" ],
		numeric:[ /^-?\d+$/,"digits" ],
		"float":[ /^\d+(\.\d+)?$/,"float number" ],
		name:[ /^[a-z ]+$/i,"letters/spaces" ],
		url:[ /^(https?|ftp):\/\/([\w\-]+\.)+\w{2,4}\/?.*$/,"a valid url address" ],
		date:[ /^(((0?[13578]|1[02])\/([0-2]?\d|3[01])|(0?[469]|11)\/([0-2]?\d|30)|0?2\/([01]?\d|2[0-8]))\/(19|20)?\d{2}|0?2\/([01]?\d|2[0-9])\/(19|20)?([024568][048]|[13579][26]))$/,"a valid date in mm/dd/yyyy format" ]
	},

	check:function (oForm,bAlert) {
		var oInp,
			oEl,
			sType,
			sErr="",
			sCurrErr="";

		this.checkboxes=[];

		for (var iInp=0;iInp<oForm.length;iInp++) {
			oInp=oForm[iInp];
			if (oInp.getAttribute("desc")==null) {
				try {
					oEl=oInp.parentNode.previousSibling;
					while (oEl.nodeName.toLowerCase()!="label" && oEl.previousSibling) oEl=oEl.previousSibling;
					oInp.setAttribute("desc",oEl.innerHTML);
				}
				catch (oErr) {}
			}

			if (oInp.type && !oInp.disabled && (sCurrErr=this.validator[this.types[oInp.type]](oInp))) sErr+=sCurrErr;
		}

		if (sErr) {
			//sErr="The following errors occured:\n"+sErr;
			if (bAlert) {
				alert(sErr);
				return false;
			}
			return sErr.replace(/^\r?\n/g,"").replace(/\n\*/g,"<li>");
		}

		return bAlert ? true : "";
	},

	checkboxes:[],

	types:{
		"text":0,
		"hidden":0,
		"textarea":0,
		"password":0,
		"file":0,
		"submit":0,
		"button":0,
		"radio":1,
		"checkbox":2,
		"select-one":3,
		"select-multiple":3
	},

	validator:[
		// text
		function (oInp) {
			if (oInp.getAttribute("template") && !Validator.templates[oInp.getAttribute("template")]) throw new Error("No such template '"+oInp.getAttribute("template")+"'");

			if (oInp.getAttribute("require")!=null && !oInp.value) return "\n* '"+oInp.getAttribute("desc")+"' is a required field.";

			if (oInp.getAttribute("template")!=null && oInp.value && !Validator.templates[oInp.getAttribute("template")][0].test(oInp.value)) return "\n* '"+oInp.getAttribute("desc")+"' should contain only "+Validator.templates[oInp.getAttribute("template")][1]+".";

			if (oInp.getAttribute("min")!=null || oInp.getAttribute("max")!=null) {
				if (oInp.getAttribute("template")=="numeric" || oInp.getAttribute("template")=="float") {
					if (
						oInp.value
						&&
						(
							(oInp.getAttribute("max") && +oInp.value>oInp.getAttribute("max"))
							||
							(oInp.getAttribute("min") && +oInp.value<oInp.getAttribute("min"))
						)
					) {
						return "\n* '"+oInp.getAttribute("desc")+"' should be "+
							(oInp.getAttribute("min") ? " over "+oInp.getAttribute("min") : "")+
							(oInp.getAttribute("min") && oInp.getAttribute("max")? " and " : "")+
							(oInp.getAttribute("max") ? " under "+oInp.getAttribute("max") : "")+
							".";
					}
				}
				else if (+oInp.value.length>oInp.getAttribute("max") || +oInp.value.length<oInp.getAttribute("min")) {
					return "\n* '"+oInp.getAttribute("desc")+"' length should be "+
						(oInp.getAttribute("min") ? " over "+oInp.getAttribute("min") : "")+
						(oInp.getAttribute("min") && oInp.getAttribute("max")? " and " : "")+
						(oInp.getAttribute("max") ? " under "+oInp.getAttribute("max") : "")+
						".";
				}
 			}

			if (oInp.getAttribute("rx")!=null && oInp.getAttribute("rxerror")!=null && !new RegExp(oInp.getAttribute("rx"),oInp.getAttribute("rxflags")).test(oInp.value)) return "\n* '"+oInp.getAttribute("desc")+"' "+oInp.getAttribute("rxerror");
		},

		// radio
		function (oInp) {
			var oRadios,
				iRd=0;

			if (oInp.getAttribute("require")!=null) {
				oRadios=oInp.form[oInp.name];

				if (!oRadios) return "";

				while (iRd<oRadios.length && !oRadios[iRd].checked) iRd++;

				if (iRd==oRadios.length) return "\n* '"+oInp.getAttribute("desc")+"'  is a required field, please select one.";
			}
		},

		// checkbox
		function (oInp) {
			var oChecks=oInp.form[oInp.name],
				iChecked=0,
				aErrors=[];

			if (Validator.checkboxes.indexOf(oChecks)>-1) return "";
			Validator.checkboxes.push(oChecks);

			if (!oChecks) return "";

			if (oChecks.length && (oInp.getAttribute("min")!=null || oInp.getAttribute("max")!=null)) {
				if (oInp.getAttribute("min")==null) {
					if (oInp.getAttribute("require")!=null) oInp.setAttribute("min",1);
					else oInp.setAttribute("min",0);
				}
				if (oInp.getAttribute("max")==null) oInp.setAttribute("max",oChecks.length);

				if (oInp.getAttribute("min")!=null && oInp.getAttribute("max")!=null) {
					oChecks=oInp.form[oInp.name];

					if (oChecks.length) {
						for (var iChk=0;iChk<oChecks.length;iChk++) if (oChecks[iChk].checked) iChecked++;
					}
					else iChecked=oInp.checked;

					if (iChecked<oInp.getAttribute("min") || iChecked>oInp.getAttribute("max")) {
						if (oInp.getAttribute("min")>0) aErrors.push("at least "+oInp.getAttribute("min")+" selected option"+(oInp.getAttribute("min")>1 ? "s" : ""));
						if (oInp.getAttribute("max")<oChecks.length) aErrors.push("at most "+oInp.getAttribute("max")+" selected option"+(oInp.getAttribute("max")>1 ? "s" : ""));

						return "\n* '"+oInp.getAttribute("desc")+"' should contain "+aErrors.join(" and ")+".";

						//"at least "+oInp.getAttribute("min")+" selected option"+(() ? "s" : "") +" and at most "+oInp.getAttribute("max")+" selected options.";
					}
				}
			}
			else if (!oInp.checked && oInp.getAttribute("require")!=null) return "\n* "+(oInp.getAttribute("error") ? oInp.getAttribute("error") : "'"+oInp.getAttribute("desc")+"' is a required field.");
		},

		// select
		function (oInp) {
			var iSelected=0,
				iOpt=0;

			if (oInp.multiple) {
				if (oInp.getAttribute("min")==null) oInp.setAttribute("min",1);
				if (oInp.getAttribute("max")==null) oInp.setAttribute("max",oInp.length);

				for (var iOpt=0;iOpt<oInp.options.length;iOpt++) {
					if (oInp.options[iOpt].selected) {
						if (oInp.getAttribute("invalid")!=null && iOpt==+oInp.getAttribute("invalid")) return "\n* '"+oInp.getAttribute("desc")+"' "+(oInp.getAttribute("require") ? "has no selected option or " : "")+"contains a disallowed selected option.";
						iSelected++;
					}
				}

				if (iSelected<+oInp.getAttribute("min") || iSelected>+oInp.getAttribute("max")) return "\n* '"+oInp.getAttribute("desc")+"' should contain at least "+oInp.getAttribute("min")+" selected options and at most "+oInp.getAttribute("max")+" selected options."
			}
			else {
				if (oInp.getAttribute("require") && oInp.getAttribute("invalid")==null) oInp.setAttribute("invalid","0");
				if (oInp.getAttribute("invalid")!=null && oInp.selectedIndex==+oInp.getAttribute("invalid")) return "\n* '"+oInp.getAttribute("desc")+"' "+(oInp.getAttribute("require") || oInp.getAttribute("invalid")=="0" ? "has no selected option." : "contains a disallowed selected option.");
			}
		}
	],

	// Textarea

	// onkeypress="return Validator.limit(this)"
	limit:function (oTA) {
		if (+oTA.value.length==oTA.getAttribute("maxlength")) return false;
	},

	// onkeyup="return Validator.count(this)"
	count:function (oTA,oCnt) {
		if (oTA.value.length>+oTA.getAttribute("maxlength")) oTA.value=oTA.value.substring(0,+oTA.getAttribute("maxlength"));
		if (oCnt) oCnt.innerText=oTA.getAttribute("maxlength")-oTA.value.length;
	}
}

/*
** QueryString
** Copyright by greyMagic Software.
*/

var QS={
	length:0,
	data:[],

	get:function (sVar) {
		return this.data[(sVar+"").toLowerCase()];
	},

	init:function () {
		if (location.href) {
			var sData=location.href.split("?")[1],
				sVar,
				rxNum=/^(\-)?\d+(\.\d+)?$/;

			if (sData) sData=sData.split("&");
			else return;

			for (var curVar=0;curVar<sData.length;curVar++) {
				sVar=sData[curVar].split("=");
				sVar[1]=!sVar[1] ? "" : unescape(sVar[1]);
				if (rxNum.test(sVar[1])) sVar[1]*=1;
				this.data[sVar[0].toLowerCase()]=sVar[1];
				this.data[curVar]={
					name:sVar[0],
					value:sVar[1]
				};

				this.length++;
			}
		}
	}

	//QS.init();QS.get("a")
}

/*
** HashString
*/

var HS={
	add:function (a,b) {
		location.href=location.href.replace(new RegExp("(?:#("+a+"\=(?:[^#])*))|[#]$|$","i"),"#"+a+"="+b);
	},

	remove:function (a) {	
		location.href=location.href.replace(new RegExp("#("+a+"\=(?:[^#])*)","i"),"");
	},

	get:function (a) {
		return new RegExp("#"+a+"\=([^#]*)","i").test(location.href) ? RegExp.$1 : null;
	}
}

/* Cookies */

var Cookies={
	get:function(sName) {
		if (DOM.doc.cookie && new RegExp("\\b"+sName+"=([^;]*)").test(DOM.doc.cookie)) return unescape(RegExp.$1);
	},

	set:function(sName,sValue) {
		var dDate=new Date();
		dDate.setTime(dDate.getTime()+365*24*3600*1000);

		DOM.doc.cookie=sName+"="+escape(sValue)+"; expires="+dDate.toGMTString();
		DOM.doc.cookie=sName+"="+escape(sValue)+"; path=/; expires="+dDate.toGMTString();
	},

	del:function(sName) {
		if (this.get(sName)) DOM.doc.cookie=sName+"=; expires=Thu, 01-Jan-70 00:00:01 UTC";
	}
}

/* Common */

var Common={
	pop:function (sURL,iWidth,iHeight) {
		return open(sURL,sURL.substring(1,sURL.indexOf(".")).replace(/\//g,"_"),"status=1,scrollbars=1,resizable=0,menubar=0,toolbar=0,location=0,width="+iWidth+",height="+iHeight+",left="+(screen.width/2-iWidth/2)+",top="+(screen.height/2-iHeight/2));
	},

	flash:function (sURL,iWidth,iHeight,sBGColor) {
		DOM.doc.write("\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"200\" height=\"55\">\n\t<param name=\"movie\" value=\""+sURL+"\" />\n\t<param name=\"quality\" value=\"high\" />\n\t<param name=\"bgcolor\" value=\""+sBGColor+"\" />\n\t<embed src=\""+sURL+"\" FlashVars=\"gatewayURL=/cfusion/flashservices/gateway&amp;cr=/cfusion&amp;loc=en_us\" quality=\"high\" bgcolor=\""+sBGColor+"\" width=\"200\" height=\"55\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>\n</object>\n");
	}
}

/* Other */

function Sel(sName,sDesc,aOpts,bReq,bMult,iSize) {
	this.name=sName;
	this.desc=sDesc;
	this.opts=aOpts;
	this.req=bReq;
	this.mult=bMult;
	this.size=iSize;
}

Sel.prototype.getStr=function () {
	var sSel="<select name=\""+this.name+"\" "+(this.req ? "require=\"true\" desc=\""+this.desc+"\" invalid=\"0\" " : "")+(this.mult ? " multiple=\"true\" size=\""+(this.size || 3)+"\" " : "")+">"+(!this.mult ? "<option value=\"\"></option>" : "");

	for (var iOpt=0;iOpt<this.opts.length;iOpt++) sSel+="<option value=\""+this.opts[iOpt][0]+"\">"+this.opts[iOpt][1]+"</option>";

	sSel+="</select>";

	return sSel;
}


function copy(sText) {
	if (Browser.moz) {
		var oTA=DOM.create("textarea");
		document.body.appendChild(oTA);
		oTA.value=sText;
	}
	else clipboardData.setData("text",sText);
}
