/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// add to shortlist class
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this class is simply calls a php script with a property number and adds this property number to the 'property_shortlist' cookie
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// written by scott jackman.  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function shortlist(){
	// constructor
	shortlist.prototype._propertyid; // lets us store the value of a field when it's focused on
	shortlist.prototype._addDiv; 
	shortlist.prototype._addingDiv;
	shortlist.prototype._connection; // request object for ajax
	}
	
// methods
shortlist.prototype.createRequestObject = function (){
	var request_o; 
	if(window.XMLHttpRequest){
		request_o = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(!request_o){
		request_o = new ActiveXObject("Msxml2.XMLHTTP");		
		}
	return request_o;
}
shortlist.prototype.add = function(){
	this._addDiv.style.display = 'none';
	this._addingDiv.style.display = 'block';
	//alert('checking!');
	this._connection = this.createRequestObject();
	this.sendQuery();
	}

shortlist.prototype.sendQuery = function(){
	this._connection.open('get', 'includes/ajax_add_to_shortlist.php?property_id='+this._propertyid+'&rand='+Math.random()*10,true);
	// Define a function to call once a response has been received. (using a curry because other wise 'this' gets confused and becomes related to the ajax request, rather than this instance of the class, which breaks our readyState 
	this._connection.onreadystatechange = curry(this.giveResult,this);
	this._connection.send(null);	
	}


shortlist.prototype.giveResult = function(){
		//alert(that._obj);
		var result = this._connection.readyState;
		//alert(result);
		/* 
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished 
		*/
		if(result < 4){
			}
		if(result == 4){ //Finished loading the response
			// get response - will either return success or fail
			if(this._connection.responseText != 'true'){
				//alert('something went wrong!');
				} else {
				this._addingDiv.style.display = 'none';
				}
			} 
	}
function curry (fn, scope) {
    var scope = scope || window;
    var args = [];
    for (var i=2, len = arguments.length; i < len; ++i) {
        args.push(arguments[i]);
    };
    return function() {
	    fn.apply(scope, args);
    };
}
