//   XMLHTTP Request common functions

if (window.ActiveXObject && !window.XMLHttpRequest) 
{
  window.XMLHttpRequest = function() 
  {
    try 
      {
        return new ActiveXObject('Microsoft.XMLHTTP');
      } 
    catch (e) 
      {
      }
    return null;
  };
}

function process_req(req, docelt)
{
	if (!req)
	{
		alert ('XMLHttpRequest is not supported');
		return;
	}
	req.onreadystatechange = 
		function()
		{
			if (req.readyState == 4 && req.status == 200) 
			{
				var respXML =  req.responseText; 
				document.getElementById(docelt).innerHTML = respXML;
			} 
			else 
				if (req.readyState == 4 && req.status != 200)
				{
					document.getElementById(docelt).innerHTML = soap_error_alert (req);
				}
	  }
}

function soap_error_alert (req)
{
  if (req.status == 500 && req.responseXML != null)
    {
      var respXML = req.responseXML;
      var elms = respXML.getElementsByTagName("Fault");
      var err = req.statusText + '\r\n';
      if (elms.length != 0)
        {
          var i;
          var elms = respXML.getElementsByTagName("faultstring");
          if (elms.length)
	    {
	      var nodes = elms[0].childNodes 
	       for (i = 0; i < nodes.length; i++)
		 {
		   if (nodes[i].nodeType == 3)
		     err = err + nodes[i].nodeValue; 
		 }  
	    }
        }
      alert (err);
      return 'Error';   
    }
  else
    {
      var ErrRecp = document.getElementById('ajaxerrors');
      if (ErrRecp)
      {
        ErrRecp.innerHTML = '<div class="head">An Error Occured...</div>\r\n<div class="error">' 
        	+ req.responseText 
        	+ '</div>'
        	+ '<br /><button name="ackerror" onClick="document.getElementById(\'ajaxerrors\').innerHTML = \'\'; document.getElementById(\'ajaxerrors\').style.display = \'none\'; return true;">OK</button><br />'
        	;
        ErrRecp.style.display = 'block';
        return req.statusText;   
      }
      else 
      {
        alert ('Error: ' + req.statusText + '\r\n' + req.responseText + '\r\n');
        return 'Error';   
      }
    }
}

var dbg = 0;

function debug(txt)
{
  if (dbg != 1) return;
  var dbgb = document.getElementById('ajaxerrors');
  var txtold = dbgb.innerHTML;
  dbgb.innerHTML = txtold + '<br />' + txt;
  dbgb.style.display = 'block';
  dbgb.style.top = '460px';
}

function fill_inner (results, tag_name)
{
  var str = "";
  if (!results || results.length != 1)
    {
      str = 'No result is returned.';
    }
  else
    {  
      var nodes = results[0].childNodes;

      if (!nodes || nodes.length == 0)
        {
          str = 'No nodes in the result returned.';
        }
      else
        {
          var i;
          for (i = 0; i < nodes.length; i++)
             {
	       if (nodes[i].nodeType == 3)
		 str = str + nodes[i].nodeValue;
             }
        }
     }
  document.getElementById(tag_name).innerHTML = str;
}


function waitingMsg()
{
  return '<div class="working"><img src="busy_spinner_medium.gif" height="20px" alt="working..." /></div>';
}

function HideScrolls()
{
  var them = document.getElementsByTagName('div');
  for (var ii=0; ii < them.length; ii ++)
  {
    if (them[ii].className == 'content')
    {
      them[ii].style.overflow = 'hidden';
    }
  }
}

function ShowScrolls()
{
  //alert('show');
  var them = document.getElementsByTagName('div');
  for (var ii=0; ii < them.length; ii ++)
  {
    if (them[ii].className == 'content')
    {
      them[ii].style.overflow = 'auto';
    }
  }
}

function resetPanels(p)
{
 if (p == 'p6')
 {
   HideScrolls();
 }
 else
 {
   ShowScrolls(); 
 }
 
 //document.getElementById('ProductsTree').innerHTML = '';
 var inc=0;
 var alltags=document.all? document.all : document.getElementsByTagName("*");
 for (i=0; i<alltags.length; i++)
 {
   if (alltags[i].className=='head')
     alltags[i].id = null;

   if (alltags[i].className=='itemselected'  && alltags[i].parentNode.parentNode.parentNode.id == p)
     alltags[i].className = null;
 }
  document.getElementById('final').style.visibility = 'hidden';
  document.getElementById('final').style.display = 'none';
  document.getElementById('p5').style.visibility = 'hidden';
  document.getElementById('p5').style.display = 'none';

  if ('p1' == p)
  {
  	document.getElementById('p3').style.display = 'none';
    document.getElementById('p3').style.visibility = 'hidden';
   	document.getElementById('p6').style.display = 'none';
    document.getElementById('p6').style.visibility = 'hidden';
  }

  if ('p2' == p)
  {
  	document.getElementById('p3').style.display = 'none';
    document.getElementById('p3').style.visibility = 'hidden';
   	document.getElementById('p6').style.display = 'none';
    document.getElementById('p6').style.visibility = 'hidden';
  }

}

function setStepHead(text)
{
    document.getElementById("stephead").innerHTML = '<span>'+text+'</span>';
}

function waiter()
{
  return;
}

function slide(target)
{
  var i;
  for (i = 0; i >= -100; i--)
  {  
    setTimeout('waiter()', 1000);
    document.getElementById(target).style.marginLeft = i+'px';
  }

}

var js_pfam = '';
var js_pform = '';
var js_pcat = '';
var js_prod = '';
var js_release_dbms = '';
var js_oni = '';
var js_ofn = '';
var js_db = '';
var js_os = '';
var js_os2 = '';

var hist = 0;

function historyChange(newLocation, historyData)
{
  debug('historyChange - newLocation: ' + newLocation + '; historyData: ' + JSON.stringify(historyData));

  if (historyData)
  {
  	js_pfam = historyData.value1; 
  	js_pform = historyData.value2;
  	js_pcat = historyData.value3;
  	js_prod = historyData.value4;
  	js_release_dbms = historyData.value5;
  	js_os = historyData.value6;
   	js_os2 = historyData.value7;
	  js_oni = historyData.value8;
	  js_ofn = historyData.value9;
	  js_db = historyData.value10;

    document.download.pfam.value = js_pfam;
    document.download.pform.value = js_pform;
    document.download.pcat.value = js_pcat;
    document.download.prod.value = js_prod;
    document.download.os.value = js_os;
    document.download.os2.value = js_os2;

	}

  if (newLocation == 'new' || newLocation == '' || !newLocation)
  {
    resetPanels('p1');
    document.getElementById('p2').style.visibility = 'hidden';
    document.getElementById('p2').style.display = 'none';
    var p1 = document.getElementById('p1');
    var cp1 = p1.getElementsByTagName('div');
    cp1[0].id = 'selected';
    return;
  }

  hist = 1;
  if (newLocation == 'format')
  {
    kickMatrix();
    doP2(null, js_pfam, js_pform);
  }

  if (newLocation == 'cat')
  {
    kickMatrix();
    document.getElementById('p6').style.visibility = 'hidden';
    document.getElementById('p6').style.display = 'none';    
    doP3(null, js_pcat, js_pform);
  }

  if (newLocation == 'dbv')
  {
    kickMatrix();
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
    doDBVer(null, js_prod, js_pform);
  }

  if (newLocation == 'cli')
  {
    kickMatrix();
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
    doClient(null, js_prod, js_pform, js_db);
  }

  if (newLocation == 'svr')
  {
    kickMatrix();
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
    doServer(js_prod, js_oni, js_ofn);
  }

  if (newLocation == 'cliv')
  {
    kickMatrix();
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
    document.getElementById('final').style.visibility = 'hidden';
    document.getElementById('final').style.display = 'none';
    doClientVer(js_prod, js_ofn, js_db);
  }

  if (newLocation == 'svrv')
  {
    kickMatrix();
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
    document.getElementById('final').style.visibility = 'hidden';
    document.getElementById('final').style.display = 'none';
    hist = 1;
    doClientVer(js_prod, js_ofn, js_db);
    hist = 1;
    doServerVer(js_prod, js_ofn, js_db);
  }

  if (newLocation == 'release')
  {
    kickMatrix();
    hist = 1;
    doP2(null, js_pfam, js_pform);
    hist = 1;
    doP3(null, js_pcat, js_pform);
    hist = 1;
    doDBVer(null, js_prod, js_pform);
    hist = 1;
    doClient(null, js_prod, js_pform, js_db);
    hist = 1;
    doSomething(js_prod, js_os2, js_os, js_db);
  }

}

function pageStart() 
{ 
  hist = 0;
  debug ('JS Start');

  dhtmlHistory.initialize();
    // subscribe to DHTML history change
    // events
  dhtmlHistory.addListener(historyChange);
  if (dhtmlHistory.firstLoad)
  {
    resetPanels('p1');
    document.getElementById('p2').style.visibility = 'hidden';
    document.getElementById('p2').style.display = 'none';
  }
  
  if (document.getElementById('supermatrix'))
  {
    addMouseOvers('supermatrix', '#D1EEF5');
    sizeMatrix();
  }
}

function historyAdd(name)
{
	var ary = new Object;
	ary.value1 = js_pfam; 
	ary.value2 = js_pform;
	ary.value3 = js_pcat;
	ary.value4 = js_prod;
	ary.value5 = js_release_dbms;
	ary.value6 = js_os;
	ary.value7 = js_os2;
	ary.value8 = js_oni;
	ary.value9 = js_ofn;
	ary.value10= js_db;
  dhtmlHistory.add(name, ary);

  document.download.pfam.value = js_pfam;
  document.download.pform.value = js_pform;
  document.download.pcat.value = js_pcat;
  document.download.prod.value = js_prod;
  document.download.os.value = js_os;
  document.download.os2.value = js_os2;
}

function kickMatrix() 
{
  var ma = document.getElementById("ProductsTree");
  if (!ma || ma == 'undefined') {
    return;
  }
  ma.innerHTML = '';
  ma.style.display = 'none';
  ma.style.visibility = 'hidden';
}

function doP2(caller, familyid, formatid)
{
    kickMatrix();
    resetPanels('p1');
    if (caller) {
      caller.className = 'itemselected';
    }
    setStepHead('Step 2: Choose the product category.');
    var target = 'p2';
    document.getElementById(target).style.display = 'block';
    document.getElementById(target).style.visibility = 'visible';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=p2&family='+familyid+'&format='+formatid);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(null);
	//slide(target);

  js_pfam = familyid;
  js_pform = formatid;
  
  if (hist == 1) {hist = 0; return;}
  historyAdd('format');
  dopositions(document.getElementById('infopane'));
  return;
}

function doP3(caller, catid, formatid)
{
    resetPanels('p2');
    if (caller)
      caller.className = 'itemselected';
    setStepHead('Step 3: Choose the product that you want.');
    var target = 'p3';
    document.getElementById(target).style.display = 'block';
    document.getElementById(target).style.visibility = 'visible';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=p3&cat='+catid+'&format='+formatid);
	req.send(null);

  js_pform = formatid;
  js_pcat = catid;

  if (hist == 1) {hist = 0; return;}
  historyAdd('cat');
  dopositions(document.getElementById('infopane'));
}

function doDBVer(caller, product, formatid)
{
    resetPanels('p3');
    if (caller)
      caller.className = 'itemselected';
    setStepHead('Step 4: Choose the database version.');
    var target = 'p6';
    document.getElementById(target).style.display = 'block';
    document.getElementById(target).style.visibility = 'visible';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=dbver&product='+product+'&format='+formatid);
	req.send(null);

  js_pform = formatid;
  js_prod = product;

  if (hist == 1) {hist = 0; return;}
  historyAdd('dbv');
  dopositions(document.getElementById('infopane'));
}

function doVAD(caller, product, formatid)
{
    resetPanels('p3');
    if (caller)
      caller.className = 'itemselected';
    setStepHead('Step 4: Choose VAD Package Version.');

  js_pform = formatid;
  js_prod = product;
  
  doSomething(product, 'virtuoso', 'virtuoso', 'nodb');

}


function doClient(caller, product, formatid, dbms_name)
{
    resetPanels('p6');
    if (caller)
      caller.className = 'itemselected';
    setStepHead('Step 5: Choose the operating system.');
    var target = 'p5';
    document.getElementById(target).style.display = 'block';
    document.getElementById(target).style.visibility = 'visible';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=os&product='+product+'&format='+formatid+'&db='+dbms_name);
	req.send(null);

  js_pform = formatid;
  js_db = dbms_name;
  js_prod = product;
  

  if (hist == 1) {hist = 0; return;}
  historyAdd('cli');
  dopositions(document.getElementById('infopane'));

  document.download.os.value = '';
  document.download.os2.value = '';
  js_os = '';
  js_os2 = '';

}

function doServer(product, oni, ofn)
{
	document.getElementById('p4').innerHTML = '';
    var target = 'p4';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=server&product='+product+'&os='+oni+'&ofn='+ofn);
	req.send(null);
    setStepHead('Step 5: Choose the operating system for the server.');

  js_ofn = ofn;
  js_oni = oni;
  js_prod = product;

  if (hist == 1) {hist = 0; return;}
  historyAdd('srv');
}

function doClientVer(product, ofn, db)
{
    var target = 'clientver';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=clientver&product='+product+'&osname='+ofn+'&db='+db);
	req.send(null);

  js_db = db;
  js_ofn = ofn;
  js_prod = product;

  if (hist == 1) {hist = 0; return;}
  historyAdd('cliv');
}

function doClientVer2(product, ofn, db)
{
    var target = 'clientver2';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=clientver&product='+product+'&osname='+ofn+'&db='+db+'&eltid=_2');
	req.send(null);

  js_db = db;
  js_ofn = ofn;
  js_prod = product;

  if (hist == 1) {hist = 0; return;}
  //historyAdd('cliv');
}

function doServerVer(product, ofn, db)
{
    var target = 'serverver';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=serverver&product='+product+'&osname='+ofn+'&db='+db);
	req.send(null);

  js_db = db;
  js_ofn = ofn;
  js_prod = product;

  if (hist == 1) {hist = 0; return;}
  historyAdd('svrv');
}
function CloseDiv(id)
{
  document.getElementById(id).style.display = 'none';
  if (id=='final')
  {
    document.getElementById('p5').style.display = 'block';
    document.getElementById('p5').style.visibility = 'visible';
  }
}
function doContinue(p, mode, db)
{
  var os = document.download.xos;
  var os2 = document.download.xos2;
  
  if (mode == 'single' && os == null)
  {
    alert('You must select an Operating System Version before you can proceed...');
    return;
  }
  else 
  if (mode == 'double' && (os == null || os2 == null))
    {
      alert('You must select an Operating System Version for both Client and Server components before you can proceed...');
      return;
    } 
    else 
    if (mode == 'virtclientonly' &&  os == null)
      {
        alert('You must select an Operating System Version before you can proceed...');
        return;
      }
    
	if (os && os.selectedIndex >= 0) {
      var osval = os.options[os.selectedIndex].value;
    }

	if (os2 && os2.selectedIndex >= 0) {
  	  var osval2 = os2.options[os2.selectedIndex].value;
    }
  if ((mode == 'single' || mode == 'virtclientonly') && osval2 == undefined)
  {
    osval2 = osval;
  }
  doSomething(p, osval2, osval, db);
}

function doMatrixContinue(p, mode)
{
  var os = document.download.m_componentos.value;
  var os2 = document.download.xos_2;
  var product = document.download.m_product.value;
  var family = document.download.m_family.value;
  var cat = document.download.m_cat.value;
  var format = document.download.m_format.value;
  
  if (mode == 'double' && os2 == null)
    {
      alert('You must select an Operating System Version for the Client component before you can proceed...');
      return;
    }  
    
	if (os2 && os2.selectedIndex >= 0) {
  	  os2 = os2.options[os2.selectedIndex].value;
    }
  if (mode == 'single')
  {
    os2 = os;
  }

  document.download.pfam.value = family;
  document.download.pform.value = format;
  document.download.pcat.value =  cat;
  document.download.prod.value = product;
  document.download.os.value = os;
  document.download.os2.value = os2;
  document.download.submit();
}

function doSomething(p, o, oc, db)
{
  document.getElementById('p5').style.visibility = 'hidden';
  document.getElementById('p5').style.display = 'none';
    var target = 'final';
  document.getElementById(target).innerHTML = '<div>'+p+'</div><div>'+o+'</div>';
  document.getElementById(target).style.visibility = 'visible';
  document.getElementById(target).style.display = 'block';
	var req = new XMLHttpRequest(); 
	document.getElementById(target).innerHTML = waitingMsg();
	process_req(req, target);
	req.open('GET', 'data.vsp?call=finalstage&product='+p+'&os2='+o+'&os='+oc+'&db='+db);
	req.send(null);
    setStepHead('Step 6: Choose Product Release &amp; Proceed to download...');

  js_prod = p;
  js_db = db;
  js_os = o;
  js_os2 = oc;

  if (hist == 1) {hist = 0; return;}
  historyAdd('release');
}

function sizeMatrix()
{
  var bigbox = document.getElementById("mustbebig");
  var matrix = document.getElementById("ProductsTree");
  
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (myWidth < 820) {
    myWidth = 820;
  }
  bigbox.style.width = (myWidth - 50) + 'px';
  matrix.style.width = (myWidth - 250) + 'px';
  //bigbox.style.width = '1000px';
  //matrix.style.width = '1000px';
}
  
function hover(x)
{
  x.style.backgroundColor = "#ddf";
}

function unhover(x)
{
  x.style.backgroundColor = "inherit";
}
  

