// GiftGrabber.js

// This function calls the Web service method 
// that returns an XmlDocument type.  
var gLastCheck = "0";
var gNextInc = 0;
var gCntTmr = 0;
function GetUpdates() 
{
    PetKaboodle.services.GiftGrabber.GetUpdates(gLastCheck,
        GiftGrabberUpdate, GiftGrabberFailed);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object, the user context, and the 
// calling method name as parameters.
function GiftGrabberUpdate(xml, userContext, methodName)
{
    var kUpdate = "/update";
    var kGrabStats = "/update/grabstats";
    var kClaims = "/update/claims/claim";
    var updateNodes;
    var updateNode;
    var grabStatNode;
    var claimNodes;
    var claimNode;    
    var cnt;
    
    var valid = false;
    var amount, incAmount;
    
/*<update lastcheck=""0"">
    <claims>
        <claim offsetTicks="" amount="" />
        <claim offsetTicks="" amount="" />
    </claims>
    <grabstats amount="""" nextInc="""" incAmount="""" />
</update>";*/
    
    if (methodName == "GetUpdates")
	{
	
	    if (document.all)
        {
            updateNode=xml.selectNodes(kUpdate);
            if (updateNode.length > 0)
            {
                gLastCheck = updateNode[0].attributes.getNamedItem("lastcheck").value;
                grabStatNode = xml.selectNodes(kGrabStats);
                if(grabStatNode.length > 0)
                {
                    amount = grabStatNode[0].attributes.getNamedItem("amount").value;
                    gNextInc = parseInt(grabStatNode[0].attributes.getNamedItem("nextInc").value);
                    incAmount = parseInt(grabStatNode[0].attributes.getNamedItem("incAmount").value);       
                    valid = true;                                 
                }
                claimNodes = xml.selectNodes(kClaims);
                for(cnt=0; cnt<claimNodes.length; cnt++)
                {
                    claimNode = claimNodes[cnt];
                    addClaim(claimNode.attributes.getNamedItem("offsetTicks").value, claimNode.attributes.getNamedItem("amount").value);
                }
            }            
        }
        else
        {
            // code for Mozilla, Firefox, Opera, etc.
            updateNodes=xml.evaluate(kUpdate, xml, null, XPathResult.ANY_TYPE, null);
            updateNode=updateNodes.iterateNext();
            if (updateNode)
            {
                gLastCheck = updateNode.attributes.getNamedItem("lastcheck").value;
                grabStatNode = xml.evaluate(kGrabStats, xml, null,XPathResult.ANY_TYPE, null).iterateNext();
                if(grabStatNode)
                {
                    amount = grabStatNode.attributes.getNamedItem("amount").value;
                    gNextInc = parseInt(grabStatNode.attributes.getNamedItem("nextInc").value);
                    incAmount = parseInt(grabStatNode.attributes.getNamedItem("incAmount").value);   
                    valid = true;                                     
                }
                claimNodes = xml.evaluate(kClaims, xml, null,XPathResult.ANY_TYPE, null);
                claimNode = claimNodes.iterateNext();
                while(claimNode)
                {
                    addClaim(claimNode.attributes.getNamedItem("offsetTicks").value, claimNode.attributes.getNamedItem("amount").value);
                    claimNode = claimNodes.iterateNext();
                }
            }
        }
        if(valid)
        {
            SetAmount(amount);
            //Set amounts and such
            if(gNextInc > 15000)
                setTimeout(GetUpdates, 15000);
            else if(gNextInc > 1000)
                setTimeout(GetUpdates, 1000);
            else
                setTimeout(GetUpdates, gNextInc);                
        }
        else
            setTimeout(GetUpdates, 15000);

        if(gCntTmr != 0)
            clearInterval(gCntTmr);
        gCntTmr = setInterval(DoCountdown, 100);
	}    
}

function addClaim(timediff, amount)
{
    var d = new Date();
    var e = new Date(d.getTime()+parseInt(timediff));
    for (var x=9;x>0;x--)
    {
        $get("claim" + (x + 1)).innerHTML = $get("claim" + (x)).innerHTML;
    }
    $get("claim1").innerHTML = amount + " - " + e.toLocaleString();
}

function DoCountdown()
{
    var x = (gNextInc>0)?gNextInc:0;
    var h = Math.floor(x/(60*60*1000)) + '';
    x -= h*60*60*1000;
    var m = Math.floor(x/(60*1000)) + '';
    x -= m*60*1000;
    var s = Math.floor(x/1000) + '';
    x -= s*1000;
    $get("lblCountdown").innerHTML = (h > 0)?(pad(h, 2) + ":"):"" + pad(m, 2) + ":" + pad(s,2) + "." + (x + '').substring(0,1);
     
    gNextInc -= 100;
}

function pad(num, count) {
  var lenDiff = count - String(num).length;
  var padding = "";
  
  if (lenDiff > 0)
    while (lenDiff--)
      padding += "0";
  
  return padding + num;
}

function checkCookie() {
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf("gg=") != 0) window.location.href=window.location.href;
	}
}

function FormatCurrency(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return '$' + amount;
}




// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function GiftGrabberFailed(error)
{
    if(gCntTmr != 0)
        clearInterval(gCntTmr);
    $get("lblCountdown").innerHTML = error.get_message();   
    setTimeout(GetUpdates, 15000); 
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

