﻿/* Ajax Object Defintion */

function AjaxObject()
{

	this.AjaxRequest			= null;
	this.XsltLoaded				= false;
	this.XsltDocument			= null;
	this.OriginalData			= '';
	this.ItemId					= 0;
	this.ItemIds				= null;
	this.CartId					= 0;
	this.AjaxResponse			= null;
	this.TargetDivId = '';
	this.CustomDataPackage		= null;
	
	
	this.getAjaxObect = function() {
		var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]

		if (window.XMLHttpRequest)
		{
			return new XMLHttpRequest();
		}
		
		for (var i=0; i<activexmodes.length; i++)
		{
			try
			{
				return new ActiveXObject(activexmodes[i])
			}
			catch(e)
			{//suppress error
			}
		} 
		
		return false;
	}

	// retrieve XML document (reusable generic function);
	// parameter is URL string (relative or complete) to
	// an .xml file whose Content-Type is a valid XML
	// type, such as text/xml; XML source must be from
	// same domain as HTML file
	this.loadXmlDoc = function(url, callBackFunction) {
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) {
			this.AjaxRequest = new XMLHttpRequest();
			this.AjaxRequest.onreadystatechange = callBackFunction;
			this.AjaxRequest.open("GET", url, true);
			this.AjaxRequest.send(null);
			// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			//isIE = true;
			this.AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.AjaxRequest) {
				this.AjaxRequest.onreadystatechange = callBackFunction;
				this.AjaxRequest.open("GET", url, true);
				this.AjaxRequest.send();
			}
		}
	}

	this.postForm = function(url, parameters, callback) {

		this.AjaxRequest = this.getAjaxObect();
		
		if( this.AjaxRequest )
		{
			this.AjaxRequest.onreadystatechange = callback;
			this.AjaxRequest.open("POST", url, true);
			this.AjaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			if (this.AjaxRequest.overrideMimeType)
			{
				this.AjaxRequest.overrideMimeType('text/xml');
			}
			this.AjaxRequest.send(parameters);
		}
	}

	this.postFormCallBack = function() {
		if (this.AjaxRequest.readyState == 4) {
			// only if "OK"
			//alert(req.status)
			if (this.AjaxRequest.status == 200) {
				var messages = null;
				this.AjaxResponse = new AjaxActionResponse();

				this.AjaxResponse.parseResponse(this.AjaxRequest.responseText);

				if (this.AjaxResponse.errorCount > 0) {

					if (this.ErrorCallback) {
						this.ErrorCallback(this);
					}
				}
				else {
					if (this.SuccessCallback) {
						this.SuccessCallback(this);
					}
				}
			}
			else {

				if (this.RequestErrorCallback) {
					this.RequestErrorCallback(AjaxRequest);
				}
			}
		}
	}
	
	this.removeItemFromCart = function(ajaxUrl, itemId, cartId, callback)
	{	
		if( confirm('Delete Item?') )
		{
			this.ItemId					= itemId;
			this.CartId					= cartId;
			var url						= ajaxUrl + "?action=removecartitem&ItemId=" + itemId + "&CartId=" + cartId + "&random=" + Math.random()*99999;
			var loadingGif				= document.getElementById("ajaxarrows");
			var originalDeleteButton	= document.getElementById("remove_" + itemId);
			
			if( loadingGif && originalDeleteButton )
			{
				this.OriginalData = originalDeleteButton.innerHTML;
				originalDeleteButton.innerHTML = loadingGif.innerHTML;
			}	
			
			this.loadXmlDoc(url, callback);
		}
	}
	
	this.removeItemAjaxCallBack = function(request, itemId, cartId)
	{
		if (request.readyState == 4) 
		{
			// only if "OK"
			//alert(req.status)		
			if (request.status == 200) 
			{
				var messages = null;
				this.AjaxResponse = new AjaxActionResponse();
				
				this.AjaxResponse.parseResponse(request.responseText);
				
				if (this.AjaxResponse.errorCount > 0 )
				{	
					var originalDeleteButton	= document.getElementById("remove_" + itemId);
					if( originalDeleteButton )
					{
						originalDeleteButton.innerHTML = this.OriginalData;
					}	
					
				}
				
			}
			else 
			{
				
				alert("There was a problem retrieving the XML data:\n" + this.AjaxRequest.statusText);
			}
			
			window.location.href = window.location.href;
		}
    }
	
	this.loadXSLTDoc = function (url, xslLoadedCallBack)
	{
	    
		if (window.XSLTProcessor)
		{
			// support Mozilla/Gecko based browsers
			this.XsltDocument = document.implementation.createDocument("", "", null);
			this.XsltDocument.addEventListener("load", xslLoadedCallBack, false);
			this.XsltDocument.load(url);
		}
		else if(window.ActiveXObject)
		{
			// support Windows / ActiveX
			this.XsltDocument = new ActiveXObject("Microsoft.XMLDOM");
			this.XsltDocument.async = true;
			this.XsltDocument.ondataavailable = xslLoadedCallBack;
			this.XsltDocument.load(url);
		}
	}
	
	this.loadXSLTDoc = function(url)
	{
		if(window.ActiveXObject)
		{
			// support Windows / ActiveX
			this.XsltDocument = new ActiveXObject("Microsoft.XMLDOM");
			//xsldoc.ondataavailable = xslLoadedCallBack;
			this.XsltDocument.async = false;
			this.XsltDocument.load(url);
		}
	    
		else if (window.XSLTProcessor)
		{
			// support Mozilla/Gecko based browsers
			this.XsltDocument = document.implementation.createDocument("", "", null);
			//xsldoc.addEventListener("load", xslLoadedCallBack, false);
			this.XsltDocument.load(url);
		}
	}
	
	this.transformXml = function(xml, xsl, theDiv)
	{
		//alert(xsl);
		// code for Mozilla, Firefox, Opera, etc.
		if (window.XSLTProcessor)
		{
			xsltProcessor=new XSLTProcessor();
			xsltProcessor.importStylesheet(xsl);
			ex = xsltProcessor.transformToFragment(xml,document);
	        
			// alert(ex);
	        
			//alert(ex.toString());
			theDiv.innerHTML = "";
			theDiv.appendChild(ex);
	        
			//alert(theDiv.innerHTML);
		}
		else if (window.ActiveXObject)
		{
			ex=xml.transformNode(xsl);
	        
			//alert(ex);
	        
			theDiv.innerHTML=ex;
		}
		alert(theDiv.innerHTML);
	}
	
	this.xslLoadedCallBack = function()
	{
		this.XsltLoaded = true;
	}
	
	
	
}
