
//http://mhc.daytondcs.com:8080/cgi-bin/ddiD46?task=getDrugList&mshv=yes&ptDrugList=%220-A^Soma%22,%220-B^carbamazepine%22,%220-C^MyMed%22


function advancedFind(newText,id)
{
	makeVisible('drugLists');
	addToList(newText,id);
	var drf = document.getElementById('initLetBox');
	drf.value = "";
	drf.focus();
	var toBox = document.getElementById('chosenList');
	toBox.selectedIndex = -1;
}

var altAr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
var altCnt = 0;

function addToList(newText,id)
{
	var attrValue = 'normal';
	var idAr = id.split("-");
	if(idAr.length > 1)
	{
		if(idAr[1] == '0')    // A HealthVault drug
		{
			id = altAr[altCnt];
			altCnt++;
		}
		else{
		id = idAr[1];
		}		
	}
	var toBox = document.getElementById('chosenList');
	var n = toBox.length;
	if(n > 1)
		document.getElementById('shortWarn').className = 'hideIt';
	if(toBox.options[0].value == 'first option'){
		n=0;
	}
	else
	{
	
		if(id == 0)  // Needed by HealthVault which has no id's
		{
			for (i=0; i<n;i++)
			{
			//	var longVal = toBox.options[i].text;
				if( toBox.options[i].text == newText)
				{
					alert('That is the same as ' + toBox.options[i].text + '.');
					return;
				}
			}
		}
		else  // This can catch different names with same genericid 
		{
			for (i=0; i<n;i++)
			{
				var longVal = toBox.options[i].value;
				if( longVal.substr(2)== id)
				{
					alert('That is the same as ' + toBox.options[i].text + '.');
					return;
				}
			}
		}
	}
	var val;
	
	if(windowVis == 'genes'){
		val = '0-' + id;
		attrValue = 'gene';
	}
	else{
		val = '0-' + id;
//		attrValue = 'normal';
	}
	toBox.options[n] = new Option(newText,val);
	toBox.options[n].setAttribute('class', attrValue);

	// ???? problem with IE????
	var drf = document.getElementById('initLetBox');
	drf.value = "";
	try{
		drf.focus();  // Fails on IE if not visible
	}
	catch(e){;}
	toBox.selectedIndex = -1;
	// MIKE - Add new 'stoplight box'
	addDrug(id, newText);
//	alert(idAr[0]);
	switch(idAr[0])
	{
		case '1':
			setDrugToStart(id);
			break;
		case '2':
			setDrugToStop(id);
			break;	
	}
}


// Now takes two different args
// drugId is the id of the drug being passed in
// type is either 'one' for deleting a single drug, or 'all' to delete the whole lot
function delSelection(drugId, type, parser)
{
	if(!parser && checkGuestUser())  // Allows deleting to put in a new example case.
		return;
//	alert(drugId);
	if(dnaTest)
	{
//	alert('in');
		if((drugId >4072 && drugId < 4088) || (drugId >4235 && drugId < 4040) || drugId == 5308 || drugId == 5307 ||drugId == 4094)
		{
			alert('Sorry, in DNA test results mode GeneMedRx cannot delete the genes from this record. However, you can delete individual drugs.');
			return;
		}
	}	
  	box = "drug";
	var boxname = 'chosenList';
	var cdH = document.getElementById(boxname);
	var target = document.getElementById('addDrugRowsHere');

	if(type == 'one')  // Delete selected item
	{
		  // Remove 'stoplight box' div
		var oldDiv = document.getElementById('drug' + drugId);
		target.removeChild(oldDiv);
	
		// Remove from secret html list
		for(i=0; i<cdH.options.length; i++)
		{
		  var valueSplit = cdH.options[i].value.split('-');
		  if (valueSplit[1] == drugId)
		  {
			// Wipe it out!
				cdH.options[i] = null;
				boxname.selectedIndex = -1;
				if(!i && !cdH.options.length)
				{
					cdH.options[0] = new Option('','first option');
				}
		  }
		}
	}
	else  // Delete all!
	{
	  // Stoplight divs
	  target.innerHTML = '';
	  // Secret html list
		cdH.options.length = 0;
		cdH.options[0] = new Option('','first option');
	}
	try{
	document.getElementById('initLetBox').focus();
	}
	catch(ex){
		var x;
	}
	theDrugSet = null;
}

/**************************************************
*** Page 1, Step 2 - Drug Continue, Start, Stop actions ***
**************************************************/
function addDrug(drugId, name)
{
  // First, check if we already have this foo'
  if (document.getElementById('drug' + drugId))
  {
    return false;
  }

  var addRow = '<table border="0" cellpadding="0" cellspacing="0" width="260"><tr id="drugLineSrc"><td class="box1" valign="middle"><a href="javascript:delSelection(\'' + drugId + '\',\'one\');"><img src="' + serverAddress + 'images/icon_delete.gif" width="11" height="11" alt="Delete" title="Delete" /></a><a href="javascript:setDrugActivate(\'' + drugId + '\', \'' + name + '\');"><img src="' + serverAddress + 'images/icon_check_on.gif" width="13" height="12" alt="Enable/Disable this" title="Enable/Disable this" hspace="2" id="active' + drugId + '" /></a></td><td class="box2" valign="top"><div><a href="javascript:setDrugActivate(\'' + drugId + '\', \'' + name + '\');">' + name + '</a></div></td><td class="box3" valign="middle" width="83"><a href="javascript:setDrugToContinue(\'' + drugId + '\');"><img src="' + serverAddress + 'images/c_continue.gif" width="13" height="14" alt="Click here if you currently use this" title="Click here if you currently use this" id="cont' + drugId + '" /></a><a href="javascript:setDrugToStart(\'' + drugId + '\');"><img src="' + serverAddress + 'images/c_off.gif" width="13" height="14" alt="Click here if you plan to start this" title="Click here if you plan to start this" hspace="4" id="start' + drugId + '" /></a><a href="javascript:setDrugToStop(\'' + drugId + '\')"><img src="' + serverAddress + 'images/c_off.gif" width="13" height="14" alt="Click here if you plan to stop this" title="Click here if you plan to stop this" id="stop' + drugId + '" /></a></td></tr></table>';

  var target = document.getElementById('addDrugRowsHere');
  var divIdName = "drug" + drugId;
  var newdiv = document.createElement('div');
  newdiv.setAttribute("id", divIdName);
  newdiv.innerHTML = addRow;
  target.appendChild(newdiv);
  return true;
}

// Set drug to continue instead of started or stopped
function setDrugToContinue(drugId)
{
  dimDrugLights(drugId);
  document.getElementById('cont'+drugId).src = serverAddress + 'images/c_continue.gif';
  setRealDrugList(drugId, 0);
  theDrugSet = null;
}

// Set drug to started
function setDrugToStart(drugId)
{
  dimDrugLights(drugId);
  document.getElementById('start'+drugId).src = serverAddress + 'images/c_start.gif';
  setRealDrugList(drugId, 1);
  theDrugSet = null;
}

// Set drug to stopped
function setDrugToStop(drugId)
{
  dimDrugLights(drugId);
  document.getElementById('stop'+drugId).src = serverAddress + 'images/c_stop.gif';
  setRealDrugList(drugId, 2);
  theDrugSet = null;
}

// Turn a drug on/off for the report
function setDrugActivate(drugId, drugName)
{
  var realList = document.getElementById('chosenList');
  var active = false;
  theDrugSet = null;
  for(i=0;i<realList.options.length;i++)
  {
    var valueFull  = realList.options[i].value;
    var valueSplit = valueFull.split('-');
    if (valueSplit[1] == drugId)
    {
      // Deactivate this - that means remove it from the hidden list and change the 'active' icon by the html representation
      active = true;
			realList.options[i] = null;
			if(!i && !realList.options.length)
			{
				realList.options[0] = new Option('','first option');
			}
			// Set icon
			document.getElementById('active' + drugId).src = serverAddress + 'images/icon_check_off.gif';
    }
  }

  if (!active)
  {
    addToList(drugName, drugId);
		// Set icon
  	document.getElementById('active' + drugId).src = serverAddress + 'images/icon_check_on.gif';
  }
}

// Turn off all 'stoplight' rollovers
function dimDrugLights(drugId)
{
  document.getElementById('cont'+drugId).src = serverAddress + 'images/c_off.gif';
  document.getElementById('start'+drugId).src = serverAddress + 'images/c_off.gif';
  document.getElementById('stop'+drugId).src = serverAddress+ 'images/c_off.gif';
}

// Update the *real* html listbox that handles what's started, stopped, etc
// statusCode is 0 (normal), 1 (start), or 2 (stopped)
function setRealDrugList(drugId, statusCode)
{
  var realList = document.getElementById('chosenList');

  for(i=0;i<realList.options.length;i++)
  {
    var valueFull  = realList.options[i].value;
    var valueSplit = valueFull.split('-');
    if (valueSplit[1] == drugId)
    {
      // Replace prefix of this drug's listbox value to the statusCode
      // Example - from 0-12345 to 1-12345
      var newValue   = statusCode + "-" + valueSplit[1];
      realList.options[i].value = newValue;
      // alert("Debug: Changing value of " + realList.options[i].text + " from " + valueFull + " to " + realList.options[i].value)
    }
  }
}

/**************************************************
*** Page 2 - Results expansion and notes        ***
**************************************************/

function expand(what)
{
  var contract = document.getElementById(what + '-contract');
  var expand2   = document.getElementById(what + '-expand');

  contract.style.display = 'none';
  expand2.style.display = 'block';
}

function contract(what)
{
  var contract2 = document.getElementById(what + '-contract');
  var expand   = document.getElementById(what + '-expand');
  contract2.style.display = 'block';
  expand.style.display = 'none';
}

function swapTabInfo()
{
//	if(parent.document.title == 'gmdrxMini'){
//		return;
//	}
	var tabName = 'tabStartStopOff';
	if(document.getElementById('start_stopIFrame').width > 375){
		tabName = 'tabStartStopOffWide';
	}
  document.getElementById('tabInfo').className = 'tabInfoOn';
  document.getElementById('tabStartStop').className = tabName;
  document.getElementById('tabInfoContent').style.display = 'block';
  document.getElementById('tabStartStopContent').style.display = 'none';
  document.getElementById('tabFade').style.visibility = 'visible';
}

function swapTabStartStop()
{
	var tabName = 'tabStartStopOn';
	if(document.getElementById('start_stopIFrame').width > 375){
		tabName = 'tabStartStopOnWide';
	}	
  document.getElementById('tabInfo').className = 'tabInfoOff';
  document.getElementById('tabStartStop').className = tabName;
  document.getElementById('tabInfoContent').style.display = 'none';
  document.getElementById('tabStartStopContent').style.display = 'block';
  document.getElementById('tabFade').style.visibility = 'hidden';
}