// JScript File

// Implements the Request object.
//
// Usage:
// 1) Instantiate the object: 
//		theRequest = new Request();
//
// 2) Post (or get) the data supplying a callback function and optionally an error callback: 
// 		var url = "/getproddata";
// 		var parameters = "pid=" + productId + "&qty=" + qty 
//		theRequest.post(url, this.showOnComplete, parameters);
//    Note: the callback function is an object not a string.
//
// 3) Receive the results:
//		this.showOnComplete = function(jsonObj){  
//			prodDataObj = jsonObj;
//		}
//
// If there is an error then the callback function will be called.  If the callback
// function is null then the error will be ignored.
//
// All the AJAX calls ae supposed to return a JSON object.  If there was an error 
// the format is  {"ERROR":"error message"} otherwise the format is whatever 
// that specific call wants. We parse the object and see if there was an error.  
// If there is no error we call the callback function passing the JSON object.
//
// If you want to show a feedback image then pass all the parameters when creating
// the object.  If no parameters are passed then there will be no feedback.
//
function Request(imagePath,image,imageBlank,imageElLocation, imageElId){

	// Constructor.
	var request = null;
   	var theCallBackFn = null;
   	var theCallBackErrFn = null;
   	var theImagePath = imagePath;
   	var theImage = image;
   	var theImageBlank = imageBlank;
   	var theImageElLocation = imageElLocation;
   	var theImageElId = imageElId;
   	var theCurrentUrl = null;
   	
	try{
		request = new XMLHttpRequest();
	}catch(trymicrosoft){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(othermicrosoft){
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(failed){
           		request = null;
			}
		}
	}
	if(request == null) alert("Error creating request object!");
	
	
	this.get = function(url, callBackFn, callBackErrFn){
		if(request != null){
			theCurrentUrl = url;
			theCallBackFn = callBackFn
			theCallBackErrFn = callBackErrFn;
			request.open("GET", url, true);
			request.onreadystatechange = this.onComplete;
			request.send(null);
			setImage("ON");
		}
	}

	this.post = function(url, callBackFn, parameters, callBackErrFn){
		if(request != null){
			theCurrentUrl = url;
			theCallBackFn = callBackFn
			theCallBackErrFn = callBackErrFn;
			request.open("POST", url, true);
			request.onreadystatechange = this.onComplete;
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request.setRequestHeader("Content-length", parameters.length);
			request.setRequestHeader("Connection", "close");
			request.send(parameters);		
			setImage("ON");
		}
	}
	
	this.onComplete = function(){
		if (request.readyState == 4) {	
			if (request.status == 200) { 	
				if(top.urchinTracker != null) {
					top.urchinTracker(theCurrentUrl);
//alert("Sent " + theCurrentUrl + " to urchinTracker");					
				}
				setImage("OFF");
				jsonObj = request.responseText.parseJSON();		
				if(!jsonObj){
					if(theCallBackErrFn != null) {
						var json = '{"ERROR":"Bad response from server","responseText":"Y"}'; 
						theCallBackErrFn(json.parseJSON(),request.responseText);
					}
				}else if(jsonObj.ERROR!=null) {
					if(theCallBackErrFn != null) theCallBackErrFn(jsonObj);
				}else{
					theCallBackFn(jsonObj);
				}
			}else{
				var json = '{"ERROR":"Server returned" + request.status,"responseText":"Y"}'; 
				theCallBackErrFn(json.parseJSON(),request.responseText);
			}
		}
	}
	
	function setImage(what){
		if(theImageElLocation == null) return;
		var theImageEl = eval(theImageElLocation + '.getElementById("' + theImageElId + '")');
		if(theImageEl!=null) {
			if(what == 'ON'){
				theImageEl.src = theImagePath + theImage;
			}else{
				theImageEl.src = theImagePath + theImageBlank;
			}
		}
	}
	
}