var brand;

function main( brandName ) {
	erase('update0');
	erase('update1');
  brand = brandName;
  switch( brand )
  {
    case "rotel":
			xmlDoc=loadXMLDoc('downloads/software/rotel-software.xml');
      break;
    case "classe":
      xmlDoc=loadXMLDoc('downloads/software/classe-software.xml');
			break;
    case "bw":
      xmlDoc=loadXMLDoc('downloads/software/bw-software.xml');
			break;
  }
  dropDown(xmlDoc);
}

function loadXMLDoc(dname) {
  if (window.XMLHttpRequest) {
    xmlDoc=new window.XMLHttpRequest();
    xmlDoc.open("GET",dname,false);
    xmlDoc.send("");
    return xmlDoc.responseXML;
  }
  // IE 5 and IE 6
  else if (new ActiveXObject("Microsoft.XMLDOM")) {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(dname);
    return xmlDoc;
  }
  alert("Error loading document");
  return null;
}

//oldHTML stores the original drop down menu data.  Global variable so later functions do not keep appending to the end of the page.	
var oldHTML;
//modelVer stores the location in the XML file where the model in question is stored.  Must be global so it can be accessed in multiple functions.
var modelVer, versionChk, rlsNotes, qtyVer;

function dropDown(xmlDoc) {
	// Creates first dropdown menu with models
  txt="<select id=\"list\" onchange=\"dropDown2(document.getElementById('list').value)\">\n";
	switch( brand )
  {
    case "rotel":
			txt = txt + "<option selected=\"selected\">Rotel: Choose a model</option>\n";
      break;
    case "classe":
			txt = txt + "<option selected=\"selected\">Classe: Choose a model</option>\n";
			break;
    case "bw":
			txt = txt + "<option selected=\"selected\">B&amp;W: Choose a model</option>\n";
			break;
  }
	
	x=xmlDoc.documentElement.getElementsByTagName("model");
  for (i=0;i<x.length;i++) {
    txt=txt + "<option value=\"" + x[i].getAttribute("modelName") + "\">" + x[i].getAttribute("modelName") + "</option>\n";
	}
  txt=txt + "</select>\n";
	// Write changes to HTML
	updateHTML(txt, 'copy');
	// Save the finished drop down menu to oldHTML variable
	oldHTML = txt;
}

// Creates second dropdown menu with versions, and how to check current version info.
function dropDown2(model) {
  // Erase any previous content
	erase('update0');
	erase('update1');
	
	findMatch(model);
	
	txt="<h2>Model: " + model + "</h2>\n";
	if( qtyVer > 1) {
	  txt=txt + "<div class=\"version\"><div><div>\n";
	  txt=txt + "<p>Please check the currently installed software in the unit, then choose the appropriate version.</p>\n";
	  txt=oldHTML + txt;

	  // Building the dropdown here
	  txt = txt + "<select id=\"versionlist\" onchange=\"dropDown3(document.getElementById('versionlist').value)\">\n";
	  txt = txt + "<option selected=\"selected\">Please choose a version</option>\n";
    for(i=0;i<modelVer.childNodes.length;i++) {
	    if(modelVer.childNodes[i].attributes != null) {
			  txt = txt + "<option value=\"" + modelVer.childNodes[i].getAttribute("versionName") + "\">" + modelVer.childNodes[i].getAttribute("versionName") + "</option>\n";
		  }
	  }
    txt=txt + "</select>\n</div></div></div>\n";			
	}
	else {
	  txt=txt + "<div class=\"version\"><div><div>\n";
	  txt=txt + "<p>Please check the currently installed software in the unit to confirm if an update is necessary.</p>\n";
		txt=txt + "</div></div></div>\n";
	  txt=oldHTML + txt;
	}
		
	// Writing how to check version info in this div
	txt = txt + "<div class=\"version\"><div><div>\n<h3>How to check the current software</h3>\n<p>" + versionChk + "</p>\n</div></div></div>\n";

	// Write new changes to HTML
	updateHTML(txt, 'copy');
	
	if (qtyVer == 1) {
	  for(i=0;i<modelVer.childNodes.length;i++) {
	    if(modelVer.childNodes[i].attributes != null) {
			  dropDown3(modelVer.childNodes[i].getAttribute("versionName"));
		  }
	  } 	  
	}
}

// Builds the recommended & optional update sections here.
function dropDown3(version) {
  var txt = "";
	
  selectedVersion = versionMatch(version);
	
	if (selectedVersion == null) {
		return;
	}
		 				
	// Loops through softwareUpdate1~3 nodes for the version in question and creates divs for each
	txt = txt + "<h2>Recommended Updates</h2>\n";
	txt = txt + "<span style=\"font-style: italic\">Please make sure to download the <b>Installation Program, Software Data File</b>, and <b>Installation Instructions</b> for each update below. These files can be downloaded from the links below by right clicking and choosing \"Save target as\" or \"Save link as\".</span>\n"; 
	for(i=0, ii=0, opt=0; i<selectedVersion.childNodes.length; i++) {
		// Recommended updates = softwareUpdate1~3 nodes
	  if((selectedVersion.childNodes[i].attributes != null) && (selectedVersion.childNodes[i].getAttribute("title") != null) ) {
			txt = txt + "<div class=\"update\">\n"; 														
			txt = txt + "<h2>" + selectedVersion.childNodes[i].getAttribute("title") + "</h2>\n";
		  txt = txt + "<b>Current Version: </b>" + selectedVersion.getElementsByTagName("versionNumber")[ii].childNodes[0].nodeValue + "<br />\n";
			txt = txt + "<b>Last Updated: </b>" + selectedVersion.getElementsByTagName("versionDate")[ii].childNodes[0].nodeValue + "<br />\n"; 
   		txt = txt + outputMethod(selectedVersion.childNodes[i].getAttribute("method"));
			txt = txt + "<div class=\"install\">\n";
		  txt = txt + "<h3>Software Files</h3>\n";
   		txt = txt + "<a href=\"downloads/software/" + brand + "/data/" + selectedVersion.getElementsByTagName("filename")[ii].childNodes[0].nodeValue + "\">Software Data File</a><br /><br />\n";
   		txt = txt + "<a href=\"downloads/software/" + brand + "/documents/" + selectedVersion.getElementsByTagName("instructions")[ii].childNodes[0].nodeValue + "\">" + selectedVersion.childNodes[i].getAttribute("title") + " Installation Instructions</a><br /><br />\n";
			txt = txt + "<a href=\"downloads/software/" + brand + "/documents/" + rlsNotes + "\">Release Notes</a><br />\n";
			txt = txt + "</div>\n</div>\n";
			ii++;  // Increments for each recommended update so total can be displayed later
		}	
		// Optional updates = option1~2 nodes
	  else if(selectedVersion.childNodes[i].attributes != null && selectedVersion.childNodes[i].getAttribute("optionTitle") != null ) {
			txt = txt + "<h2>Optional Update</h2>";
  	  txt = txt + "<div class=\"option\">";
			txt = txt + "<h2>" + selectedVersion.childNodes[i].getAttribute("optionTitle") + "</h2>";
			txt = txt + "<a href=\"" + selectedVersion.childNodes[i].getAttribute("link") + "\">To find out more about this update click here.</a>\n";
			txt = txt + "</div>\n";
			opt++;  // Increments for each optional update so total can be displayed later
		}
	}

	// Create the line of text advising how many recommended & optional updates are available
	txt1 = "<b>There is " + ii + " recommended and " + opt + " optional updates available for this unit.</b><br />\n";

  // Release Notes link here
	txt1 = txt1 + "<span style=\"font-style: italic\">For detailed information on what changes or updates were made with each software revision, please download the <b><a href=\"downloads/software/" + brand + "/documents/" + rlsNotes + "\">release notes</a></b>.</span>"; 

	updateHTML(txt1, 'update0');
	updateHTML(txt, 'update1');
}

function findMatch(model) {
	// Loops through the model nodes until it find the match, when it does it saves version check, release notes & model versions to variables for use later.
	x=xmlDoc.documentElement.getElementsByTagName("model");
  for (i=0;i<x.length;i++) {
		if (x[i].getAttribute("modelName") == model) {	
		  versionChk = (xmlDoc.documentElement.getElementsByTagName("versionCheck")[i].childNodes[0].nodeValue);
		  rlsNotes = (xmlDoc.documentElement.getElementsByTagName("releaseNotes")[i].childNodes[0].nodeValue);
			modelVer = (xmlDoc.documentElement.getElementsByTagName("modelVersions")[i]);
			qtyVer = (xmlDoc.documentElement.getElementsByTagName("qtyVersions")[i].childNodes[0].nodeValue);
		} 
	}
}

function versionMatch(version) {
  // Loops through until it finds the node that the user selected in the version dropdown. 
	for(i=0;i<modelVer.childNodes.length;i++) {
	  if(modelVer.childNodes[i].attributes != null) {
			if(modelVer.childNodes[i].getAttribute("versionName") == version ) {
				return modelVer.childNodes[i];				
			} 
		}
	}
	// Returns null if user selected 'please choose a version' title by mistake.
	return null;
}

function updateHTML(txt, div) {
	document.getElementById(div).innerHTML=txt;
}

function erase(div) {
  document.getElementById(div).innerHTML="";
} 