
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="/img/product/sample/";
    this.SamplePrefix="_yellow_s.jpg";
    //
    this._maxSampleLineItem=1;
    this.SAMPLE_LINE_MAX_REACHED_id="SAMPLE_LINE_MAX_REACHED";
    this._maxSampleAll=3;
    this.SAMPLE_MAX_REACHED_id="SAMPLE_MAX_REACHED";
    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) {
             this.lastSampleCode=_sampleProdCode;
             this.lastSampleParentPrdCode=_sampleParentPrdCode
             this.lastSampleName=_sampleName;
     
             if(!IsAjaxLoaded()) {return false;}
                try
                {
                    HideSampleShopCrtBubble();
                    DoWait();
                    MicrosoftFrance.MCS.Commerce.WS.AJAXProject.ShoppingCartWebService.AddSampleItem(GetCurrentCustomerID(), _sampleProdCode,this._maxSampleLineItem,this._maxSampleAll,this.AddSampleItemToBagResultHandler,this.AddSampleItemToBagOnError)

                }
                catch (e)
                {
                    DoDefault();
                    window.status = e.message;
                }
    }


    this.AddSampleItemToBagResultHandler=function(result) {

      DoDefault();
       if (result.ActivityStatus==true) { 
           AjaxSamplingObject.SampleCounter= AjaxSamplingObject.SampleCounter +1;
       }
      _ShoppingCartBubble_Class.Hide();
       AjaxSamplingObject.Show();
      if (result.ActivityStatus==true) { 
           //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.
      }
      else  {
            if (result.ErrorString.indexOf('The maximum amount of the shopping cart has been reached')!=-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
                }
                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() {
       var lastMainObj=$get(this.SAMPLE_JUST_ADDED_MAINLAYER);
       if (lastMainObj!=null) { lastMainObj.style.display="";}
       var lastIMGObj=$get(this.SAMPLE_JUST_ADDED_IMAGEID);
       if (lastIMGObj!=null) { lastIMGObj.src=this.SampleImagePath + this.lastSampleParentPrdCode + this.SamplePrefix;}
       var lastNameObj=$get(this.SAMPLE_JUST_ADDED_PRDNAME);
       if (lastNameObj!=null) { lastNameObj.innerHTML=this.lastSampleName;}
       //manage header icons
      // this.ManageSampleIcon("SAMPLEICON_1");
       
    }

    /*
     this.ManageSampleIcon=function(_imgID) {
       var obj=$get("SAMPLEICON_1");
       if (obj!=null) {
          var src=obj.src;
          if (src.indexOf("TLN_ICN_EmptySmpl.gif")!=-1) {
           src=src.replace("TLN_ICN_EmptySmpl.gif","TLN_ICN_FlldSmpl.gif");
           obj.src=src;
           alert(obj.src);
          }
       }
     }
     */
     
     this.PostBackSampleBubble=function(smplPrdCode)
     {	
       
                    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$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();
  
}



function SampleCartBeforeClose() {
    //show drop downs
     ToggleDropDownsOnIE6(true);
  
}

function ManageIcons() {
//alert(CustomerSamplesSelected);
 var   _CustomerSamplesSelected=CustomerSamplesSelected  +  AjaxSamplingObject.SampleCounter;
//alert(AjaxSamplingObject.SampleCounter);
    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("TLN_ICN_EmptySmpl.gif")!=-1) {
           src=src.replace("TLN_ICN_EmptySmpl.gif","TLN_ICN_FlldSmpl.gif");
           obj.src=src;
           
          }
       }
     }
