﻿// Instantiate Namespace if it doesn't alreay exist
if(typeof(CFWI)==='undefined'||!CFWI){
	var CFWI={};	
}
// Add converter to convert string values to their number representations
String.prototype.toNumber=function(){
	/* 
		Purpose:	To convert a string to Numeric representation of the String
					Strips all white space and non-digit characters, with the exception of the decimal.
					e.g. "300.3" to 300.3, "$425.00" to 425.00, "100%" to 1.00 (or it's decimal equivalent);
					Intentionally Limited & Narrow in Scope
		Returns:	Number (Integer|Decimal)
	*/
	var n=Number(this.replace(/[^\d.-]/g,''));
	if(this.match('%')!==null){
		n=(n/100);
	}
	return n;
};
String.prototype.format=function(format){
	var ri=this;
	if(typeof(format)!=='undefined'&&format!==null){
		format=format.toString();
		switch(format.toLowerCase()){
			case 'number':
				var n=this.split('.'),pInt,pDec,regex;
				pInt=n[0];
				pDec=n.length>1?'.'+n[1]:'';
				regex=/(\d+)(\d{3})/;
				while(regex.test(pInt)){
					pInt=pInt.replace(regex, '$1'+','+'$2');
				}
				ri=pInt+pDec;
				break;
			default:
				break;
		}
		return ri;
	}
};
CFWI.Controls=function(){
	return {
		Common:{
			//IsDefined
			IsDefined:function(element){
				var ri=false;
				if(typeof(element)!=='undefined'&&element!==null){
					ri=true;
				}
				return ri;
			},
			//TypeOf
			TypeOf:function(element){
				var C=CFWI.Controls.Common,ri='';
				if(C.IsDefined(element)){
					ri=typeof(element);
				}
				return ri;
			},
			//GetType
			GetType:function(element){
				var C=CFWI.Controls.Common,ri='';
				if(C.IsDefined(element)){
					if(element.type){
						ri=element.type;
					}
				}
				return ri;
			},
			//GetKeypressEventData
			GetKeypressEventData:function(e){
				var sender=e?e:null,key,keychar,ri=null;
				if(sender!==null){
					key=sender.keyCode||sender.which;
					if(!CFWI.Controls.Common.IsDefined(key)){
						key=sender.charCode;
					}
					keychar=String.fromCharCode(key);
					ri={ 'Key':key,'Character':keychar }
				}
				return ri;
			},
			Serialize:function(json){
				var ri = null;
				if(this.IsDefined(json)){
					var oJS = Sys.Serialization.JavaScriptSerializer;
					if(this.IsDefined(oJS)){
						ri = oJS.serialize(json);
					}
				}
				return ri;
			},
			Deserialize:function(json){
				var ri = null;
				if(this.IsDefined(json)){
					var oJSS = Sys.Serialization.JavaScriptSerializer;
					if(this.IsDefined(oJSS)){
						ri = oJSS.deserialize(json);
					}
				}
				return ri;
			},
			SetCookie:function(name, value, expires, path, domain, secure){
				document.cookie = (name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toUTCString() : "") + ("; path=/") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""));
			},
			GetCookie:function(name){
				var ri=null;
				if(typeof(name)!=='undefined'&&name!==null){
					var dc = document.cookie,prefix=name+"=",begin=dc.indexOf("; "+prefix),end=null;
					if (begin == -1){
						begin = dc.indexOf(prefix);
						if (begin == -1) return null;
					} else {
						begin += 2;
					}
					end = document.cookie.indexOf(";", begin);
					if (end == -1){
						end = dc.length;
					}
					ri=unescape(dc.substring(begin + prefix.length, end));
				}else{
					ri=null;
				}
				return ri;
			},
			DeleteCookie:function(name, path, domain){
				if (CFWI.Controls.Common.GetCookie(name)){ document.cookie = name + "=" + ("; path=/") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; }
			}
		}
	}
}();

CFWI.Controls.Grid=function(){
	var m_this=this;
	var m_grid=null;
	var m_gridcontainer=null,m_loadctrl=null,m_loadctrl=null,m_gridcontainercss=null,m_gridupdatecss=null,m_gridhidecss=null,m_gridsliderdisabledcss=null;
	var defined=CFWI.Controls.Common.IsDefined,oDE=Sys.UI.DomElement,m_data=null,m_pagesize=null,m_footerid=null;
	var m_pagesize=10,m_onpopulatestart=null,m_onpopulatecomplete=null,m_onclearstart=null,m_onclearcomplete=null;
	//if(m_grid===null||typeof(m_grid)==='undefined'){if(Error){throw new Error('Failed to Create CFWI.Controls.Grid Class. A valid instance of a ComponentArt Grid Control Client Object was not supplied');}}
	return {
		GetControl:function(){ return m_grid; },
		GetElement:function(){ return m_grid.get_element(); },
		GetContainer:function(){ return m_gridcontainer; },
		SetContainer:function(element){
			var container=element?(typeof(element)==='string'&&element.length>0)?$get(element):(typeof(element)==='object'&&element!==null)?element:null:null;
			if(container!==null){ m_gridcontainer = container; }
		},
		SetLoadingElement:function(element){
			var ctrl=element?(typeof(element)==='string'&&element.length>0)?$get(element):(typeof(element)==='object'&&element!==null)?element:null:null;
			if(ctrl!==null){ m_loadctrl = ctrl; }
		},
		GetData:function(){ return m_data; },
		SetData:function(dataset,deserialize){
			var b = typeof(deserialize)==='boolean'?deserialize:true; 
			var oJSS = CFWI.Controls.Common,D=null;
			if(defined(oJSS)){
				if(b){ D = oJSS.Deserialize(dataset); } else { D = dataset; }
				if(D.hasOwnProperty('d')){
					m_data = D.d;
				}else{
					m_data = D;
				}
			}
		},
		SetContainerCssClass:function(v){
			if(defined(v)&&v.length>0){
				if(typeof(v)==='string'){
					m_gridcontainercss = v;
				}else{
					m_gridcontainercss='cfwi-grid-container';
				}
			}else{
				m_gridcontainercss='cfwi-grid-container';
			}
		},
		SetUpdateCssClass:function(v){
			if(defined(v)&&v.length>0){
				if(typeof(v)==='string'){
					m_gridupdatecss = v;
				}else{
					m_gridupdatecss='update-grid';
				}
			}else{
				m_gridupdatecss='update-grid';
			}
		},
		SetHideCssClass:function(v){
			if(defined(v)&&v.length>0){
				if(typeof(v)==='string'){
					m_gridhidecss = v;
				}else{
					m_gridhidecss='hide-grid';
				}
			}else{
				m_gridhidecss='hide-grid';
			}
		},
		SetSliderDisabledCssClass:function(v){
			if(defined(v)&&v.length>0){
				if(typeof(v)==='string'){
					m_gridsliderdisabledcss = v;
				}else{
					m_gridsliderdisabledcss='slider-disabled';
				}
			}else{
				m_gridsliderdisabledcss='slider-disabled';
			}
		},
		PageSize:function(v){
			var n;
			if(defined(v)){
				if(typeof(v)==='number'){
					n = v;
				}else if(typeof(v)==='string'){
					n = v.toNumber();
				}
				if(defined(n)&&!isNaN(n)){
					m_pagesize = n;
				}
			}else{
				return m_pagesize;
			}
		},
		GetPageSize:function(){
			return m_pagesize;
		},
		SetPageSize:function(v){
			var n;
			if(defined(v)){
				if(typeof(v)==='string'){
					n=v.toNumber();	
				}else if(typeof(v)==='number'){
					n=v;
				}
			}
			if(!(isNaN(n))){
				m_pagesize = n;
				m_this.Populate();
			}
		},
		OnPageSizeChange:function(e){var ddl = e?e.target?e.target:null:null;if(ddl!==null){var v=ddl.options[ddl.selectedIndex].value;m_this.SetPageSize(v);}},
		OnPopulateStart:function(fn){if(typeof(fn)==='function'){m_onpopulatestart=fn;}},
		OnPopulateComplete:function(fn){if(typeof(fn)==='function'){m_onpopulatecomplete=fn;}},
		OnClearStart:function(fn){if(typeof(fn)==='function'){m_onclearstart=fn;}},
		OnClearComplete:function(fn){if(typeof(fn)==='function'){m_onclearcomplete=fn;}},
		ShowPaging:function(){
			var id=null,footer=null,td=null;
			if(typeof(m_grid)==='object'&&m_grid!==null){
				id=m_grid.get_id()+'_footer';
				footer=$get(id);
				if(footer!==null){
					if(footer.getElementsByTagName('table')[0]!==undefined){
						td=footer.getElementsByTagName('table')[0].getElementsByTagName('td')[0];
					}else{
						td=null;
					}
					if(td!==null){
						if(m_grid.get_pageCount()>1){
							oDE.removeCssClass(td,m_gridsliderdisabledcss);
						}else{
							oDE.addCssClass(td,m_gridsliderdisabledcss);
						}
					}
				}
			}
		},
		ShowLoading:function(b){
			var bounds;
			if(typeof(b)==='boolean'){
				if(m_grid!==null&&m_loadctrl!==null){
					bounds=oDE.getBounds(m_grid.get_element());
					if(b){
						m_loadctrl.style.width=bounds.width+'px';
						m_loadctrl.style.height=bounds.height+'px';
						m_loadctrl.style.display='block';
					}else{
						m_loadctrl.style.display='none';
						m_loadctrl.style.width='0px';
						m_loadctrl.style.height='0px';
					}
				}
			}
		},
		Show:function(){
			if(m_gridcontainer!==null){
				if(oDE.containsCssClass(m_gridcontainer,m_gridhidecss)){ oDE.removeCssClass(m_gridcontainer,m_gridhidecss); }
				if(oDE.containsCssClass(m_gridcontainer,m_gridupdatecss)){ oDE.removeCssClass(m_gridcontainer,m_gridupdatecss); }
			}
			m_this.ShowLoading(false);
			setTimeout(function(){m_this.ShowPaging();},100);
		},
		Hide:function(){
			if(m_gridcontainer!==null){
				if(!(oDE.containsCssClass(m_gridcontainer,m_gridhidecss))){
					oDE.addCssClass(m_gridcontainer,m_gridhidecss);
				}
				m_this.ShowLoading(false);
			}
		},
		Populate:function(b){
			var data=m_this.GetData(),ri=false,israndom=false;
			if(typeof(m_onpopulatestart)==='function'){ m_onpopulatestart(m_grid); }
			if(typeof(b)==='boolean'){israndom=b;}
			if(typeof(data)==='object'&&data!==null){
				if(israndom){data.sort(function(){return (0.5 - Math.random());});}
				m_grid.beginUpdate();
				m_grid.get_table().clearData();
				m_grid.set_pageSize(m_pagesize);
				m_grid.load(data);
				m_grid.page(0);
				m_grid.endUpdate();
				m_grid.get_element().getElementsByTagName('tr')[0].getElementsByTagName('td')[0].style.height='auto';
				ri=true;
			}
			if(typeof(m_onpopulatecomplete)==='function'){ m_onpopulatecomplete(m_grid); }
			return ri;
		},
		Clear:function(){
			if(typeof(m_onclearstart)==='function'){ m_onclearstart(m_grid); }
			m_grid.beginUpdate();
			m_grid.get_table().clearData();
			m_grid.endUpdate();
			m_grid.get_element().getElementsByTagName('tr')[0].getElementsByTagName('td')[0].style.height='auto';
			if(typeof(m_onclearcomplete)==='function'){ m_onclearcomplete(m_grid); }
		},
		Create:function(control){
			if(defined(control)&&typeof(control)==='object'){
				m_this=this;
				m_grid=control;
				if(m_gridupdatecss===null){ m_this.SetUpdateCssClass(); }
				if(m_gridhidecss===null){ m_this.SetHideCssClass(); }
				if(m_gridcontainercss===null){ m_this.SetContainerCssClass(); }
				if(m_gridsliderdisabledcss===null){ m_this.SetSliderDisabledCssClass(); }
				if(defined(oDE)){ oDE.addCssClass(m_gridcontainer,m_gridcontainercss); }
				m_this.ShowPaging();
			}else{
				throw new Error('Failed to Create A New CFWI.Controls.Grid Object. A valid instance of a ComponentArt Grid Control Client Object was not supplied');
			}
		}
	}
}
		
CFWI.Controls.UI=function(){
	return {
		Validators:{
			TestKey:function(e,sender,regexp){
				if(!regexp.test(sender.Character)){
					if(sender.Key===36||sender.Key===46){
						return sender.Key; 
					}else{
						if(e.preventDefault){e.preventDefault();}
						e.returnValue = false;
						return false;
					}
				}
			},
			TextBox:{
				LimitTo:{
					ReadOnly:function(e){
						if(e){
							if(e.preventDefault){e.preventDefault();}
							e.returnValue=false;
							return false;
						}
					},
					Alpha:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/[a-zA-Z\-\']|\0|[\b]|\t/;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					},
					Name:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/[a-zA-Z\-\']|\0|[\b]|\t|\s/;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					},
					AlphaNumeric:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/[\w\-\']|\s|\0|[\b]|\t/;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					},
					Integer:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/\d|\0|[\b]|\t/;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					},
					Decimal:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/\d|\.|\0|[\b]|\t/;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					},
					Email:function(e){
						var sender=e?CFWI.Controls.Common.GetKeypressEventData(e):null,regexp;
						if(sender!==null){
							regexp=/((^[^\.])([\w\W-]+)([^\.])@([^\.])(?:[\w\W]+)([a-zA-Z]{2,4}))/g;
							return CFWI.Controls.UI.Validators.TestKey(e,sender,regexp);
						}
					}
				},
				Email:function(e,isevent){
					var sender = e?e.target?e.target:e:null,regexp,msg;
					if(typeof(isevent)==='undefined'||isevent===null){isevent=true;}
					if(sender!==null){
						regexp=/((^[^\.])([\w\W-]+)([^\.])@([^\.])(?:[\w\W]+)([a-zA-Z]{2,4}))/g;
						if(sender.value&&sender.value.length>0){
							if(!regexp.test(sender.value)){
								msg='The email address you entered appears to be invalid.\r\nPlease double-check your entry and make sure it is properly formatted.\r\nExample: test@cfwebmasters.com';
								if(isevent){alert(msg);}
								return false;
							}
						}
					}
				},
				Price:function(e,isevent){
					var sender = e?e.target?e.target:e:null,val;
					if(typeof(isevent)==='undefined'||isevent===null){isevent=true;}
					if(sender!==null){
						val = sender.value.toNumber();
						if(val.toString().length>0&&val!==0){
							sender.value=CFWI.Controls.UI.Formatting.TextBox.Price(e);
						}else{
							sender.value='';
						}
					}
				}
			}
		},
		Formatting:{
			TextBox:{
				Phone:function(e){
					var sender = e?e.target?e.target:e:null,s,val;
					if(sender!==null){
						s=sender.value?sender.value:null;
						if(s!==null){
							if(s.length < 7){
								return false;
							}else{
								if(s.length===7){
									val='{0}-{1}';
									val=val.replace('{0}',s.substr(0,3)).replace('{1}',s.substr(3,4));
								}else if(s.length===10){
									val='({0}) {1}-{2}';
									val=val.replace('{0}',s.substr(0,3)).replace('{1}',s.substr(3,3)).replace('{2}',s.substr(6,4));
								}else{
									val = s;
								}
								sender.value = s;
								return true;
							}
						}
					}
				},
				Currency:function(e,dec){
					var sender = e?e.target?e.target:e:null,val,sign,cents,includedecimal=true;
					if(typeof(dec)==='boolean'){includedecimal=dec;}
					if(sender!==null){
						val=sender.value?sender.value:null;
						if(val!==null){
							val=val.toNumber();
							if(isNaN(val)){ val=0; }
							sign=(val==(val=Math.abs(val)));
							val=Math.floor(val*100+0.50000000001);
							cents=val%100;
							val=Math.floor(val/100).toString();
							if(cents<10)cents="0"+cents;
							for(var i=0;i<Math.floor((val.length-(1+i))/3); i++){
								val=val.substring(0,val.length-(4*i+3))+','+val.substring(val.length-(4*i+3));
							}
							if(includedecimal){
								return(((sign)?'':'-')+'$'+val+'.'+cents);
							}else{
								return(((sign)?'':'-')+'$'+val);
							}
						}
					}
				},
				Price:function(e){
					return CFWI.Controls.UI.Formatting.TextBox.Currency(e,false);
				},
				Integer:function(e){
					var sender = e?e.target?e.target:e:null,val,regexp=/(^-?\d\d*$)/,ri=false;
					if(sender!==null){
						val=sender.value?sender.value:null;
						if(val!==null){
							if(val.length>0){
								val=val.toNumber();
								if(regexp.test(val)){
									if(val===0){ sender.value=''; }else{ sender.value=val; }
								}else{
									sender.value='';
								}
								ri=true;
							}
						}
					}
					return ri;
				}
			}
		}
	}
}();

CFWI.Controls.Search=function(){
	var m_querystring=''
	return {
		UsePrefix:function(){
			var m_prefixes;
			return {
				GetPrefix:function(){
					return m_prefixes;
				},
				SetPrefix:function(p){
					//expects object prefixes
					//ex: {'checkbox':['cbxCL_','cbxSS_'],'radiobutton':['rdo']}
					var C = CFWI.Controls.Common,ri=false;
					if(C.IsDefined(p)){
						if(C.TypeOf(p)==='object'){
							m_prefixes = p;
							ri = true;
						}
					}
					return ri;
				}
			}
		},
		GetParamName:function(id,find,exclude){
			var ri=null,found='',skip;
			if(typeof(id)==='string'){
				if((typeof(exclude)!=='string')&&(typeof(exclude)!=='undefined'&&exclude!==null)){
					if(exclude.length>0){
						for(var i=0,L=exclude.length;i<L;i++){
							var ex=exclude[i].toLowerCase();
							if(id.toLowerCase().indexOf(ex)>-1){
								found=ex;
								break;
							}
						}
						if(found.length>0){
							find=null;
						}
					}
				}else if(typeof(exclude)==='string'){
					if(id.toLowerCase().indexOf(exclude.toLowerCase())>-1){
						find=null;
					}
				}
				if((typeof(find)!=='string')&&(typeof(find)!=='undefined')&&(find!==null)){
					if(find.length>0){
						for(var i=0;i<find.length;i++){
							var f=find[i].toLowerCase();
							if(id.toLowerCase().indexOf(f)>-1){
								found=f;
								break;
							}
						}
						if(found.length>0){
							find=found;
						}	
					}
				}
				if(typeof(find)==='string'){
					if(id.toLowerCase().indexOf(find.toLowerCase())>-1){
						ri=id.substring((id.toLowerCase().indexOf(find.toLowerCase())+(find.length)),id.length);
					}
				}else if(find===null){
					ri=null;
				}else{
					ri=id;
				}
			}
			return ri;
		},
		BuildQueryStringFromForm:function(collection){
			//collection should be an object with two required properties: type:input|select|textarea, items:HTMLCollection AND two optional properties: prefixes:{},exempt:{}
			/*
				ex: { 
					'type':'input',
					'items':HTMLInputElementCollection,
					'prefixes':{'checkbox':['cbxCL_','cbxSS_'],'radiobutton':['rdo']},
					'exclude':{'checkbox':['cbxCL_','%cbxSS_']} 
				}
			*/
			var C = CFWI.Controls.Common;
			var S = CFWI.Controls.Search;
			var m_querystring='',ri=false,list=null,li=null,param=null,t,p,prefix;
			if(C.IsDefined(collection)){
				if(C.TypeOf(collection)==='object'){
					if(collection.type&&C.TypeOf(collection.type)==='string'){
						t=collection.type.toLowerCase();
						list=collection.items;
						p=collection.prefixes?collection.prefixes:null;
						ex=collection.exclude?collection.exclude:null;
						if(list.length>0){
							for(var i=0;i<list.length;i++){
								li=list[i];
								switch(t){
									case 'input':
										if(li.type==='text'&&li.value.length>0){
											exclude=ex?ex.text?ex.text:null:null;
											prefix=p?p.text?p.text:null:null;
											if(prefix!==null){
												param=S.GetParamName(li.id,prefix,exclude);
											}else{
												param=S.GetParamName(li.id,'txt',exclude);
											}
											if(param!==null){ 
												val=li.value;
												if(li.id.toLowerCase().indexOf('price')>-1){
													val=li.value.toNumber();
												}
												param+='='+val;
											}
										}else if(li.type==='hidden'&&li.value.length>0){
											exclude=ex?ex.hidden?ex.hidden:null:null;
											prefix=p?p.hidden?p.hidden:null:null;
											if(prefix!==null){
												param=S.GetParamName(li.id,prefix,exclude);
											}else{
												param=S.GetParamName(li.id,'txt',exclude);
											}
											if(param!==null){ 
												val=li.value;
												if(li.id.toLowerCase().indexOf('price')>-1){
													val=li.value.toNumber();
												}
												param+='='+val;
											}
										}else if(li.type==='checkbox'&&li.checked){
											exclude=ex?ex.checkbox?ex.checkbox:null:null;
											prefix=p?p.checkbox?p.checkbox:null:null;
											if(prefix!==null){
												param=S.GetParamName(li.id,prefix,exclude);
											}else{
												param=S.GetParamName(li.id,'cbx',exclude);
											}
											if(param!==null){ param+='='+li.value; }
										
										}else if(li.type==='radio'&&li.checked){
											exclude=ex?ex.radio?ex.radio:null:null;
											prefix=p?p.radio?p.radio:null:null;
											if(prefix!==null){
												param=S.GetParamName(li.id,prefix,exclude);
											}else{
												param=S.GetParamName(li.id,'rdo',exclude);
											}
											if(param!==null){ param+='='+li.value; }
										}
										break;
									case 'select':
										if(li.type==='select-one'&&li.options[li.selectedIndex].value.length > 0){
											exclude=ex?ex.select-one?ex.select-one:null:null;
											prefix=p?p.select-one?p.select-one:null:null;
											if(prefix!==null){
												param=S.GetParamName(li.id,prefix,exclude);
											}else{
												param=S.GetParamName(li.id,'ddl',exclude);
											}
											if(param!==null){ param+='='+li.options[li.selectedIndex].value; }
										}else if(li.type==='select-multiple'){
										
										}
										break;
									case 'textarea':
										if(li.id.indexOf('txt')>-1 && li.value.length > 0){
											param=S.GetParamName(li.id,'txt');
											if(param!==null){ param+='='+li.value; }
										}
										break;
									default:
										break;
								}
								if(typeof(param)!=='undefined'&&param!==null){
									if(m_querystring.length===0){
										m_querystring=param;
									}else{
										m_querystring+='&'+param;
									}
								}
								param=null;
							}
						}
						S.SetQueryString(m_querystring);
						ri = true;
					}
				}
			}
			return ri;
		},
		GetQueryString:function(){
			return m_querystring;
		},
		SetQueryString:function(qs){
			if(typeof(qs)==='string'&&qs.length>0){
				if(m_querystring.length===0){
					m_querystring=qs;
				}else{
					m_querystring+='&'+qs;
				}
			}
		},
		ResetQueryString:function(){
			m_querystring='';
			return m_querystring;
		},
		GetCount:function(querystring,webservice){
			if(typeof(querystring)==='string'&&typeof(webservice)==='object'){
				var oWS = webservice.Reference();
				if(oWS){
					oWS.GetListingCount(querystring, webservice.OnCompletedEventHandler(), webservice.OnErrorEventHandler(), webservice.OnTimeOutEventHandler());
				}
			}
		}
	}
}();

CFWI.Controls.Category=function(element,title,link,label,cssclass,height){
	var m_container=element?(typeof(element)==='string')?$get(element):(typeof(element)==='object'&&element!==null)?element:null:null;
	var m_title=title?(typeof(title)==='string')?$get(title):(typeof(title)==='object'&&title!==null)?title:null:null;
	var m_link=link?(typeof(link)==='string')?$get(link):(typeof(link)==='object'&&link!==null)?link:null:null;
	var m_label=label?(typeof(label)==='string')?$get(label):(typeof(label)==='object'&&label!==null)?label:null:null;
	var m_cssclass=cssclass?(typeof(cssclass)==='string'&&cssclass.length>0)?cssclass:'expanded':'expanded';
	var m_height=height?(typeof(height)==='number')?height:(typeof(height)==='string'&&height.length>0)?height.toNumber():parseInt(height):40;
	var m_target=null,m_listcontainer=null,m_collection=[],m_activeitemcss=null,m_this=null,m_changehandler=null,m_selectall=null,m_deselectall=null,oDE=Sys.UI.DomElement;
	var m_selectedcategories='';
	if(Error){if(m_container===null){throw new Error('Failed to Create Category Class: No HTMLDomElement was found matching the provided \'element\' parameter [string elementID|HTMLDomElement]');}}
	function m_returnobject(element){
		var ri=element?(typeof(element)==='string')?$get(element):(typeof(element)==='object'&&element!==null)?element:null:null;
		return ri;
	}
	function m_checkboxclick(e){
		var ri=null,sender=e?e.target?e.target:(typeof(e)==='object'&&e.type==='checkbox')?e:null:null,target=m_target,value='',v;
		if(sender!==null&&sender.type==='checkbox'){
			v=sender.value.toString();
			if(target){
				m_this.UpdateList(target,v,sender.checked);
				if(sender.checked){
					if(!(oDE.containsCssClass(sender.parentNode,m_activeitemcss))){
						oDE.addCssClass(sender.parentNode,m_activeitemcss);
					}
				}else{
					if(oDE.containsCssClass(sender.parentNode,m_activeitemcss)){
						oDE.removeCssClass(sender.parentNode,m_activeitemcss);
					}
				}
				ri=target;
			}
		}
		return ri;
	}
	function m_toggleall(checked){
		var target=null;
		if(typeof(checked)==='boolean'){
			for(var i=0,L=m_collection.length;i<L;i++){
				var cbx=(m_collection[i].type==='checkbox')?m_collection[i]:null;
				if(cbx!==null){
					cbx.checked=checked;
					target=m_checkboxclick(cbx);
				}
			}
		}
		return target;
	}
	function m_load(){
		for(var i=0,L=m_collection.length;i<L;i++){
			var cbx=(m_collection[i].type==='checkbox')?m_collection[i]:null;
			if(cbx!==null){
				if(cbx.checked){
					if(!(oDE.containsCssClass(cbx.parentNode,m_activeitemcss))){
						oDE.addCssClass(cbx.parentNode,m_activeitemcss);
					}
				}
			}
		}
	}
	function m_raiseclickevent(e){
		var target=m_checkboxclick(e);
		if(target!==null){
			m_this.Change(target);
		}
	}
	function m_raiseselectallevent(e){
		var target=m_toggleall(true);
		if(target!==null){ m_this.Change(target); }
		if(e.preventDefault){ e.preventDefault(); }
		if(e.returnValue){ e.returnValue=false; }
		return false;
	}
	function m_raisedeselectallevent(e){
		var target=m_toggleall(false);
		if(target!==null){ m_this.Change(target); }
		if(e.preventDefault){ e.preventDefault(); }
		if(e.returnValue){ e.returnValue=false; }
		return false;
	}
	return {
		GetElement:function(){
			return m_container;
		},
		GetSelectedCategoryNames:function(){
			return m_selectedcategories;
		},
		GetListContainer:function(){
			return m_listcontainer;
		},
		SetListContainer:function(element){
			m_listcontainer=m_returnobject(element);
		},
		SetActiveItemCssClass:function(cssclass){
			m_activeitemcss = cssclass?(typeof(cssclass)==='string')?cssclass:'item-active':'item-active';
		},
		SetTargetTextbox:function(element){
			m_target=m_returnobject(element);
		},
		SetChangeEventHandler:function(fn){
			if(typeof(fn)==='function'){
				m_changehandler=fn;
			}
		},
		SetSelectAllLink:function(element){
			m_selectall=m_returnobject(element);
			if(m_selectall!==null){
				if($addHandler){
					$addHandler(m_selectall,'click',m_raiseselectallevent);
				}
			}
		},
		SetDeselectAllLink:function(element){
			m_deselectall=m_returnobject(element);
			if(m_deselectall!==null){
				if($addHandler){
					$addHandler(m_deselectall,'click',m_raisedeselectallevent);
				}
			}
		},
		UpdateList:function(target,val,checked){
			if((typeof(target)==='object'&&target!==null)&&(typeof(val)==='string')&&typeof(checked)==='boolean'){
				var v=val,value=target.value;
				if(value.length===0){
					if(checked){
						value=v;
					}
				}else if(value.length>0){
					if(value.match(v)===null){
						if(checked){
							value+='|'+v;
						}
					}else{
						if(!checked){
							if(value.indexOf('|'+v)>-1){
								value=value.replace(('|'+v),'');
							}else if(value.indexOf(v+'|')>-1){
								value=value.replace((v+'|'),'');
							}else if(value.indexOf(v)>-1){
								value=value.replace(v,'');
							}
						}
					}
				}
				target.value=value;
			}
		},
		Click:function(e){
			if(m_link!==null&&m_label!==null){
				mode=(oDE.containsCssClass(m_link,m_cssclass));
				if(mode){
					//m_container.style.height=m_height+'px';
					m_label.innerHTML='expand';
					oDE.removeCssClass(m_link,m_cssclass);
					oDE.removeCssClass(m_container,m_cssclass);
				}else{
					//m_container.style.height='auto';
					m_label.innerHTML='collapse';
					oDE.addCssClass(m_link,m_cssclass);
					oDE.addCssClass(m_container,m_cssclass);
				}
			}else{
				if(m_listcontainer!==null&&m_listcontainer.style.display==='none'){
					var cbx=null,count=0;
					if(m_collection!==null&&m_collection.length>0){
						for(var i=0,L=m_collection.length;i<L;i++){
							cbx=m_collection[i];
							if(cbx.checked){count++;}
						}
						if(m_collection.length===count){
							m_raisedeselectallevent(e);
						}else{
							m_raiseselectallevent(e);
						}
					}
				}
			}
			if(e.preventDefault){e.preventDefault();}
			if(e.returnValue){e.returnValue=false;}
			return false;
		},
		Change:function(element){
			var sender=element?typeof(element)==='object'?element:null:null;
			if(sender!==null&&typeof(m_changehandler)==='function'){
				m_changehandler();
			}else{
				if(Error){throw new Error('Missing Event Handler. You Must Specify the Change Event Handler using Instance.SetChangeEventHandler(functionreference);')}
			}
		},
		Create:function(){
			m_this = this;
			var collection=null;
			if(m_listcontainer!==null){
				collection=(m_listcontainer.getElementsByTagName)?m_listcontainer.getElementsByTagName('input'):null;
				if(collection!==null&&collection.length>0){
					for(var i=0,L=collection.length;i<L;i++){
						if(collection[i].type==='checkbox'){
							if($addHandler){
								$addHandler(collection[i],'click',m_raiseclickevent);
							}
							m_collection.push(collection[i]);
						}
					}
				}
			}
			if(m_title!==null){
				if($addHandler){
					$addHandler(m_title,'click',m_this.Click);
				}
			}
		},
		Load:function(){
			m_load();
		}
	}
}

CFWI.Controls.Form=function(element){
	var m_container=element?(typeof(element)==='string')?$get(element):(typeof(element)==='object'&&element!==null)?element:null:null;
	if(m_container===null){if(typeof(element)==='string'){m_container = document.getElementById(element);}}
	var m_inputs=null,m_selects=null,m_textareas=null,m_populated=false,m_collection=[];
	function m_combinecollections(){
		if(m_inputs!==null&&m_inputs.length>0){	for(var i=0;i<m_inputs.length;i++){ m_collection.push(m_inputs[i]); }}
		if(m_selects!==null&&m_selects.length>0){ for(var i=0;i<m_selects.length;i++){ m_collection.push(m_selects[i]); }}
		if(m_textareas!==null&&m_textareas.length>0){ for(var i=0;i<m_textareas.length;i++){ m_collection.push(m_textareas[i]); }}
		return m_collection;
	}
	return {
		GetElement:function(){
			return m_container;
		},
		GetFields:function(t){
			var m_type=t?(typeof(t)==='string')?t.toLowerCase():'all':'',ri=null;
			switch(t){
				case 'input':
					ri = m_inputs;
					break;
				case 'select':
					ri = m_selects;
					break;
				case 'textarea':
					ri = m_textareas;
					break;
				case 'all':
					ri = { 'all':true,'inputs':m_inputs,'selects':m_selects,'textareas':m_textareas };
				default:
					ri = m_combinecollections();
					break;
			}
			return ri;
		},
		GetFieldsByInputType:function(t){
			var m_type=t?(typeof(t)==='string')?t.toLowerCase():null:null,m_item=null,collection=[];
			if(m_type===null){
				throw new Error('Parameter Mismatch: GetFieldsByInputType requires parameter: string t (where t="select-one"|"checkbox", etc.)');
			}else{
				var m_inputtypes='text|checkbox|radio|hidden|password|file|button|submit|image';
				var m_selecttypes='select-one|select-multiple';
				var m_textareatypes='textarea';
				if(m_inputtypes.match(t)!==null){
					if(m_inputs!==null&&m_inputs.length>0){
						for(var i=0,L=m_inputs.length;i<L;i++){
							m_item=m_inputs[i];
							if(m_item.type===t){
								collection.push(m_item);
							}
						}
					}
				}else if(m_selecttypes.match(t)!==null){
					if(m_selects!==null&&m_selects.length>0){
						for(var i=0,L=m_selects.length;i<L;i++){
							m_item=m_selects[i];
							if(m_item.type===t){
								collection.push(m_item);
							}
						}
					}
				}else if(m_textareatypes.match(t)!==null){
					if(m_textareas!==null&&m_textareas.length>0){
						for(var i=0,L=m_textareas.length;i<L;i++){
							m_item=m_textareas[i];
							if(m_item.type===t){
								collection.push(m_item);
							}
						}
					}
				}
				if(collection!==null&&collection.length===0){
					throw new Error('No Matches Were Found. Available options are: text|checkbox|radio|hidden|password|file|button|submit|image|select-one|select-multiple|textarea');
				}
				return collection;
			}
		},
		SetFields:function(collection){
			var C = CFWI.Controls.Common,ri=false;
			if(C.IsDefined(collection)){
				if(C.TypeOf(collection)==='object'){
					// expects an object with key:[collection] pair.
					// ex: { 'inputs':[HTMLTextBox,HTMLTextBox,HTMLTextBox],'selects':[HTMLSelect,HTMLSelect] }
					m_inputs = collection.inputs?collection.inputs:null;
					m_selects = collection.selects?collection.selects:null;
					m_textareas = collection.textareas?collection.textareas:null;
					m_populated = true;
					ri = true;
				}
			}
			return ri;
		},
		Init:function(){
			var ri=false,oC=this.GetElement();
			if(!m_populated){
				m_inputs = oC.getElementsByTagName('input');
				m_selects = oC.getElementsByTagName('select');
				m_textareas = oC.getElementsByTagName('textarea');
				m_populated = true;
				ri = true;
			}
			return ri;
		}
	}
};

CFWI.Controls.WebService=function(){
	var C=CFWI.Controls.Common,m_webservice,m_start,m_completed,m_timedout,m_error,m_cancel=false,m_context=null;
	return {
		Reference:function(webservice){
			if(C.IsDefined(webservice)){
				if(C.TypeOf(webservice)==='function'){
					m_webservice = webservice;
				}
			}else{
				return m_webservice;
			}
		},
		GetContext:function(){
			return m_context;
		},
		SetContext:function(context){
			if(typeof(context)!=='undefined'){
				m_context = context;
			}
		},
		Cancel:function(b){
			if(C.IsDefined(b)){
				if(typeof(b)==='boolean'){
					m_cancel = b;
				}
			}else{
				return m_cancel;
			}
		},
		OnStartEventHandler:function(handler){
			if(C.IsDefined(handler)){
				if(C.TypeOf(handler)==='function'){
					m_start = handler;
				}
			}else{
				return m_start;
			}
		},
		OnCompletedEventHandler:function(handler){
			if(C.IsDefined(handler)){
				if(C.TypeOf(handler)==='function'){
					m_completed = handler;
				}
			}else{
				return m_completed;
			}
		},
		OnErrorEventHandler:function(handler){
			if(C.IsDefined(handler)){
				if(C.TypeOf(handler)==='function'){
					m_error = handler;
				}
			}else{
				return m_error;
			}
		},
		OnTimeOutEventHandler:function(handler){
			if(C.IsDefined(handler)){
				if(C.TypeOf(handler)==='function'){
					m_timedout = handler;
				}
			}else{
				return m_timedout;
			}
		}
	}
};

CFWI.Controls.QueryString=function(){
	var m_qs='';
	return {
		GetQueryString:function(){
			return m_qs;
		},
		SetQueryString:function(qs){
			if(typeof(qs)==='string'){
				m_qs = qs;
			}
		},
		AddParameter:function(param){
			if(m_qs.length===0){
				m_qs = param;
			}else{
				m_qs+='&'+param;
			}
		},
		GetParameters:function(){
			return m_qs;
		},
		GetQueryStringAsList:function(){
			var qs=m_qs.split('&'),qsitem,querystring={};
			for(var i=0;i<qs.length;i++){
				qsitem=qs[i].split("=");
				if(qsitem.length===2){
					querystring[qsitem[0]]=qsitem[1];
				}
			}
			return querystring;
		}
	}
};

CFWI.Controls.IDX=function(){
	return {
		Search:function(element){
		
		}
	}
}();