function saveToConfigurationDate(newDate)
{ 
    GiantEagle.Applications.InternetSites.Web.Services.AdminService.SetCmsConfigurationDate(newDate,callbackSetCmsConfigurationDate, callbackAjaxError);    
}

function changeCacheSetting(isEnabled)
{
    GiantEagle.Applications.InternetSites.Web.Services.AdminService.ChangeCacheSetting(isEnabled, callbackEnableCache, callbackAjaxError);    
}

function clearCache()
{
    GiantEagle.Applications.InternetSites.Web.Services.AdminService.ClearCache(callbackEnableCache, callbackAjaxError);    
}

function callbackAjaxError(result)
{
    var message = "There was an error with the remote call : \n" + 
        result._exceptionType + 
        result._message +
        result._stackTrace +
        "\nTime Out: " + result._timedOut;
        alert(message);   
} 

function callbackSetCmsConfigurationDate(result)
{
    if (!result.OperationSucceeded)
    {
        alert("There was an error setting the Configuration Date.\n\n" + result.ClientErrorMessage);
        return;
    }
    else
    {
        document.location = document.location;
    }
}

function callbackEnableCache(result)
{
    if (!result.OperationSucceeded)
    {
        alert("There was an error changing your cache setting.\n\n" + result.ClientErrorMessage);
        return;
    }
    else
    {
        document.location = document.location;
    }
}


//In the dashboard New Member Registration
function EnterCheck(e, loginControl)
{
    var key;
    if(window.event)
    {
        key = e.keyCode;
    }
    else
    {
        key = e.which;
    }
       
    //keyCode for Enter key is 13    
    if(key == 13)
    {
       __doPostBack(loginControl,''); //'dashboard$MyAccountControl$Login'
    }
}


//In the dashboard MyAccout control, 'enter' key press in the password field should invoke Login button event
function EnterCheckLogin(e, loginControl)
{
    var key;
    if(window.event)
    {
        key = e.keyCode;
    }
    else
    {
        key = e.which;
    }
       
    //keyCode for Enter key is 13    
    if(key == 13)
    {
        // will this fix the login problem on dashboard?
        performSecureLogin();
    }
}


var _controlsToUpdate = new Array('MyPageManageList$updateLink', 'MyPageRecipes$LoadRecipeLink', 'MyPageFuelperks$LoadFuelperksLink', 'MyPageStores$LoadMyStore', 'MyPageShoppingHistory$LoadHistory');
var _controlsFieldToCheck = new Array('MyPageManageList_LoadingArrow', 'MyPageRecipes_LoadingArrow', 'MyPageFuelperks_LoadingArrow', 'MyPageStores_LoadingArrow', 'MyPageShoppingHistory_LoadingArrow');
var _controlsFieldToCheckAfter = new Array('loadedList', 'loadedRecipes', 'loadedFuelperks', 'loadedStores', 'loadedHistory');

var _currentControl = 0;

function updateMyPageNextControl()
{
    // wait until the last control loads before doing the next
    if (_currentControl > 0)
    {
        if (!document.getElementById(_controlsFieldToCheckAfter[_currentControl - 1]))
        {
            setTimeout("updateMyPageNextControl()", 500);
            return;
        }
    }
    
    if (_currentControl >= _controlsToUpdate.length)
	{
		return;
	}
	
	if (document.getElementById(_controlsFieldToCheck[_currentControl]))
    {        
        __doPostBack(_controlsToUpdate[_currentControl],'');
	}
	_currentControl++;
	setTimeout("updateMyPageNextControl()", 500);
}

// These functions were moved from the Dashboard.js file as the dashboard was being removed from the site.
function emailSubmitRegisterEmail(e)
{
    emailSubmit(e, "RegisterEmail");
}

function emailSubmitTop(e) 
{
    emailSubmit(e, "TopEmailAddress");
}
function emailSubmitPromo(e) 
{
    emailSubmit(e, "PromoEmailAddress");
}
function emailSubmitBottom(e) 
{
    emailSubmit(e, "BottomEmailAddress");
}
function submitEmailDash(e)
{
    emailSubmit(e, "UserName");
}

function emailSubmit(e, element) 
{
	if (e.which != undefined && e.which == 13)
	{
	    if (_currentBrowser.isSafari)
        {
            event.cancelBubble = true;
            event.returnValue = false;
        }
		submitEmailAddress(element);
	}
	else if (e.keyCode != undefined && e.keyCode == 13)
	{
	    event.cancelBubble = true;
        event.returnValue = false;
		submitEmailAddress(element);		
	}	
}

// This routine will take the email address and pass it to the register email page.
// This is called from the quick registration controls
function submitEmailAddress(emailAddressElement)
{
    var emailAddress = document.getElementById(emailAddressElement).value;
    if (emailAddress == undefined || emailAddress == "")
    {
        alert("Please enter an email address");
        return;
    }
    var q = _geApplicationRoot + "/Users/RegisterEmail.aspx?email=" + emailAddress;
    document.location = q;
}

function clearField(element, originalValue)
{
    if (element.value == originalValue)
    {
        element.value = "";
    }
}


// community events
var _linksLoaded = false;
function loadLinksForYou()
{
    if (_linksLoaded)
    {
        return;
    }
    _linksLoaded = true;
    document.getElementById("EventCalendar").innerHTML = "<img src='/Images/Icons/LoadingArrow.gif'> Loading";
    document.getElementById("HealthWire").innerHTML = "<img src='/Images/Icons/LoadingArrow.gif'> Loading";
    GiantEagle.Applications.InternetSites.Web.Services.Dashboard.GetNewsWire(callbackNewsWire, callbackErrorCommunity);
    GiantEagle.Applications.InternetSites.Web.Services.Dashboard.GetCommunityEvents(callbackCommunityEvents, callbackErrorCommunity);
}
function callbackNewsWire(result)
{
    if (result.OperationSucceeded)
    {
        document.getElementById("HealthWire").innerHTML = result.ClientData;
    }
}

function callbackErrorCommunity(errorCommunity)
{
    alert("error" + errorCommunity);
    // do nothing
}
function callbackCommunityEvents(result)
{
    var s = "";
    if (result.OperationSucceeded)
    {
        var communityEvents = result.ClientData;

        if (communityEvents.length == 0)
        {
            s = "There are no events in your area at this time.<br><br><p><a href=\"/StoreLocator/Default.aspx\">Find a store</a></p>";
        }
        for (var i = 0; i < communityEvents.length; i++)
        {

            //s += "<img src=\"/Images/Layout/GrayLineFade.gif\" style=\"border-width:0px;\" /><br /><div style='padding-bottom:5px'><font color='red'>";
            //s += "<div style='padding-bottom:5px'>";
            s += "<p><b>" + communityEvents[i].StartDate.Month + "/" + communityEvents[i].StartDate.Day + "/" + communityEvents[i].StartDate.Year + "</b>";
            s += "<br><a href='http://www.gianteagle.com/Community/CommunityEvents.aspx?cntid=";
            s += communityEvents[i].ContentId.Value;
            s += "'>";
            s += communityEvents[i].Title;
            s += "</a></p>";
        }

        s += "<a href=\"/Community/CommunityEvents.aspx?cntid=182291\">Event Calendar</a>";
        
        document.getElementById("EventCalendar").innerHTML = s;
    }
}

function storeLocatorZip(elementId)
{
    var zipElement = document.getElementById(elementId);
    var zipCode = zipElement.value;

    if (isNaN(zipCode) || zipCode.length != 5)
    {
        alert("Please enter a 5 digit zip code");
        return;
    }

    document.location = "/StoreLocator/Default.aspx?ZipCode=" + zipCode;
}
// end of moved functions