PDA

View Full Version : Calculate time:minutes


BigO
12-17-2006, 02:22 PM
Greetings.

I am having a script that moves data up and down in a table. When something is moved I need to recalculate the time:minutes fields but it grabs those fields.data that are initially in the html file. Not the DOM tree.

Any body able to help here?

Move script:

function findParObj(elm, tag) {
for (i=0; elm.tagName!=tag; elm = elm.parentNode);
return elm;
}

function myMove(elm, dir) {
var newElm, aDivs, cont=gE("cont");
elm = findParObj(elm, "DIV");
newElm = elm.cloneNode(true);

aDivs = gA(cont, "DIV");
if (dir=="up") {
for ( i=1; i<aDivs.length; i++ ) {
if ( aDivs[i]==elm ) {
cont.removeChild(elm);
cont.insertBefore(newElm, gA(cont, "DIV")[i-1]);
}
}
}
else if (dir=="down") {
for ( i=1; i<aDivs.length; i++ ) {
if ( aDivs[i-1]==elm ) {
cont.removeChild(elm);
if (aDivs.length>i) {
cont.insertBefore(newElm, gA(cont, "DIV")[i]);
}
else {
cont.appendChild(newElm);
}
break;
}
}
}
if (navigator.userAgent.toLowerCase().indexOf("opera")==-1) cont.innerHTML = cont.innerHTML + "";

calc_all();
}


And the calculating script:

var d=document;
function gE(id){return d.getElementById(id)}
function gA(o,tag){return o.getElementsByTagName(tag)}


function calc_time(n)
{
if (n==0)
{
var t1 = gE('start_time'+[n]).value;
}
else if (n>0)
{
var t1 = gE('real_time'+[n-1]).value;
}

var t2 = gE('nom_time'+[n]).value;

if (n==0)
{
var t3 = gE('trans_time'+[n]).value="00:00";
}
else if (n>0)
{
var t3 = gE('trans_time'+[n]).value;
}

tid1 = sek(t1);
tid2 = sek(t2);
tid3 = sek(t3);
if (n==0)
tid = tid1 + tid2;
else
tid = tid1 + tid2 + tid3;
strtidtim = Math.floor(tid/3600);
strtidmin = Math.floor((tid % 3600)/60);
if (strtidtim < 10) strtidtim = "0" + strtidtim;
if (strtidmin < 10) strtidmin = "0" + strtidmin;
return (strtidtim + ":" + strtidmin);
}

function sek(str)
{
tiden = str.slice(0, 2) * 60 * 60;
tiden += str.slice(3, 5) * 60;
return tiden;
}


function calc_all()
{
var inpts, divs = gA(gE("cont"), "DIV");

for ( i=0; i<divs.length; i++ )
{
inpts = gA(divs[i], "INPUT");
for ( j=0; j<inpts.length; j++ )
{
if (inpts[j].className=="showTimeInp")
{
inpts[j].value = gE(inpts[j].id).value;
if ( inpts[j].id.replace(/[0-9]/,"") == "real_time")
{
inpts[j].value = calc_time(i);
}
}
}
}
}