var DoSearch = Class.create();
DoSearch.prototype = {
	initialize: function(options){
		this.options = {
			formID: 'AdvancedSearchForm',
			waitTillOnload:true
		}
		Object.extend(this.options, options || {})
		
		this.elements = {}
		this.submitUrl = '';
		if( this.options.waitTillOnload )
			Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
		else
			this.onLoad( null );
	},
	
	onLoad: function(event){
		this.bindEvents();
	},
	
	bindEvents: function (){
		this.elements.form = $(this.options.formID);
		this.submitUrl = this.elements.form.attributes['action'].nodeValue +'/';
		
		this.submitUrl = this.submitUrl.replace( "searchPost", "view" );
		
		Event.observe(this.elements.form, 'submit', this.submit.bindAsEventListener(this))
	},
	
	submit: function(event){
		Event.stop(event);
		
		var propTypeString = '';
		
		$A(this.elements.form.getElements()).each(
			function(iter){
				if(iter.name != null && iter.name != ''){
					if(iter.className.indexOf('SelectAll') >= 0 && iter.options.length > 0){
						var values = false;
						for(var i=0; i < iter.options.length; i++){
							if(iter.options[i].value == '' || iter.options[i].value == ' ' || iter.options[i].value == null){
								continue;
							}
							if(!values){
								values = iter.options[i].value;
							} else {
								values += ","+ iter.options[i].value;
							}
						}
						if( values != ''  )
							this.submitUrl += iter.name +'/'+ values +'/';							
					}
					else if (iter.type == 'checkbox') {
						if(iter.name == "Property_Type"){
							if(propTypeString != ''){
								if(iter.getValue() != null && iter.getValue() != '' && iter.getValue() != false && iter.getValue() != false){
									propTypeString += ',' + iter.getValue();
								}
							} else {
								if(iter.getValue() != null && iter.getValue() != '' && iter.getValue() != false && iter.getValue() != false){
									propTypeString += '/Property_Type/' + iter.getValue();
								}
							}							
						} else {
							var values = iter.getValue();
							if(values != null && values != '' && values != ' '){
								if(typeof(values) == 'string'){
									this.submitUrl += iter.name +'/'+ values +'/'; 
								} else {
									this.submitUrl += iter.name +'/'+ $A(values).join(',') +'/'; 
								}
							}							
						}
					} else {						
						var values = iter.getValue();
						if(values != null && values != '' && values != ' ' && values != 'Min Price' && values != 'Max Price'){
							if(typeof(values) == 'string'){
								this.submitUrl += iter.name +'/'+ values +'/'; 
							} else {
								this.submitUrl += iter.name +'/'+ $A(values).join(',') +'/'; 
							}
						}
					}
				}
			}.bind(this)
		)
		this.submitUrl = this.submitUrl.replace('//', '/');
		if(propTypeString != false) {
			this.submitUrl += propTypeString + '/'; 	
		}		
		document.location = this.submitUrl;
	}
}

DoSearch.HookQuickSearch = function(){
	var quick_forms = $$("form.Quick");
	if( quick_forms.length > 0 )
		new DoSearch( {formID: quick_forms[0], waitTillOnload:false } );
}

Event.observe( window, 'load', DoSearch.HookQuickSearch );
var DoSearch = Class.create();
DoSearch.prototype = {
	initialize: function(options){
		this.options = {
			formID: 'AdvancedSearchForm',
			waitTillOnload:true
		}
		Object.extend(this.options, options || {})
		
		this.elements = {}
		this.submitUrl = '';
		if( this.options.waitTillOnload )
			Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
		else
			this.onLoad( null );
	},
	
	onLoad: function(event){
		this.bindEvents();
	},
	
	bindEvents: function (){
		this.elements.form = $(this.options.formID);
		this.submitUrl = this.elements.form.attributes['action'].nodeValue +'/';
		
		this.submitUrl = this.submitUrl.replace( "searchPost", "view" );
		
		Event.observe(this.elements.form, 'submit', this.submit.bindAsEventListener(this))
	},
	
	submit: function(event){
		Event.stop(event);
		
		var propTypeString = '';
		
		$A(this.elements.form.getElements()).each(
			function(iter){
				if(iter.name != null && iter.name != ''){
					if(iter.className.indexOf('SelectAll') >= 0 && iter.options.length > 0){
						var values = false;
						for(var i=0; i < iter.options.length; i++){
							if(iter.options[i].value == '' || iter.options[i].value == ' ' || iter.options[i].value == null){
								continue;
							}
							if(!values){
								values = iter.options[i].value;
							} else {
								values += ","+ iter.options[i].value;
							}
						}
						if( values != ''  )
							this.submitUrl += iter.name +'/'+ values +'/';							
					}
					else if (iter.type == 'checkbox') {
						if(iter.name == "Property_Type"){
							if(propTypeString != ''){
								if(iter.getValue() != null && iter.getValue() != '' && iter.getValue() != false && iter.getValue() != false){
									propTypeString += ',' + iter.getValue();
								}
							} else {
								if(iter.getValue() != null && iter.getValue() != '' && iter.getValue() != false && iter.getValue() != false){
									propTypeString += '/Property_Type/' + iter.getValue();
								}
							}							
						} else {
							var values = iter.getValue();
							if(values != null && values != '' && values != ' '){
								if(typeof(values) == 'string'){
									this.submitUrl += iter.name +'/'+ values +'/'; 
								} else {
									this.submitUrl += iter.name +'/'+ $A(values).join(',') +'/'; 
								}
							}							
						}
					} else {						
						var values = iter.getValue();
						if(values != null && values != '' && values != ' ' && values != 'Min Price' && values != 'Max Price' && values != 'Search by MLS# or Address'){
							if(typeof(values) == 'string'){
								this.submitUrl += iter.name +'/'+ values +'/'; 
							} else {
								this.submitUrl += iter.name +'/'+ $A(values).join(',') +'/'; 
							}
						}
					}
				}
			}.bind(this)
		)
		this.submitUrl = this.submitUrl.replace('//', '/');
		if(propTypeString != false) {
			this.submitUrl += propTypeString + '/'; 	
		}		
		document.location = this.submitUrl;
	}
}

DoSearch.HookQuickSearch = function(){
	var quick_forms = $$("form.Quick");
	if( quick_forms.length > 0 )
		new DoSearch( {formID: quick_forms[0], waitTillOnload:false } );
}

Event.observe( window, 'load', DoSearch.HookQuickSearch );
