//////////////////
// Ajax controls
/////////////////

var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function doSearch(forwhat) {
    createXMLHttpRequest();
    var xmlFile = "/files/templates/inc/isv_profile.xml";
    xmlHttp.open("GET", xmlFile, true);
    xmlHttp.onreadystatechange = function () {
        if(xmlHttp.readyState == 4) {
            if(xmlHttp.status == 200) {
                parseResults(forwhat);
            }
        }
    }
    xmlHttp.send(null);
}

function clearPreviousResults() {
    document.getElementById("information").innerHTML = "";
}

function parseResults(forwhat) {
    var results = xmlHttp.responseXML;
    var stem = null;
    var buffer = new Array();
    var branches = null;
    switch (forwhat){

	  case "selector":
	      branches = results.getElementsByTagName("profile");
	      var textlabel;
	      for(var i = 0; i < branches.length; i++) {
	          stem = branches[i];
	          textlabel = stem.getElementsByTagName("company")[0].firstChild.nodeValue;
		  document.getElementById("partnerList").appendChild(formatMenu(textlabel,i));
	      }
	  break;

	  case "listout":
	      branches = results.getElementsByTagName("profile");
	      var textlabel;
	      var list = "";
	      for(var i = 0; i < branches.length; i++) {
	          stem = branches[i];
	          textlabel = stem.getElementsByTagName("company")[0].firstChild.nodeValue;
	          list += formatList(textlabel,i);
	      }
	      div = document.getElementById("information");
	      div.innerHTML = list;
	      document.getElementById("partnerList").selectedIndex=0;
	  break;

	  default:
	      alert("Error: Missing parseResults parameter");
	  break;

	}
}

function formatList(a,n) {
    var mylist = "\n<a href=\"javascript:void(0)\" onclick=\"getDetails("+ n +")\" class=\"columnlist\">"+ a +"</a>";
    return mylist;
}

function formatMenu(a,n) {
    var myoption = document.createElement("option");
    var mytext = document.createTextNode(a);
    myoption.setAttribute("value",n);
    myoption.appendChild(mytext);
    return myoption;
}

function showDetails(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    if (evt) {
        var select = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (select && select.options.length > 1) {
            getDetails(select.value);
        }
    }
}

function getDetails(whichprofile) {
    var selection = parseInt(whichprofile) + 1;
    var item = xmlHttp.responseXML.getElementsByTagName("profile")[whichprofile];
    var div = document.getElementById("information");
    clearPreviousResults();
    div.innerHTML = formatDetails(item);
    document.getElementById("partnerList").selectedIndex=selection;
}

function formatDetails(a) {
    var buffer = new Array(
    	'company','logo','website','description','benefits','customer_quote','isv_quote',
    	'solution1title','solution1','solution2title','solution2',
    	'solution3title','solution3','solution4title','solution4',
    	'solution5title','solution5','sq1','sq2'
    );
    var mytext = "";

    for(var i = 0; i < buffer.length; i++) {
    	buffer[buffer[i]] = a.getElementsByTagName(buffer[i])[0].firstChild.nodeValue;
    }

    mytext += "\n<img src=\"/files/images/logos/"+ buffer['logo'] +"\" align=\"right\" vspace=\"5\"/>";

    mytext += "\n<h2>"+ buffer['company'] +"</h2>";

    if (buffer['website'] != "-")
    	mytext += "\n<small><a href=\""+ buffer['website'] +"\" target=\"_blank\">"+ buffer['company'] +" Homepage</a></small><hr style=\"margin-top: 20px\"/>";
    if (buffer['description'] != "-")
    	mytext += "\n    <p><b>Company Profile</b><br />"+ buffer['description'] +"</p>";
    if (buffer['benefits'] != "-")
    	mytext += "\n    <p><b>Value Proposition/Benefits</b><br />"+ buffer['benefits'] +"</p>";
    if (buffer['customer_quote'] != "-")
    	mytext += "\n    <blockquote>"+ buffer['customer_quote'] +"</blockquote>";
    if (buffer['isv_quote'] != "-")
    	mytext += "<br />\n    <blockquote>"+ buffer['isv_quote'] +"</blockquote>";
    if ( (buffer['solution1'] != "-") || (buffer['solution2'] != "-") )
    	mytext += "\n    <h4 class=\"rule\">Solutions</h4>";
    if (buffer['solution1'] != "-")
    	mytext += "\n    <p><b>"+ buffer['solution1title'] +"</b><br />"+ buffer['solution1'] +"</p>";
    if (buffer['solution2'] != "-")
    	mytext += "\n    <p><b>"+ buffer['solution2title'] +"</b><br />"+ buffer['solution2'] +"</p>";
    if (buffer['solution3'] != "-")
    	mytext += "\n    <p><b>"+ buffer['solution3title'] +"</b><br />"+ buffer['solution3'] +"</p>";
    if (buffer['solution4'] != "-")
    	mytext += "\n    <p><b>"+ buffer['solution4title'] +"</b><br />"+ buffer['solution4'] +"</p>";
    if (buffer['solution5'] != "-")
    	mytext += "\n    <p><b>"+ buffer['solution5title'] +"</b><br />"+ buffer['solution5'] +"</p>";
    if (buffer['sq1'] != "-")
    	mytext += "\n    <blockquote>"+ buffer['sq1'] +"</blockquote>";
    if (buffer['sq2'] != "-")
    	mytext += "\n<br />\n    <blockquote>"+ buffer['sq2'] +"</blockquote>";

    return mytext;
}

////////////////////////////////////
// Timers are necessary for Firefox,
// since it has a bad habit of not
// being able to run multiple xmlHttp
// requests in close proximities.

    setTimeout("doSearch('listout')", 1);
    setTimeout("doSearch('selector')", 400);

