// cartAJAXfunctions.js
// Copyright 2009-2010 Elysium Software, Inc.
// All Rights Reserved
//
// Implements JavaScript code to handle AJAX methods for the
// cart
//
// Version 1.1
// February 9, 2010
//-------------------------------------------------------------
// JavaScript Trim Function
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
//-------------------------------------------------------------
// Promo code
//-------------------------------------------------------------
function AddPromoByCode (userid)
{		
	var postString = "&user_id=" + userid;
	postString += "&promocode=" + (document.getElementById("promocodeid").value);

	postData('addpromobycode.php', postString, AddPromoByCodeCallback);
}	
//-------------------------------------------------------------
function AddPromoByCodeCallback (resultText)
{
	// Refresh 
	if (resultText.trim() != "")
		document.getElementById("promocoderesult").innerHTML = '<b><font color="red">' + resultText + '</font></b>';
	else
    {
		document.getElementById("promocodeid").value = '';
		window.location.reload();
    }
}
//-------------------------------------------------------------
// Delete item from cart
//-------------------------------------------------------------
function DeleteItemFromCart (userid, itemid, itemsize, itemcolor)
{		
	var postString = "&user_id=" + userid;
	postString += "&item_id=" + itemid;
	postString += "&item_size=" + itemsize;
    postString += "&item_color=" + itemcolor;

	var confirmationResult = confirm("Are you sure you want to delete this item?");

	if (confirmationResult == true)
	{
		postData('deletefromcart.php', postString, DeleteItemFromCartCallback);
	}
    else
        return false;
}	
//-------------------------------------------------------------
function DeleteItemFromCartCallback (itemsDeleted)
{
	// Refresh 
	window.location.reload();
}	   	
//-------------------------------------------------------------
// Add to cart
//-------------------------------------------------------------
function AddToCartCallback (userID)	
{
	//tb_show (null,cartPath + 'viewcart.php?uid=' + userID.trim() + '&KeepThis=true&TB_iframe=true&height=500&width=900', false);
	document.getElementById("add_to_cart_msg").innerHTML = '<b>Added!</b>';

	cartPath = "";
}	   	
//-------------------------------------------------------------
function AddToCartWithCartPath (tempCartPath, userid, numberofitems)	
{		
	var postString = "";
		 
	var postItemIDName = ""
	var elementItemIDName = "";
		 
	var postQuantityName = ""
	var elementQuantityName = "";
		 
	var postSizeName = ""
	var elementSizeName = "";

	var postColorName = ""
	var elementColorName = "";

    postString = "&user_id=" + userid;
	postString += "&num_items=" + numberofitems;
		  
	for (itemIndex = 0; itemIndex < numberofitems; itemIndex++)
	{
		postItemIDName = "&itemid_" + itemIndex;
		elementItemIDName = "itm_id_" + itemIndex + "_id";
		
		postQuantityName =  "&itemquantity_" + itemIndex;
		elementQuantityName = "qtytot_" + itemIndex + "_id";
			
		postSizeName =  "&itemsize_" + itemIndex;
		elementSizeName = "size_" + itemIndex + "_id";

		postColorName =  "&itemcolor_" + itemIndex;
		elementColorName = "color_" + itemIndex + "_id";

        postString += postItemIDName + "=" + (document.getElementById(elementItemIDName).value);
			
		postString += postQuantityName + "=" + (document.getElementById(elementQuantityName).value);
	    
		var elementSizeNameObj = document.getElementById(elementSizeName); 
		if (elementSizeNameObj)
			postString += postSizeName + "=" + encodeURI(document.getElementById(elementSizeName).value);

		var elementColorNameObj = document.getElementById(elementColorName);
		if (elementColorNameObj)
			postString += postColorName + "=" + encodeURI(document.getElementById(elementColorName).value);

        document.getElementById(elementQuantityName).value = 0; //Clear out value
	}

	cartPath = "modules/Cart/";

	postData(cartPath + 'addtocart.php', postString, AddToCartCallback);	
}
//-------------------------------------------------------------
function AddToCart (userid, numberofitems)	
{		
	AddToCartWithCartPath ("", userid, numberofitems);	
}	  
//-------------------------------------------------------------
//Add to cart
//-------------------------------------------------------------
function AddItemIDToCartCallback (itemIndex)	
{
	var elementNameHeader = "add_to_cart_msg_";
	var elementName = '';
	
	// Clear out the existing ones first
	var totalToClear = 100;
	
	for (elementIndex = 0; elementIndex < totalToClear; elementIndex++)
	{
		elementName = elementNameHeader + elementIndex + "_id";
		var elementAddedObj = document.getElementById(elementName);
		if (elementAddedObj)
			document.getElementById(elementName).innerHTML = '';
	}

	elementName = elementNameHeader + itemIndex.trim() + "_id";
	document.getElementById(elementName).innerHTML = '<b>Added!</b>';
}	   	
//-------------------------------------------------------------
function AddItemIDToCart (userid, itemIndex)	
{		
	var postString = "";
		 
	var postItemIDName = ""
	var elementItemIDName = "";
		 
	var postQuantityName = ""
	var elementQuantityName = "";
		 
	var postSizeName = ""
	var elementSizeName = "";

	var postColorName = ""
	var elementColorName = "";

	postString = "&user_id=" + userid;
	postString += "&item_index=" + itemIndex;
		  
	postItemIDName = "&itemid_" + itemIndex;
	elementItemIDName = "itm_id_" + itemIndex + "_id";
	
	postQuantityName =  "&itemquantity_" + itemIndex;
	elementQuantityName = "qtytot_" + itemIndex + "_id";
		
	postSizeName =  "&itemsize_" + itemIndex;
	elementSizeName = "size_" + itemIndex + "_id";

	postColorName =  "&itemcolor_" + itemIndex;
	elementColorName = "color_" + itemIndex + "_id";

	postString += postItemIDName + "=" + (document.getElementById(elementItemIDName).value);
		
	postString += postQuantityName + "=" + (document.getElementById(elementQuantityName).value);
    
	var elementSizeNameObj = document.getElementById(elementSizeName); 
	if (elementSizeNameObj)
		postString += postSizeName + "=" + encodeURI(document.getElementById(elementSizeName).value);

	var elementColorNameObj = document.getElementById(elementColorName);
	if (elementColorNameObj)
		postString += postColorName + "=" + encodeURI(document.getElementById(elementColorName).value);

    document.getElementById(elementQuantityName).value = 0; //Clear out value

	cartPath = "modules/Cart/";

	postData(cartPath + 'addtocart.php', postString, AddItemIDToCartCallback);	
}
//-------------------------------------------------------------
// Update cart
//-------------------------------------------------------------
function UpdateItemQuantityInCartCallback (itemsUpdated)
{
	// Refresh the thickbox
	//document.getElementById('TB_iframeContent').contentDocument.location.reload(true);
	window.location.reload();
}	 
//-------------------------------------------------------------
function UpdateItemQuantityInCart (userid, itemid, elementid, itemsize, itemcolor)
{		
	qty_item_elementID = "qty_item_" + elementid;
	new_quantity = document.getElementById(qty_item_elementID).value;

	var postString = "&user_id=" + userid;
	postString += "&item_id=" + itemid;
	postString += "&item_qty=" + new_quantity;
	postString += "&item_size=" + itemsize;
	postString += "&item_color=" + itemcolor;

    if (!isNaN(new_quantity))  // NaN is 'Not a Number'
    {
        if (new_quantity == 0)
        {
            if (!DeleteItemFromCart (userid, itemid, itemsize, itemcolor))
            	window.location.href=window.location.href;
        }
        else
    		postData('updatefromcart.php', postString, UpdateItemQuantityInCartCallback);
    }
}	  
//-------------------------------------------------------------
function updateTotals()
{
	total_qty_in = document.getElementById("total_qty_id").value; 
	
	subtotal = 0;
	total = 0;
	
	for (itemnumber = 0; itemnumber < total_qty_in; itemnumber++)
	{
		qty_item_var = "qty_item_"+itemnumber;
		item_price_var = "item_price_"+itemnumber;
		sub_total_var = "item_subtotal_"+itemnumber;	
	
		item_qty = document.getElementById(qty_item_var).value;
		item_price = document.getElementById(item_price_var).value;

		subtotal = item_qty * item_price;
	 
		document.getElementById(sub_total_var).innerHTML = "$"+subtotal.toFixed(2); 

		total += subtotal;
	}

	document.getElementById("grand_total").innerHTML = "$"+total.toFixed(2); 
}
//-------------------------------------------------------------
// Submission functions
//-------------------------------------------------------------
function postData (url, data, callback)
{ 
	var XMLHttpRequestObject = false; 
	var urlRoot = '';
	
	if (window.XMLHttpRequest) 
		XMLHttpRequestObject = new XMLHttpRequest();
  
    if (XMLHttpRequestObject.overrideMimeType) 
    {
    	// Set type accordingly to anticipated content type
    	XMLHttpRequestObject.overrideMimeType('text/html');
    }
    else if (window.ActiveXObject) 
    {
    	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(XMLHttpRequestObject) 
    {
    	XMLHttpRequestObject.open("POST", urlRoot + url); 
    	XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	
    	XMLHttpRequestObject.onreadystatechange = function() 
    	{ 
    		if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) 
    		{			
    			callback(XMLHttpRequestObject.responseText); 
    			delete XMLHttpRequestObject;
    			XMLHttpRequestObject = null;
    		} 
    		else
    		{
    			// document.getElementById("status").innerHTML = '<b>Processing...</b>';
    		}
    	}

    	XMLHttpRequestObject.send(data); 
    }
}
//-------------------------------------------------------------
