﻿function Step3LicenseController(oNewModel, oNewView) {
	/****************************************************/
	/*                                                  */
	/*                                                  */
	/*                 Private Variables                */
	/*                                                  */
	/*                                                  */
	/****************************************************/
	var shopID = "7";
	var oModel;
	var oView;
	
	var oViewHelper;
	
	/****************************************************/
	/*                                                  */
	/*                                                  */
	/*                Accessors/Mutators                */
	/*                                                  */
	/*                                                  */
	/****************************************************/
	
	this.setModel = function(oNewModel) {
		if (oNewModel) {
			oModel = oNewModel;
			oModel.addObserver(this);
		}
	}
	
	this.setView = function(oNewView) {
		if (oNewView)
			oView = oNewView;
	}
	
    /****************************************************/
	/*                                                  */
	/*                                                  */
	/*                Observer Interface                */
	/*                                                  */
	/*                                                  */
	/****************************************************/
	
	this.update = function(oArguments) {
	    var sProductCategory = oArguments.category;
		var iIndex = oArguments.index;
		switch (oArguments.event) {
			case "License Item Added":
			case "License Item Modified":
			case "License Item Removed":
		    case "Region Changed":
		        oViewHelper.updateSummary(oModel.getLicenseItemArray(), oModel.getNetSuiteItems(), oModel.getPromotions(), oModel.getRegionByName(oModel.getRegion()));
		        $("buyNowLink").href = GenerateNetSuiteLink(oModel);
		        break;
		}
	}
	
	function format(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;
    
    };
    
	function GenerateNetSuiteLink(modelObject)
    {
        if (queryString("siteid") == "kiwi")
        {
            shopID = "9";
        }
        
        var rRegion = oModel.getRegion();
        var dataObject = oModel.getCurrentSubsidiaryAndCurrencyByRegion(rRegion);
        
        var licenses = modelObject.getNetSuiteLinkString();
        var redirectUrlBase = escape(format(netSuiteRedirectUrl, format("/c.{0}/sc.1/.f?buyid=multi&qtyadd=1&multi={1}", "638609", licenses),shopID));
        return format(netSuiteShopUrl, format("?selcurrency={2}&c={0}&selsubsidiary={1}&redirect={3}", "638609", dataObject.subsidiary, dataObject.currency, redirectUrlBase),shopID);
    }
    
    function queryString(parameter) { 
          var loc = location.search.substring(1, location.search.length);
          var param_value = false;

          var params = loc.split("&");
          for (i=0; i<params.length;i++) {
              param_name = params[i].substring(0,params[i].indexOf('='));
              if (param_name.toLowerCase() == parameter.toLowerCase()) {
                  param_value = params[i].substring(params[i].indexOf('=')+1)
              }
          }
          if (param_value) {
              return param_value;
          }
          else {
              return ""; //Here determine return if no parameter is found
          }
        }


	/****************************************************/
	/*                                                  */
	/*                                                  */
	/*                 Initialize Class                 */
	/*                                                  */
	/*                                                  */
	/****************************************************/
	
	this.setModel(oNewModel);
	this.setView(oNewView);
	
	if (oModel && oView) {
	    oViewHelper = new Step3LicenseViewHelper(oView);
	}
	
}