﻿var UpdateInterval = 1000;
var NextDayFunds = false;
var ShowFundingClock = true;
var MoneyDay="Tomorrow";
var FundingCutoff;
var ServerDateTime;
var NextEeDate;
var UftToId;
var GstToId;
var CmpId;
var IsDst;

function Invoke() {
    GetCmpId();
    GetIsDst();
}

function GetServerTime() {
    clearTimeout(UftToId);
    clearTimeout(GstToId);
    
    var WsPath='/OfficialTime.asmx';
    var WsMthd='GetServerTime';
    Sys.Net.WebServiceProxy.invoke(WsPath,WsMthd,false,{},GetServerTime_Success,GetServerTime_Fail,'User Context',1000000);
    
    setTimeout("GetServerTime();",1000*60*60);
}

function SetNextDayFunds() {
    var TestDate = new Date(ServerDateTime);
    TestDate.setDate(ServerDateTime.getDate()+1);
    
    NextEeDate=new Date(NextEeDate);
    
    if(TestDate.getFullYear()==NextEeDate.getFullYear() && 
       TestDate.getMonth()==NextEeDate.getMonth() && 
       TestDate.getDate()==NextEeDate.getDate()
       ) {
        ShowFundingClock=true;
        MoneyDay="Tomorrow";
    } else {
        ShowFundingClock=true;
        MoneyDay=DayName(NextEeDate);
    }
}

function GetServerTime_Success(result, eventArgs) {
    ServerDateTime=new Date(result);
    SetNextDayFunds();
    
    FundingCutoff=new Date(ServerDateTime);
    FundingCutoff=DateToUtc(FundingCutoff);
    if(IsDst) {
        FundingCutoff.setHours(23,0,0,0);
    } else {
        FundingCutoff.setHours(0,0,0,0);
    }
    
    UpdateFundingTime();
}

function GetServerTime_Fail(error) {
    NextDayFunds=false;
    ServerDateTime="1/1/1970 11:00:00 PM";
    FundingCutoff="1/1/1970 11:00:00 PM";
}

function GetNextEeDate() {
    var WsPath='/OfficialTime.asmx';
    var WsMthd='GetNextEeDate';
    Sys.Net.WebServiceProxy.invoke(WsPath,WsMthd,false,{'CmpId':CmpId},GetNextEeDate_Success,GetNextEeDate_Fail,'User Context',1000000);
}

function GetNextEeDate_Success(result, eventArgs) {
    NextEeDate=new Date(result);
    
    GetServerTime();
}

function GetNextEeDate_Fail(error) {
    NextEeDate="1/1/1970 11:00:00 PM";
}

function GetCmpId() {
    var WsPath='/OfficialTime.asmx';
    var WsMthd='GetCmpId';
    Sys.Net.WebServiceProxy.invoke(WsPath,WsMthd,false,{},GetCmpId_Success,GetCmpId_Fail,'User Context',1000000);
}

function GetCmpId_Success(result, eventArgs) {
    CmpId=result;
    GetNextEeDate();
}

function GetCmpId_Fail(error) {
    CmpId=0;
}

function GetIsDst() {
    var WsPath='/OfficialTime.asmx';
    var WsMthd='IsDst';
    Sys.Net.WebServiceProxy.invoke(WsPath,WsMthd,false,{},GetIsDst_Success,GetIsDst_Fail,'User Context',1000000);
}
function GetIsDst_Success(result, eventArgs) {
    IsDst=result;
}
function GetIsDst_Fail(error) {
    IsDst=false;
}


function UpdateFundingTime() {
    //if((DateNow<Deadline) && NextDayFunds) {
    if(ShowFundingClock) {
        var DateNow = DateToUtc(ServerDateTime);
        var Deadline = new Date(FundingCutoff);
        var TimeLeft = GetTimeLeft(Deadline)
    
        document.getElementById('FundingTime').innerHTML=TimeLeft;
        document.getElementById('FundingDay').innerHTML="Get Cash by " + MoneyDay;
        document.getElementById('FundingClock').style.display='block';
        
        UftToId=setTimeout("UpdateFundingTime();",UpdateInterval);
    } else {
        document.getElementById('FundingClock').style.display='none';
    }
}

function GetTimeLeft(Deadline) {
    DateNow = DateToUtc(new Date());
    Days = (Deadline-DateNow)/1000/60/60/24;
    Hours = (Deadline-DateNow)/1000/60/60-(24*Math.floor(Days));
    Minutes = (Deadline-DateNow)/1000/60-(24*60*Math.floor(Days))-(60*Math.floor(Hours));
    Seconds = (Deadline-DateNow)/1000-(24*60*60*Math.floor(Days))-(60*60*Math.floor(Hours))-(60*Math.floor(Minutes));
    
    return NumberFormat(Math.floor(Hours)) + ":" + NumberFormat(Math.floor(Minutes)) + ":" + NumberFormat(Math.floor(Seconds));
}

function NumberFormat(Num) {
    var StrNum = new String(Num);
    if(StrNum.length<2) StrNum = "0" + StrNum;
    return StrNum;
}

function DateToUtc(xDate) {
    var Year=xDate.getUTCFullYear();
    var Month=xDate.getUTCMonth();
    var Day=xDate.getUTCDate();
    var Hour=xDate.getUTCHours();
    var Min=xDate.getUTCMinutes();
    var Sec=xDate.getUTCSeconds();
    
    UtcDate=new Date(Year,Month,Day,Hour,Min,Sec);
    
    return UtcDate;
}

function DayName(xDate) {
    var ThisDate = new Date(xDate);
    var DayNames = new Array(7);
    DayNames[0]="Sunday";
    DayNames[1]="Monday";
    DayNames[2]="Tuesday";
    DayNames[3]="Wednesday";
    DayNames[4]="Thursday";
    DayNames[5]="Friday";
    DayNames[6]="Saturday";
    return DayNames[ThisDate.getDay()];
}