function calcage(secs, num1, num2) {
  return ((Math.floor(secs/num1))%num2).toString();
}

function CountBack(secs,id,refresh) {   
    if (secs <= 0) {      
        if(refresh == "true"){
            setTimeout("location.reload(true);", Math.floor(Math.random()*2000));
            return;
        }
        else if(refresh == "reload"){
             // Make the timer start over
             secs = 60;          
             
             // Get variables
             cur_hea = parseFloat(document.getElementById('curheafield').firstChild.nodeValue);
             max_hea = parseFloat(document.getElementById('maxheafield').firstChild.nodeValue);
             cur_cha = parseFloat(document.getElementById('curchafield').firstChild.nodeValue);
             max_cha = parseFloat(document.getElementById('maxchafield').firstChild.nodeValue);
             cur_sta = parseFloat(document.getElementById('curstafield').firstChild.nodeValue);
             max_sta = parseFloat(document.getElementById('maxstafield').firstChild.nodeValue);
              
             // UPDATE HEALTH  
             new_hea = cur_hea + Regeneration;
             if( new_hea > max_hea){
                new_hea = max_hea;
             }
             document.getElementById('curheafield').firstChild.nodeValue = Math.round(new_hea * 100)/100;
             document.getElementById('healthbar').setAttribute("width",Math.round((( new_hea / max_hea ) * 100)*100)/100 + '%');
                
             // UPDATE Chakra
             new_cha = cur_cha + Regeneration;
             if( new_cha > max_cha){
                new_cha = max_cha;
             }
             document.getElementById('curchafield').firstChild.nodeValue = Math.round(new_cha * 100)/100;
             document.getElementById('chakrabar').setAttribute("width",Math.round((( new_cha / max_cha ) * 100)*100)/100 + '%');
             
             // UPDATE Stamine
             new_sta = cur_sta + Regeneration;
             if( new_sta > max_sta){
                new_sta = max_sta;
             }
             document.getElementById('curstafield').firstChild.nodeValue = Math.round(new_sta * 100)/100;
             document.getElementById('staminabar').setAttribute("width",Math.round((( new_sta / max_sta ) * 100)*100)/100 + '%');
        }
        else{
            document.getElementById(id).innerHTML = "Done";
            return;
        }
    }
    
    // Calculate time left
    days = calcage(secs,86400,100000);
    hours = calcage(secs,3600,24);
    
    // Show "60 seconds" for the regentimer
    if(id == "regentimer"){
        minutes = calcage(secs,61,61);
        seconds = calcage(secs,1,61);
    }
    else{
        minutes = calcage(secs,60,60);
        seconds = calcage(secs,1,60);
    }
    
                                                                             
    
    if(days == 0){DisplayStr = DisplayFormat.replace(/%%D%%/g, "");}else{DisplayStr = DisplayFormat.replace(/%%D%%/g, "" + days + " days, ");}
    if(hours == 0){DisplayStr = DisplayStr.replace(/%%H%%/g, "");}else{DisplayStr = DisplayStr.replace(/%%H%%/g, "" + hours + " hours, ");}
    if(minutes == 0){DisplayStr = DisplayStr.replace(/%%M%%/g, "");}else{DisplayStr = DisplayStr.replace(/%%M%%/g, "" + minutes + " minutes, ");}
    DisplayStr = DisplayStr.replace(/%%S%%/g, "" + seconds + " seconds ");
       
    document.getElementById(id).innerHTML = DisplayStr;
      
    if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ", '" + id + "', '" + refresh + "')", SetTimeOutPeriod);
}

function putspan(id) {
    document.write("<span id='" + id + "' style='color:black'></span>");
}

function timer(id,timediff,refresh,regen){
    if (regen == null){
      regen = 1;
    }
    // Set constants
    CountActive   = true;
    Starttimer = timediff;
    CountStepper  = -1;
    LeadingZero   = false;
    DisplayFormat = "%%D%% %%H%% %%M%% %%S%%";
    Regeneration = parseFloat(regen);
    
    // Edit the Stepper                   
    if (CountStepper == 0)
      CountActive = false;
                        
    // Timer period                                                                 
    SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
    
    // Create the span
    putspan(id); 
    
    // The time difference
    ddiff = new Date(timediff);
    gsecs = Math.floor(ddiff.valueOf());
    
    // Start counter
    CountBack(gsecs,id,refresh);
}

