function XHR(url, params, open){
	this.url = url;
	this.open = open || "GET";
	this.params = params || "";
	this.request = null;
	this.responseText = null;
	this.responseXML = null;
}

XHR.prototype.Ready = function(){
	if(this.request.readyState == 4 && this.request.status == 200){
		this.responseText = this.request.responseText;
		this.responseXML = this.request.responseXML;
		return true;
	}
	
	return false;
}

XHR.prototype.Request = function(func){
	this.request = (window.XMLHttpRequest)? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	
	if(this.request){
		this.request.onreadystatechange = func;
		this.request.open(this.open, this.url, true);
		
		if(this.open.toLowerCase() == "post"){
			this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.request.setRequestHeader("Content-length", this.params.length);
			if( !document.all ) {
				this.request.setRequestHeader("Connection", "close");
			}
		}

		this.request.send(this.params);
	}
}

var ajax = {
	
	rpc: "/product/options.php",
	
	lookup: {
		1: "style",
		2: "size",
		3: "quantity"
	},
	
	getId: function(id){
		if(typeof id == "string"){
			return document.getElementById(id);
		}
		
		return id;
	},
	
	disable: function(objs){
		if(objs){			
			if(objs.constructor == Array){
				for(var a = 0, l = objs.length; a < l; a ++){
					if(ajax.getId(ajax.lookup[objs[a]])){
						ajax.getId(ajax.lookup[objs[a]]).disabled = true;
						ajax.reset(ajax.getId(ajax.lookup[objs[a]]));
					}
				}
			} else {
				if(typeof objs == "number"){
					if(ajax.lookup[objs]){
						ajax.getId(ajax.lookup[objs]).disabled = true;
						ajax.reset(ajax.getId(ajax.lookup[objs]));
					}
				} else if(ajax.getId(objs)){
					ajax.getId(objs).disabled = true;
					ajax.reset(ajax.getId(objs));
				}
			}
		}
	},
	
	enable: function(objs){
		if(objs){
			if(objs.constructor == Array){
				for(var a = 0, l = objs.length; a < l; a ++){
					if(ajax.getId(objs[a])){
						ajax.getId(objs[a]).disabled = false;
					}
				}
			} else {
				if(typeof objs == "number"){
					if(ajax.lookup[objs]){
						ajax.getId(ajax.lookup[objs]).disabled = false;
					}
				} else {
					if(ajax.getId(objs)){
						ajax.getId(objs).disabled = false;
					}
				}
			}
		}
	},
	
	reset: function(obj){
		obj.length = 1;	
	},
	
	populate: function(obj, data){
		if(obj && data){
			var obj = ajax.getId(obj);
			var fragment = document.createDocumentFragment();
			
			ajax.reset(obj);
			
			for(var opt in data){
				if(data[opt].id && data[opt].name) {
					var option = document.createElement("option");
	
					option.value = data[opt].id;
					option.appendChild(document.createTextNode(data[opt].name));
					fragment.appendChild(option);
				}
			}
			
			obj.appendChild(fragment);
			ajax.enable(obj);
		}
	},
	
	load: function(to_load, data, to){
		if(to_load && ajax.lookup[to_load] && data && data.from){
			var params = '';
			
			if(data.from.constructor == Array){
				for(var a = 0, l = data.from.length; a < l; a ++){
					if(typeof data.from[a] == "number"){
						params += "&" + ajax.lookup[data.from[a]] + "_id=" + ajax.get_value(ajax.getId(ajax.lookup[data.from[a]]));
					} else {
						params += "&" + ajax.lookup[ajax.lookup_find(data.from[a].id)] + "_id=" + ajax.get_value(data.from[a]);
					}
				}
			} else {
				params += "&" + ajax.lookup[ajax.lookup_find(data.from.id)] + "_id=" + ajax.get_value(data.from);
			}
		}
	
		params += "&=ts" + ( new Date().getTime() );
	
		var AJAX = new XHR(ajax.rpc, params, "POST");
		
		AJAX.Request(function(){
			if(AJAX.Ready()){
				if( to.disable ) {
					ajax.disable(to.disable);
				}

				if(AJAX.responseText.length > 2){
					var _data = eval(AJAX.responseText);
				
					ajax.populate(ajax.lookup[to_load], _data);
				} 
			}
		});
	},
	
	get_value: function(obj){
		if(obj && obj.options){
			return obj.options[obj.selectedIndex].value;
		}
		
		return 0;
	},
	
	lookup_find: function(needle){
		for(var key in ajax.lookup){
			if(ajax.lookup[key] == ajax.clean(needle)){
				return key;
				break;
			}
		}
	},
	
	clean: function(str){
		return str.toString().replace(/_/g, " ");
	},
	
	remove_all: function(id){
		if(ajax.getId(id).hasChildNodes()){
			ajax.getId(id).removeChild(ajax.getId(id).firstChild);
		}
	},
	
	enableSubmit: function(obj){
		var ref = document.getElementById( 'product-submit' );

		if( obj && obj.selectedIndex > 0 ) {
			ref.disabled = false;
		} else {
			ref.disabled = true;		
		}
	}
}
