﻿
//Create Ajax Application namespace
YAHOO.namespace("Ajax");

//This is the default global response handler
YAHOO.Ajax.Return = {
    Success:function(o){ this.Process(o); },
    Failure:function(o){ },
    RefreshCart:function(o)
    {
       YAHOO.Ajax.Call.Start(null,o.argument[0],null,o.argument[1]);
    },
    Process:function(o)
    {
        if (o.argument[0])
        {
            if (!o.responseXML.documentElement.firstChild) { return; }
            var data = '';
            //Lets Append all XML data into a single response value
            for (var i=0; i<o.responseXML.documentElement.childNodes.length; i++) {
                data += o.responseXML.documentElement.childNodes[i].nodeValue;
            }
            StoreContainer.innerHTML = data;
            
            //This Fixes the minHeight issue with some browsers
//                var ul = document.getElementById("ProductMaster");
//                if (ul!=null)
//                {
//                    for (var i=0; i < ul.childNodes.length; i++)  {
//                        if ((ul.childNodes[i].nodeName.toUpperCase() == "LI") && (ul.childNodes[i].clientHeight < 285)) {
//                            ul.childNodes[i].style.height = "285px";
//                        }
//                    }
//                }
            //End
            
            //This moves the page scroll back to the top as if the page is loading for the first time.
                scroll(0,0);
            //End
        }
    }
};

//This is the default global call back parameters
var globalCallback = {
    success:YAHOO.Ajax.Return.Success,
    failure:YAHOO.Ajax.Return.Failure,
    scope:YAHOO.Ajax.Return,
    argument:[true]
};

//This is the default global call back parameters
var globalCallbackNoWrite = {
    success:YAHOO.Ajax.Return.Success,
    failure:YAHOO.Ajax.Return.Failure,
    scope:YAHOO.Ajax.Return,
    argument:[false]
};

//This is the default Yahoo Ajax hendler for calling a web service method
YAHOO.Ajax.Call = 
{ 
    //This Attempts to integrate the YUI History Manager for page history
	Start:function(url, webmethod, callback, parameters)
    {
        //This Try/Catch handles Unsupported browsers such as Opera and ensures they can still use the store
        try
        {
            //Let Strip out Cart update calls to only return the cart so we don't update cart during navigation
            if ((webmethod.indexOf('Cart') > -1) && (webmethod!="GetShoppingCart") && (webmethod!="CartCheckout") && (webmethod!="CartCheckoutAllPassUser") && (webmethod!="CartCheckoutAllPassUserWithCouponCode"))
            {
                var CartCallback = 
                {
                    success:YAHOO.Ajax.Return.RefreshCart,
                    failure:YAHOO.Ajax.Return.Failure,
                    scope:YAHOO.Ajax.Return,
                    argument: ["GetShoppingCart", parameters]
                }
                //Set default URL if not provided
                if (url==null) { url = YahooAjaxGlobalUrl; }
                //Make the update call with an empty return then make the GetShoppingCart call to refresh the UI  
                parameters = Salem.String.Format('storeId={0}&cartId={1}&CartLocation={2}&PostPage={3}&{4}', Salem.Global.StoreID, Salem.Global.CartID.Store, Salem.Global.CartLocation.Store, PostPage, parameters);
                YAHOO.util.Connect.asyncRequest('POST', url+webmethod, CartCallback, parameters);
            }
            else
            {
                //alert(webmethod+'|'+parameters);
                //Register the current Ajax request with the History Manager
                YAHOO.util.History.navigate("history", webmethod+'|'+parameters);
            }
        }
        catch (ex) 
        {
            //alert(ex.message);
            //Set default URL if not provided
            if (url==null) { url = YahooAjaxGlobalUrl; }
            //Set default CallBack if not provided
            if (callback==null) { callback = globalCallback; }
            //Send the current request
    	    YAHOO.util.Connect.asyncRequest('POST', url+webmethod, callback, parameters);
    	}
    },
    
    //This is used as the response when the History Manager is implemented successfully
    Execute:function(url, webmethod, callback, parameters)
    {
        PageManager(webmethod);
        //Set default URL if not provided
        if (url==null) { url = YahooAjaxGlobalUrl; }
        //Set default CallBack if not provided
        if (callback==null) { callback = globalCallback; }
        //Send the current request
    	YAHOO.util.Connect.asyncRequest('POST', url+webmethod, callback, parameters);
    } 
};