/*
================================================================================
C    a    s    h      s    h    o    p    p    i    n    g      c    a    r    t
================================================================================
*/
// Aplication settings
// ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

// trace highlight color
var cashColor = "#f00";
var fileGamePortlet = "cash_jogo.asp";
if (debugMode) { fileGamePortlet += "?debugMode=" + debugMode; }
var fileItemPortlet = "cash_moeda.asp";
var fileCartPortlet = "cash_compra.asp";

// Global variables
// ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

// =============================================================================
function loadGamePortlet(idGenero){
	/*
	Purpose: initializes the jogo canvas.
	Inputs:  none;
	Return:  undefined.
	*/
	trace("Function loadGamePortlet()", cashColor);
	
	idGenero = idGenero==undefined ? "" : idGenero; // fix undefineds

	requestDocument(fileGamePortlet+"?idGen="+idGenero, displayGamePortlet);
	
	// exit function
	return undefined;
}

// =============================================================================
function loadItensPortlet(){
	/*
	Purpose: initializes the moeda canvas.
	Inputs:  none;
	Return:  undefined.
	*/
	trace("Function loadItensPortlet()", cashColor);
	
	requestDocument(fileItemPortlet, displayItensPortlet);
	
	// exit function
	return undefined;
}

// =============================================================================
function loadCartPortlet(){
	/*
	Purpose: initializes the compra canvas.
	Inputs:  none;
	Return:  undefined.
	*/
	trace("Function loadCartPortlet()", cashColor);
	
	requestDocument(fileCartPortlet, "displayCartPortlet(0)");
	
	// exit function
	return undefined;
}

// =============================================================================
function displayItens(gameId, gameName, gameThumbImg, gameURL){
	/*
	Purpose: display the itens for the current game.
	Inputs:  gameId:   integer, game id from database;
	         gameName:     string,  the game name to insert at the headre of portlet
	         gameThumbImg: string, filename of the thumbnail file
	Return:  undefined.
	*/
	trace("Function displayItens(" + gameId + ", \"" + gameName + "\", \"" + gameThumbImg + "\", \"" + gameURL + "\")", cashColor);
	
	if ( gameThumbImg == undefined ) { gameThumbImg = ""; }
	if ( gameURL == undefined )      { gameURL = ""; }
	requestDocument(fileItemPortlet + "?game=" + gameId + "&name=" + gameName + "&thumb=" + gameThumbImg + "&url=" + gameURL, displayItensPortlet);
	
	// exit function
	return undefined;
}

// =============================================================================
function displayGamePortlet(){
	/*
	Purpose: display the game portlet canvas in to the UI.
	Inputs:  none;
	Return:  undefined.
	*/
	trace("Function displayGamePortlet()", cashColor);
	
	var portletContents = getDocument("text");
	if ( portletContents != null ) {
		var objCanvasItem = document.getElementById("tdGame");
		objCanvasItem.innerHTML = portletContents;
	}
	
	// exit function
	return undefined;
}

// =============================================================================
function displayItensPortlet(){
	/*
	Purpose: insert the incoming itens canvas into the document.
	Inputs:  gameName:     string,  the game name to insert at the headre of portlet
	         gameThumbImg: string, filename of the thumbnail file
	Return:  undefined.
	*/
	trace("Function displayItensPortlet()", cashColor);
	
	var portletContents = getDocument("text");
	if ( portletContents != null ) {
		var objCanvasItem = document.getElementById("tdItem");
		objCanvasItem.innerHTML = portletContents;
	}
	
	// exit function
	return undefined;
}

// =============================================================================
function manageCart(itemId, amount){
	/*
	Purpose: Manage the shopping cart, adding or removing itens.
	Inputs:  itemId: integer, item id from database;
	         amount: integer, amount to add or remove from shopping cart
	Return:  undefined.
	*/
	trace("Function manageCart(" + itemId + ", " + amount + ")", cashColor);
	
	requestDocument(fileCartPortlet + "?item=" + itemId + "&amount=" + amount, "displayCartPortlet(0)");
	
	// exit function
	return undefined;
}

// =============================================================================
function resetCart(){
	/*
	Purpose: Clean the current cart.
	Inputs:  none;
	Return:  undefined.
	*/
	trace("Function resetCart()", cashColor);
	
	requestDocument(fileCartPortlet + "?reset=true", "displayCartPortlet(0)");
	
	// exit function
	return undefined;
}

// =============================================================================
function displayCartPortlet(isSubmit){
	/*
	Purpose: update the shopping cart in the UI.
	Inputs:  isSubmit: boolean: indicate the contents of a given hidden will be submited.
	Return:  undefined.
	*/
	trace("Function displayCartPortlet(" + isSubmit + ")", cashColor);
	
	var portletCart = getDocument("text");
	if ( portletCart != null ) {
		var objCanvasItem = document.getElementById("tdCart");
		trace("<b>Insert</b> data into canvas");
		objCanvasItem.innerHTML = portletCart;
		
		if (isSubmit){
			trace("document.getElementById(\"submitTo\").value: " + document.getElementById("submitTo").value);
			window.location = document.getElementById("submitTo").value;
		}
	}
	

	// exit function
	return undefined;
}

// =============================================================================
function submitCart(){
	/*
	Purpose: submit the current cart.
	Inputs:  argument name: data type, explanation;
	Return:  data type, explanation of the value returned.
	*/
	trace("Function submitCart()", cashColor);
	
	requestDocument(fileCartPortlet + "?submit=true", "displayCartPortlet(1)");
	
	// exit function
	return undefined;
}

// =============================================================================
function myFunction(){
	/*
	Purpose: what the procedure does (not how).
	Inputs:  argument name: data type, explanation;
	Return:  data type, explanation of the value returned.
	*/
	trace("Function myFunction()", cashColor);
	
	// ...
	
	// exit function
	return undefined;
}
// =============================================================================
