123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- // ---------------- variables --------------------------------------------------
- var clock_host = 'http://clock.time.in.ua/';
- var clock_html = 'clock.html';
- var offsetCookie = 'to';
- var req, reqstart, reqend, response, offset;
- var time_width,time_height,time_border,time_bg,w,h,undef;
- // ---------------- SNTP via HTTP syncronization -------------------------------
- function loadXMLDoc(url) {
- // branch for native XMLHttpRequest object
- if (window.XMLHttpRequest) {
- req = new XMLHttpRequest();
- req.onreadystatechange = processReqChange;
- req.open("GET", url, true);
- req.setRequestHeader('Pragma', 'no-cache');
- req.setRequestHeader('Cache-control', 'no-cache');
- req.send(null);
- }
- else if (window.ActiveXObject) {
- // branch for IE/Windows ActiveX version
- req = new ActiveXObject("Microsoft.XMLHTTP");
- if (req) {
- req.onreadystatechange = processReqChange;
- req.open("GET", url, true);
- req.setRequestHeader('Pragma', 'no-cache');
- req.setRequestHeader('Cache-control', 'no-cache');
- req.send();
- }
- }
- }
- function processReqChange()
- {
- // only if req shows "complete"
- if (req.readyState == 4) {
- // only if "OK"
- if (req.status == 200 || req.responseText != undef) {
- // ...processing statements go here...
- response = req.responseText;
- reqend = (new Date()).getTime();
- offset = (reqend-reqstart)/2;
- // real time (in microseconds) is response * 1000 + offset
- // user's time (in microseconds) is reqend
- // difference is reqend - (response * 1000 + offset)
- offset = reqend - response*1000 - offset;
- var expire = new Date();
- expire.setTime(expire.getTime() + 300 * 1000); // 1 hour
- if (Math.abs(offset) < 1e4) setCookie(offsetCookie, offset, expire.toGMTString());
- display();
- }
- else {
- alert("There was a problem retrieving the XML data:\n" + req.statusText);
- }
- }
- }
- function display() {
- var timer = (new Date()).getTime();
- var real = timer + offset;
- real = Math.round(real);
- real = real.toString();
- real = 1000-real.substr(real.length-3);
- setTimeout("displayTime()", real);
- var im = document.getElementById("img");
- if (im) {
- if (Math.abs(offset) > 1000) im.src = "http://clock.time.in.ua/images/clock-no.png";
- else im.src = "http://clock.time.in.ua/images/clock-ok.png";
- im.title = "Your system clock is "+(offset >= 0 ? 'faster' : 'slower')+" in "+Math.abs(offset)+" ms";
- }
- }
- function startSync() {
- offset = parseInt(getCookie(offsetCookie));
- if (offset) {
- display(offset);
- }
- else {
- url = clock_host + "?r=" + Math.random();
- loadXMLDoc(url);
- }
- }
- function displayTime() {
- var div = document.getElementById("time");
- var time = new Date();
- time.setTime(time.getTime() - offset);
- div.innerHTML = d2(time.getHours())+":"+d2(time.getMinutes())+":"+d2(time.getSeconds());
- setTimeout("displayTime()", 1000);
- }
- function d2(val) {
- if (val < 10) val = "0"+val;
- return val;
- }
- function setCookie (name, value, expires, path, domain, secure) {
- document.cookie = name + "=" + escape(value) +
- ((expires) ? "; expires=" + expires : "") +
- ((path) ? "; path=" + path : "") +
- ((domain) ? "; domain=" + domain : "") +
- ((secure) ? "; secure" : "");
- }
- function getCookie(name) {
- var cookie = " " + document.cookie;
- var search = " " + name + "=";
- var setStr = null;
- var offset = 0;
- var end = 0;
- if (cookie.length > 0) {
- offset = cookie.indexOf(search);
- if (offset != -1) {
- offset += search.length;
- end = cookie.indexOf(";", offset);
- if (end == -1) end = cookie.length;
- setStr = unescape(cookie.substring(offset, end));
- }
- }
- return(setStr);
- }
- // ---------------- CSS definition --------------------------------------------
- function css() {
- try {
- if (time_width == undef || time_height == undef || time_width == 0 || time_height == 0) {
- time_width = undef;
- time_height = undef;
- }
- if (time_width == undef) time_width = 88;
- if (time_height == undef) time_height = 31;
- var w=time_width,h=time_height;
- if (time_border == undef) time_border = 'black 1px solid';
- if (time_border == '') time_border = '0px none';
- if (match = time_border.match(/\b([0-9]+)px\b/)) {
- w -= match[1]*2;
- h -= match[1]*2;
- }
- if (time_bg == undef) time_bg = 'http://clock.time.in.ua/images/clock-bg-'+time_width+'-'+time_height+'-1.gif';
- var obj = document.getElementById("timeinua");
- if (obj) {
- if (obj.style) {
- obj.style.margin = '0px';
- obj.style.padding = '0px';
- obj.style.fontSize = '1px';
- obj.style.width = w+'px';
- obj.style.height = h+'px';
- obj.style.border = time_border;
- obj.style.overflow = 'hidden';
- obj.style.whiteSpace = 'nowrap';
- if (time_bg) {
- if (time_bg.indexOf('#') == 0) obj.style.backgroundColor = time_bg;
- else if (time_bg.indexOf('http://') == 0) obj.style.background = "url('"+time_bg+"')";
- }
- }
- var obj1 = obj.getElementsByTagName('div');
- for(var i=0; i<obj1.length; i++) {
- if (obj1[i].style) {
- obj1[i].style.font = '8pt Verdana,Tahoma';
- obj1[i].style.textAlign = 'center';
- if (i == 0) {
- obj1[i].style.lineHeight = '0.9';
- obj1[i].style.height = '10px';
- obj1[i].style.overflow = 'hidden';
- }
- }
- }
- obj1 = obj.getElementsByTagName('a');
- if (obj1.length && obj1[0] && obj1[0].href.indexOf('http://time.in.ua/') == 0) {
- obj1[0].style.color = '#0000a0';
- obj1[0].style.textDecoration = 'none';
- }
- else {
- clock_html += '?error';
- }
- }
- else {
- clock_html += '?error';
- }
- }
- catch(e) { }
- }
- // ---------------- main programm start --------------------------------------
- if (window.location.href.indexOf(clock_host) == 0) {
- if (window.location.href.indexOf('error') == -1) {
- reqstart = (new Date()).getTime();
- startSync();
- }
- else {
- var obj = document.getElementById("img");
- if (obj) {
- obj.style.visibility = 'hidden';
- obj.style.width = '0px';
- }
- obj = document.getElementById("time");
- obj.innerHTML = 'code error';
- }
- }
- else {
- css();
- document.write('<iframe src="'+clock_host+clock_html+'" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" width="'+w+'" height="18" scrooling="no" allowtransparency="true"></iframe>');
- }
|