
function openWindow(url, name) {
 popupWin = window.open(url, name, "scrollbars,menubar,toolbar,location,resizable,locationbars,width=640,height=440");
} 

function Retrieve() {
	//var url_param = '?inputString='+document.frmMain.inputString.value;	
	
	document.location.href = "index.php?action=Retrieve";
	//document.location.href = "CMMRetrieve.asp";
}

function notEmptyEmptyDruglist() {
	
	if (document.frmMain.Ilist.length >= 1) {
		//return true;
		document.frmSave.submit();
	} else {
		alert("You can't save an empty drug list.");
		//return false;
	}
}

function Add(){
	
	
	var IDrug_Selected = document.frmMain.druglist.selectedIndex;
	var thisDrugID = '';
	
	if (IDrug_Selected >= 1){
		var stext = document.frmMain.druglist.options[document.frmMain.druglist.selectedIndex].text;
		var svalue = document.frmMain.druglist.options[document.frmMain.druglist.selectedIndex].value;
		
		var IDloc = svalue.indexOf(';drugid=');
		var strIDonly = String(svalue);
		var thisDrugID = '';
		
		var i = parseInt(IDloc) + 7;
		while(i<svalue.length - 1) {
			thisDrugID = thisDrugID + strIDonly.charAt(i+1);
			i++;
		}
		/* Add this item to the right box if not already added
			 do this by evaluating the pre-add list from frmMain.fullidlist
			 eg. it doesn't include the one just selected
		*/
		var inputVal = String(document.frmMain.fullidlist.value);
		if (inputVal.indexOf(thisDrugID) > 0) {
		} else {
			var j = eval(document.frmMain.Ilist.length);
			document.frmMain.Ilist.options[j] = new Option(stext, svalue);
			
			i = 8; // starts at 0, add 8 for drugname=
			while(i<IDloc - 1) {
				Nameonly = Nameonly + strIDonly.charAt(i+1);
				document.frmMain.mainidlist.value = document.frmMain.mainidlist.value + strIDonly.charAt(i+1);
				document.frmSearch.mainidlist.value = document.frmSearch.mainidlist.value + strIDonly.charAt(i+1);
				document.frmSearch.fullidlist.value = document.frmSearch.fullidlist.value + strIDonly.charAt(i+1);
				document.frmInter.mainidlist.value = document.frmInter.interidlist.value + strIDonly.charAt(i+1);
				document.frmInter.fullidlist.value = document.frmInter.fullidlist.value + strIDonly.charAt(i+1);
				document.frmSave.fullidlist.value = document.frmSave.fullidlist.value + strIDonly.charAt(i+1);
				i++;
			}
			
			var firstCharOfName = strIDonly.charAt(9);
			
			var strNameonly = String(svalue);
			var strBoth = String(svalue);
			
			// add the ; to separate the name from the id (changed to *)
			document.frmMain.mainidlist.value = document.frmMain.mainidlist.value + '*';
			document.frmSearch.mainidlist.value = document.frmSearch.mainidlist.value + '*';
			document.frmSearch.fullidlist.value = document.frmSearch.fullidlist.value + '*';
			document.frmInter.mainidlist.value = document.frmInter.mainidlist.value + '*';
			document.frmInter.fullidlist.value = document.frmInter.fullidlist.value + '*';
			document.frmSave.fullidlist.value = document.frmSave.fullidlist.value + '*';
			document.frmInter.interidlist.value = document.frmInter.interidlist.value + '&drugid=';
			
			//alert("::: "+document.frmInter.interidlist.value);
			document.frmSearch.interidlist.value = document.frmInter.interidlist.value;
			
			i = parseInt(IDloc) + 7;
			while(i<svalue.length - 1) {
				IDonly = IDonly + strIDonly.charAt(i+1);
				document.frmMain.mainidlist.value = document.frmMain.mainidlist.value + strIDonly.charAt(i+1);
				document.frmSearch.mainidlist.value = document.frmSearch.mainidlist.value + strIDonly.charAt(i+1);
				document.frmSearch.fullidlist.value = document.frmSearch.fullidlist.value + strIDonly.charAt(i+1);
				document.frmInter.mainidlist.value = document.frmInter.mainidlist.value + strIDonly.charAt(i+1);
				document.frmInter.interidlist.value = document.frmInter.interidlist.value + strIDonly.charAt(i+1);
				document.frmInter.fullidlist.value = document.frmInter.fullidlist.value + strIDonly.charAt(i+1);
				document.frmSave.fullidlist.value = document.frmSave.fullidlist.value + strIDonly.charAt(i+1);
				i++;
			}
			// add | separator for the next item
			IDonly = IDonly + '*' + firstCharOfName + '|';
			document.frmSearch.fullidlist.value = document.frmSearch.fullidlist.value + '|';
			document.frmInter.fullidlist.value = document.frmInter.fullidlist.value + '|';
			document.frmSave.fullidlist.value = document.frmSave.fullidlist.value + '|';
			
			document.frmSave.mainidlist.value = IDonly;
			document.frmInter.mainidlist.value = IDonly;
			document.frmSearch.mainidlist.value = IDonly;
		
			// Now add the item to frmMain 
			document.frmMain.fullidlist.value = document.frmSearch.fullidlist.value;
			
//			alert(document.frmSearch.interidlist.value);
		}
		
	} else {
		alert("Select an item from the drug list to add!");
	}
}

function Logout() {
	document.location.href = "index.php?action=logout";
}

function Remove(){
	
	var IDrug_Selected = document.frmMain.Ilist.selectedIndex;
	var leftSelect = document.frmMain.Ilist;
	
	
	if (IDrug_Selected >= 1){
		
		var stext = document.frmMain.Ilist.options[document.frmMain.Ilist.selectedIndex].text;
		var svalue = document.frmMain.Ilist.options[document.frmMain.Ilist.selectedIndex].value;
		var inputVal = String(document.frmMain.mainidlist.value);
		var fromIndex = inputVal.indexOf(svalue);
		
		var toIndex = fromIndex + svalue.length;
		var inpLength = inputVal.length;
		var splitVal = inputVal.substr(0,(fromIndex-1)) + inputVal.substr(toIndex, inpLength);
		
		var i=0,j=0,k=0;
		while(i<leftSelect.options.length) {
			if(leftSelect.options[i].selected) {
				//rightSelect.options[rightSelect.options.length] = new Option(leftSelect.options[i].text,leftSelect.options[i].value,false,false);
				k++;
			}
			else if(i-k<leftSelect.options.length) {
				leftSelect.options[i-k].text = leftSelect.options[i].text;
				leftSelect.options[i-k].value = leftSelect.options[i].value;
			}
			i++;
		}

		leftSelect.options.length -= k;
		leftSelect.selectedIndex = -1;
		leftSelect.blur();
					
		document.frmMain.mainidlist.value = splitVal;
		
		// Remove the selected item from the hidden field
		var IDloc = svalue.indexOf(';drugid=');
		var strIDonly = '';
		var strNameonly = '';
		var firstCharOfName = svalue.charAt(9);
		
		i = parseInt(IDloc) + 7;
		while(i<svalue.length - 1) {
			strIDonly = strIDonly + svalue.charAt(i+1);
			i++;
		}
		
		var endat = document.frmSave.mainidlist.value.indexOf(strIDonly + '*' + firstCharOfName + '|');
		var startat2 = endat + strIDonly.length + 3; // the 3 incl. the * and |
		var endat2 = document.frmSave.mainidlist.value.length;
		
		document.frmSave.mainidlist.value = document.frmSave.mainidlist.value.substring(0, endat) + document.frmSave.mainidlist.value.substring(startat2, endat2);
		document.frmSearch.mainidlist.value = document.frmSearch.mainidlist.value.substring(0, endat) + document.frmSearch.mainidlist.value.substring(startat2, endat2);
		document.frmInter.mainidlist.value = document.frmInter.mainidlist.value.substring(0, endat) + document.frmInter.mainidlist.value.substring(startat2, endat2);
		
		IDonly = document.frmSave.mainidlist.value;

		
		
		
		
		/* Now remove it from the full id list, with the full names */
		strIDonly = '';
		var IDloc = svalue.indexOf(';drugid=');
		i = parseInt(IDloc) + 7;
		while(i<svalue.length - 1) {
			strIDonly = strIDonly + svalue.charAt(i+1);
			i++;
		}
		
		var strNameonly = '';
		var Nameloc = svalue.indexOf('drugname=');
		i = parseInt(Nameloc) + 8;
		while(i<IDloc - 1) {
			strNameonly  = strNameonly  + svalue.charAt(i+1);
			i++;
		}
		
		
		//alert(strNameonly + '*' + strIDonly + '|');
		
		endat = document.frmSave.fullidlist.value.indexOf(strNameonly + '*' + strIDonly + '|');
		startat2 = endat + (strNameonly + '*' + strIDonly + '|').length; //+ 3; // the 3 incl. the * and |
		endat2 = document.frmSave.fullidlist.value.length;
		
		document.frmSave.fullidlist.value = document.frmSave.fullidlist.value.substring(0, endat) + document.frmSave.fullidlist.value.substring(startat2, endat2);
		document.frmSearch.fullidlist.value = document.frmSearch.fullidlist.value.substring(0, endat) + document.frmSearch.fullidlist.value.substring(startat2, endat2);
		document.frmInter.fullidlist.value = document.frmInter.fullidlist.value.substring(0, endat) + document.frmInter.fullidlist.value.substring(startat2, endat2);
		
		//alert(''+document.frmInter.fullidlist.value.substring(0, endat) + document.frmInter.fullidlist.value.substring(startat2, endat2));
		
		
		/* Now remove it from the interactions list (with &drugid=) */
		strIDonly = '';
		var IDloc = svalue.indexOf(';drugid=');
		i = parseInt(IDloc) + 7;
		while(i<svalue.length - 1) {
			strIDonly = strIDonly + svalue.charAt(i+1);
			i++;
		}
		
		endat = document.frmInter.interidlist.value.indexOf('&drugid=' + strIDonly);
		startat2 = endat + ('&drugid=' + strIDonly).length;
		endat2 = document.frmInter.interidlist.value.length;
		
		document.frmInter.interidlist.value = document.frmInter.interidlist.value.substring(0, endat) + document.frmInter.interidlist.value.substring(startat2, endat2);
		document.frmSearch.interidlist.value = document.frmInter.interidlist.value;
	} 
	
	else {
		alert("Select an item from the Medications list to remove!");		
	}
	
}

function changeInterMsg() {
	document.frmInter.Inter2.value='Retrieving Interactions...'; 
	document.frmInter.Inter2.disabled=true;
	document.frmInter.submit();
}
