﻿AjaxSampling_Class = function() {
    //
    this.SampleBubbleID = "TLN_SMPL_Cntnr"
    //
    this.SAMPLE_JUST_ADDED_MAINLAYER = 'SAMPLE_JUST_ADDED_MAINLAYER';
    this.SAMPLE_JUST_ADDED_IMAGEID = 'SAMPLE_JUST_ADDED_IMAGEID';
    this.SAMPLE_JUST_ADDED_PRDNAME = 'SAMPLE_JUST_ADDED_PRDNAME';
    this.SampleImagePath = "/kiehls/img/product/sample/";
    //this.SamplePrefix = "_grey_s.jpg"; //_white_s ///kiehls/img/product/sample/sa818_m.jpg//put 3 to 1,
	this.SamplePrefix = "_m.jpg"; //_grey_s and _white_s is not need again.
    //
    this._maxSampleLineItem = 1;
    this.SAMPLE_LINE_MAX_REACHED_id = "SAMPLE_LINE_MAX_REACHED";
	//Change this for new promotion 20100729
    this._maxSampleAll = 9;
    this._maxSampleByRootCategory = 3;
	//this._maxSampleAll = 100;
    //this._maxSampleByRootCategory = 100;
    this.SAMPLE_MAX_REACHED_id = "SAMPLE_MAX_REACHED";
    this.SAMPLE_MAX_REACHED_FOR_CATEGORY_id = "SAMPLE_MAX_REACHED_FOR_CATEGORY";
    this.lastSampleCode = "";
    this.lastSampleParentPrdCode = "";
    this.lastSampleName = "";
    //
    this.ErrorMaxSampleLineIteamReached = "ErrorMaxSampleLineItemReached";
    this.ErrorMaxSampleAllLineIteamReached = "ErrorMaxSampleAllLineItemReached";

    this.SampleCounter = 0;
    /**
    *
    * Events
    */
    /**
    * the name of the function to be called before the popup opens
    */
    this.beforeOpen = null;

    this.beforeClose = null;

    this.AddSampleItemToBag = function(_sampleProdCode, _sampleParentPrdCode, _sampleName) {
	//----------------------
	var total_yong=0;
var lbyongnum = 0;
	var kiehslname='';
	$('.productList222 tr').each(function(){
						var jThis = $(this);
						var prdCode = $(this).attr("id");	
						pNum = parseInt(jThis.find('.number select').val(), 10);
							pPrc = parseFloat(jThis.find('.unitPrice').text().replace(",","").replace("¥",""));
							total_yong += pNum * pPrc; 							
							});
							
if($("#ifislb").length>0) { //#ifislb is place in paySample.html in top img part...
		//this._maxSampleByRootCategory=6;
		kiehslname="(仅限科颜氏kiehl's产品)";
		//-------------
	 	$('#linkSCSBU li img').each(function(){
		   var src_text = $(this).attr("src");
		    //alert(src_text);
		
		   if (src_text.indexOf("luxb_sample")!= -1){
			    lbyongnum = parseInt(lbyongnum)+1;
		   
		   }
		  		   
		   });	  
	
	 this._maxSampleByRootCategory=parseInt(lbyongnum)+3;
		
		//-----------------
		}
		else {this._maxSampleByRootCategory=3;
		var kiehslname="";
		}
							 
							if(total_yong<100){alert('对不起，只有购物超过100元'+kiehslname+'，才可以挑选试用装！');return false;}
							 
							//--------------------
        this.lastSampleCode = _sampleProdCode;
        this.lastSampleParentPrdCode = _sampleParentPrdCode
        this.lastSampleName = _sampleName;
		
		

        if (!IsAjaxLoaded()) { return false; }
        try {
            HideSampleShopCrtBubble();
            DoWait();
            MicrosoftFrance.MCS.Commerce.WS.AJAXProject.ShoppingCartWebService.AddSampleItemByCategory(GetCurrentCustomerID(), _sampleProdCode, this._maxSampleLineItem, this._maxSampleAll, this._maxSampleByRootCategory, this.AddSampleItemToBagResultHandler, this.AddSampleItemToBagOnError)

        }
		//{"customerId":"4cfa1b15-5375-4264-9eff-133e6c7c14ad","productCode":"S0250600U","maxSampleLineItem":1,"maxSampleAll":9,"maxSampleByCategory":3}
        catch (e) {
            DoDefault();
            window.status = e.message;
			//sample error test
        }
    }


    this.AddSampleItemToBagResultHandler = function(result) {
        DoDefault();     
        //_ShoppingCartBubble_Class.Hide();			
        //AjaxSamplingObject.Show();
        if (result.ActivityStatus == true) {
		$("#TLN_SMPL_Cntnr").show();
		 AjaxSamplingObject.SampleCounter = AjaxSamplingObject.SampleCounter + 1;       
         AjaxSamplingObject.PopulateLatestSampleAdded();
            //position the screen
            //show the bubble
       AjaxSamplingObject.PostBackSampleBubble(this.lastSampleCode);		
            // hide product bubble if opened
            // here we need to somwhoe display the sample bag bubble
            // later to do it.
			ManageIcons22();
			if($("#kieSSBox").hasClass("testbag_lb")) {$("#kieSSBox").addClass("testbag_lb_hover");
			}
        }
        else {
            if (result.ErrorString.indexOf('The maximum amount of the shopping cart is reach') != -1 && result.ErrorString.indexOf(':') != -1) {
                var error_array = result.ErrorString.split(':');
                error_array[0] = 'The total amount of your order may not exceed $';
                alert(error_array[0] + formatAsMoney(error_array[1].replace(" ", "")));
            }
            else {
                var _err = result.ErrorString;
                /*if ((_err == AjaxSamplingObject.SAMPLE_MAX_REACHED_id) || (_err == AjaxSamplingObject.SAMPLE_LINE_MAX_REACHED_id)) {
                    // if (_err.indexOf("SAMPLE_MAX_REACHED")!=-1 || _err.indexOf("SAMPLE_LINE_MAX_REACHED")!=-1) {
                    AjaxSamplingObject.ToggleErrorLayer(_err); //rules error
                }*/
				 if (_err == AjaxSamplingObject.SAMPLE_LINE_MAX_REACHED_id) {
                    alert('很抱歉，每件产品试用装您只能选择1次！');}
				else if(_err == AjaxSamplingObject.SAMPLE_MAX_REACHED_FOR_CATEGORY_id)
				{//alert('很抱歉，试用装每人仅限'+AjaxSamplingObject._maxSampleByRootCategory+'件');
				alert('很抱歉，您本次购物享用的试用装限额已满！');	
                   				
				}
                else {
                    alert(_err); //rules error
                }


            }
        }
    }

    this.ToggleErrorLayer = function(_ErrCode) {
        var obj = $get(_ErrCode);
        if (obj != null) { obj.style.display = ""; }
    }

    this.HideErrorLayers = function() {
        var obj1 = $get(this.SAMPLE_LINE_MAX_REACHED_id);
        var obj2 = $get(this.SAMPLE_MAX_REACHED_id);
        if (obj1 != null) { obj1.style.display = "none"; }
        if (obj2 != null) { obj2.style.display = "none"; }
    }
    //
    this.Show = function() {
        var obj = $get(this.SampleBubbleID);
        obj.style.visibility = 'visible';
        if (this.beforeOpen != null) {
            eval(this.beforeOpen + "();");
        }
        this.HideErrorLayers();
    }

    //
    this.Hide = function() {
        var obj = $get(this.SampleBubbleID);
       // obj.style.visibility = 'hidden';
        if (this.beforeClose != null) {
            eval(this.beforeClose + "();");
        }
        this.HideErrorLayers();
    }
    this.PopulateLatestSampleAdded = function() {
       $("#SAMPLE_JUST_ADDED_MAINLAYER").show();
	   $("#SAMPLE_JUST_ADDED_IMAGEID").attr('src',this.SampleImagePath + this.lastSampleParentPrdCode + this.SamplePrefix);
	   $("#SAMPLE_JUST_ADDED_PRDNAME").text(this.lastSampleName);
      }
    this.PostBackSampleBubble = function(smplPrdCode) {
	
	/* edit 11.11 by jason
        var mf = GetMainFormName();
        //var PostbackLinkID = GetElementID('SampleBubbleTriggerPartialPostBack',mf);
        var inputsmplObjID = GetElementID('samplelatestprdcode', mf);
        var objsmplInput = $get(inputsmplObjID);
        objsmplInput.value = smplPrdCode;
        //__doPostBack('ctl00$ctl00$brandlayout0$ctl00$ctl00$ctl00$ctl01$ctl03$SampleBubbleTriggerPartialPostBack','');
        //__doPostBack('ctl00$ctl00$brandlayout0$ctl00$ctl00$ctl00$ctl01$ctl03$ctl00$ctl00$ctl00$SampleBubbleTriggerPartialPostBack', '');
		*/
		var _et = document.getElementById('linkSCSBU');
		var _tt = _et.getElementsByTagName('a')[0];
		var _tid = _tt.id.replace(/_/g,'$');	
		__doPostBack(_tid, '');
		//$("#linkSCSBU").append('app--'+_tid);
		//app-lb-ctl00$ctl00$brandlayout0$ctl00$ctl00$ctl00$ctl00$ctl00$ctl00$ctl01$ctl01$ctl01$SampleBubbleTriggerPartialPostBack
		//  app--ctl00$ctl00$brandlayout0$ctl00$ctl00$ctl00$ctl00$ctl00$ctl00$ctl00$ctl02$ctl01$SampleBubbleTriggerPartialPostBack
		//__doPostBack('ctl00$ctl00$brandlayout0$ctl00$ctl00$ctl00$ctl00$ctl00$ctl00$ctl00$ctl02$ctl01$SampleBubbleTriggerPartialPostBack', '');	
    }
    this.AddSampleItemToBagOnError = function(result) {
        DoDefault();
    }
}

var AjaxSamplingObject = new AjaxSampling_Class();
AjaxSamplingObject.beforeOpen = "SampleCartBeforeOpen";
AjaxSamplingObject.beforeClose = "SampleCartBeforeClose";
function SampleCartBeforeOpen() {
    // position the screen
   // ScrollWindowToShoppingCart();
    //close the product bubble if on
    //ToggleProductBubble('hidden');
    //hide drop downs
   // ToggleDropDownsOnIE6(false);
    //manage the dislay related products
    //ManageIcons();
    //ToggleDisplayMenuLayers('ShoppingCartBubble_Div', '', 'hide', 'SearchDropDownContainer', '', 'hide');

}

function SampleCartBeforeClose() {
    //show drop downs
    ToggleDropDownsOnIE6(true);

}

function ManageIcons22() {
    //alert(CustomerSamplesSelected);
    var _CustomerSamplesSelected = CustomerSamplesSelected + AjaxSamplingObject.SampleCounter;
    //alert(AjaxSamplingObject.SampleCounter);
    //alert(_CustomerSamplesSelected);
    if (_CustomerSamplesSelected > 0) {
        if (_CustomerSamplesSelected == 1) {
            ManageSampleIcon("SAMPLEICON_1");
        }
        else if (_CustomerSamplesSelected == 2) {
            ManageSampleIcon("SAMPLEICON_1");
            ManageSampleIcon("SAMPLEICON_2");

        }
        else {
            ManageSampleIcon("SAMPLEICON_1");
            ManageSampleIcon("SAMPLEICON_2");
            ManageSampleIcon("SAMPLEICON_3");

        }

    }
}
function ManageSampleIcon(_imgID) {
    var obj = $get(_imgID);
    if (obj != null) {
        var src = obj.src;
		if (src.indexOf("kie_ico_blank.jpg") != -1) {
				src = src.replace("kie_ico_blank.jpg", "kie_ico_filled.jpg");
				obj.src = src;
		 }
        if (src.indexOf("DD_Smpl_ICN_Blank.gif") != -1) {
            src = src.replace("DD_Smpl_ICN_Blank.gif", "DD_Smpl_ICN_Filled.gif");
            obj.src = src;
        }
    }
}

