﻿// Contains all the functions to facilitate AJAX calls
//      This file has the following dependancies:
//          [*] main.js must be included

var g_szAjaxFile = "/aux_incl/ajax/ajax.aspx?";

// This function handles switching the checkout-payment screen to different inputs
function switchPaymentScreen(_arrayShowIDs, _arrayHideIDs)
{
    // Parameters:
	//		_arrayShowIDs = a list of IDs of the tags to show
	//      _arrayHideIDs = a list of the IDs of the tags to hide
	
    // Make sure the supplied parameters are valid
    if(!_arrayShowIDs || !_arrayHideIDs)
    {
        return;
    }
    
    // Split apart the supplied IDs
    _arrayShowIDs = _arrayShowIDs.split("|");
    _arrayHideIDs = _arrayHideIDs.split("|");
    
    // Hide the objects
    for(var i = 0; i < _arrayHideIDs.length; i++)
	{
	    if(!_arrayHideIDs[i]) { continue; }
	    // Hide the element
	    hideElement(_arrayHideIDs[i]);
	}
    
    // Show the objects
    for(var i = 0; i < _arrayShowIDs.length; i++)
	{
	    if(!_arrayShowIDs[i]) { continue; }
	    // Show the element
	    showElement(_arrayShowIDs[i]);
	}
}

// This fades an element on the page before hiding it
var hideElementTimeout = null; // houses the id of the last hideElement timeout from the fadeElement function
var valueElementTimeout = null; // houses the id of the last setValue timeout from the fadeElement function
var idElementTimeout = null; // houses the id of the last elementID timeout from the fadeElement function
function fadeElement(_elementID, _fadeLevel, _resetText, _hideAfterCompletion)
{
    // Step 1.  Get the object
    if(!_elementID) { return; }
    var objElement = getElement(_elementID);
    if(!objElement || !objElement.style) { return; }
    if(!_fadeLevel) { _fadeLevel = 0; } // reset fade level to instant fade
	if(_hideAfterCompletion == null || typeof(_hideAfterCompletion) == "undefined") { _hideAfterCompletion = 1; } // reset the hide after completion to true
    else { _fadeLevel = parseInt(_fadeLevel); } // get the fade level as an integer
    // If we have timeouts going from a previous operation
    if(idElementTimeout && idElementTimeout == _elementID)
    {
        if(hideElementTimeout)
        {
            clearTimeout(hideElementTimeout);
        }
        if(valueElementTimeout)
        {
            clearTimeout(valueElementTimeout);
        }
    }
    
    // Step 2.  Determine the background color
    var szBackgroundColor = "#b1d1a3";
    switch(_fadeLevel)
    {
        case 3:
            szBackgroundColor = "#b1d1a3";
            break;
        case 2:
            szBackgroundColor = "#d1e5c8";
            break;
        case 1:
            szBackgroundColor = "#e5f0e0";
            break;
        case 0:
            // We're done fading, show a standard white background and set the element to hide
            szBackgroundColor = "#fff";
            idElementTimeout = _elementID;
			if(_hideAfterCompletion == 1)
			{
            	hideElementTimeout = setTimeout("hideElement('"+_elementID+"')", 3000);
			}
            if(_resetText)
            {
               valueElementTimeout = setTimeout("setValue('"+_elementID+"', '"+_resetText+"')", 3050);
            }
            break;
        default:
            // We don't understand this value, so we'll reset it and start at 3
            szBackgroundColor = "#b1d1a3";
            _fadeLevel = 3;
            break;
    }
    // Update the _fadeLevel for the next pass
    _fadeLevel = _fadeLevel - 1;
    
    // Step 3.  Change the background color of the element
    objElement.style.backgroundColor = szBackgroundColor;
    
    // Step 4.  Call this same method again with the new fade level
    if(_fadeLevel >= 0)
    {
        if(_resetText)
        {
            setTimeout("fadeElement('"+_elementID+"','"+_fadeLevel+"','"+_resetText+"', '"+_hideAfterCompletion+"')", 1500);
        }
        else
        {
            setTimeout("fadeElement('"+_elementID+"','"+_fadeLevel+"',null,'"+_hideAfterCompletion+"')", 1500);
        }
    }
}


// -------------- AJAX Wish List Functions ------------- //
// This is a global parameter used by the send e-mail function to perform AJAX calls
var g_objWishList = null;
var g_bSendingEmail = null;
// This function sends an e-mail about a wish list to another user
function WishList_SendEmail(emailButton, wishListID, senderEmailBox, senderNameBox, emailAddressBox, emailMessageBox, hideDiv, showDiv, messageID, errorMessageID)
{
    // Parameters:
    //      emailButton = the ID for the button <input> tag that called this function (we'll change it's value to show it's posting)
    //      wishListID = the ID for the <input> tag that contains the WishListID to send the e-mail for
    //      senderEmailBox = the ID of the <input> tag containing the sender's email address
    //      senderNameBox = the ID of the <input> tag containing the sender's name
    //      emailAddressBox = the ID of the <input> tag containing the email address (comma delimited)
    //      emailMessageBox = the ID of the <input> tag containing the body of the email
    //      hideDiv = the ID of the <div> tag to collapse after the AJAX call completes
    //      showDiv = the ID of the <div> tag to show after the AJAX call completes
    //      messageID = the ID of the HTML tag to set the innerHTML value with the AJAX call results
    //      errorMessageID = the ID of the <li> tag to set the innerHTML valeu with the AJAX call results
    
    // Make sure the parameters are valid
    if(!emailAddressBox || !emailMessageBox || !wishListID || !senderEmailBox || !senderNameBox || !errorMessageID || g_bSendingEmail) { return; }
    var objEmailAddressBox = getElement(emailAddressBox);
    var objEmailMessageBox = getElement(emailMessageBox);
    var objWishListID = getElement(wishListID);
    var objSenderEmailBox = getElement(senderEmailBox);
    var objSenderNameBox = getElement(senderNameBox);
    var objErrorMessage = getElement(errorMessageID);
    if(!objEmailAddressBox || !objEmailMessageBox || !objWishListID || !objSenderEmailBox || !objSenderNameBox || !objErrorMessage) { return; }
    if(!hideDiv) { hideDiv = ""; }
    if(!showDiv) { showDiv = ""; }
    if(!messageID) { messageID = ""; }
    
    // Indicate the email is being sent
    if(emailButton) {
        g_bSendingEmail = getValue(getElement(emailButton));
        ShowTextEllipses(emailButton, "Sending Email.");
    }
    
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=wishlist_send-email&emailAddress=" + escape(getValue(objEmailAddressBox)) +
        "&emailMessage=" + escape(getValue(objEmailMessageBox)) + "&hideDiv=" + escape(hideDiv) + "&showDiv=" + escape(showDiv) +
        "&messageID=" + escape(messageID) + "&emailButton=" + escape(emailButton) +
        "&wishList=" + escape(getValue(objWishListID)) + "&senderEmail=" + escape(getValue(objSenderEmailBox)) + "&senderName=" +
        escape(getValue(objSenderNameBox)) + "&errorMessageID=" + errorMessageID;
        
    // Create an ajax object
	g_objWishList = new BellwetherAjax(null, null);
	g_objWishList.AjaxRequest(szAjaxRequest);
	
	// Create a call to check the status of the ajax request every 100 miliseconds
	setTimeout("WishList_SendEmail_Check()", 250);
}
// This function checks if the ajax request has been completed and displays the result
function WishList_SendEmail_Check()
{
	// Parameters:
	//		None --- there is a global AJAX object and it's result will pass back all necessary IDs
	
	// Make sure we have an ajax request in progress
	if(!g_objWishList) { return; }
    
	// Check if the ajax call is complete
    if(g_objWishList.HasResponse())
    {
        // Get the response 
        var szResponse = g_objWishList.AjaxResult();
        if(!szResponse) { return; }
        
        // Parse the response
        var szResponseValue = "", szHideDivID = "", szShowDivID = "", szMessageID = "", szEmailButtonID = "", szClearInputs = "0",
            szErrorMsgID = "";
        szResponseValue = GetXmlNodeValue(szResponse, "ReturnValue");
        szHideDivID = GetXmlNodeValue(szResponse, "HideDivID");
        szShowDivID = GetXmlNodeValue(szResponse, "ShowDivID");
        szMessageID = GetXmlNodeValue(szResponse, "MessageID");
        szEmailButtonID = GetXmlNodeValue(szResponse, "ButtonID");
        szErrorMsgID = GetXmlNodeValue(szResponse, "ErrorMsgID");
        szClearInputs = GetXmlNodeValue(szResponse, "ClearInputs");
        szEmailMessage = GetXmlNodeValue(szResponse, "EmailMessage");
		
		// Update the button to its normal text
        if(szEmailButtonID)
        {
            // Add back the onclick
            StopTextEllipses();
            setValue(getElement(szEmailButtonID), g_bSendingEmail);
            g_bSendingEmail = null;
        }
		
		// Hide the hide div and show the show div
		var bClearInputs = false;
		if(szClearInputs && szClearInputs == "1")
		{
		    bClearInputs = true;
		}
		
		// Update the page
		if(bClearInputs)
		{
		    if(!szEmailMessage) { szEmailMessage = ""; }
		    
		    var szInnerHTML =
		        '<h3 class="center">E-mails Sent</h3>' +
		        '<p id="wishlist_popup_send-email_successMessage" class="center confmessage">' + unescape(szEmailMessage) + '</p>' +
		        '<div class="clear">' +
		            '<a href="#" onclick="showMessage(&quot;wishlist_send-email&quot;, null, 0, 0); return false;">' +
		            '<img src="/images/buttons/btn_close2.gif" alt="close" class="right" />' +
		            '</a>' +
		        '</div>';
		        
		    setValue(getElement(szHideDivID), szInnerHTML);
		    fadeElement('wishlist_popup_send-email_successMessage', 3, null, false);
		}
		else
		{
		    // Update the message tag
		    if(szErrorMsgID)
		    {
		        var objErrorMsg = getElement(szErrorMsgID);
		        showElement(szErrorMsgID);
		        setValue(objErrorMsg, szResponseValue);
		        setValue(objErrorMsg, szResponseValue);
		        fadeElement(objErrorMsg, 3, null, true);
		    }
		}
        
		// Reset the comment ajax object
		g_objWishList = null;
		
        return;
    }
    else
    {
        // If we don't have a response, we'll wait 250 miliseconds before trying again
        setTimeout("WishList_SendEmail_Check()", 250);
    }
}

// This function adds an item to a person's active wish list
function WishList_AddToWishList(linkID, partNO, vNotes, modelID, engID, qtyID)
{
    // Parameters:
    //      linkID = the ID for the <a> tag that called this function
    //      partNO = the partNO to add to the wish list
    //      qtyID = the ID for the <input> tag that contains the part quantity
    //      vNotes = the PQs in an post->escape() string for the PartNO to add to the wish list
    //      modelID = the ModelID for the vehicle used when buying the part
    //      engID = the EngID for the vehicle used when buying the part
    
    // Make sure the parameters are valid
    if(!linkID || !partNO || !qtyID) { return; }
    
    // Get the objects
    var objLink = getElement(linkID);
    var objQty = getElement(qtyID);
    
    if(!objLink || !objQty) { return; }
    
    var szText = GetRegexValue(getValue(objLink), "added(.+)", 1);
    if(szText && szText.length > 0)
    {
        // This has already been updated
        window.location = objLink.href;
        return;
    }
    
    // Get the first half of the PartNO (in case it contains vehicle or color information)
    var aPartNO = partNO.split("|");
    
    // Define our static variables
    var szPopupID = "wishlist_added-to-wishlist";
    var szContentID = "wishlist_added-to-wishlist_dynamicContent";
    
    // Get the quantity value
    var nQty = parseInt(getValue(objQty), 10);
    if(nQty == null || nQty < 1) { nQty = 1; }
    var nModelID = parseInt(modelID, 10);
    if(nModelID == null || nModelID < 1) { nModelID = 0; }
    var nEngID = parseInt(engID, 10);
    if(nEngID == null || nEngID < 1) { nEngID = 0; }
        
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=wishlist_add-to-wishlist&partNO=" + escape(aPartNO[0]) +
        "&qty=" + nQty + "&vNotes=" + vNotes + "&modelID=" + nModelID + "&engID=" + nEngID + "&linkID=" + escape(linkID) +
        "&popupID=" + escape(szPopupID) + "&contentID=" + escape(szContentID);
        
    // Create an ajax object
	g_objWishList = new BellwetherAjax(null, null);
	g_objWishList.AjaxRequest(szAjaxRequest);
	
	// Show that we are adding this item
    ShowTextEllipses(linkID, "Adding.");
	
	// Create a call to check the status of the ajax request every 100 miliseconds
	setTimeout("WishList_AddToWishList_Check()", 250);
}
// This function checks if the ajax request has been completed and displays the result
function WishList_AddToWishList_Check()
{
	// Parameters:
	//		None --- there is a global AJAX object and it's result will pass back all necessary IDs
	
	// Make sure we have an ajax request in progress
	if(!g_objWishList) { return; }
    
	// Check if the ajax call is complete
    if(g_objWishList.HasResponse())
    {
        // Get the response 
        var szResponse = g_objWishList.AjaxResult();
        if(!szResponse) { StopTextEllipses(); return; }
        
        // Parse the response
        var szResponseValue = "", szLinkID = "", szPopupID = "", szContentID = "",
            szWishListID = "", szWishListsDelimited = "", szPartDescr = "",
            szPartLC = "", szPartImage = "", szPartNO = "", szWishListName = "";
        szResponseValue = GetXmlNodeValue(szResponse, "ReturnValue");
        szLinkID = GetXmlNodeValue(szResponse, "LinkID");
        szPopupID = GetXmlNodeValue(szResponse, "PopupID");
        szContentID = GetXmlNodeValue(szResponse, "ContentID");
        szWishListID = GetXmlNodeValue(szResponse, "WishListID");
        szPartDescr = GetXmlNodeValue(szResponse, "PartDescr");
        szPartLC = GetXmlNodeValue(szResponse, "PartLC");
        szPartImage = GetXmlNodeValue(szResponse, "PartImage");
        szPartNO = GetXmlNodeValue(szResponse, "PartNO");
        szWishListsDelimited = GetXmlNodeValue(szResponse, "WishLists");
        szWishListName = GetXmlNodeValue(szResponse, "WishListName");
        
        if(szWishListID) { szWishListID = unescape(szWishListID); }
        
        // Check if we need to show a pop up
        if(szResponseValue == "1" && szWishListName && szPartNO && szPartLC && szPartImage && szPartDescr && szPopupID && szContentID)
        {
            // Since we have a response for popup content, we will show the popup
            szWishListName = unescape(szWishListName);
            szPartNO = unescape(szPartNO);
            szPartDescr = unescape(szPartDescr);
            szPopupID = unescape(szPopupID);
            szContentID = unescape(szContentID);
            
            // Create the innerHTML for the content
            var szInnerHTML =
                '<h3 class="center">' + szPartDescr + '</h3>' +
                '<div class="hr"></div>' +
                '<img src="/aux_incl/images.ashx?i=' + szPartImage + '&partNo=' + szPartLC + '&w=145&h=145" alt="" class="left" />' +
                //'<p><small>Part Number: ' + szPartNO + '</small></p>' +
                '<p class="confmessage"><small>Item successfully added.<br />Added to: <a href="/wishlist/manage-list.aspx?id=' + szWishListID + '">' + szWishListName + '</a></small></p>';
            if(szWishListsDelimited)
            {
                szWishListsDelimited = unescape(szWishListsDelimited);
                szInnerHTML = szInnerHTML +
                '<p>Move to another wish list:</p>' +
                '<span id="wishlist_added-to-wishlists_movemsg" class="invis"></span>' +
                '<select id="wishlist_added-to-wishlists_moveitem" name="wishlist_added-to-wishlists_moveitem" onchange="WishList_MoveWishListItem(&quot;wishlist_added-to-wishlists_moveitem&quot;, &quot;wishlist_added-to-wishlists_movemsg&quot;);"></select>';
            }
            szInnerHTML = szInnerHTML +
                '<div class="clear"></div>' +
                '<div class="disc center">' +
                    '<p><a href="/wishlist/manage-list.aspx?id=' + szWishListID + '">View Wish List</a> See all the items in your wish list</p>' +
                '</div>' +
                '<div class="clear">' +
                    '<a href="#" onclick="showMessageCentered(&quot;' + szPopupID + '&quot;); return false;">' +
                        '<img src="/images/buttons/btn_close2.gif" alt="close" class="right" />' +
                    '</a>' +
                '</div>';
            
            // Set the popup content
            setValue(getElement(szContentID), szInnerHTML);
            // Populate the drop down
            if(szWishListsDelimited)
            {
                var objDropDown = getElement('wishlist_added-to-wishlists_moveitem');
                clearDropDown(objDropDown);
                populateDropDown(objDropDown, szWishListsDelimited);
            }
            // Show the popup
            showMessageCentered(szPopupID);
        }
        
		// Stop the ellipses
		StopTextEllipses();
		
		// Show that the item has been added
		var objLink = getElement(szLinkID);
		if(szResponseValue == "1")
		{
		    setValue(objLink, 'Added. View wish list.');
		    objLink.href = '/wishlist/manage-list.aspx?id=' + szWishListID;
		}
		else
		{
		    setValue(objLink, 'Add failed. Please try again.');
		}
		
		fadeElement(szLinkID, 3, null, false);
		
		// Reset the comment ajax object
		g_objWishList = null;
		
        return;
    }
    else
    {
        // If we don't have a response, we'll wait 250 miliseconds before trying again
        setTimeout("WishList_AddToWishList_Check()", 250);
    }
}
// This function moves a wish list item to a new wish list
function WishList_MoveWishListItem(dropdownID, confMessageID)
{
    // Parameters:
    //      dropdownID = the ID for the <select> drop down tag that contains wish list move information
    //      confMessageID = the ID for the <span> tag to place the confirmation message
    
    // Make sure the parameters are valid
    if(!dropdownID || !confMessageID) { return; }
    
    // Make sure the objects are valid
    var objDropDown = getElement(dropdownID);
    var objConfMessage = getElement(confMessageID);
    if(!objConfMessage || !objDropDown || !objDropDown.value) { return; }
    
    // Update the confirmation message text
    setValue(confMessageID, "");
    
    // Make sure the selected value is valid
    var aIdArray = objDropDown.value.split("^");
    if(aIdArray.length < 3) { return; }
    
    // Get the values
    var nWishListID = aIdArray[0];
    var nWishListItemID = aIdArray[1];
    var nNewWishListID = aIdArray[2];
    
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=wishlist_move-wishlist-item&wishListID=" + escape(nWishListID) +
        "&wishListItemID=" + escape(nWishListItemID) + "&newWishListID=" + escape(nNewWishListID) +
        "&confMessageID=" + escape(confMessageID) + "&dropdownID=" + escape(dropdownID);
        
    // Create an ajax object
	g_objWishList = new BellwetherAjax(null, null);
	g_objWishList.AjaxRequest(szAjaxRequest);
	
	// Show that we're moving this item
	showElement(confMessageID);
	ShowTextEllipses(confMessageID, "Moving.");
	
	// Create a call to check the status of the ajax request every 100 miliseconds
	setTimeout("WishList_MoveWishListItem_Check()", 250);
}
// This function checks if the ajax request has been completed and displays the result
function WishList_MoveWishListItem_Check(objDropDown, confMessageID)
{
	// Parameters:
	//		None --- there is a global AJAX object and it's result will pass back all necessary IDs
	
	// Make sure we have an ajax request in progress
	if(!g_objWishList) { return; }
    
	// Check if the ajax call is complete
    if(g_objWishList.HasResponse())
    {
        // Get the response 
        var szResponse = g_objWishList.AjaxResult();
        if(!szResponse) { StopTextEllipses(); return; }
        
        // Parse the response
        var szResponseValue = "", szConfMessageID = "", szDropDownID = "", szWishListsDelimited = "", szConfMessage = "";
        szResponseValue = GetXmlNodeValue(szResponse, "ReturnValue");
        szConfMessageID = GetXmlNodeValue(szResponse, "ConfMessageID");
        szConfMessage = GetXmlNodeValue(szResponse, "ConfirmMessage");
        szDropDownID = GetXmlNodeValue(szResponse, "DropDownID");
        szWishListsDelimited = GetXmlNodeValue(szResponse, "WishLists");
        
        // Check if the move was successful
        if(szResponseValue == "1" && szWishListsDelimited && szDropDownID)
        {
            // Populate the drop down
            szDropDownID = unescape(szDropDownID);
            szWishListsDelimited = unescape(szWishListsDelimited);
            
            var objDropDown = getElement(szDropDownID);
            clearDropDown(objDropDown);
            populateDropDown(objDropDown, szWishListsDelimited);
        }
        
		// Stop the ellipses
		StopTextEllipses();
		
		if(szConfMessageID && szConfMessage)
		{	
		    szConfMessageID = unescape(szConfMessageID);
		    	
		    setValue(getElement(szConfMessageID), szConfMessage);		
		    fadeElement(szConfMessageID, 3, null, false);
		}
		
		// Reset the comment ajax object
		g_objWishList = null;
		
        return;
    }
    else
    {
        // If we don't have a response, we'll wait 250 miliseconds before trying again
        setTimeout("WishList_MoveWishListItem_Check()", 250);
    }
}

// -------------- AJAX Gift Card Functions ------------- //
// This is a global parameter used to perform AJAX calls for Gift Card functions
var g_objGiftCard = null;
var g_bAddingGiftCard = false;

// This function adds a gift card to the person's cart and refreshes the page
function GiftCard_AddToCart(partDropDownID, statusMessageID, popupID)
{
    // Parameters:
    //      partDropDownID = the ID of the <select> tag to get the PartNO of the Gift Card from
    //      statusMessageID = the ID of the XHTML tag to update as we process
    //      popupID = the ID of the popup to show after the adding is complete
    
    // Make sure the parameters are valid
    if(!partDropDownID) { return; }
    var objDropDown = getElement(partDropDownID);
    if(!objDropDown) { return; }
    
    var szStatusMessageID = "";
    if(arguments.length > 1) { szStatusMessageID = escape(unescape(statusMessageID)); }
    var szPopupID = "";
    if(arguments.length > 2) { szPopupID = escape(unescape(popupID)); }
    
    // Show that we are adding the item
    if(arguments.length > 1)
    {
        setValue(statusMessageID, "Adding to Cart.");
        showElement(statusMessageID);
        ShowTextEllipses(statusMessageID, "Adding To Cart.");
    }
    
    // Add the item to the cart
    
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=giftcard_addtocart&partNO=" + escape(getValue(objDropDown)) +
        "&confMessageID=" + szStatusMessageID + "&popupID=" + szPopupID +
        "&loc=" + escape(window.location);
        
    // Create an ajax object
	g_objGiftCard = new BellwetherAjax(null, null);
	g_objGiftCard.AjaxRequest(szAjaxRequest);
	
	// Create a call to check the status of the ajax request
	setTimeout("GiftCard_AddToCart_Check()", 250);
}
// This function adds a gift card to the person's cart and refreshes the page
function GiftCard_AddToCart_Email(partDropDownID, statusMessageID, popupID, nameTo, nameFrom, cardMessage, emailAddress)
{
    // Parameters:
    //      partDropDownID = the ID of the <select> tag to get the PartNO of the Gift Card from
    //      statusMessageID = the ID of the XHTML tag to update as we process
    //      popupID = the ID of the popup to show after the adding is complete
    //      nameTo = the ID of the <input> tag to get the name of the person who the gift card is To
    //      nameFrom = the ID of the <input> tag to get the name of the person who the gift card is From
    //      cardMessage = the ID of the <textarea> tag to get the message for the gift card
    //      emailAddress = the ID of the <input> tag to get the recipient email address from
    
    // Make sure the parameters are valid
    if(arguments.length < 7 || !partDropDownID || !statusMessageID || !popupID || !nameTo || !nameFrom || !cardMessage || !emailAddress) { return; }
    
    // Get the objects
    var objDropDown = getElement(partDropDownID);
    var objNameTo = getElement(nameTo);
    var objNameFrom = getElement(nameFrom);
    var objCardMessage = getElement(cardMessage);
    var objEmailAddress = getElement(emailAddress);
    if(!objDropDown || !objNameTo || !objNameFrom || !objCardMessage || !objEmailAddress) { return; }
    
    statusMessageID = escape(unescape(statusMessageID));
    popupID = escape(unescape(popupID));
    
    // Show that we are adding the item
    setValue(statusMessageID, "Adding to Cart.");
    showElement(statusMessageID);
    ShowTextEllipses(statusMessageID, "Adding To Cart.");
    
    // Add the item to the cart
    
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=giftcard_addtocart&partNO=" + escape(getValue(objDropDown)) +
        "&confMessageID=" + statusMessageID + "&popupID=" + popupID +
        "&to=" + escape(getValue(objNameTo)) + "&from=" + escape(getValue(objNameFrom)) +
        "&message=" + escape(getValue(objCardMessage)) + "&email=" + escape(getValue(objEmailAddress)) +
        "&loc=" + escape(window.location);
        
    // Create an ajax object
	g_objGiftCard = new BellwetherAjax(null, null);
	g_objGiftCard.AjaxRequest(szAjaxRequest);
	
	// Create a call to check the status of the ajax request
	setTimeout("GiftCard_AddToCart_Check()", 250);
}
// This function adds a gift card to the person's cart and refreshes the page
function GiftCard_AddToCart_Print(partDropDownID, statusMessageID, popupID, nameTo, nameFrom, cardMessage)
{
    // Parameters:
    //      partDropDownID = the ID of the <select> tag to get the PartNO of the Gift Card from
    //      statusMessageID = the ID of the XHTML tag to update as we process
    //      popupID = the ID of the popup to show after the adding is complete
    //      nameTo = the ID of the <input> tag to get the name of the person who the gift card is To
    //      nameFrom = the ID of the <input> tag to get the name of the person who the gift card is From
    //      cardMessage = the ID of the <textarea> tag to get the message for the gift card
    
    // Make sure the parameters are valid
    if(arguments.length < 6 || !partDropDownID || !statusMessageID || !popupID || !nameTo || !nameFrom || !cardMessage) { return; }
    
    // Get the objects
    var objDropDown = getElement(partDropDownID);
    var objNameTo = getElement(nameTo);
    var objNameFrom = getElement(nameFrom);
    var objCardMessage = getElement(cardMessage);
    if(!objDropDown || !objNameTo || !objNameFrom || !objCardMessage) { return; }
    
    statusMessageID = escape(unescape(statusMessageID));
    popupID = escape(unescape(popupID));
    
    // Show that we are adding the item
    setValue(statusMessageID, "Adding to Cart.");
    showElement(statusMessageID);
    ShowTextEllipses(statusMessageID, "Adding To Cart.");
    
    // Add the item to the cart
    
    // Generate the Ajax Request String
    var szAjaxRequest =
        g_szAjaxFile + "cmd=giftcard_addtocart&partNO=" + escape(getValue(objDropDown)) +
        "&confMessageID=" + statusMessageID + "&popupID=" + popupID +
        "&to=" + escape(getValue(objNameTo)) + "&from=" + escape(getValue(objNameFrom)) +
        "&message=" + escape(getValue(objCardMessage)) +
        "&loc=" + escape(window.location);
        
    // Create an ajax object
	g_objGiftCard = new BellwetherAjax(null, null);
	g_objGiftCard.AjaxRequest(szAjaxRequest);
	
	// Create a call to check the status of the ajax request
	setTimeout("GiftCard_AddToCart_Check()", 250);
}
// This function checks if the ajax request has been completed and displays the result
function GiftCard_AddToCart_Check()
{
	// Parameters:
	//		None --- there is a global AJAX object and it's result will pass back all necessary IDs
	
	// Make sure we have an ajax request in progress
	if(!g_objGiftCard) { return; }
    
	// Check if the ajax call is complete
    if(g_objGiftCard.HasResponse())
    {
        // Get the response 
        var szResponse = g_objGiftCard.AjaxResult();
        if(!szResponse) { return; }
        
        // Parse the response
        var szNewLocation = "";
        szNewLocation = GetXmlNodeValue(szResponse, "NewLocation");
        
        // Reset the comment ajax object
		g_objGiftCard = null;
        
        // Redraw the page
        if(szNewLocation) {
            window.location = unescape(szNewLocation);
        }
		
        return;
    }
    else
    {
        // If we don't have a response, we'll wait 250 miliseconds before trying again
        setTimeout("GiftCard_AddToCart_Check()", 250);
    }
}