// Global AJAX Server Page Name
//var AjaxServerPageName = 'http://localhost:4894/httpdocs/checkout/AjaxServer.aspx';
var AjaxServerPageName = 'http://www.tuccini.com/UnSecuredAjaxServer.aspx';

// Event Position
// var refreshCartSource, bDoNotSlideCart;
// Site URL
// var siteUrl = 'http://localhost:2197/'

// Global Declarations
// var lbViewCart, hdnItemCount;
// Request Xml
// var xmlResponse;

/* 
function refreshCart(e, pAjaxPageName, pDoNotSlideCart)
{
    refreshCartSource = e;
    bDoNotSlideCart = pDoNotSlideCart;    

    var iCartItemsQuantity,iOBIndex,iCBIndex;
    lbViewCart = document.getElementById('_ctl0_ajaxCart_lbViewCart');
    hdnItemCount = document.getElementById('_ctl0_ajaxCart_hdnNoOfItems');
    pItemCount = hdnItemCount.value;
     
    if(pItemCount <= 0)
    {
        alert('There are no items in the cart');
        return false;
    }    
    var sViewCart = GetTextValue(lbViewCart);
    iOBIndex = sViewCart.indexOf('(');
    iCBIndex = sViewCart.indexOf(')');
    iCartItemsQuantity = parseInt(sViewCart.substring(iOBIndex+1,iCBIndex))
    //IF the number of items in cart is greater than 5 we should redirect to CartView page. No need for AJAX request
    if (iCartItemsQuantity == 0) 
    {
        alert('There are no items in the cart');
        return false;
    }
    if (pItemCount > 5) return true;
    
    var requestXML = "<reqXml><input_data rid='2' /></reqXml>";				    
    $.ajax({
        url: pAjaxPageName
        , type: "POST"
        , contentType: "text/xml"
        , processData: false
        , data: requestXML
        , dataType: "xml"
        , success: function(response) 
        {
            var xmlResponse = response.documentElement;
            if (xmlResponse != null && xmlResponse != undefined) {
                processRefreshCart(xmlResponse);
            }
        }
    });
    return false;
}

function processRefreshCart(cartResponse)
{
    var cart1Image,spnCartItemPrice,divCartItemTotal,spnCartItemQty,divCartItemGroup,cartHTML;
    var ifcart2Container,cart2Container,lblCart2SubTotal;
    var cartHTML = '';
    var resID = $(cartResponse).find("resData").attr("resid");
    if (resID == 2)
    {
        cartHTML += "<table border=0 cellspacing=\"0\" width=\"700\" cellpadding=\"4\" border=\"0\" summary=\"cart contents\">";                        
        cartHTML += "<tr bgcolor='#dedfe0'><th width=\"92\" style=\"text-align:center;\">Image</th><th width=\"150\">Item</th><th width=\"55\">Price</th><th width=\"34\">Qty</th><th width=\"66\">Total</th><th width=\"67\">Delete</th></tr>";    
        // Looping through the Cart Items and framing the AJAX cart
        $(cartResponse).find("mf_order_items").each(function()
        {
            cartHTML += "<tr><td colspan=\"8\"><div style=\"font-size:1px; border-top:1px solid #8d8d8d;\">&nbsp;</div></td></tr>";
            cartHTML += "<tr valign='top'><td class='center'>";
		    cartHTML += $.format("<img src='{0}' width='80' height='60' style='border-style:none' alt='SampleImg' /><br />",$(this).find("item_image_url").text());
            cartHTML += "</td><td class='cartdata'>";
			cartHTML += $.format("<div class='bold linepad'>{0}\n Item#: {1}</div></td>",$(this).find("order_item_description").text(),$(this).find("order_item_cd").text());
            cartHTML += $.format("<td class='cartdata' id='CartPrice{0}'>{1}</td>",$(this).find("item_guid").text(),$(this).find("order_item_unit_price").text());
            cartHTML += $.format("<td class='cartdata'><input type=\"text\" onchange=TrackChange('{0}','{1}'); name=\"CartQuantity{0}\" id=\"CartQuantity{0}\" size='2' maxlength='3' class=\"qtyBox\" onkeypress=\"return CheckNumeric(event);\" onblur=\"CheckValidQuantity(event);\" value={2} style=\"width:20px;\">",$(this).find("item_guid").text(),$(this).find("order_item_guid").text(),$(this).find("order_item_quantity").text());
            cartHTML += $.format("<br /></td><td><table border=0 cellspacing=0 cellpadding=0><tr><td colspan=2><div style=\"padding:2px;\" id=\"CartTotal{0}\">{1}</div></td></tr></table></td>",$(this).find("item_guid").text(),$(this).find("item_price_total").text());
            cartHTML += "<td class='cartdata smaller'>";
            cartHTML += $.format("<div style='margin-bottom:2px;' align='center'><img src='{0}images/delete.gif' style=\"cursor:pointer;\" onclick=\"removeCartItem('{0}site/cart.aspx','{1}','{2}');\" alt='Remove' /><br />",siteUrl,$(this).find("order_item_guid").text(),$(this).find("item_guid").text());
            cartHTML += "</div></td></tr>";
        });
        cartHTML += "</table>";
        ifcart2Container = $("#ifcart2Container");
        cart2Container = $('#cart2Container')[0];
        $('#cart2div').html(cartHTML);
        lblCart2SubTotal = $('#cart2SubTotal');
        lblCart2SubTotal.text($(cartResponse).find("mf_orders").find("order_sub_total").text());
        
		// Populate Last Cart Item Data
		populateLCI($(cartResponse).find("lciData")[0]);			
		if (refreshCartSource != null && refreshCartSource != undefined) {
		    var lblViewCart = $('#_ctl0_ajaxCart_lbViewCart')[0];
		    oCart2 = $('#cart2Container')[0];
		    oCart2.style.top = findPosY(lblViewCart) + 27;
		    oCart2.style.left = findPosX(lblViewCart) - 17;
		    if (bDoNotSlideCart == null || bDoNotSlideCart == undefined || bDoNotSlideCart == 'false')
		        ypSlideOutMenu.showMenu('cart2');
		}        
    }
}

// This funciton populates the last cart item
function populateLCI(lastCartItem) {
    var divCart = $('#_ctl0_ajaxCart_divCart')[0];
    if (lastCartItem != null && lastCartItem != undefined) {
        var lciImage, lciDesc, lciPrice,lciQuantity;
        lciImage = $('#_ctl0_ajaxCart_imgLastCartItemImg')[0];
        lciDesc = $('#_ctl0_ajaxCart_lblCartItemDesc')[0];
        lciPrice = $('#_ctl0_ajaxCart_lblCartItemPrice')[0];
        lciQuantity = $('#_ctl0_ajaxCart_lblCartItemQuantity')[0];

        if (lciImage != null && lciImage != undefined) {
            lciImage.src = $(lastCartItem).find("lci_image_url").text();
            lciDesc.text($(lastCartItem).find("lci_item_desc").text());
            lciPrice.text($(lastCartItem).find("lci_item_price").text());
            lciQuantity.text($(lastCartItem).find("lci_item_quantity").text());            
            divCart.style.display = 'block';
        }
    }
    else {
        divCart.style.display = 'none';
    }
} */

function addToCart(pItemGuId,pTxtQuantityId,pItemImageUrl,pItemShortDesc)
{
    var ddlQuantity = $('#' + pTxtQuantityId);
    // Encoding the special character '&' before including it in the request xml
    pItemShortDesc = pItemShortDesc.replace(/&/g,'&amp;');
    var requestXML = $.format("<reqXml reqID='2' item_guid='{0}' item_quantity='{1}' item_short_desc='{2}' />",pItemGuId,ddlQuantity.val(),pItemShortDesc);
    $.ajax({
        url: AjaxServerPageName
        , type: "POST"
        , contentType: "text/xml"
        , processData: false
        , data: requestXML
        , dataType: "xml"
        , success: function(response) 
        {
            var xmlResponse = response.documentElement;
            var resXml,cart1Image,ciTitle,ciCode,ciPrice,ciQuantity,cTotalQuantity,cSubTotal,oTextSpan1,oTextSpan2;
            if (xmlResponse != null && xmlResponse != undefined) {
                resXml = xmlResponse.firstChild;
                var resID = $(resXml).attr("resid");
			    if(resID == 1)
			    {
			        var result = $(resXml).attr("result");
			        cart1Image = $('#_ctl0_ajaxCart_imgCartItem')[0];
			        ciTitle = $('#_ctl0_ajaxCart_tdCITitle');
			        ciCode = $('#_ctl0_ajaxCart_lblCICode');
			        ciPrice = $('#_ctl0_ajaxCart_tdCIPrice');
			        ciQuantity = $('#_ctl0_ajaxCart_lblCIQuantity');
			        cTotalQuantity = $('#_ctl0_ajaxCart_lblTotalQuantity');
			        cSubTotal = $('#_ctl0_ajaxCart_tdSubTotal');
			        oTextSpan1 = $('#_ctl0_TextSpan1');
			        oTextSpan2 = $('#_ctl0_TextSpan2');
			        
		            cart1Image.src = pItemImageUrl;
		            ciPrice.html($(resXml).attr('item_unit_price'));
		            cSubTotal.html($(resXml).attr('item_price_total'));
                    ciTitle.html($(resXml).attr('item_title'));
                    if (parseInt($(resXml).attr('cartItemCount')) == 1)
                        cTotalQuantity.text($(resXml).attr('cartItemCount') + ' item in Bag');
                    else
                        cTotalQuantity.text($(resXml).attr('cartItemCount') + ' items in Bag');
                    oTextSpan1.text($(resXml).attr('cartItemCount'));
                    oTextSpan2.text($(resXml).attr('item_price_total'));                    
		            ciQuantity.text(ddlQuantity.val());
		            ShowLastCartItem();
		            setTimeout("HideLastCartItem();",5000);
			    }
            }
        }
        , error: function(response)
        {
        	alert(response);
        }
    });
    return false;    
}

function ShowLastCartItem() {
    $('#_ctl0_ajaxCart_divMiniCartPopup').middleCenter().slideDown("slow");
    $(window).scroll(function() {
        $('#_ctl0_ajaxCart_divMiniCartPopup').middleCenter();
    });
}
function HideLastCartItem() {
	$('#_ctl0_ajaxCart_divMiniCartPopup').fadeOut();
    $(window).unbind('scroll');
}

jQuery.fn.middleCenter = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.outerHeight()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.outerWidth()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

jQuery.fn.topLeft = function() {
    this.css("position", "absolute");
    this.css("top", "0px");
    this.css("left", "0px");
    return this;
}
jQuery.fn.scrollTopLeft = function() {
    this.css("position", "absolute");
    this.css("top", $(window).scrollTop() + "px");
    this.css("left", "0px");
    return this;
}

jQuery.fn.topRight = function() {
    this.css("position", "absolute");
    this.css("top", "0px");
    this.css("left", ($(window).width() - this.outerWidth()) + "px");
    return this;
}
jQuery.fn.scrollTopRight = function() {
    this.css("position", "absolute");
    this.css("top", $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.outerWidth()) + $(window).scrollLeft() + "px");
    return this;
}

jQuery.fn.bottomLeft = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.outerHeight()) + "px");
    this.css("left", "0px");
    return this;
}
jQuery.fn.scrollBottomLeft = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.outerHeight()) + $(window).scrollTop() + "px");
    this.css("left", "0px");
    return this;
}

jQuery.fn.bottomRight = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.outerHeight()) + "px");
    this.css("left", ($(window).width() - this.outerWidth()) + "px");
    return this;
}
jQuery.fn.scrollBottomRight = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.outerHeight()) + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.outerWidth()) + $(window).scrollLeft() + "px");
    return this;
}


