/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set_cookie class
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this class is simply calls a php with a request: cookie name, cookie value, cookie duration
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// written by scott jackman.  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function set_cookie(){
	// constructor
	set_cookie.prototype._cookieName; 
	set_cookie.prototype._cookieValue;
	set_cookie.prototype._cookieDuration;
	set_cookie.prototype._debugOn = false;
	set_cookie.prototype._connection; // request object for ajax
	}
	
// methods
set_cookie.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;
}
set_cookie.prototype.setCookie = function(){
	this._connection = this.createRequestObject();
	this.sendQuery();
	}

set_cookie.prototype.sendQuery = function(){
	this._connection.open('get', 'includes/ajax_set_cookie.php?name='+this._cookieName+'&value='+this._cookieValue+'&duration='+this._cookieDuration+'&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);	
	}


set_cookie.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'){
				if(this._debugOn){
					alert('something went wrong!!');
				}
				}
			} 
	}
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);
    };
}
