﻿//The APIUrl value should be on the page.

var currentTab = 'OnlineSearchResults'; // default to online search results
var totalGroceryCount = 0;

function GetOnlineSearchResults(keyword, index, id, reloadData, page)
{
    if ($('#' + id).html().length == 0 || reloadData) {
        
        $('#' + id).show();        
        ShowLoadingMessage(id, "Loading Search Results..."); 
        
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: false,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=OnlineSearch&keyword='+keyword+'&index='+index+'&page='+page,
            success: function(retval)
            {            
                $("#" + id).html(retval); 
                $('#' + id).show();               
            }
        });
	} else
	    $('#' + id).show();
	
	$('a[class="bold NoLinkEffect"]').removeClass();
    $('#OnlineSearchResultsAnchor').attr('class', 'bold NoLinkEffect');
        
    if (currentTab != id)
    {
        $('#' + currentTab).hide();        
        currentTab = id;
    }
}

function GetLocalCouponResults(keyword, zip, id, reloadData, page) 
{
    if ($('#' + id).html().length == 0 || reloadData) {
        
        $('#' + id).show();        
        ShowLoadingMessage(id, "Loading Local Coupons...");        
        
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: false,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=LocalCouponSearch&zip='+zip+'&keyword='+keyword+'&page='+page,
            success: function(retval)
            {
                $("#" + id).html(retval);          
            }
        });
	} else
	    $('#' + id).show();
	
	$('a[class="bold NoLinkEffect"]').removeClass();
    $('#LocalSearchResultsAnchor').attr('class', 'bold NoLinkEffect');
        
	if (currentTab != id)
    {
        $('#' + currentTab).hide();
        currentTab = id;
    }
}

function GetFreeSamplesResults(keyword, id, reloadData, page)
{
    if ($('#' + id).html().length == 0 || reloadData) {
        
        $('#' + id).show();        
        ShowLoadingMessage(id, "Loading Free Samples..."); 
        
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: false,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=FreeSamplesSearch&keyword='+keyword+"&page="+page,
            success: function(retval)
            {
                $("#" + id).html(retval);
            }
        });
	} else	
	    $('#' + id).show();
	    
	$('a[class="bold NoLinkEffect"]').removeClass();
    $('#FreeSampleSearchResultsAnchor').attr('class', 'bold NoLinkEffect');
        
	if (currentTab != id)
    {
        $('#' + currentTab).hide();
        currentTab = id;
    }
}

function GetGroceryCouponResults(keyword, id, reloadData, page)
{
    if ($('#' + id).html().length == 0 || reloadData) {       
        
        $('#' + id).show();        
        ShowLoadingMessage(id, "Loading Grocery Coupons..."); 
        
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: false,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GroceryCouponSearch&keyword='+keyword+"&page="+page,
            success: function(retval)
            {
                $("#" + id).html(retval);
            }
        });
	} else	
	    $('#' + id).show();
	    
	$('a[class="bold NoLinkEffect"]').removeClass();
    $('#GrocerySearchResultsAnchor').attr('class', 'bold NoLinkEffect');
        
	if (currentTab != id)
    {
        $('#' + currentTab).hide();
        currentTab = id;
    }
}

function ShowLoadingMessage(id, message)
{
    // Show loading message
    $("#" + id).html('<img id="AnimationImage" src="images/mozilla_blu.gif" />&nbsp;&nbsp;' + message);     
}

function GetOnlineSearchCount() 
{
    $.ajax
    ({ 
        dataType: 'html',
        async: true,
        cache: true,
        type: 'GET',
        url: ApiUrl + 'webapi/SAHAPI.aspx',
        data: 'method=OnlineSearchCount&keyword='+ searchText,
        success: function(retval)
        {
            if (retval == 0)
            {
                $('#OnlineSearchResultsLink').hide();
                //$('#OnlineSearchResults').hide();
            }
            else
                $('#OnlineCountHeader').html(retval);
        },
        error: function(e){
            $('#OnlineSearchResultsLink').hide();
            //$('#OnlineSearchResults').hide();
        }
    });
}

function GetGroceryCounts() 
{
    // Printable Grocery Coupons
    $.ajax
      ({
        dataType: 'html',
        async: true,
        cache: true,
        type: 'GET',
        url: ApiUrl + 'webapi/SAHAPI.aspx',
        data: 'method=TotalGroceryCouponSearchCount&keyword='+ searchText,
        success: function(retval)
        {
            if (retval == 0)
            {
                $('#GrocerySearchResultsLink').hide();
                $('#trGroceryCoupons').hide();
                $('#trGroceryCouponsSeperator').hide();
            }
            else
            {
                $('#GroceryCountHeader').html(retval);
                $('#GroceryCountSearchResults').html(retval);
            }
        },
        error: function(e)
        {  
            $('#GrocerySearchResultsLink').hide();
            $('#trGroceryCoupons').hide();
            $('#trGroceryCouponsSeperator').hide();
        }
    });
}

function GetLocalCouponCount(zipCode) 
{
    if (!zipCode || zipCode == 0)
        zipCode = '';
        
    $.ajax
    ({
        dataType: 'html',
        async: true,
        cache: true,
        type: 'GET',
        url: ApiUrl + 'webapi/SAHAPI.aspx',
        data: 'method=LocalCouponSearchCount&zip=' + zipCode + '&keyword='+ searchText,
        success: function(retval)
        {
            // If the user searched by zip and there were no results don't hide the local link so they can go back and change the zip to see new results
            if (retval == 0 && zipCode == '') 
            {
                $('#LocalSearchResultsLink').hide();
                $('#trLocalCoupons').hide();
                $('#trLocalCouponsSeperator').hide();
            }
            else
            {
                $('#LocalCountHeader').html(retval);
                $('#LocalCountSearchResults').html(retval);                
            }
        },
        error: function(e){  
            $('#LocalSearchResultsLink').hide();
            $('#trLocalCoupons').hide();
            $('#trLocalCouponsSeperator').hide();
        }
    });
}

function GetFreeSamplesCount() 
{
    $.ajax
    ({ 
        dataType: 'html',
        async: true,
        cache: true,
        type: 'GET',
        url: ApiUrl + 'webapi/SAHAPI.aspx',
        data: 'method=FreeSamplesSearchCount&keyword='+ searchText,
        success: function(retval)
        {
            if (retval == 0)
            {
                $('#FreeSampleSearchResultsLink').hide();
                $('#trFreeSamples').hide();
            }
            else
            {
                $('#FreeSamplesCountHeader').html(retval);
                $('#FreeSampleCountSearchResults').html(retval);
            }                
        },
        error: function(e){  
            $('#FreeSampleSearchResultsLink').hide();
            $('#trFreeSamples').hide();
        }
    });
}

function LoadTab(tab)
{
    switch (tab)
    {
        case 'Online':
            GetOnlineSearchResults(searchText, 'All', 'OnlineSearchResults', false, 1);
            break;
        case 'Grocery':
            GetGroceryCouponResults(searchText, 'GroceryCouponResults', false, 1);
            break;
        case 'Local':
            GetLocalCouponResults(searchText, '', 'LocalCouponResults', false, 1);
            break;
        case 'FreeSamples':
            GetFreeSamplesResults(searchText, 'FreeSamplesResults', false, 1);
            break;
        default:
            GetOnlineSearchResults(searchText, 'All', 'OnlineSearchResults', false, 1);
            break;
    }
}

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash)
{
    // restore ajax loaded state
    if($.browser.msie)
        hash = encodeURIComponent(hash);
    LoadTab(hash);
}

function getParameterByName( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}
