// About Page and How to find Page and Quick Code Functionality
function toggle_section(on,off,off2) {
    document.getElementById(on).style.display = '';
    document.getElementById(off).style.display = 'none';
    if (document.getElementById(off2)){ document.getElementById(off2).style.display = 'none'; }
}


function updatePointBalance(points)
{
    if (window.top){
        if (window.top.document){
            if (window.top.document.getElementById("headerPointsBalance")){
                if (window.top.document.getElementById("headerPointsBalance").innerHTML){
                    window.top.document.getElementById("headerPointsBalance").innerHTML = points;
                }
            }

            if (window.top.document.getElementById("rightPointsBalance")){
                if (window.top.document.getElementById("rightPointsBalance").innerHTML){
                    window.top.document.getElementById("rightPointsBalance").innerHTML = points;
                }
            }

        }
    }
}

function submitCodeOnEnter(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
   {
       submitQuickCode();
       return false;
   }
    else
       return true;
}

/* for new ajax quick code */
function submitQuickCode(){
	clearme(document.quickcode.code1,getEnterCodeMessage());
	document.getElementById('right').style.display = 'none';
	document.getElementById('working').style.display = 'block';
	$.post('ajaxCode.html',{
       code1: $('#redemption_code1').val()
     }, function(xml) {
            code = $('code',xml).text();
            points = $('points',xml).text();
            qstatus = $('status',xml).text();
			if( qstatus != 'SessionExpired' )
			{
                updatePointBalance( points );
            }
            changeQuickCodeMessage(qstatus, code);
			if( qstatus == 'Redeemed' )
			{
                if( self.location.href.indexOf('points-history.html') != -1 )
                {
                    getPointSummary();
                    if( document.getElementById('points_collected').style.display == '' )
                    {
                        getEarnHistory();
                    }

                }
                if( self.location.href.indexOf('index.html') != -1 )
                {
                    getSpecialOffers();
                    getSpotlightOffers();
                }

            }
			document.getElementById('working').style.display = 'none';
			if( qstatus == 'SessionExpired' )
			{
				timeoutRedirect();
            }
            else
            {
				document.getElementById('right').style.display = 'block';
			}
 },"xml");
 return false;
}

function getPointHistory( tab, page, pagenbr )
{
    var url = 'ajax-points-history.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
    var loading = 'loadingPointsHistory';
    if( page != undefined )
    {
        url = url + '&tab=' + tab + '&' + page + '=' + pagenbr;
        loading = 'loading_' + tab;
    }
    else
    {
        document.getElementById('gtg_points_history').style.display = 'none';
    }
    document.getElementById(loading).style.display = 'block';
    $.get(url, function(data) {
        document.getElementById(loading).style.display = 'none';
            document.getElementById('gtg_points_history').innerHTML = data;
            document.getElementById('gtg_points_history').style.display = 'block';
            if( data.length < 100 )
            {
                    var f = data.search("session-timeout")
                    if (f != -1) {
                       timeoutRedirect();
                    }
            }

    });
}

function getEarnHistory( pagenbr )
{
    var url = 'ajax-earn-history.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
    var loading = 'loadingEarnHistory';
    if( pagenbr != undefined )
    {
        url = url + '&pageNumber=' + pagenbr;
        loading = 'loading_points_collected';
    }
    else
    {
        document.getElementById('ajax_earn_history').style.display = 'none';
    }
    document.getElementById(loading).style.display = 'block';
    $.get(url, function(data) {
        document.getElementById(loading).style.display = 'none';
            document.getElementById('ajax_earn_history').innerHTML = data;
            document.getElementById('ajax_earn_history').style.display = 'block';
            if( data.length < 100 )
            {
                    var f = data.search("session-timeout")
                    if (f != -1) {
                       timeoutRedirect();
                    }
            }

    });
}

function getRedeemHistory( pagenbr )
{
    var url = 'ajax-redeem-history.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
    var loading = 'loadingRedeemHistory';
    if( pagenbr != undefined )
    {
        url = url + '&pageNumber=' + pagenbr;
        loading = 'loading_points_collected';
    }
    else
    {
        document.getElementById('ajax_redeem_history').style.display = 'none';
    }
    document.getElementById(loading).style.display = 'block';
    $.get(url, function(data) {
        document.getElementById(loading).style.display = 'none';
            document.getElementById('ajax_redeem_history').innerHTML = data;
            document.getElementById('ajax_redeem_history').style.display = 'block';
            if( data.length < 100 )
            {
                    var f = data.search("session-timeout")
                    if (f != -1) {
                       timeoutRedirect();
                    }
            }

    });
}

function getPointSummary()
{
    var url = 'ajax-points-summary.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
    var loading = 'loadingPointsSummary';
    document.getElementById(loading).style.display = 'block';
    $.get(url, function(data) {
        document.getElementById(loading).style.display = 'none';
            document.getElementById('ajax_points_summary').innerHTML = data;
            document.getElementById('ajax_points_summary').style.display = 'block';
            if( data.length < 100 )
            {
                    var f = data.search("session-timeout")
                    if (f != -1) {
                       timeoutRedirect();
                    }
            }

    });
}


function getSpecialOffers()
{
	var url = 'ajax-special-offers.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
	document.getElementById('aso').style.display = 'none';
    document.getElementById('loadingSpecialOffers').style.display = 'block';
	$.get(url, function(data) {
	    document.getElementById('loadingSpecialOffers').style.display = 'none';
		document.getElementById('aso').innerHTML = data;
		document.getElementById('aso').style.display = 'block';
	});
}

function getSpotlightOffers()
{
	var url = 'ajax-spotlight-offers.html?dummy=' + new Date().getTime(); //need time so it does not get page from cache
	document.getElementById('aspo').style.display = 'none';
    document.getElementById('loadingSpotlightOffers').style.display = 'block';
	$.get(url, function(data) {
	    document.getElementById('loadingSpotlightOffers').style.display = 'none';
		document.getElementById('aspo').innerHTML = data;
		document.getElementById('aspo').style.display = 'block';
	});
}

function decode(str)
{
	var result = "";
	for (var i = 0; i < str.length; i++)
	{
		if (str.charAt(i) == "+")
		{
			result += " ";
		}
		else
		{
			result += str.charAt(i);
		}
	}
	return unescape(result);
}

function redirectHome(){
    window.location = "/index.html"
}

function timeoutRedirect()
{
    setTimeout('redirectHome()', 500)
}
