function checkForRedirect(){
  var divID;
  var url = document.location.href;
  var redirect = url.split('?open=');
  if (redirect.length > 1){
    divID=redirect[1].split(':');
    showMain(divID[0]);
    if (divID.length>1){
      switch (divID[0]){
	case 'information' :
	  showInfo(divID[1]);
	  if (divID.length>2) showSoftware(divID[2]);
	  break;
	case 'download' :
	  showDown(divID[1]);
	  break;
	case 'howto' :
	  showHowto(divID[1]);
	  break;
      }
    }
  } else {
    recordView('home');
  }
}

function showMain(divID){
  recordView(divID);
  hideAllMain();
  document.getElementById(divID).style.display='inline';
  if (divID == 'information') showInfo('theSoftware');
  if (divID == 'howto') showHowto('useScript');
}

function showInfo(infoID){
  recordView(infoID);
  hideAllInformation();
  document.getElementById(infoID).style.display='inline';
  if (infoID == 'theSoftware') showSoftware('overview');
}

function showSoftware(softID){
  recordView(softID);
  hideAllSoftware();
  document.getElementById(softID).style.display='inline';
}

function showHowto(howtoID){
  recordView(howtoID);
  hideAllHowto();
  document.getElementById(howtoID).style.display='inline';
}

function showDown(downID){
  recordView(downID);
  hideAllDown();
  document.getElementById(downID).style.display='inline';
  if (downID == 'manual') showDownloadOptions('all');
  if (downID == 'guided') {
    document.getElementsByName('alreadyInstalled').item(0).selectedIndex = 0;
    hideGuided(["intentLi","preferenceLi"]);
  }
}

function showDownloadOptions(choice){
  var i,optionDiv,mainOptions;
  hideAllOptions();
  switch (choice){
    case 'all' :
      optionDiv=["script","iso","mathkubuntu","mathubuntu","kubuntu","ubuntu","kubuntu32bit","kubuntu64bit","ubuntu32bit","ubuntu64bit"];
      mainOptions=2;
      break;
    case 'kscript' :
      optionDiv=["script","mathkubuntu"];
      mainOptions=1;
      break;
    case 'uscript' :
      optionDiv=["script","mathubuntu"];
      mainOptions=1;
      break;
    case 'kiso' :
      optionDiv=["iso","kubuntu","kubuntu32bit","kubuntu64bit"];
      mainOptions=1;
      break;
    case 'uiso' :
      optionDiv=["iso","ubuntu","ubuntu32bit","ubuntu64bit"];
      mainOptions=1;
      break;
    case 'iso' :
      optionDiv=["iso","kubuntu","kubuntu32bit","kubuntu64bit","ubuntu","ubuntu32bit","ubuntu64bit"];
      mainOptions=1;
      break;
  }
  for(i=0;i<mainOptions;i++)
    document.getElementById(optionDiv[i]).style.display='inline';
  for(i=mainOptions;i<optionDiv.length;i++)
    document.getElementById(optionDiv[i]).style.display='list-item';
}

function showGuided(guideID){
  if (guideID == 'intentLi') hideGuided(["preferenceLi"]);
  document.getElementById(guideID).style.display='list-item';
}

function showThankyouDownload(location){
  var element = document.getElementById('thankyouLocation');
  element.innerHTML = 'http://mathbuntu.org/' + location;
  element.href = location;
  document.getElementById('thankyouDownload').style.display = 'inline';
}

function hideAllMain(){
  var i;
  var mainDiv=["home","information","download","howto","purchase","thankyouPurchase","views"];
  for(i=0;i<mainDiv.length;i++)
    document.getElementById(mainDiv[i]).style.display='none';
}

function hideAllInformation(){
  var i;
  var infoDiv=["theSoftware","theTexts","about","links"];
  for(i=0;i<infoDiv.length;i++)
    document.getElementById(infoDiv[i]).style.display='none';
}

function hideAllSoftware(){
  var i;
  var softDiv=["overview","sage","maxima","geogebra","r","octave","latex","lyx","pidgin"];
  for(i=0;i<softDiv.length;i++)
    document.getElementById(softDiv[i]).style.display='none';
}

function hideAllHowto(){
  var i;
  var howtoDiv=["useScript","useDVD","learnMore","installOther"];
  for(i=0;i<howtoDiv.length;i++)
    document.getElementById(howtoDiv[i]).style.display='none';
}

function hideAllDown(){
  var i;
  var downDiv=["guided","manual","script","iso","thankyouDownload"];
  for(i=0;i<downDiv.length;i++)
    document.getElementById(downDiv[i]).style.display='none';
}

function hideAllOptions(){
  var i;
  var optionDiv=["script","iso","mathkubuntu","mathubuntu","kubuntu","ubuntu","kubuntu32bit","kubuntu64bit","ubuntu32bit","ubuntu64bit"];
  for(i=0;i<optionDiv.length;i++)
    document.getElementById(optionDiv[i]).style.display='none';
}

function hideGuided(guideDiv){
  var i,selectName;
  for(i=0;i<guideDiv.length;i++) {
    selectName=guideDiv[i].split('Li')[0];
    document.getElementById(guideDiv[i]).style.display='none';
    document.getElementsByName(selectName).item(0).selectedIndex = 0;
  }
}

function switchImage(num,ext,wid,hei) {
  var spaceElement=document.getElementById('space'+num);
  if (spaceElement.innerHTML=="") {
    spaceElement.innerHTML="<img src=\"images/javascript/image"+num+ext+"\" width=\""+wid+"\" height=\""+hei+"\">";
  } else {
    spaceElement.innerHTML="";
  }
}

function alreadyInstalledChanged(){
  var value = selectedValue('alreadyInstalled');
  hideGuided(["intentLi","preferenceLi"]);
  hideAllOptions();
  switch (value) {
    case 'No' :
      showGuided('preferenceLi');
      break;
    case 'Yes, Kubuntu (or MathKubuntu)' : case 'Yes, Ubuntu (or MathUbuntu)' :
      showGuided('intentLi');
      break;
  }
}

function intentChanged(){
  var flavor;
  var value = selectedValue('intent');
  switch (value) {
    case '-- Select one --' :
      hideGuided(["preferenceLi"]);
      break;
    case 'Install Mathbuntu now' :
      flavor = selectedValue('alreadyInstalled');
      switch (flavor){
	case 'Yes, Kubuntu (or MathKubuntu)' :
	  showDownloadOptions('kscript');
	  break;
	case 'Yes, Ubuntu (or MathUbuntu)' :
	  showDownloadOptions('uscript');
	  break;
      }
      break;
    case 'Create a Mathbuntu DVD or USB stick' :
      showGuided('preferenceLi');
      break;
  }
}

function preferenceChanged(){
  var value = selectedValue('preference');
  if (value == '-- Select one --'){
    hideAllOptions();
    return;
  }
  switch (value) {
    case 'No preference' :
      showDownloadOptions('iso');
      break;
    case 'Kubuntu' :
      showDownloadOptions('kiso');
      break;
    case 'Ubuntu' :
      showDownloadOptions('uiso');
      break;
  }
}

function selectedValue(selectName){
  var element = document.getElementsByName(selectName).item(0);
  return element.options[element.selectedIndex].text;
}

var requestJ87Hyd9;
function recordDownload(page){
  var EmailAddress = document.getElementsByName('EmailAddress').item(0).value;
  var url = 'php/mathbuntu.php?op=recordDownload&fileName=' + page + '&EmailAddress=' + EmailAddress + '&agent=' + navigator['userAgent'];
  if (requestJ87Hyd9==null) requestJ87Hyd9=getXmlHttpObject();
  if (requestJ87Hyd9==null) return;
  requestJ87Hyd9.open("GET",doctorURL(url),true);
  requestJ87Hyd9.onreadystatechange=recordDownloadDone;
  requestJ87Hyd9.send(null);
}
function recordDownloadDone(){
  var res,messageElement;
  if (requestJ87Hyd9.readyState==4 || requestJ87Hyd9.readyState=='complete') {
    res = requestJ87Hyd9.responseText;
    showThankyouDownload(res);
    window.location = res;
  }
}

var requestQr45tY7;
function recordView(page){
  var url = 'php/mathbuntu.php?op=recordView&divID=' + page + '&agent=' + navigator['userAgent'];
  if (requestQr45tY7==null) requestQr45tY7=getXmlHttpObject();
  if (requestQr45tY7==null) return;
  requestQr45tY7.open("GET",doctorURL(url),true);
  requestQr45tY7.onreadystatechange=recordViewDone;
  requestQr45tY7.send(null);
}
function recordViewDone(){
  var res,messageElement;
  if (requestQr45tY7.readyState==4 || requestQr45tY7.readyState=='complete') {
    res = requestQr45tY7.responseText;
    if(res=="home") getHomeHits();
  }
}

var requestU8h6Gh7;
function getHomeHits(){
  var url = 'php/mathbuntu.php?op=getHomeHits';
  if (requestU8h6Gh7==null) requestU8h6Gh7=getXmlHttpObject();
  if (requestU8h6Gh7==null) return;
  requestU8h6Gh7.open("GET",doctorURL(url),true);
  requestU8h6Gh7.onreadystatechange=getHomeHitsDone;
  requestU8h6Gh7.send(null);
}
function getHomeHitsDone(){
  var res,messageElement;
  if (requestU8h6Gh7.readyState==4 || requestU8h6Gh7.readyState=='complete') {
    res = requestU8h6Gh7.responseText;
    document.getElementById('views').style.display='block';
    document.getElementById('viewnum').innerHTML=res;
  }
}

