var chapter1Time="";
var chapter1Step="";
var chapter1=0;
var chapter2Min="";
var chapter2Sec="";
var chapter2=0;
var chapter3Step="";
var chapter3Min="";
var chapter3Sec="";
var chapter3=0;
var chapter4Record="";
var chapter4=0;
var chapter5Record="";
var chapter5=0;

leggiCookie = function()
{	
 xmlHttpNotification=GetXmlHttpObject();
 xmlHttpNotification.open("GET","http://www.rifo.it/processing/db/getRecords.asp",true);
 xmlHttpNotification.onreadystatechange=ricevutaRisposta;
 xmlHttpNotification.send(null);

 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf('SporewarsTimeRecord=') == 0){
     chapter1Time=c.substring('SporewarsTimeRecord='.length,c.length);
     chapter1=chapter1+1;
  }
  else if(c.indexOf('SporewarsStepRecord=') == 0){
     chapter1Step=c.substring('SporewarsStepRecord='.length,c.length);
     chapter1=chapter1+1;
  }
  else if(c.indexOf('Sporewars2MinRecord=') == 0){
     chapter2Min=c.substring('Sporewars2MinRecord='.length,c.length);
     chapter2=chapter2+1;
  }
  else if(c.indexOf('Sporewars2SecRecord=') == 0){
     chapter2Sec=c.substring('Sporewars2SecRecord='.length,c.length);
     chapter2=chapter2+1;
  }
  else if(c.indexOf('Sporewars3StepRecord=') == 0){
     chapter3Step=c.substring('Sporewars3StepRecord='.length,c.length);
     chapter3=chapter3+1;
  }
  else if(c.indexOf('Sporewars3MinRecord=') == 0){
     chapter3Min=c.substring('Sporewars3MinRecord='.length,c.length);
     chapter3=chapter3+1;
  }
  else if(c.indexOf('Sporewars3SecRecord=') == 0){
     chapter3Sec=c.substring('Sporewars3SecRecord='.length,c.length);
     chapter3=chapter3+1;
  }
  else if(c.indexOf('Sporewars4Record=') == 0){
     chapter4Record=c.substring('Sporewars4Record='.length,c.length);
     chapter4=chapter4+1;
  }
  else if(c.indexOf('Sporewars5Record=') == 0){
     chapter5Record=c.substring('Sporewars5Record='.length,c.length);
     chapter5=chapter5+1;
  }
 }
 if(chapter1>1){
   document.getElementById("Chapter1Record").innerHTML = "Your best score:     Steps: "+chapter1Step+" (in: "+chapter1Time+")";
 }else{
   document.getElementById("Chapter1Record").innerHTML = "Your best score:     (none yet)";
 }
 if(chapter2>1){
   document.getElementById("Chapter2Record").innerHTML = "Your best score:     Time: "+chapter2Min+"' "+chapter2Sec+"''";
 }else{
   document.getElementById("Chapter2Record").innerHTML = "Your best score:     (none yet)";
 }
 if(chapter3>1){
   document.getElementById("Chapter3Record").innerHTML = "Your best score:     Steps: "+chapter3Step+" (in: "+chapter3Min+"' "+chapter3Sec+"'')";
 }else{
   document.getElementById("Chapter3Record").innerHTML = "Your best score:     (none yet)";
 }
 if(chapter4>0){
   var arr=chapter4Record.split("-");
   document.getElementById("Chapter4Record").innerHTML = "Your best score:     Steps: "+Math.floor(arr[2])+" (in: "+Math.floor(arr[0])+"' "+Math.floor(arr[1])+"'')";
 }else{
   document.getElementById("Chapter4Record").innerHTML = "Your best score:     (none yet)";
 }
 if(chapter5>0){
   var arr=chapter5Record.split("-");
   document.getElementById("Chapter5Record").innerHTML = "Your best score:     Time: "+Math.floor(arr[0])+"' "+Math.floor(arr[1])+"''";
 }else{
   document.getElementById("Chapter5Record").innerHTML = "Your best score:     (none yet)";
 }
};

function apri(id)
{
 if(id==1){
   window.open('http://www.rifo.it/processing/Chapter1/Chapter1.html','Chapter1','width=650,height=650 ,location=no,toolbar=no, menubar=no,scrollbars=no,resizable=no,status=no,directories=no');
 }else if(id==2){
     window.open('http://www.rifo.it/processing/Chapter2/Chapter2.html','Chapter2','width=650,height=650 ,location=no,toolbar=no, menubar=no,scrollbars=no,resizable=no,status=no,directories=no');
 }else if(id==3){
     window.open('http://www.rifo.it/processing/Chapter3/Chapter3.html','Chapter3','width=650,height=650 ,location=no,toolbar=no, menubar=no,scrollbars=no,resizable=no,status=no,directories=no');
 }else if(id==4){
     window.open('http://www.rifo.it/processing/Chapter4/Chapter4.html','Chapter4','width=650,height=650 ,location=no,toolbar=no, menubar=no,scrollbars=no,resizable=no,status=no,directories=no');
 }else if(id==5){
     window.open('http://www.rifo.it/processing/Chapter5/Chapter5.html','Chapter5','width=650,height=650 ,location=no,toolbar=no, menubar=no,scrollbars=no,resizable=no,status=no,directories=no');
 }

};

function GetXmlHttpObject()
{
 var objXMLHttp=null
 if (window.XMLHttpRequest)
 {
   objXMLHttp=new XMLHttpRequest()
   return objXMLHttp;
 } else {
   try {
     objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP.4.0");
   } catch(e) { }
   if (objXMLHttp!=null) {
     return objXMLHttp;
   }
   try {
     objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
   } catch(e) { }
   if (objXMLHttp!=null) {
     return objXMLHttp;
   }
   try {
     objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
   } catch(e) { }
   if (objXMLHttp!=null) {
     return objXMLHttp;
   }
   try {
     objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
   } catch(e) { }
   if (objXMLHttp!=null) {
     return objXMLHttp;
   }
 }
 return objXMLHttp;
};

function ricevutaRisposta()
{
 if (xmlHttpNotification.readyState==4 || xmlHttpNotification.readyState=="complete")
 {
  if (xmlHttpNotification.responseText == null || xmlHttpNotification.responseText == '')
  {
    document.getElementById("Chapter1BestRecord").innerHTML = "-";
    xmlHttpNotification.abort();
  } else {
    var i = xmlHttpNotification.responseText.split(";");
    var j = i[0].split("-");
//    document.getElementById("Chapter1BestRecord").innerHTML = "Best score of the entire universe: Steps: "+j[2]+" (in: "+j[3]+"' "+j[4]+"'') - Tot. games successfully completed: "+j[1];
    document.getElementById("Chapter1BestRecord").innerHTML = "Best score of the entire universe: Steps: "+j[2]+" (in: "+j[3]+"' "+j[4]+"'')";
    var m = i[1].split("-");
//    document.getElementById("Chapter2BestRecord").innerHTML = "Best score of the entire universe: Time: "+m[3]+"' "+m[4]+"'' - Tot. games successfully completed: "+m[1];
    document.getElementById("Chapter2BestRecord").innerHTML = "Best score of the entire universe: Time: "+m[3]+"' "+m[4]+"''";
    var k = i[2].split("-");
//    document.getElementById("Chapter3BestRecord").innerHTML = "Best score of the entire universe: Steps: "+k[2]+" (in: "+k[3]+"' "+k[4]+"'') - Tot. games successfully completed: "+k[1];
    document.getElementById("Chapter3BestRecord").innerHTML = "Best score of the entire universe: Steps: "+k[2]+" (in: "+k[3]+"' "+k[4]+"'')";
    var n = i[3].split("-");
    document.getElementById("Chapter4BestRecord").innerHTML = "Best score of the entire universe: Steps: "+n[2]+" (in: "+n[3]+"' "+n[4]+"'')";
    var o = i[4].split("-");
    document.getElementById("Chapter5BestRecord").innerHTML = "Best score of the entire universe: Time: "+o[3]+"' "+o[4]+"''";
  }
 }
};
