
  /*
   *  TimeServer - Simple Ajax Updater
	*  -----------------------------------------------------------------
	*  Copyright (C) 2006 SalesEmotion AdSolutions GmbH
	*	
	*/

	var timeServer = {
	         	   
	   dateObj: null,
	   timeObj: null,
	   
	   loopCount:0,
	   current: null,
	   
	   init: function(timeId,dateId) {
	      this.dateObj=document.getElementById(dateId);
	      this.timeObj=document.getElementById(timeId);
	      this.current=new Date();
         this.getTimeFromServer();	      	      
	   },
	   
      getObject: function() {
          var xmlHttp = null;
          
          try {
              xmlHttp = new XMLHttpRequest();
          } catch ( ex ) {
              var progIds = [
                  'MSXML2.XMLHTTP', 
                  'Microsoft.XMLHTTP', 
                  'MSXML2.XMLHTTP.5.0', 
                  'MSXML2.XMLHTTP.4.0', 
                  'MSXML2.XMLHTTP.3.0'
              ];
              
              var success = false;
              
              for ( var iterator = 0; ( iterator < progIds.length ) && !success; iterator++ ) {
                  try {
                      xmlHttp = new ActiveXObject( progIds[iterator] );
                      success = true;
                  } catch ( ex ) {
                  }
              }
              
              if ( !success ) {
                  return null;
              }
          }
          
          return xmlHttp;
      },
	   
	   getTimeFromServer: function() {
	      var objRef=this;
	      var xhttp=objRef.getObject(); 
	      if (!xhttp) {
	         this.current.setTime(Date.now());	         
	         this.updater(this);
	         return;
	      }
	      xhttp.onreadystatechange=function() { objRef.updateHandler(xhttp,objRef); }
	      xhttp.open('GET','/js/time.php',true);
	      xhttp.send('');
	   },
	   
	   updateHandler: function(xhttp,objRef) {
         if (xhttp.readyState!=4) { return; }	               
         this.current.setTime(Date.parse(xhttp.responseText));
         objRef.updater(objRef);
	   },
	   
	   updater: function(objRef) {	
	      if (objRef.dateObj) objRef.dateObj.innerHTML=objRef.current.toLocaleDateString();
         if (objRef.timeObj) objRef.timeObj.innerHTML=objRef.current.toLocaleTimeString();
         objRef.current.setTime( objRef.current.getTime()+1000);
         objRef.loopCount++;
         if (objRef.loopCount==60) {
            objRef.loopCount=0;
            objRef.getTimeFromServer();
         } else {
            window.setTimeout(function() { objRef.updater(objRef); }, 1000);
         }         
	   }
	   
	}