/*-------------------------------------------|
| ClassName : YellowCart                     |
| Author    : Ivo Silva (ivosilva@seara.com) |
| Date      : 2007-03-28                     |
| Purpose   : Ajax shopping cart             |
|-------------------------------------------*/

var yCartBuy = new YellowCart;

yCartBuy.properties["post_url"] = "/revendedor/loja/comprar.asp";
yCartBuy.properties["post_arguments"] = new Array("id", "qtd", "req");
yCartBuy.properties["info_url"] = "/revendedor/loja/info.asp";
yCartBuy.properties["info_id"] = "cart_info";

var yCartRemove = new YellowCart;

yCartRemove.properties["post_url"] = "/revendedor/loja/comprar.asp";
yCartRemove.properties["post_arguments"] = new Array("remove", "req");
yCartRemove.properties["info_url"] = "/revendedor/loja/info.asp";
yCartRemove.properties["info_id"] = "cart_info";

/*

Parameters:
	1 - element to display the warning message ("added to chart", "invalid quantity")
	2 - product id
	3 - quantity

*** first parameter is required
*** other parameters must be specified in the properties["post_arguments"] in the correct order

yCart.buyItem("warning_123", "123", 4);

*/

function YellowCart()
{
	/*---------------------------|
	| variables                  |
	|---------------------------*/
	var IE = document.all;
	var properties = new Array();
	
	/*---------------------------|
	| properties                 |
	|---------------------------*/
	this.properties = new Array();
	
	this.properties["post_url"] = "";
	this.properties["post_arguments"] = new Array();
	this.properties["info_url"] = "";
	this.properties["info_id"] = "";
	this.properties["class_error"] = "cart_error";
	this.properties["class_warning"] = "cart_warning";
	this.properties["class_success"] = "cart_success";
	
	/*---------------------------|
	| methods                    |
	|---------------------------*/
	this.buyItem = buy;
	this.removeItem = remove;
	
	function buy()
	{
		properties = this.properties;
		
		var warningId = arguments[0];
		
		elWarning = document.getElementById(warningId);
		
		var args = properties["post_arguments"];
		
		if( (arguments.length - 1) != args.length )
		{
			alert("Incorrect number of arguments");
			
			return false;
		}
		
		var url = this.properties["post_url"] + "?";
		
		for(i = 1 ; i < arguments.length ; i++)
		{
			url += args[i-1] + "=" + arguments[i] + "&";
		}
		
		if(url.length > 0) AJAX_call(url, elWarning, "");
		
		return false;
	}
	
	function remove()
	{
		properties = this.properties;
		
		var args = properties["post_arguments"];
		
		if( (arguments.length) != args.length )
		{
			alert("Incorrect number of arguments");
			
			return false;
		}
		
		var url = this.properties["post_url"] + "?";
		
		for(i = 0 ; i < arguments.length ; i++)
		{
			url += args[i] + "=" + arguments[i] + "&";
		}
		
		if(url.length > 0) AJAX_call(url, "", "remove");
		
		return false;
	}
	
	function process()
	{
		var message = arguments[0];
		var el = arguments[1];
		var type = arguments[2];
		
		if(el)
		{
			if(type == "info")
				el.innerHTML = message;
			else
			{
				el.innerHTML = processMessage(message);
				setTimeout(clearMessage, 3000, el.id);
			}
		}
	}
	
	function processMessage(message)
	{
		var result = "";
		
		if(message.indexOf("#") != -1)
		{
			var parts = message.split("#");
			
			var response = parts[0];
			var message = parts[1];
			
			if(response == "ERROR")
			{
				result = error(message);
			}
			else if(response == "WARNING")
			{
				result = warning(message);
			}
			else if(response == "SUCCESS")
			{
				result = success(message);
			}
		}
		
		return result;
	}
	
	function error(message)
	{
		return '<span class="' + properties["class_error"] + '">' + message + '</span>';
	}
	
	function warning(message)
	{
		return '<span class="' + properties["class_warning"] + '">' + message + '</span>';
	}
	
	function success(message)
	{
		return '<span class="' + properties["class_success"] + '">' + message + '</span>';
	}
	
	function clearMessage(id)
	{
		if(el = document.getElementById(id))
		{
			el.innerHTML = "";
		}
	}
	
	function AJAX_call(url, el, type)
	{
		var xmlhttp = false;
		
	    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
		{
	        xmlhttp = new XMLHttpRequest();
	    }
		else
		{
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }
		
		xmlhttp.open("GET", url, true);
		
	    xmlhttp.onreadystatechange = function()
		{
	        if(xmlhttp.readyState==4)
			{
	            if(xmlhttp.status == 200)
				{
					var message = xmlhttp.responseText;
					
					process(message, el, type);
					
					if(type != "info")
					{
						elInfo = document.getElementById(properties["info_id"]);
						
						if(properties["info_url"].length > 0)
						{
							AJAX_call(properties["info_url"], elInfo, "info");
						}
					}
					
					return true;
				}
				else {}
			}
		}
		
		xmlhttp.send(null);
	}
}