// cartUtil.js:
//
// This file contains some functions used when AJAXing the cart.
//


// handleResponse_cart:
//
// This handles the response from the XMLHttpRequest call.
//
function handleResponse_cart()
{	
	if(gAJAXutil.request.readyState == 4)
	{
		if(gAJAXutil.request.status == 200)                         // Complete
		{	
			// alert(gAJAXutil.request.responseText);		
		
			var objCartInfo = eval('(' + gAJAXutil.request.responseText + ')');	
			if(objCartInfo)
			{
				if(objCartInfo.success == 'false')
					alert(objCartInfo.message);
				else
					handleItemAddedToCart(objCartInfo);					
			}
		}
		else
			alert(	"A problem occurred in communicating between the XMLHttpRequest object and the server program.");
	}	
		
} // handleResponse_cart


// userIsSignedIn:
//
// Returns:
//  true  - If the user is signed in.
//  false - If she is not.
//
function userIsSignedIn() 
{
    if(getCookie('STARFISH_USER')!= '')     // If we're signed in, we'll have a 'STARFISH_USER' with the Custnum.
        return true;  
    else        
        return false;
    
} // userIsSignedIn


// AddToCart:
//
// This does an AJAX call to add the item to the cart.	
// This displays a message that says the product has been added to the cart.
//
// isbn:      The isbn of the product you want to add to the cart.
// quantity:  The number of these products that you want to add to the cart.
// basePath:  This is www.starfish.com on LIVE and http://192.168.100.77:134/ on DEV.     
// returnURL: (Optional) If we specify this parm it means the user cannot add this item to the cart unless they are signed in.
//                       The URL is where the user will be returned to after completing the sign in procedure.
// price:     (Optional) The price of the product you are adding to the cart.
//                       If you don't pass a price, the price in centralProducts will be used.
//
// *Note: Under this configuration you can't pass in a price without passing in a returnURL. 
//        We only pass in a price for courses and you have to be signed in to add a course to the cart, so this is fine.
//
function AddToCart(isbn, quantity, baseSitePath, returnURL, price,regURL) 
{
    if(arguments.length >= 4)                              // The user must be signed in to add a course to the cart.
    {
        if(!userIsSignedIn())
        {
            alert('You must sign in before you can add a course to your cart.');
			
            var url = regURL;  // Take them to the signin page.
            window.location = url;
            return;
        }                
    } 
           
    // alert(arguments.length);
    // alert('isbn:' + isbn + ' quantity:' + quantity + ' baseSitePath:' + baseSitePath + ' returnURL:' + returnURL + ' price:' + price);


	if(arguments.length < 5)       // We will not always pass in price.
	    var url = baseSitePath + "/store/AJAX/cartAdd.cfm?site=starfish" + "&isbn=" + isbn + "&quantity=" + quantity;       
    else
  	    var url = baseSitePath + "/store/AJAX/cartAdd.cfm?site=starfish" + "&isbn=" + isbn + "&quantity=" + quantity + "&price=" + price;   

	gAJAXutil.httpRequest("GET", url, handleResponse_cart, true);   // The result will be returned to the handleResponse function.
	
} // AddToCart


// handleItemAddedToCart:
//
// This displays a message that says the product has been added to the cart.
// 
// Note: Your message div will need an id of divId_(isbn number).
//
function handleItemAddedToCart(objCartInfo) 
{
	// alert(objCartInfo.message);
	var divID = 'divId_' + objCartInfo.isbn;        // See if the page wants to display a product specific message.
    
    if(!document.getElementById(divID))             // Maybe it wants to display a generic message then.
        divID = "genericCartAddMessage";
        
    if(document.getElementById(divID))
    {       
        document.getElementById(divID).style.display			= 'block';
        document.getElementById(divID).style.marginTop			= '2px';				
        document.getElementById(divID).style.padding			= '2px';			
        document.getElementById(divID).style.color				= '#555555';
        document.getElementById(divID).style.backgroundColor	= '#EEEEEE';		
        document.getElementById(divID).innerHTML	 			= objCartInfo.message;
        document.getElementById(divID).blur();					// This gets rid of the dotted lines around the button that was pressed.
    }
    
    // Update the "View Cart" info at the top of every page.
    document.getElementById('viewCart').innerHTML = 'View Cart (' + objCartInfo.numItemsInCart + ' items)';
        	
} // handleItemAddedToCart
