// ----------------------------------------------------------------------
//  Project:          Tiscali.DRM
//  Title:            clientCheck -- Checks Client Configuration 
//  File:             .../scripts/clientCheck.js
//  Date:             2004-06-28
//  Version:          1.0
//  Author(s):        Tobias Kunze
//  Status:           in Bearbeitung
//  Changes:   
//       Version 1.0 (2004-06-28; Tobias Kunze):
//         - initial version
// 
//  (C) Copyright 2004 horz informatik, all rights reserved.
//  ----------------------------------------------------------------------

// Maximum time difference allowed between client and server (milliseconds)
MAX_TIME_DIFFERENCE = 15 * 60 * 1000;

// Returns true if the client uses Microsoft Internet Explorer
function hasInternetExplorer() 
{
    var agent = navigator.userAgent.toLowerCase();
    return (((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1)));
}
// function hasInternetExplorer()

// Returns the MediaPlayer version taken from the DRM ActiveX object
// or "0.0.0.0" if the security version cannot be determined
function getCLIENTVERSION()
{
    var si    = false;
    try {
       if (document.LicenseObj) {
	   	si = document.LicenseObj.GetSystemInfo();
	   }
       if (si) {
			var start = si.indexOf("<CLIENTVERSION>");
	   		var stop  = si.indexOf("</CLIENTVERSION>");
	   		if (start && stop) {
				return (si.substring(start + 15, stop));
			}
       }	else	{
/*			test = "<OBJECT ID='vPlayer' height='0' width='0' "
				+"CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'>"
				+"<PARAM NAME='URL' VALUE=''>"
			+"</OBJECT>";
			$('systemcheck').update(test);
			alert($('vPlayer'));
			alert($('vPlayer').versionInfo);
			alert($('vPlayer').version);
			alert($('vPlayer').name);
			$('vPlayer').remove(); */
			
/*			var netobj = new GeckoActiveXObject("DRM.GetLicense.1");
			alert(netobj);
		    si = netobj.GetSystemInfo();
			alert("SI: "+si);*/
	   } // if
    } // try
    catch (e) {
		//alert(e.message);
	// ignore
    }
    return("0.0.0.0");
}
// function getCLIENTVERSION()

// Returns the DRM security version taken from the DRM ActiveX object
// or "0.0.0.0" if the security version cannot be determined
function getSECURITYVERSION()
{
    var si    = false;
    try {
       if (document.LicenseObj)
          si = document.LicenseObj.GetSystemInfo();
       if (si) {
	   var start = si.indexOf("<SECURITYVERSION>");
	   var stop  = si.indexOf("</SECURITYVERSION>");
	   if (start && stop) 
	       return(si.substring(start+17, stop));
       } // if
    } // try
    catch (e) {
	// ignore
    }
    return("0.0.0.0");
}
// function getSECURITYVERSION()

// Returns the user's Windows version: one of the strings
// "2003", "XP", "2000", "ME", "98", "NT", "95", "3.1"
function getWINDOWSVERSION()
{
    var agt = navigator.userAgent.toLowerCase();
    var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );

    if ((agt.indexOf("windows nt 5.2")!=-1))
      return "2003";

    if ((agt.indexOf("windows nt 5.1")!=-1))
      return "XP";

    if ((agt.indexOf("windows nt 5.0")!=-1))
      return "2000";

    if ((agt.indexOf("win 9x 4.90")!=-1))
      return "ME";

    if ((agt.indexOf("win98")!=-1) 
	|| (agt.indexOf("windows 98")!=-1))
      return "98";
    
    if ((agt.indexOf("winnt")!=-1) 
	|| (agt.indexOf("windows nt")!=-1))
      return "NT";

    if ((agt.indexOf("win95")!=-1) 
	|| (agt.indexOf("windows 95")!=-1))
      return "95";
    
    if ((agt.indexOf("windows 3.1")!=-1) 
	|| (agt.indexOf("win16")!=-1) 
	|| (agt.indexOf("windows 16-bit")!=-1))
      return "3.1";

    return navigator.userAgent+"/"+navigator.plattform;
}
// function getWINDOWSVERSION()

// Returns the current datetime of the client's computer in a textual
// form
function getDATETIME()
{
    var NOW    = new Date();
    return NOW.toLocaleString();
}
// function getDATETIME()

// Converts the timestamp 'millis' (in milliseconds since epoch) into
// a textual representation.
function toLOCALTIME(millis)
{
    var d = new Date();
    d.setTime(millis);
    return d.toLocaleString();
}
// function toLOCALTIME(...)


// Checks whether the client fulfills the following conditions:
//  - 1. IE used
//  - 2. WMRM support available
//  - 3. Media Player available
//  - 4. MediaPlayer >= 7.1
//  - 5. SecurityVersion >= 2.2.0.1
//  - 6. Time difference too large
// If one of those conditions is violated, the function returns the
// corresponding number (e.g., doClientCheck() returns 3 if an
// insufficient MediaPlayer is installed). If all checks are OK, the
// number 0 is returned.
//   serverTimeMillis -- current server time in milliseconds since epoch
function doClientCheck(serverTimeMillis)
{
    // (1) IE is required
    if (! hasInternetExplorer())
	return 1;
	
    // (2) WMRM support is required
    if (! document.LicenseObj)
	return 2;

    // (3) At least Media Player version 7.1 is required
    var cv  = getCLIENTVERSION();
    if (!cv || cv == "")
	return 3;

    var dot1 = cv.indexOf(".");
    var dot2 = cv.indexOf(".", dot1+1);
    var cv_major = parseInt(cv.substring(0,      dot1));
    var cv_minor = parseInt(cv.substring(dot1+1, dot2));
    if (cv_major < 7 || (cv_major == 7 && cv_minor == 0))
	return 4;

    // (4) At least security version 2.2.0.0 is required
    var sv = getSECURITYVERSION();
    dot1 = sv.indexOf(".");
    dot2 = sv.indexOf(".", dot1+1);
    var sv_major = parseInt(sv.substring(0,      dot1));
    var sv_minor = parseInt(sv.substring(dot1+1, dot2));
    if (sv_major < 2 || (sv_major == 2 && sv_minor < 2))
	return 5;

    // (5) Test time difference between client and server
    var clientTimeMillis = (new Date()).getTime();
    var difference       = Math.abs(clientTimeMillis - serverTimeMillis);
    if (difference > MAX_TIME_DIFFERENCE){
        return 0;
    }
    
    return 0;
}
// function doClientCheck()


// Perform the client check using doClientCheck(). Redirect to error
// page or set clientInfo and clientTime in the HTML-Form 'infoForm'
// and submit it.
//   serverTimeMillis -- current server time in milliseconds since epoch
function submitClientInfo(serverTimeMillis,test)	{
    var resp = doClientCheck(serverTimeMillis);
    var output;
    
    if (resp != 0)	{
        //document.write("Test: " + resp);
//        document.location = "modules.php?name=VoDLicence&clientEC="+resp;
		output = "Fehler: "+resp;
	}	else	{	
//		document.infoForm.clientInfo.value = LicenseObj.GetSystemInfo();
		output = LicenseObj.GetSystemInfo();
		if(test){	
			var si = LicenseObj.GetSystemInfo();	
			var start = si.indexOf("<MACHINECERTIFICATE>");	
		   	var stop  = si.indexOf("</MACHINECERTIFICATE>");	
			if(start>=0){
				var text1 = si.substring(0, start);	
				var text2 = si.substring(stop+21, si.length);	
				output = text1 + text2;
			}

			var start = output.indexOf("<REVINFO>");	
		   	var stop  = output.indexOf("</REVINFO>");	
			if(start>=0){
				var text1 = output.substring(0, start);	
				var text2 = output.substring(stop+10, si.length);	
				output = text1 + text2;
			}
			
			//document.getElementById('test').innerHTML = "<pre>"+LicenseObj.GetSystemInfo()+"</pre>";
			//alert('test');		
//			document.infoForm.submit();	
		}	else	{
//			document.infoForm.submit();
	   	}
    }
    return output;
}
// function submitClientInfo()

// --- end of file
