/*
	Provide functions to allow the retrieval of Try and Buy product feeds in a JSON format
	$Id;$
*/
	
function TNB(){
	
	var self = this;
	
	this.xmlhttp;
	this.baseurl="/tryandbuy/ws/serve.jsp?content-type=json";
	
	/* An AJAX Call requesting a JSON feed passing the result to a callback function */
	function loadJSONObject(url,callback){
		
		//Get the XML Http Object
		self.xmlhttp=null;
		if (window.XMLHttpRequest)  {// code for IE7, Firefox, Opera, etc.
			self.xmlhttp=new XMLHttpRequest();
		}else if (window.ActiveXObject)  {// code for IE6, IE5
			self.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (self.xmlhttp!=null){
			
			self.xmlhttp.onreadystatechange=function(){state_Change(callback)};
			self.xmlhttp.open("GET",url,true);
			self.xmlhttp.send(null);
			
		}else{
			alert("Your browser does not support XMLHTTP.");
		}
		
	}
	
	function state_Change(callbackF){
		if (self.xmlhttp.readyState==4){// 4 = "loaded"
		
			if (self.xmlhttp.status==200){// 200 = "OK"
			
				try{				
					var parsedJSONObj = eval("(" + self.xmlhttp.responseText + ")");
					
					callbackF(parsedJSONObj);
				}catch(ex){
					alert(ex);
				}		
			}else{
				
			}
		}
	}
	
	/* Assorted getters */
	
	function loadCountryCategories(country, callback, respectpricing){
		var url = self.baseurl;		
		url += "&region=" + country;
		url += "&justCats";
		url += respectpricing == true ? "" : "&disrespectPricing"; 
		loadJSONObject(url, callback);
	}
	
	this.getCountryCategories = function(country,callback, respectpricing){
		loadCountryCategories(country, callback, respectpricing);
	}
	
	function loadCountryCategoryProducts(country, category, callback, respectpricing){
		var url = self.baseurl;
		url += "&region=" + country;
		url += "&catuuid=" + category;
		url += respectpricing == true ? "" : "&disrespectPricing";
		loadJSONObject(url, callback);
	}
	
	this.getCountryCategoryProducts = function(country, category, callback, respectpricing){
		loadCountryCategoryProducts(country, category, callback, respectpricing)
	}
	
	function loadCountryProduct(country, product, callback, respectpricing){
		var url = self.baseurl;
		url += "&region=" + country;
		url += "&productuuid=" + product;
		url += respectpricing == true ? "" : "&disrespectPricing";
		loadJSONObject(url, callback);
	}
	
	this.getCountryProduct = function(country, product, callback, respectpricing){
		loadCountryProduct(country, product, callback, respectpricing);
	}
	
	function loadAllProducts(category, callback){
		var url = self.baseurl;
		url += "&catuuid="+category+"&allTnBProducts&disrespectPricing";
		loadJSONObject(url, callback);
	}
	
	this.getAllProducts = function(category, callback){
		loadAllProducts(category, callback);
	}
	
	function loadProduct(product, callback, respectpricing){
		var url = self.baseurl;		
		url += "&productuuid=" + product;
		url += respectpricing == true ? "" : "&disrespectPricing";
		loadJSONObject(url, callback);
	}
	
	this.getProduct = function(product, callback, respectpricing){
		loadProduct(product, callback, respectpricing);
	}
	
	function loadProductWithoutBlocking(product,callback){
	      var url = self.baseurl;		
	      url += "&productuuid=" + product;
	      url += "&allTnBProducts&disrespectPricing";
	      loadJSONObject(url, callback);
	}
	
	this.getProductWithoutBlocking = function(product,callback){
	     loadProductWithoutBlocking(product,callback);
	}
	
}
