var cfCookieLessString = new String("");
if (window.location.getParameter("CFID") != "")
{
	cfCookieLessString = "CFID=" + window.location.getParameter("CFID") + "&CFTOKEN=" + window.location.getParameter("CFTOKEN");
}

var ShoppingCart = Class.create();

ShoppingCart.temporaryOptions = null;

/********************************
  All ShoppingCart url must be
  inside of ShoppingCart.Url
*********************************/

ShoppingCart.Url = {}
ShoppingCart.Url = {
  add : "/frontEndComponents/specificComponents/b2c/servlet/shoppingCart/addToCart.cfm",
  remove : "",
  update : "",
  itemList : "/frontEndComponents/specificComponents/b2c/servlet/shoppingCart/addToCart.cfm",
  itemCount : "/frontEndComponents/specificComponents/b2c/servlet/shoppingCart/viewCartItems.cfm"
}

/********************************
  ShoppingCart Constants
*********************************/

ShoppingCart.Constants = {}
ShoppingCart.Constants = {
  idItemCount : "ShoppingCartItemCount",
  idItemList : "ShoppingCartItemList"
}

/********************************
  ShoppingCart User Interface
  manipulation
*********************************/
ShoppingCart.UI = {}
ShoppingCart.UI.extend({
  itemList : new Object(),

  refreshItemCount : function(){
    new SmartAjax.Request(ShoppingCart.Url.itemCount + "?" + cfCookieLessString, {onComplete: _ShoppingCart_UI_refreshItemCount });
  }
})

/********************************
  ShoppingCart default options
*********************************/

ShoppingCart.defaultOptions = {
  messageTimeout : 0,
  shopCartSucess:0,
  onSuccess : _ShoppingCart_action_OnSuccess,
  onFailure: _ShoppingCart_action_OnFailure
}

/********************************
  ShoppingCart methods creation
  with prototyping
*********************************/

ShoppingCart.prototype = {
  initialize : function(){},

  add : function(options){
    this.send(ShoppingCart.Url.add + "?keepThisPage=Y&lang=" + options.lang  + "&" + cfCookieLessString, options) ;
  },

  get : function(options){

  	this.send(ShoppingCart.Url.itemList + "?keepThisPage=Y&displayOnly=Y&lang=" + options.lang +  "&" + cfCookieLessString, options);
  },

  getPopup : function(options){
    	this.send(options.src, options);
  },

  remove : function(options){
    this.send(ShoppingCart.Url.remove, options);
  },

  update : function(options){
    this.send(ShoppingCart.Url.update, options);
  },

  send : function(url, options){
    var sendOptions = ShoppingCart.defaultOptions.clone();
    sendOptions.extend(options || {})
    ShoppingCart.temporaryOptions = sendOptions;
    new SmartAjax.Request(url, sendOptions);
  },

  addRedirect : function(form){
  	form.action = ShoppingCart.Url.add + "?keepThisPage=N";
  	form.submit();
  }
}

/********************************
  ShoppingCart AJAX functions
*********************************/
function _ShoppingCart_UI_refreshItemCount(request){
    document.getElementById(ShoppingCart.Constants.idItemCount).innerHTML = request.responseText;
}


function _ShoppingCart_action_OnSuccess(request){
  SmartMessage.display(request.responseText, ShoppingCart.temporaryOptions);
  if (document.getElementById(ShoppingCart.Constants.idItemCount)){
    ShoppingCart.UI.refreshItemCount();
    ShoppingCart.defaultOptions.shopCartSucess = 1;
  }
  ShoppingCart.UI.refreshItemCount();
}

function _ShoppingCart_action_OnSuccessFlyDown(request){
  if (document.getElementById(ShoppingCart.Constants.idItemCount)){
    ShoppingCart.UI.refreshItemCount();
    ShoppingCart.defaultOptions.shopCartSucess = 1;
  }
  ShoppingCart.UI.refreshItemCount();
}

function _ShoppingCartFromCheckOut_action_OnSuccess(request){

	ret_er = false;
	if(request.responseText.toLowerCase().indexOf("<error>") != -1){
  			ret_er = true;
	  	}
	  redirectPage = true;
	if(shoppingCartItems==0&&ret_er&& ShoppingCart.defaultOptions.shopCartSucess==0)
	{
		SmartMessage.display(request.responseText, ShoppingCart.temporaryOptions);
		redirectPage = false;
	}
}

function setOptions(){} // if errors delete  this function please


function _ShoppingCart_action_OnFailure(request){
  var message = new String("An error has occured when processing your action.<p onclick='SmartMessage.hide()' style='cursor:pointer'>Close</p>");
  if (request.responseText.length){
  	message = message.concat("<p>" + request.responseText + "</p>");
  }
  SmartMessage.display(message , { messageTimeout : ShoppingCart.temporaryOptions.messageTimeout });
}

addEvent(document, "click", document_onclick, true);

function document_onclick(){
	SmartMessage.hide();
}
