function in_array(needle, haystack, strict) {
    var found = false, key;
	strict = !!strict;
     for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    } 
    return found;
}
  
  
var AutoSave = {};

AutoSave.Base = new Class({

	options: {
		minLength: 1,
		className: 'AutoSave',
		observerOptions: {}
	},

	initialize: function(el,options,type, target) {
		this.setOptions(options);
		this.element = $(el);
		this.target = target;
		this.build();
		this.type = type;
		this.observer = new Observer(this.element, type, this.prefetch.bind(this), $merge({
			delay: 400
		}, this.options.observerOptions));
		this.value = this.observer.value;
		this.queryValue = null;
	},


	build: function() {
		this.element.setProperty('autocomplete', 'off');
	},

	prefetch: function() {
		this.query();
	}

});

AutoSave.Base.implement(new Events);
AutoSave.Base.implement(new Options);

AutoSave.Ajax = {};

AutoSave.Ajax.Base = AutoSave.Base.extend({

	options: {
		postVar: 'value',
		postData: {},
		ajaxOptions: {},
		onRequest: Class.empty,
		onComplete: Class.empty
	},

	initialize: function(el, allinputfields, type, target, url, unhide, options) {
		this.parent(el, options, type, target);
		this.el = el;
		this.wholeform = allinputfields;
		this.unhide = unhide;
		this.ajax = new Ajax(url, $merge({
			autoCancel: true
		}, this.options.ajaxOptions));
		this.ajax.addEvent('onComplete', this.queryResponse.bind(this));
		this.ajax.addEvent('onFailure', this.queryResponse.bind(this, [false]));
		doUnhides(unhide,el);
	},

	query: function(){
		var data = $extend({}, this.options.postData);
		//alert(this.allow);
		for (attribsIndex in this.wholeform) {
			if(!isNaN(attribsIndex)) {
				//alert (attribsIndex+','+this.wholeform[attribsIndex].value);
				data[attribsIndex] = this.wholeform[attribsIndex].value;
			}
		}
		// hardcode the target info
		data['target'] = this.target;
		this.fireEvent('onRequest', [this.element, this.ajax]);
		//alert(this.unhide);
		doUnhides(this.unhide,this.el,this.cbox);
		this.ajax.request(data);
	},
	
	queryResponse: function(resp) {
		this.value = this.queryValue = this.element.value;
		this.selected = false;
		//this.hideAdvert();
		this.fireEvent(resp ? 'onComplete' : 'onFailure', [this.element, this.ajax], 20);
	}

});


	
function doUnhides(unhide,el) {
	//if(unhide='') { return; }
	if(unhide=='2') {
		if(el.name=='industry_sector') {
			if(el.value=='3') {
			  $('fadvisor').setStyle('display','block');
			}
			else{
			  $('fadvisor').setStyle('display','none');
			}
		}
	}
	// do the unhides based on choices
	var belong_to_network = $('belong_to_network').checked;
	if (belong_to_network) {
		key = 1;//el['value'];
		if(unhide[key]){
			toshow = unhide[key].toString();
		}
		else{
			toshow = '';
		}
	
		for (attribsIndex in unhide) {
			if(unhide[attribsIndex]==toshow) {
				reveals = toshow.split(",");
				for ( i=0; i < reveals.length; i++ ) {
					findme = reveals[i];				 
					//alert(findme);
					$(findme).setStyle('display','block');
					//reveal = document.getElementById(findme);
					//reveal.style.display = 'block';
				};
			}
		}
		
	}
	else {
		key = el['value'];
		if(unhide[key]){
			toshow = unhide[key].toString();
		}
		else{
			toshow = '';
		}
	
		for (attribsIndex in unhide) {
			if(unhide[attribsIndex]==toshow) {
				reveals = toshow.split(",");
				for ( i=0; i < reveals.length; i++ ){
					findme = reveals[i];				 
					//alert(findme);
					hide = document.getElementById(findme);
					if(hide) {
						hide.style.display = 'none';
					}
				};
			}
		}
		//$('property_value').value = '';
		//$('secured_on_property').value = '';
	}
}

AutoSave.Ajax.Json = AutoSave.Ajax.Base.extend({

	queryResponse: function(resp) {
		this.parent(resp);
		var advert = Json.evaluate(resp || false);
		if (!advert || !advert.length) return;
		this.updateAdvert(advert);
	}

});