﻿function BuyNowModule(itemsToInit,generateProductLnk,generateStoreHomeLnk)
{
    try
    {
        var _arrPrices = null;
        
        var _currency = null;
        var _subsidiary = null;
        var _currencySign = null;
        var _generateProductLnk = typeof generateProductLnk != 'undefined' ? generateProductLnk : false;
		var _generateStoreHomeLnk = typeof generateStoreHomeLnk != 'undefined' ? generateStoreHomeLnk : false;
            
        var KIWI_STORE = "9";
        var SW_KIWI_SITE = "638609";
        var BASE_URL_FORMAT = "http://shopping.netsuite.com/app/site/backend/intl/setshopperregioninfo.nl{0}&n={1}";
        var REDIRECT_URL_FORMAT = "http://shopping.netsuite.com/app/site/backend/additemtocart.nl/n.{0}{1}";
        var REDIRECT_PRODUCT_PAGE_FORMAT = "http://shopping.netsuite.com/s.nl/c.{0}/n.{1}/sc.81/category.{2}/.f";
		var REDIRECT_HOME_PAGE_FORMAT = "http://shopping.netsuite.com/s.nl?c={0}&n={1}";
        
		var setPriceForProductAndControl = function(product, control)
        {
            try
            {
                if(_arrPrices && typeof _arrPrices[product] != 'undefined')
                {
                    var cntrl = document.getElementById(control);
                    
                    if(cntrl)
                    {
                        if( cntrl.nodeName.toUpperCase () != "AREA")
                        {
                            cntrl.innerHTML = _currencySign + _arrPrices[product];
                        }
                        
                        if(cntrl.nodeName.toUpperCase () == "AREA" || cntrl.nodeName.toUpperCase () == "A")
                        {
                            cntrl.href = generateStoreLink(product);
                        }
                    }
                }
            }
            catch(exc){}
        }
		
		var setStoreHomeLinkForControl = function(control, category)
        {
            try
            {
                var cntrl = document.getElementById(control);
                    
                if(cntrl)
                {                       
                    if(cntrl.nodeName.toUpperCase () == "AREA" || cntrl.nodeName.toUpperCase () == "A")
                    {
                        cntrl.href = generateStoreProductPageLink(category);
                    }
                }
            }
            catch(exc){}
        }
        
        var setPriceForProductAndControlHomePage = function(product, control, category)
        {
            try
            {
                if(_arrPrices && typeof _arrPrices[product] != 'undefined')
                {
                    var cntrl = document.getElementById(control);
                    
                    if(cntrl)
                    {
                        if( cntrl.nodeName.toUpperCase () != "AREA")
                        {
                            cntrl.innerHTML = _currencySign + _arrPrices[product];
                        }
                        
                        if(cntrl.nodeName.toUpperCase () == "AREA" || cntrl.nodeName.toUpperCase () == "A")
                        {
                            cntrl.href = generateStoreProductPageLink(category);
                        }
                    }
                }
            }
            catch(exc){}
        }
        
        var Format = function(text)
        {
            //check if there are two arguments in the arguments list
            if ( arguments.length <= 1 )
            {
                //if there are not 2 or more arguments there’s nothing to replace
                //just return the original text
                return text;
            }

            //decrement to move to the second argument in the array
            var tokenCount = arguments.length - 2;
            for( var token = 0; token <= tokenCount; token++ )
            {
                //iterate through the tokens and replace their placeholders from the original text in order
                text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
            }

            return text;
        };


        var generateStoreLink = function(bid)
        {
            var redirectURL = escape(Format(REDIRECT_URL_FORMAT,KIWI_STORE,Format("/c.{0}/sc.1/.f?buyid={1}&qty={2}",SW_KIWI_SITE,bid,1)));
            return Format(BASE_URL_FORMAT,Format("?selcurrency={0}&c={1}&selsubsidiary={2}&redirect={3}",_currency,SW_KIWI_SITE,_subsidiary,redirectURL), KIWI_STORE);
        }
        
        var generateStoreProductPageLink = function(category)
        {
            var redirectURL = escape(Format(REDIRECT_PRODUCT_PAGE_FORMAT,SW_KIWI_SITE,KIWI_STORE,category));
            return Format(BASE_URL_FORMAT,Format("?selcurrency={0}&c={1}&selsubsidiary={2}&redirect={3}",_currency,SW_KIWI_SITE,_subsidiary,redirectURL), KIWI_STORE);
        }
		
		var generateStoreHomeLink = function()
        {
            var redirectURL = escape(Format(REDIRECT_HOME_PAGE_FORMAT,SW_KIWI_SITE,KIWI_STORE));
            return Format(BASE_URL_FORMAT,Format("?selcurrency={0}&c={1}&selsubsidiary={2}&redirect={3}",_currency,SW_KIWI_SITE,_subsidiary,redirectURL), KIWI_STORE);
        }
        
        var getCurrencySign = function(currency)
        {
            switch(currency)
            {
                case "2":
                    return "£";
                case "4":
                    return "€";
                default:
                    return "$";
            }
        }
        
        // Get data related logic
        var _xhr = null;
        
        // Need to perform additional initialization logic due to IE
        var getXMLHTTP = function()
        {
            var xmlhttp;
            try 
            {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) 
            {
                try 
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (E) 
                {
                  xmlhttp = false;
                }
            }
            if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
                xmlhttp = new XMLHttpRequest();
            }
            return xmlhttp;
        }
        
        var getPricesForProducts = function()
        {
            var url = formatXHRUrl("/private/LicenseWrapper.php?alllicenses=all");
            _xhr = getXMLHTTP();
            
            //Hooray we're ready to get data
            _xhr.onreadystatechange = processRequest;
            _xhr.open( "GET", url, true );
            _xhr.send( null );
        }
        
        var processRequest = function() 
        {
            if ( _xhr.readyState == 4 && _xhr.status == 200 ) 
            {
                var info = eval("("+_xhr.responseText+")");

                // No parsing necessary with JSON!        
                _arrPrices = info.Prices; 
                _currency = info.Currency;
                _subsidiary = info.Subsidiary;
                _currencySign = getCurrencySign(_currency);    
                
                for(var i=0; i < itemsToInit.length; ++i)
                {
					if(!_generateStoreHomeLnk)
					{
	                    if(!_generateProductLnk)
	                    {
	                        setPriceForProductAndControl(itemsToInit[i].product, 
	                            itemsToInit[i].control);
	                    }
	                    else
	                    {
	                        setPriceForProductAndControlHomePage(itemsToInit[i].product, 
	                            itemsToInit[i].control, itemsToInit[i].category);
	                    }
					}
					else
					{
						setStoreHomeLinkForControl(itemsToInit[i].control, itemsToInit[i].category);
					}
				}
            }
        }
        
        var formatXHRUrl = function(url)
        {
            var prettyUrl = Format("{0}//{1}{2}",window.location.protocol, window.location.host,url);
            return prettyUrl;
        }
       
        getPricesForProducts();
    }catch(ex){}
}