var id1="name";
var id2="phone";
jQuery("#"+id1).click(function(){adjust1st_field(id1);}).keyup(function(){adjust1st_field(id1);})
jQuery("#"+id2).click(function(){adjust_field(id1,id2);}).keyup(function(){adjust_field(id1,id2);})
var first_field_init_value=0;
var already_added=0;
var val1_exploded;
var val1_timestamp;
function adjust1st_field(id1) {
first_field_init_value=jQuery("#"+id1).val();
val1_exploded=explode(':',first_field_init_value);
val1_timestamp=val1_exploded[0]*60*60+val1_exploded[1]*60-already_added;//время в секундах
}
function adjust_field(id1,id2) {
if(first_field_init_value==0) {
adjust1st_field(id1);
}
var val2=$("#"+id2).val();// - берет значение 2
var val2_exploded=explode(':',val2);
var val2_timestamp=val2_exploded[0]*60*60+val2_exploded[1]*60;//время в секундах
var total_timestamp;
total_timestamp= val2_timestamp+ val1_timestamp;
jQuery("#"+id1).val(secondsToHm(total_timestamp));//записывает новое значение
already_added=val2_timestamp;
}
function secondsToHm(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
while(h>=24) {
h=h-24;
}
return ((h > 0 ? h + ":" + (m < 10 ? "0" : "") : "") + m);
}
function explode( delimiter, string ) { // Split a string by string
//
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: kenneth
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
var emptyArray = { 0: '' };
if ( arguments.length != 2
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined' )
{
return null;
}
if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}
if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}
if ( delimiter === true ) {
delimiter = '1';
}
return string.toString().split ( delimiter.toString() );
}