﻿    var loadingString = "Please wait loading...";
    
    function resetStars(){ 
        $('.inline-rating').show();        
     }
    
    //The APIUrl value should be on the page.
    function GetReviews(sortBy) //0=date desc, 1=rating % desc
    {
      $('#storereviews').css('cursor','wait');
      $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: isAnon,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetReviews&mid='+mid+'&c='+useCachedReviews+'&cnt='+maxReviewCount+'&returnUrl='+storeUrl+'&sort='+sortBy+'&storeReviewUrl='+storeReviewUrl,
            success: function(retval)
                    {         //alert('reviews finishing');               
                        $('#storereviews').html(retval);
                        $('#storereviews').css('cursor',"default");                        
                        resetStars(); tb_reinit(); 
                    },
            error: function(e){  
                //alert("error=" + e.responseText);                  
                }
      });
    }       
    
    function toggExpCoupons(e) { $('#expiredCoupons').toggle();}
    
    //The APIUrl value should be on the page.
    function GetExpiredCoupons()
    {    
      $('#container').css('cursor','wait');
      $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: true,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetExpiredCoupons&mid='+mid,
            success: function(retval)
                    {   //alert('exp coupons finishing');
                        $('#expiredCoupons').html(retval).show();
                        $('#aOuterDiv').unbind('click');
                        $('#aOuterDiv').removeAttr('onclick');
                        $('#aOuterDiv').bind("click", toggExpCoupons); 
                        $('#container').css('cursor',"default");
                        //tb_reinit(); 
                        resetStars();
                    },
            error: function(e){  
                //alert("error=" + e.responseText);                  
                }
      });
    }       
    
   function bookmarkMe()
    {
       //netscape="Netscape User's hit CTRL+D to add a bookmark to this site."     
        if(window.external && window.external.AddFavorite)
            window.external.AddFavorite(location.href,document.title);
        else if(window.sidebar != 'undefined')
            window.sidebar.addPanel(document.title,location.href,'http://www.shopathome.com');
        else if(window.opera && window.print){ // opera
            var elem = document.createElement('a');
            elem.setAttribute('href',location.href);
            elem.setAttribute('title',document.title);
            elem.setAttribute('rel','sidebar');
            elem.click();
        }
    }                  
    function toggle_visibility(id) {
        $('#'+id).toggle();
    } 
    
    function ReviewPopupCallBack(newPageUrl)
    {
        window.location.replace( newPageUrl );
    }
    
    //The APIUrl value should be on the page.
    function GetFaveCount() 
    {
      $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: false,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=StoreFavoriteCount&mid='+mid+'&c=false',
            success: function(retval)
                    {   
                        $('#favecount').html(retval);
                        resetStars();
                    },
            error: function(e){                
                }
      });
    }
        
    function SetFaveObjects()
    {
        //if (bIsFavorite)
            GetFaveCount();
            $('#imgAdd2').toggle();
            $('#imgRemove2').toggle();
            $('#addFave1').toggle();
            $('#remFave1').toggle();
            $('#lRemMsg').toggle();           
            $('#lAddMsg').toggle();
    }
        
  function AddFavorite(showReview) 
       {            
        $.ajax
          ({
            dataType: 'json',
            async: false,
            type: 'GET',
            url: ApiUrl+'WebAPI/SAHAPI.aspx',
            data: 'method=AddAlert&cid=' + customerId + '&mid=' + mid,
            success: function(json)
            {
                if (json.error != null && json.error.length > 0)
                {
                    $('#favesActionMsg').html(json.error).show();
                    $('#alerterror2').html(json.error).show();                    
                }
                else //success
                {
                    $('#favesActionMsg').html(json.success).show();
                    $('#alerterror2').html(json.success).show();
                    
                    // Post to facebook if the user allows for it
                    post_to_facebook_stream("favorite_store", mid, null, function (result) {
                        // Now allow user to review
                        bIsFavorite = true;
                        SetFaveObjects();
                        if (showReview) {
                            //this is such a hack but can't append params to thickbox url
                            var surl = unescape( storeReviewUrl).replace("&RatingType=", "&fave=t&RatingType=")
                            tb_show('Review This Store', surl);
                        }
                        resetStars();
                    });
                }
            }
        });         
        //return (true);
       }
       
       function RemoveFavorite() 
       {           
        $.ajax
          ({
            dataType: 'json',
            async: true,
            type: 'GET',
            url: ApiUrl+'WebAPI/SAHAPI.aspx',
            data: 'method=RemoveAlert&cid=' + customerId + '&mid=' + mid,
            success: function(json)
            {
                if (json.error != null && json.error.length > 0)
                {
                    $('#favesActionMsg').html(json.error).show();
                    $('#alerterror2').html(json.error).show();
                }
                else //success
                {
                    $('#favesActionMsg').html(json.success).show();
                    $('#alerterror2').html(json.success).show();
                    bIsFavorite = false;
                    SetFaveObjects();
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $('#alerterror2').html(XMLHttpRequest.responseText).show();
            }
        });           
        //return (true);
       }

    function ToggleCouponComments(SpecialsId, MerchantId, SpecialsPostId, LinkClicked)
    {
        if ($('#CouponComments' + SpecialsId).is(":hidden"))
        {
            $('#CouponComments' + SpecialsId).slideDown('slow');
            LinkClicked.innerHTML=LinkClicked.innerHTML.replace(/View/, 'Close');
            
            // If the comments have not been previously loaded... load them now.  Otherwise show comments from previous load.
            if ($('#CouponCommentsControl' + SpecialsId).html().length == 0)
            {
                $('#CouponCommentsControl' + SpecialsId).html(loadingString);
                
                LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId);
            }
        }
        else
        {
            $('#CouponComments' + SpecialsId).slideUp('slow');
            LinkClicked.innerHTML=LinkClicked.innerHTML.replace(/Close/, 'View');
        }
      
        return false;
    }
    
    function ShowCapcha()
    {
        if ($('#Captcha').is(":hidden"))
        {
            $('#Captcha').slideDown('slow');
        }
        return false;
    }
      
    function RefreshCouponComments(SpecialsId, MerchantId, SpecialsPostId)
    {
        $('#CouponCommentsControl' + SpecialsId).html(loadingString);
        if ($('#CouponComments' + SpecialsId).is(":hidden"))
            $('#CouponComments' + SpecialsId).slideDown('slow');
            
        LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId);
        
    }
    function LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId)
    {
        // Load the coupons comments
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: isAnon,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetCouponComments&mid='+MerchantId+'&sid='+SpecialsId+'&postid='+SpecialsPostId,
            success: function(retval)
                {
                    $('#CouponCommentsControl' + SpecialsId).replaceWith('<div id="CouponCommentsControl' + SpecialsId + '">'+retval+'</div>');
                    $('#CouponCommentsControl' + SpecialsId).css('cursor',"default");
                },
            error: function(e)
                {
                    $('#CouponCommentsControl' + SpecialsId).replaceWith('<div id="CouponCommentsControl' + SpecialsId + '">Error loading comments. Please check back later. <br /><br />' + e.responseText + '</div>');
                }
        });
    }