﻿if(typeof(CFWI)==='undefined'||!CFWI){
	var CFWI={};	
}
CFWI.Controls={
	Common:function(){
		var m_webservice,m_completed,m_timedout,m_error,m_params;
		return {
			GetParameters:function(){
				return m_params;
			},
			SetParameters:function(params){
				if(typeof(params)!=='undefined'&&params!==null){
					m_params=params;
				}
			},
			BuildParameters:function(c){
				var m_container=c?(typeof(c)==='string')?$get(c):(typeof(c)==='object'&&c!==null)?c:null:null;
				var aI,aS,aT,item,qs='';
				function m_ParseParamName(id,val){
					var ri=null;
					if(typeof(id)!=='undefined'&&id!==null){
						if(id.indexOf(val)>-1){
							ri=id.substring((id.indexOf(val)+(val.length)),id.length);
						}
					}
					return ri;
				}
				function m_BuildParams(collection,T,prefix){
					var ri=false,li=null,param=null,t=T?T.toString().toLowerCase():null,p=prefix?prefix:'cbx';
					if(t!==null){
						if(typeof(collection)!=='undefined'&&collection!==null){
							if(collection.length > 0){
								for(var i=0;i<collection.length;i++){
									li=collection[i];
									if(!(li.disabled)){
									
										switch(t){
											case 'input':
												if(li.type==='text'){
													if(li.id.indexOf('txt')>-1 && li.value.length > 0){
														param=m_ParseParamName(li.id,'txt');
														if(param!==null){ 
															val=li.value;
															if(li.id.toLowerCase().indexOf('price')>-1){
																val=li.value.replace(/\$/g,'').replace(/,/g,'');
															}
															param+='='+val;
														}
													}
												}else if(li.type==='checkbox'){
													if(li.id.toLowerCase().indexOf('lfcbx')===-1){
														if(li.id.indexOf(p)>-1 && li.checked){
															param=m_ParseParamName(li.id,p);
															if(li.value=='on'){li.value=true;}
															if(param!==null){ param+='='+li.value; }
														}
													}
												}else if(li.type==='radio'){
												
												}
												break;
											case 'select':
												if(li.type==='select-one'){
													if(li.id.toLowerCase().indexOf('showimages')===-1){
														if(li.id.indexOf('ddl')>-1 && (li.options[li.selectedIndex].value!==''&&li.options[li.selectedIndex].value>'0')){
															param=m_ParseParamName(li.id,'ddl');
															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=m_ParseParamName(li.id,'txt');
													var __id=li.id.toLowerCase(),__value='';
													if(__id.indexOf('mlsnum')>-1||__id.indexOf('postal')>-1){
														__value=li.value.split('\n').join('|').replace(/,/g,'|').replace(/\s/g,'|').replace(/\t/g,'|').replace(/;/g,'|').replace(/\|\|/g,'|');
														if(__value.lastIndexOf('|')===__value.length-1){
															__value=__value.substr(0,__value.lastIndexOf('|'));
														}
														param+='='+__value;
													}else{
														if(param!==null){ param+='='+li.value; }
													}
												}
												break;
										}
									}
									if(param!==null){
										if(qs.length===0){
											qs=param;
										}else{
											qs+='&'+param;
										}
									}
									param=null;
								}
							}
							ri = true;
						}
					}
					return ri;
				}
				if(m_container!==null&&typeof(m_container)!=='undefined'){
					aI=m_container.getElementsByTagName('input');
					aS=m_container.getElementsByTagName('select');
					aT=m_container.getElementsByTagName('textarea');
					m_BuildParams(aI,'input');
					m_BuildParams(aS,'select');
					m_BuildParams(aT,'textarea');
					this.SetParameters(qs);
				}
			},
			GetWebService:function(){
				return m_webservice;
			},
			SetWebService:function(wsref){
				if(typeof(wsref)==='function'){
					m_webservice = wsref;
				}
			},
			GetCount:function(c){
				var m_container=c?(typeof(c)==='string')?$get(c):(typeof(c)==='object'&&c!==null)?c:null:null;
				var aI,aS,aT,item,qs='';
				function m_ParseParamName(id,val){
					var ri=null;
					if(typeof(id)!=='undefined'&&id!==null){
						if(id.indexOf(val)>-1){
							ri=id.substring((id.indexOf(val)+(val.length)),id.length);
						}
					}
					return ri;
				}
				function m_BuildParams(collection,T,prefix){
					var ri=false,li=null,param=null,t=T?T.toString().toLowerCase():null,p=prefix?prefix:'cbx';
					if(t!==null){
						if(typeof(collection)!=='undefined'&&collection!==null){
							if(collection.length > 0){
								for(var i=0;i<collection.length;i++){
									li=collection[i];
									if(!(li.disabled)){
									
										switch(t){
											case 'input':
												if(li.type==='text'){
													if(li.id.indexOf('txt')>-1 && li.value.length > 0){
														param=m_ParseParamName(li.id,'txt');
														if(param!==null){ 
															val=li.value;
															if(li.id.toLowerCase().indexOf('price')>-1){
																val=li.value.replace(/\$/g,'').replace(/,/g,'');
															}
															param+='='+val;
														}
													}
												}else if(li.type==='checkbox'){
													if(li.id.toLowerCase().indexOf('lfcbx')===-1){
														if(li.id.indexOf(p)>-1 && li.checked){
															param=m_ParseParamName(li.id,p);
															if(li.value=='on'){li.value=true;}
															if(param!==null){ param+='='+li.value; }
														}
													}
												}else if(li.type==='radio'){
												
												}
												break;
											case 'select':
												if(li.type==='select-one'){
													if(li.id.toLowerCase().indexOf('showimages')===-1){
														if(li.id.indexOf('ddl')>-1 && (li.options[li.selectedIndex].value!==''&&li.options[li.selectedIndex].value>0)){
															param=m_ParseParamName(li.id,'ddl');
															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=m_ParseParamName(li.id,'txt');
													var __id=li.id.toLowerCase(),__value='';
													if(__id.indexOf('mlsnum')>-1||__id.indexOf('postal')>-1){
														__value=li.value.split('\n').join('|').replace(/,/g,'|').replace(/\s/g,'|').replace(/\t/g,'|').replace(/;/g,'|').replace(/\|\|/g,'|');
														if(__value.lastIndexOf('|')===__value.length-1){
															__value=__value.substr(0,__value.lastIndexOf('|'));
														}
														param+='='+__value;
													}else{
														if(param!==null){ param+='='+li.value; }
													}
												}
												break;
										}
									}
									if(param!==null){
										if(qs.length===0){
											qs=param;
										}else{
											qs+='&'+param;
										}
									}
									param=null;
								}
							}
							ri = true;
						}
					}
					return ri;
				}
				if(m_container!==null&&typeof(m_container)!=='undefined'){
					aI=m_container.getElementsByTagName('input');
					aS=m_container.getElementsByTagName('select');
					aT=m_container.getElementsByTagName('textarea');
					m_BuildParams(aI,'input');
					m_BuildParams(aS,'select');
					m_BuildParams(aT,'textarea');
					this.SetParameters(qs);
					var oWS = this.GetWebService();
					oWS.GetListingCount(qs, CFWI_GetCount_Completed, CFWI_GetCount_Error, CFWI_GetCount_TimedOut);	
				}
			}
		}
	}(),
	//Requires jQuery 1.4.2
	MultiListBox:function(origin,target){
		if(typeof(jQuery)!=='undefined'&&window.jQuery){
			var $a=(typeof(origin)==='string'&&typeof(origin)!=='undefined'&&origin.length>0)?jQuery(origin):[];
			var $b=(typeof(target)==='string'&&typeof(target)!=='undefined'&&target.length>0)?jQuery(target):[];
			var $c,$hf,$m,$a1,$aa,$r1,$ra,m_events=[],m_loadingcssclass='multilistbox-load';
			if($a.length>0&&$b.length>0){
				var arr=[],vals=[],$selected,$current,$item,results,m_lbx,m_disabled=false,m_limit=50,msg,m_searchType='',clearall=false;
				var limitmessage='<div class="alert"><span class="alert">Sorry...</span>Only {0} {items} may be selected for searching at one time.\r\nPlease narrow your list of selections by {1} {items} to continue.</div>';
				
				var m_move=function($from,$to,all){
					if(typeof(all)==='boolean'){clearall=all}else{clearall=false}
					if(arr.length>0){arr.length=0;}
					if(clearall){
						$selected=$from.find('option').remove();
					}else{
						$selected=$from.find('option:selected').remove();
					}
					$current=$to.find('option').remove();
					if($current.length>0){
						$current.each(function(){
							arr.push({text:this.text,val:this.value});
						});
					}
					if($selected.length>0){
						$selected.each(function(){
							arr.push({text:this.text,val:this.value});				
						});
					}
					if(arr.length>0){
						arr.sort(function(a,b){
							return a.text>b.text?1:a.text===b.text?0:-1;				  
						});
					}
					m_lbx=$to.get(0);
					m_lbx.options.length=0;
					for(var i=0,L=arr.length;i<L;i++){
						m_lbx.options[i]=new Option(arr[i].text,arr[i].val);
					}
					if($c.length>0&&$hf.length>0){
						if(vals.length>0){vals.length=0;}
						jQuery('option',$b).each(function(){
							vals.push(this.value);				  
						});
						$c.get(0).value=vals.join('|');
						$hf.get(0).value=$c.val();
					}
					$current=null;
					$selected=null;
					arr.length=0;
				}
				var m_maxreached=function(){
					var x=$a.find('option:selected').length;
					var y=$b.find('option').length;
					var ri=false;
					if(!(x+y>m_limit)){
						ri=false
					}else{
						var z=(x+y)-m_limit;
						msg=limitmessage.replace('{0}',m_limit).replace('{1}',z).replace(/\{items\}/g,m_searchType);
						ri=true;
					}
					return ri;
				}
				var m_showmessage=function(b){
					if(typeof(b)==='boolean'){
						if($m.length>0){
							var $h=$m.find('div.alert').outerHeight(true);
							if(b){
								$m.html(msg);
								$m.animate({
									height:$h,
									opacity:1
								},400);
							}else{
								setTimeout(function(){
									$m.animate({
										height:0,
										opacity:0
									},400,m_hidecomplete)
								},500);
							}
						}
					}
				}
				var m_hidecomplete=function(){
					$m.html('');
				}
				var SelectOne_Click=function(e){
					if(!(m_disabled)){
						if(!(m_maxreached())){
							m_move($a,$b);
							m_showmessage(false);
						}else{
							m_showmessage(true);
						}
					}
					if(e){
						if(e.preventDefault){e.preventDefault();}
						if(e.returnValue){e.returnValue=false;}
					}
					return false;
				}
				/*
				var SelectAll_Click=function(e){
					if(!(m_disabled)){
						if(!(m_maxreached())){
							m_move($a,$b,true);
							m_showmessage(false);
						}else{
							m_showmessage(true);
						}
					}
					if(e){
						if(e.preventDefault){e.preventDefault();}
						if(e.returnValue){e.returnValue=false;}
					}
					return false;
				}
				*/
				var RemoveAll_Click=function(e){
					if(!(m_disabled)){
						m_move($b,$a,true);
						if(!(m_maxreached())){
							m_showmessage(false);
						}else{
							m_showmessage(true);
						}
					}
					if(e){
						if(e.preventDefault){e.preventDefault();}
						if(e.returnValue){e.returnValue=false;}
					}
					return false;
				}
				var RemoveOne_Click=function(e){
					if(!(m_disabled)){
						m_move($b,$a);
						if(!(m_maxreached())){
							m_showmessage(false);
						}else{
							m_showmessage(true);
						}
					}
					if(e){
						if(e.preventDefault){e.preventDefault();}
						if(e.returnValue){e.returnValue=false;}
					}
					return false;
				}
				var Execute_CompletedEvents=function(){
					if(m_events.length>0){
						for(var i=0,L=m_events.length;i<L;i++){
							var fn=m_events[i];
							if(typeof(fn)==='function'){
								fn();
							}
						}
					}
				}
				var KeyUp=function(e){
					if(e.which&&(e.which===38||e.which===40)){
						if(!(m_maxreached())){
							m_showmessage(false);
						}else{
							m_showmessage(true);
						}
					}
				}
				var MouseUp=function(e){
					if(!(m_maxreached())){
						m_showmessage(false);
					}else{
						m_showmessage(true);
					}
				}
				if($a.length>0){
					$a.mouseup(function(e){
						MouseUp(e);
					}).keyup(function(e){
						KeyUp(e);
					})
				}
				return {
					TextBox:function(id){
						if(typeof(id)==='undefined'||id===null||(typeof(id)==='string'&&id.length===0)){
							return $c;
						}else{
							$c=(typeof(id)==='string'&&id.length>0)?jQuery(id):[];
						}
					},
					HiddenField:function(id){
						if(typeof(id)==='undefined'||id===null||(typeof(id)==='string'&&id.length===0)){
							return $hf;
						}else{
							$hf=(typeof(id)==='string'&&id.length>0)?jQuery(id):[];
						}
					},
					Buttons:function(b){
						if(typeof(b)==='object'&&b!==null){
							$a1=(b.SelectOne&&typeof(b.SelectOne)==='string')?jQuery(b.SelectOne):[];
							$r1=(b.RemoveOne&&typeof(b.RemoveOne)==='string')?jQuery(b.RemoveOne):[];
							//$aa=(b.SelectAll&&typeof(b.SelectAll)==='string')?jQuery(b.SelectAll):[];
							$ra=(b.RemoveAll&&typeof(b.RemoveAll)==='string')?jQuery(b.RemoveAll):[];
							if($a1.length>0){
								$a1.click(function(e){
									SelectOne_Click(e);
									Execute_CompletedEvents();
								});
							}
							if($r1.length>0){
								$r1.click(function(e){
									RemoveOne_Click(e);
									Execute_CompletedEvents();
								});
							}
							/*
							if($aa.length>0){
								$aa.click(function(e){
									SelectAll_Click(e);
								});
							}
							*/
							if($ra.length>0){
								$ra.click(function(e){
									RemoveAll_Click(e);
									Execute_CompletedEvents();
								});
							}
							
						}else{
							return {
								'SelectOne':$a1,
								'RemoveOne':$r1,
								//'SelectAll':$aa,
								'RemoveAll':$ra
							}
						}
					},
					MessageElement:function(id){
						if(typeof(id)==='undefined'||id===null||(typeof(id)==='string'&&id.length===0)){
							return $m;
						}else{
							$m=(typeof(id)==='string'&&id.length>0)?jQuery(id):[];
						}
					},
					Disable:function(b){
						if(typeof(b)==='boolean'){
							m_disabled=b;
							if($a.length>0&&$b.length>0){
								$a.get(0).disabled=m_disabled;
								$b.get(0).disabled=m_disabled;							
							}
							if($c.length>0){$c.get(0).disabled=m_disabled;}
						}
					},
					Limit:function(v){
						if(typeof(v)==='number'){
							m_limit=v;
						}else{
							return m_limit;
						}
					},
					SearchType:function(s){
						if(typeof(s)==='string'&&typeof(s)!=='undefined'&&s.length>0){
							m_searchType=s;
						}else{
							return m_searchType;
						}
					},
					AddCompletedEvent:function(fn){
						if(typeof(fn)==='function'){
							m_events.push(fn);
						}
					},
					Init:function(){
						if($hf.val().length > 0){
							try {
								var selected = '|'+$hf.val()+'|';
								var $options;
								if($a.length>0){
									$options=$a.find('option').each(function(){
										if(selected.indexOf('|'+this.value+'|')>-1){
											this.selected=true;
										}
									})
									this.Buttons().SelectOne.click();
								}
							}catch(ex){
								if(console){console.log(ex);}
								CFWI.Controls.Common.GetCount(m_container);
							}
						}
					}
				}
			}
		}
	},
	//Requires jQuery 1.4
	CheckBoxList:function(id){
		if(typeof(id)!=='undefined'&&(typeof(id)==='string'&&id.length>0)){
			if(typeof(jQuery)!=='undefined'&&window.jQuery){
				var $textbox=null;
				var $c=jQuery(id);
				return {
					TextBox:function(id){
						if(typeof(id)!=='undefined'&&(typeof(id)==='string'&&id.length>0)){
							$textbox=jQuery(id);
						}else{
							return $textbox;
						}
					},
					GetChecked:function(){
						var ri=null
						if($c.length>0){
							ri=$c.find('input:checkbox:checked');
						}
						return ri;
					},
					SetValue:function(){
						var $checked=this.GetChecked(),features='';
						if($textbox.length>0&&$checked!==null){
							features=$checked.map(function(){return this.value;}).toArray().join('|');
							$textbox.val(features);
						}
					},
					Init:function(){
						var Feature=this;
						if($c.length>0){
							$c.click(function(e){
								Feature.SetValue();
							});
							if($textbox!==null&&$textbox.length>0){
								if($textbox.val().length>0){
									var values='|'+$textbox.val()+'|';
									$c.find('input:checkbox').filter(function(){
										var ri=false
										if(values.indexOf('|'+this.value+'|')>-1){
											this.checked=true;
											ri=true;
										}
										return ri;
									})
								}
							}
						}
					}
				}
			}
		}
	},
	MultiListBoxCollection:[]
};
