//
//
//
function addSxTip() {}
function UnTip() {}

function addSxEvent(sxNode){
	if(getClassName(sxNode) == "clickable") {
		var sxJSName = sxNode.getAttribute("_name"), sxJSId = sxNode.getAttribute("_id");

        if (sxAdd != undefined)
        {
		    sxAdd.addSx("",sxJSName,sxJSId,sxJSName,sxNode);
        }
        else if (parent.sxAdd != undefined)
        {
		    parent.sxAdd.addSx("",sxJSName,sxJSId,sxJSName,sxNode);
        }
        else
        {
		    sxAdd.addSx("",sxJSName,sxJSId,sxJSName,sxNode);
        }
		UnTip();
	}
}

function getClassName(node) {
    if(node && node.className) return node.className;
//	else if(node.class) return node.class;
	else return "";
}

// ------------------------------------------------------------------

function DrugObj(synId, genericName, brandName){
	this.synId = synId;
	this.generic = genericName;
	this.brand = brandName;
}

var drugList = new Array();
var addedDrugs =new Object();
var addedDrugsStr = "";
var drugInput = new Object();
var drugPickAction = "DrugPickListAction.do";
var inprogress =0; //indicates wheather drug is loading (1) or not (0)
var hightedRowIndex = 0;
var sxSearchInputObj;
var sxChosenInputObj;


function loadPickListByName(drugNameP,submitForm){
	if (inprogress ==1) //already loading, wait
		return;
	inprogress = 1;

	if (submitForm!=undefined){
	    submitForm.target = "hiddenFrame";
	    submitForm.command.value = "searchDrug";
	    submitForm.submit();
	}else{
		document.drugPickForm.drugName.value= drugNameP;
		document.drugPickForm.submit();
  }

}

function loadPickListById(listIdP){
	if (inprogress ==1)
		return;
	inprogress = 1;
        document.drugPickForm.listId.value= listIdP;
	document.drugPickForm.submit();


}


function setDrugListByName(newDrugList){
	inprogress = 0;	 //finished loading
	drugList = newDrugList;
	if (drugList.length == 0)
		drugList[0] = new DrugObj("0","N/A","N/A");
	showPickList(drugInput,addedDrugsStr,"loading");
}

function setDrugListById(newDrugList){
	inprogress = 0; //finished loading
	drugList = newDrugList;

	if (drugList.length == 0)
		drugList[0] = new DrugObj("0","N/A","N/A");
	showPickList(drugInput,addedDrugsStr,"loading",0);
}

function showPickList(_drugInput,drugIdObj,something, listIdP, submitForm){

	//alert(drugInput.value + " progress"+inprogress+" something "+something +" listIdP"+listIdP+" drugList length"+ drugList.length);
	drugInput = _drugInput;

	addedDrugs = document.all[drugIdObj];
	addedDrugsStr = drugIdObj;

	if (listIdP != undefined && listIdP != 0){
		drugInput.value ="";
		if (document.drugPickForm != 'undefined')
        		document.drugPickForm.drugName.value = "";
		loadPickListById(listIdP);
		return;

	}else{

		if (something != 'loading' && drugInput.value.length <= 2){
			drugList = new Array();
			hidePickList();
			return;
		}
		if (drugList.length ==0){

			loadPickListByName(drugInput.value, submitForm);
			return;
		}
	}

	var parent = drugInput.offsetParent;
	var top = 0;
	var left =0;
	if (parent){
		top = parent.offsetTop;
		left = parent.offsetLeft;
	}
	for(var i=0; i<10; i++){
		parent = parent.offsetParent;
		if (parent){
			top += parent.offsetTop;
			left += parent.offsetLeft;
		}else{
			break;
		}
	}

	document.all.matchedDiv.style.width= document.all.selectedDrugs.width;
	setDivContent(drugInput.value.toLowerCase());

	document.all.matchedDiv.style.pixelLeft = drugInput.offsetLeft+ left;
	document.all.matchedDiv.style.pixelTop = drugInput.offsetTop + drugInput.clientHeight+top;
	document.all.matchedDiv.style.width = document.all.matchedDiv.offsetWidth + 15;
	document.all.matchedDiv.style.visibility= "visible";
	document.all.matchedDiv.style.display= "block";
}

function highlite(tablecell){
	tablecell.className = "highlighted";

}

function unhighlite(tablecell){
	tablecell.className = "unhighlighted";
}

function highliterow(rownum){
	//if (rownum!=hightedRowIndex)
	//	unhighliterow(hightedRowIndex);
	if (document.getElementById('drug_row_left'+rownum)){
		document.getElementById('drug_row_left'+rownum).className = "inputLeftRound_h";
		document.getElementById('drug_row_right'+rownum).className = "inputRightRound_h";
		document.getElementById('drug_row_middle'+rownum).className = "highlighted";
		hightedRowIndex = rownum;
	}else
	hightedRowIndex = -1;
}

function unhighliterow(rownum){
	if (document.getElementById('drug_row_left'+rownum)){
		document.getElementById('drug_row_left'+rownum).className = "inputLeftRound";
		document.getElementById('drug_row_right'+rownum).className = "inputRightRound";
		document.getElementById('drug_row_middle'+rownum).className = "unhighlighted";
		//hightedRowIndex = rownum;
	}else
	hightedRowIndex = -1;
}

function highliterow2(rownum){

	if (document.getElementById('sx_row_tl'+rownum)){
		//alert(rownum +":"+hightedRowIndex);
		//if (rownum!=hightedRowIndex)
		//	unhighliterow(hightedRowIndex);
	document.getElementById('sx_row_tl'+rownum).className = "gray_tl_s";
	document.getElementById('sx_row_tr'+rownum).className = "gray_tr_s";
	document.getElementById('sx_row_bl'+rownum).className = "gray_bl_s";
	document.getElementById('sx_row_br'+rownum).className = "gray_br_s";
	document.getElementById('sx_row_top'+rownum).className = "bggrey";
	document.getElementById('sx_row_middle'+rownum).className = "bggrey";
	document.getElementById('sx_row_bottom'+rownum).className = "bggrey";
	document.getElementById('sx_row_middle_td'+rownum).className = "bggrey bodywhite";
	//alert(rownum);
	hightedRowIndex = rownum;
}else
	hightedRowIndex = -1;

}

function unhighliterow2(rownum){
	if (document.getElementById('sx_row_tl'+rownum)){
	document.getElementById('sx_row_tl'+rownum).className = "blue_tl_s";
	document.getElementById('sx_row_tr'+rownum).className = "blue_tr_s";
	document.getElementById('sx_row_bl'+rownum).className = "blue_bl_s";
	document.getElementById('sx_row_br'+rownum).className = "blue_br_s";
	document.getElementById('sx_row_top'+rownum).className = "bgltblue";
	document.getElementById('sx_row_middle'+rownum).className = "bgltblue";
	document.getElementById('sx_row_bottom'+rownum).className = "bgltblue";
	document.getElementById('sx_row_middle_td'+rownum).className = "bgltblue body";
	hightedRowIndex = rownum;
}else
	hightedRowIndex = -1;
}


function highlight(inputObj){
    inputObj.select();
}
//populates the drug dropdown with values
function setDivContent(drug){
	content = "";
	var mod_row=drugList.length;

	for (var i=0;i<drugList.length;i++){
		var drugName = drugList[i].brand.toLowerCase();
		var index = drugName.indexOf(drug);
		mod_row=i%3;
		if (mod_row == 0)
			content +="<tr>"
		if (index == 0 || (index >0 && (drugName.charAt(index-1)<'a' || drugName.charAt(index-1)>'z')))
			content +="<TD onmouseover='highlite(this)' onmouseout='unhighlite(this)' onClick='addDrug("+i+")'><span class='body' onmouseover='highlite(this)' onmouseout='unhighlite(this)'>"+drugList[i].brand+"</span></TD>\n";

		if (mod_row == 2)
			content +="</tr>"
	}
	matchedDiv.innerHTML = "<TABLE class=div border=1 bordercolor=#369 cellpadding=2 id=matchedDrugs>\n" +content + "</TABLE>";
}

function hidePickList(){
    drugList = new Array();
		document.all.matchedDiv.style.visibility="hidden";
		document.all.matchedDiv.style.display="none";
}

//adds drug to selected list

function addDrug(index){

	addedDrugs.value +=","+drugList[index].synId+ "="+ drugList[index].drugId +":"+drugList[index].brand;
	displayDrugs(drugList[index].synId,drugList[index].brand,drugList[index].drugId);

	showQs();

}



function showDivFunc(divId){
    var divObj = document.getElementById(divId);
    divObj.className="showDiv";
}



function showGeneralInfo(drugName){

    showDiv('drugGenInfo');
}



function reloadDrugs(drugIdObj){
	addedDrugs = document.getElementById(drugIdObj);

	addedDrugsStr = drugIdObj;
	if (addedDrugs!=undefined){
		drugStr = addedDrugs.value;
	}else{
		return;
	}
	drugStr = addedDrugs.value;
	if (drugStr.length == 0) return;

	drugStr = drugStr.replace("{","");
	drugStr = drugStr.replace("}","");
	addedDrugs.value = drugStr;
    var drugs = drugStr.split(",");
    var rowcount = 0;

    for(var i=0; i< drugs.length; i++)
    {
    	var drug = drugs[i];
			 //alert(drug);
    	var synId = drug.substring(0, drug.indexOf("="));
     	var name = drug.substring(drug.indexOf(":")+1);
     	var pdrugId = drug.substring(drug.indexOf("=")+1,drug.indexOf(":"));
     	if (drug.length >0){
	   		displayDrugs(synId,name,pdrugId);
	   		rowcount++;
	   		//alert("more"+i);
	   	}
  	}
}

function removeDrug(synonymId){
	var drugIds = addedDrugs.value;
	var index = drugIds.indexOf(","+synonymId+"=");
	var beginning = drugIds.substring(0,index);
	var ending = drugIds.substring(index+1);


	var indexEnd = ending.indexOf(",");
	if (indexEnd == -1)
		ending = "";
	else
		ending = ending.substring(indexEnd);

	//alert(addedDrugs.value);

	addedDrugs.value= beginning + ending ;//addedDrugs.value.substring(0,index) + addedDrugs.value.substring(index + synonymId.length+2);
	for(var i=0; i< document.getElementById('selectedDrugs').rows.length;i++){
		var synId = 'r'+synonymId;
		if (document.getElementById('selectedDrugs').rows[i].id == synId)
			document.getElementById('selectedDrugs').deleteRow(i);
	}

	showQs();
	saveChanges();
}

function saveChanges(){
	if (document.getElementById("save_changes")){
		show("save_changes");
	}
}

var xmlHttp;
var thisClickedObj;
var mouseLocX;
var mouseLocY;

function createXMLHttpRequestDrug() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }

}

function findDrugInfo(clickedObj, e, drugId){

	 createXMLHttpRequestDrug();
    var url = "dbkmdservice";

    thisClickedObj = clickedObj;
		if (e)
        getMouseXY(e);


    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = showDrugs;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    //alert(drugInputP + ":"+drugGroupIdP);

    //alert(searchByGeneric);
    params = "command=seeall&searchDrugId="+drugId + "&cache=searchDrugId";


    xmlHttp.send(params);

}


function findDrugs(e, drugInputP, drugGroupIdP, urlP, startsWithP, searchByAlphaP, lastindexP, directionP, drugListName, searchByGenericP) {

    var lastIndex = "0";
    var direction = "";
    var searchByAlpha = "";
    var startsWith = true;
    var searchByGeneric = "";
 		firstClick=true;
	  if (e)
        getMouseXY(e);

    if (window.event) // IE
		{
		    keynum = e.keyCode;
		} else if (e.which) // Netscape/Firefox/Opera
		{
		    keynum = e.which;
		}


    if (keynum ==38 || keynum==40) {
        handleUpDownEnter(e);
        return false;
    }else if (keynum == 13)
  	{

  		addCurrentDrug();
  		return false;

  	}

    if (lastindexP != undefined)
        lastIndex = lastindexP;

    if (searchByAlphaP != undefined)
        searchByAlpha = searchByAlphaP;

    if (directionP!=undefined)
        direction = directionP;

    if (startsWithP!= undefined){
        startsWith = startsWithP;
    }

    if (searchByGenericP!=undefined)
        searchByGeneric = searchByGenericP;

    if (drugGroupIdP != undefined && drugGroupIdP != "" )
    {
    	drugInputP = "";
    	var searchInput = document.getElementById("drugField");
    	if (searchInput)
        	searchInput.value="";
    } else if (drugInputP!=undefined && drugInputP.length < 2)
    {
        if (document.getElementById("emdDrugPickComp").innerHTML !="")
            document.getElementById("emdDrugPickComp").innerHTML !="";
        hide("div_drug_container");
        hide("emdDrugPickComp");
        return;
    }

    //alert(searchByAlpha + ":"+drugGroupIdP);


    createXMLHttpRequestDrug();
    var url = "../dbkmdservice";
    if (urlP!= undefined)
        url = urlP;

    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = showDrugs;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");

    if (drugGroupIdP  != undefined && drugGroupIdP != "" ){
        params = "listId="+drugGroupIdP+"&requestMethod=2&searchByAlpha="+searchByAlpha +"&lastIndex="+lastIndex+"&direction="+direction+"&startsWith="+startsWith+"&cache=listId";
    }else if (drugListName  != undefined && drugListName != "" ) {

        params = "requestMethod=2&drugListName="+drugListName+"&cache=drugListName";
    }else if (searchByGeneric  != undefined && searchByGeneric != "" ) {
    //alert(searchByGeneric);
        params = "requestMethod=2&command=searchByGeneric&drugName="+drugInputP+"&cache=drugName";
    }else{
        params = "drugName="+drugInputP+"&requestMethod=2&searchByAlpha="+searchByAlpha+"&lastIndex="+lastIndex+"&direction="+direction+"&startsWith="+startsWith+"&cache=drugName";
    }



    hightedRowIndex = 0;
    document.getElementById("emdDrugPickComp").innerHTML !="Searching..";
   //alert(url+"?"+params);
    xmlHttp.send(params);



}

function hideDrugList(){
    	if (document.getElementById("emdDrugPickComp")!=undefined)
	    document.getElementById("emdDrugPickComp").style.visibility = "hidden";

}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

function showDrugs() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {


var responseText = xmlHttp.responseText;

if (responseText.length ==0) return;

document.getElementById("emdDrugPickComp").innerHTML = responseText; //appendChild(responseText);


	var searchInput = document.getElementById("drugField");
	var parent;
  var top = 0;
	var left =0;
  var drugListDiv = document.getElementById("div_drug_container");



	  drugListDiv.style.width = 630+ "px";

	if (searchInput && thisClickedObj == undefined){

			var obj1TopX = getLeft(searchInput);
			var obj1TopY = getTop(searchInput);
			var obj1BottomX = getLeft(searchInput) - 20 ; //+searchInput.offsetWidth;
			 obj1BottomX = calculateWindow(obj1BottomX, searchInput);
			var obj1BottomY = getTop(searchInput)+searchInput.offsetHeight+ 6;
			drugListDiv.style.left = obj1BottomX + "px";
			drugListDiv.style.top = obj1BottomY+ "px";
		}else{

		drugListDiv.style.width = 400+ "px";
			var obj1TopX = getLeft(thisClickedObj);
			var obj1TopY = getTop(thisClickedObj);
			var obj1BottomX = getLeft(thisClickedObj) - 20 ; //+searchInput.offsetWidth;

			 //alert(obj1TopX + ": " + obj1BottomX);
			 show("div_drug_container");
			mousex = calculateWindow(obj1BottomX, drugListDiv);
			mousey = getTop(thisClickedObj)+thisClickedObj.offsetHeight+ 6;
	    drugListDiv.style.left = mousex +"px";
	    drugListDiv.style.top = mousey + "px";
  	}
  	//firstClick = true;


		show("emdDrugPickComp");
	  show("div_drug_container");
	  thisClickedObj = null;

}
}
}

function clearSxList(){
	document.getElementById("div_searchedSymptoms").innerHTML = "";
}
function addDrugToList(synId,drugId, brand){


	addedDrugs.value +=","+synId+ "="+ drugId +":"+ brand;
	displayDrugs(synId,brand,drugId);
	showQs();
	clearInput('drugField');
	saveChanges();
	hide('div_drug_container');
}

function clearInput(inputName){
	document.getElementById(inputName).value="";
}
function hidePickListNoFrame(){
  //document.getElementById("emdDrugPickComp").innerHTML = "";
  if (document.activeElement!= document.getElementById("emdDrugPickComp")){
  document.getElementById("emdDrugPickComp").style.visibility = "hidden";
  document.getElementById("emdDrugPickComp").style.display = "none";
}
}


function showDrugsByGeneric(thisObj,e){
    thisClickedObj = thisObj;
    findDrugs(e, thisObj.innerHTML, undefined, 'dbkmdservice',undefined, undefined, undefined, undefined, undefined, 'searchByGeneric');
}

function showDrugsByCondition(thisObj, conditionName,e){
    thisClickedObj = thisObj;
    findDrugs(e,undefined, undefined, 'dbkmdservice',undefined, undefined, undefined, undefined, conditionName);
}


function ObjectOverlap(obj1, obj2)
{
	var obj1TopX = getLeft(obj1);
	var obj1TopY = getTop(obj1);
	var obj1BottomX = getLeft(obj1)+obj1.offsetWidth;
	var obj1BottomY = getTop(obj1)+obj1.offsetHeight;

	//alert(obj1BottomX, obj1BottomY);

	var obj2TopX = getLeft(obj2);
	var obj2TopY = getTop(obj2);
	var obj2BottomX = getLeft(obj2)+obj2.offsetWidth;
	var obj2BottomY = getTop(obj2)+obj2.offsetHeight;

	var overlapOnX = (obj1TopX < obj2BottomX && obj2TopX < obj1BottomX);
	var overlapOnY = (obj1TopY < obj2BottomY && obj2TopY < obj1BottomY);

	return (overlapOnX && overlapOnY);
}
//Positioning functions
function getObjLeft(anObject)
{
	if (anObject)
		return(anObject.offsetParent ? (getObjLeft(anObject.offsetParent) + anObject.offsetLeft) : anObject.offsetLeft);
}

function getObjTop(anObject)
{
	if (anObject)
	return(anObject.offsetParent ? (getObjTop(anObject.offsetParent) + anObject.offsetTop) : anObject.offsetTop);
}


function getLeft(anObject)
{
	return(getObjLeft(anObject));
}

function getTop(anObject)
{
	return(getObjTop(anObject));
}


function quicksearchForSymptom(e, inputObj, divToShow, chosenInputObj, searchMode)
{
    var keynum
		if (window.event) // IE
		{
		    keynum = e.keyCode;
		} else if (e.which) // Netscape/Firefox/Opera
		{
		    keynum = e.which;
		}


    if (keynum ==38 || keynum==40) {
        handleUpDownEnter(e);
        return false;
    }else if (keynum == 13)
  	{
  		if (document.getElementById("sx_row_middle0"))
  		{
	  		addCurrentSx();
	  		return false;
	  	}

  	}


    sxSearchInputObj = inputObj;
    sxChosenInputObj = chosenInputObj;

    if (inputObj.value.length > 2){
	    document.forms['drugtoolForm'].target = "hiddenFrame";

	    document.forms['drugtoolForm'].action.value="dbkmdservice";
	    //alert(searchMode);
	    if (searchMode && searchMode == "symptom"  ){
	    	document.forms['drugtoolForm'].searchSymptom.value = inputObj.value;
	    }

 			if(keynum==13){
				//alert(inputObj.value);
				hide('div_sx_container');
				return searchChosenSx(inputObj.value);


 			}else{
	 			if (searchMode && searchMode =="condition")
	 	    	document.forms['drugtoolForm'].cache.value="searchCondition";
	 			else
	 	    	document.forms['drugtoolForm'].cache.value="searchSymptom";

 	    	document.forms['drugtoolForm'].command.value="quicksearch";
	    	hightedRowIndex = -1;
		    document.forms['drugtoolForm'].submit();
	    }

	  }else{

	  	if (document.getElementById('div_sx_container') !=undefined
	  		&& document.getElementById('div_sx_container').style.visibility=='visible')
	  		hide('div_sx_container');

	  	return;
		}


}

var _searchMode;

function quicksearchForSymptom2(e, inputObj, divToShow, chosenInputObj, searchMode)
{
    var keynum
    var params
		if (window.event) // IE
		{
		    keynum = e.keyCode;
		} else if (e.which) // Netscape/Firefox/Opera
		{
		    keynum = e.which;
		}


    if (keynum ==38 || keynum==40) {
        handleUpDownEnter(e);
        return false;
    }else if (keynum == 13)
  	{
  		if (document.getElementById("sx_row_middle0"))
  		{
	  		addCurrentSx();
	  		return false;
	  	}

  	}


    sxSearchInputObj = inputObj;
    sxChosenInputObj = chosenInputObj;

    if (inputObj.value.length > 1){
	    if (searchMode && searchMode == "symptom"  ){
	    	params = "searchSymptom="+sxSearchInputObj.value;
	    }

 			if(keynum==13){

				hide('div_sx_container');
				return searchChosenSx2(inputObj.value);


 			}else{

 			createXMLHttpRequestDrug();
		    var url = "dbkmdservice";

		    xmlHttp.open("POST", url, true);
		    xmlHttp.onreadystatechange = showQuickSearch2;
		    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");


	 			if (searchMode && searchMode =="condition")
	 			{
	 				params += "&cache=searchCondition&searchMode=condition";
	 				params += "&searchCondition="+sxSearchInputObj.value;
	 			}else
	 			{
	 	    	params += "&cache=searchSymptom&searchMode=symptom";

 				}
 				_searchMode = searchMode;
 	    	params += "&command=quicksearch";

	    	hightedRowIndex = -1;
	    	//alert(params);
		    xmlHttp.send(params);

	    }

	  }else{

	  	if (document.getElementById('div_sx_container') !=undefined
	  		&& document.getElementById('div_sx_container').style.visibility=='visible')
	  		hide('div_sx_container');

	  	return;
		}


}

function showQuickSearch2(){

    if( xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        var responseText = xmlHttp.responseText;
        //alert(responseText);
        if( responseText && responseText.length > 0 ) {
            document.getElementById('quicksymptom_results').innerHTML = responseText;

            if( _searchMode && _searchMode=="condition" ) {
                showQuickSearchResults(document.getElementById('searchCondition'),'div_sx_container');
            }
            else {
				showQuickSearchResults(sxSearchInputObj,'div_sx_container');
            }
        }
    }
    return ;
}

function searchChosenSx2(sx){
     hide('div_sx_container'); // dan-need to this before iframe is populated - firefox display bug

	 createXMLHttpRequestDrug();
		    var url = "dbkmdservice";

		    xmlHttp.open("POST", url, true);
		    xmlHttp.onreadystatechange = setSymptomList2;
		    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");

 params = "searchSymptom2="+ unescape(sx);
 params += "&cache=searchSymptom2";
 params += "&command=sxsearch";
	xmlHttp.send(params);
}

function hideSxList()
{
	hide('div_sx_container');
}

function hideSx2List()
{
    hide('div_searchedSymptoms');
}
function setSymptomList2(){

if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {


	var responseText =xmlHttp.responseText;
	//alert(responseText);
	document.getElementById('div_searchedSymptoms').innerHTML = responseText;

	//alert(document.getElementById('div_searchedSymptoms').innerHTML);
    document.getElementById('div_searchedSymptoms').style.visibility = "visible";
    document.getElementById('div_searchedSymptoms').style.display = "block";

    if (sxSearchInputObj){
    	sxSearchInputObj.value = "";
    	sxSearchInputObj.focus();
    }
  }
}

}
function showQuickSearchResults(divClicked,divContainer ){

	//alert(sxSearchInputObj);
	var divClickedObj = sxSearchInputObj;
	var divContainerObj = document.getElementById(divContainer);

	if (divContainerObj!=undefined){

	var obj1TopX = getLeft(divClickedObj);
					var obj1TopY = getTop(divClickedObj);
					var obj1BottomX = getLeft(divClickedObj) + 2; //- (divClickedObj.offsetWidth*2 - 50);
					var obj1BottomY = getTop(divClickedObj)+divClickedObj.offsetHeight+ 3;
					divContainerObj.style.left = obj1BottomX + "px";
					divContainerObj.style.top = obj1BottomY+ "px";


	divContainerObj.style.visibility = "visible";
	divContainerObj.style.display="block";

}
}
function setSymptomList(divChild){
    //alert(document.getElementById('div_searchedSymptoms'));
    document.getElementById('div_searchedSymptoms').innerHTML = divChild.innerHTML;
    document.getElementById('div_searchedSymptoms').style.visibility = "visible";
    document.getElementById('div_searchedSymptoms').style.display = "block";

    if (sxSearchInputObj){
    	sxSearchInputObj.value = "";
    	sxSearchInputObj.focus();
    }

}
function addSx(searchTerm,type,id,name,addButton){

	var searched = unescape(searchTerm);

	if (document.getElementById('addedSymptoms'))
		document.getElementById('addedSymptoms').value +=",,"+id+ "="+type+searched+":"+ name;
	else if (document.getElementById(sxChosenInputObj))
		document.getElementById(sxChosenInputObj).value +=",,"+id+ "="+type+searched+":"+ name;
	displaySymptom(id,name);
	if (addButton){
		addButton.onclick= "";
		addButton.innerText = "ADDED";
		addButton.className = "linkvisited";
	}
	saveChanges();

}

function displaySymptom(id,name){
  var myNewRow = document.getElementById('selectedSymptoms').insertRow(0);
	myNewRow.id = "r"+id;
	var myCell = myNewRow.insertCell(0);
	myCell.style.width = "300px";
	myCell.className = "body";
	myCell.innerHTML = unescape(name);
	var myCell2 = myNewRow.insertCell(1);
	myCell2.style.width = "200px";
	myCell2.innerHTML = "<img class='trashicon' alt='Remove' src='./skins/base/images/1024/trash.gif' onClick='removeSymptom(\"" + id + "\")'/>";
}

//displays the drug after it is selected
function displayDrugs(synId,brand,drugId){

    var tableID = "selectedDrugs" ;

    var myNewRow = document.getElementById( tableID ).insertRow(0);
	myNewRow.id = "r"+synId;
	var myCell = myNewRow.insertCell(0);
	myCell.style.width = "300px";
	myCell.className = "body";
	myCell.innerHTML = brand;

		var myCell2 = myNewRow.insertCell(1);
	myCell2.style.width = "130px";
	myCell2.className = "body";
	myCell2.innerHTML = "<span class='tellmeMore' onclick='findDrugInfo(this, event,  \""+drugId+":"+brand+"\")'>ALL SIDE EFFECTS +</span>";

	var myCell3 = myNewRow.insertCell(2);
	myCell3.style.width = "100px";
	myCell3.innerHTML = "<img class='trashicon' alt='Remove' src='./skins/base/images/1024/trash.gif' onclick='removeDrug(\""+synId + "\")'/>";

	if (document.getElementById('drugName') !=undefined)
	    document.getElementById('drugName').value = addedDrugs.value;


}
function removeSymptom(id){

	var symptomIds;
	var localSxChosen;
		if (document.getElementById(sxChosenInputObj))
			localSxChosen = document.getElementById(sxChosenInputObj);
		else
			localSxChosen = document.getElementById('addedSymptoms');

	symptomIds = localSxChosen.value;

	var index = symptomIds.indexOf(",,"+id+"=");
	var beginning = symptomIds.substring(0,index);
	var ending = symptomIds.substring(index+1);


	var indexEnd = ending.indexOf(",,");
	if (indexEnd == -1)
		ending = "";
	else
		ending = ending.substring(indexEnd);

	localSxChosen.value= beginning + ending ;
	for (var i=0; i< document.getElementById('selectedSymptoms').rows.length;i++){
		var synId = 'r'+id;
		if (document.getElementById('selectedSymptoms').rows[i].id == synId)
			document.getElementById('selectedSymptoms').deleteRow(i);
	}

	saveChanges();
}

function searchChosenSx(sx){

document.forms['drugtoolForm'].searchSymptom.value = unescape(sx);
searchForSymptom();

}

function searchForSymptom()
{
    //alert(document.all.command.value);
    document.forms['drugtoolForm'].cache.value="searchSymptom";
    document.forms['drugtoolForm'].target = "hiddenFrame";
    document.forms['drugtoolForm'].command.value="sxsearch";
    document.forms['drugtoolForm'].submit();

}

function reloadSx(drugIdObj){

	if (drugIdObj)
		sxChosenInputObj = drugIdObj;
 	addedSx = document.getElementById(drugIdObj);
	addedDrugsStr = drugIdObj;
	if (addedSx!=null){
		drugStr = addedSx.value;
	}else{
		return;
	}
	drugStr = addedSx.value;
	if (drugStr.length == 0) return;

    var drugs = drugStr.split(",,");
    for(var i=0; i< drugs.length; i++){
    	var drug = drugs[i];
    	if (drug.indexOf("=") != -1){
    		var synId = drug.substring(0, drug.indexOf("="));
     		var name = drug.substring(drug.indexOf("=")+1);
     		name = name.substring(name.indexOf(":")+1);
	     	displaySymptom(synId,name);
	   	}
  	}
}


function evaluatedisclaimer()
{
    document.forms['drugtoolForm'].cache.value="";
    document.forms['drugtoolForm'].target = "_top";
    document.forms['drugtoolForm'].command.value="disclaimer";
    document.forms['drugtoolForm'].submit();

}



function enterDrugs()
{
    document.forms['drugtoolForm'].cache.value="";
    document.forms['drugtoolForm'].target = "_top";
    document.forms['drugtoolForm'].command.value="enterDrugs";
    document.forms['drugtoolForm'].submit();

}

function enterSymptoms()
{
    document.forms['drugtoolForm'].cache.value="";
    document.forms['drugtoolForm'].target = "_top";
    document.forms['drugtoolForm'].elements['command'].value="enterSymptoms";
    document.forms['drugtoolForm'].submit();

}

function enterConditions()
{
    document.forms['drugtoolForm'].cache.value="";
    document.forms['drugtoolForm'].target = "_top";
    document.forms['drugtoolForm'].elements['command'].value="enterConditions";
    document.forms['drugtoolForm'].submit();

}

function evaluateDrugs()
{
    document.forms['drugtoolForm'].cache.value="";
    document.forms['drugtoolForm'].target = "_top";
    document.forms['drugtoolForm'].elements['command'].value="evaluate";
    document.forms['drugtoolForm'].submit();

}

function addCond(searchTerm,type,id,name){

	var searched = unescape(searchTerm);
	document.getElementById('addedConditions').value +=",,"+id+ "="+type+searched+":"+ name;
	displayCondition(id,name);
	hide('div_sx_container');
	if (document.getElementById('searchCondition')){
    	document.getElementById('searchCondition').value = "";
    	document.forms['drugtoolForm'].searchCondition.focus();
    }
}

function displayCondition(id,name){

	var myNewRow = document.getElementById('selectedConditions').insertRow(0);
	myNewRow.id = "r"+id;
	var myCell = myNewRow.insertCell(0);
myCell.style.width = "300px";
	myCell.className = "body";
	myCell.innerHTML = unescape(name);
	var myCell2 = myNewRow.insertCell(1);
	myCell2.style.width = "200px";
	myCell2.innerHTML = "<img alt='Remove' class='trashicon' src='./skins/base/images/1024/trash.gif' onClick='removeCondition(\""+id + "\")'/>";
}

function removeCondition(id){
	//alert('remove' + addedConditions.value);
	var symptomIds = document.getElementById('addedConditions').value;
	var index = symptomIds.indexOf(",,"+id+"=");
	var beginning = symptomIds.substring(0,index);
	var ending = symptomIds.substring(index+1);


	var indexEnd = ending.indexOf(",,");
	if (indexEnd == -1)
		ending = "";
	else
		ending = ending.substring(indexEnd);

	document.getElementById('addedConditions').value= beginning + ending ;

	//alert(document.getElementById('selectedConditions'));
	for(var i=0; i< document.getElementById('selectedConditions').rows.length;i++){
		var synId = 'r'+id;
		if (document.getElementById('selectedConditions').rows[i].id == synId)
			document.getElementById('selectedConditions').deleteRow(i);
	}
}


function reloadCond(drugIdObj){

	var addedCond = document.getElementById(drugIdObj);

	if (addedCond == undefined){
		return;
	}
	var condStr = addedCond.value;

	if (condStr.length == 0) return;

    var conditions = condStr.split(",,");

    for(var i=0; i< conditions.length; i++){
    	var condition = conditions[i];
    	if (condition.indexOf("=") != -1){
    		var synId = condition.substring(0, condition.indexOf("="));
     		var name = condition.substring(condition.indexOf("=")+1);
     		name = name.substring(name.indexOf(":")+1);
	     	displayCondition(synId,name);
	   	}
  	}
}




function handleUpDownEnter(evt){
	evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        switch (evt.keyCode) {
            case 38:
                scrollVisibleList('up');
                break;
            case 40:
            		scrollVisibleList('down');
                break;
         }
    }
}

function addCurrentDrug()
{
	var currentTR = document.getElementById("drug_row"+hightedRowIndex);

	if (currentTR)
	{
		//alert(currentTR.attributes['toAdd'].value);
		eval(currentTR.attributes['onclick'].value);
	}

}


function addCurrentSx()
{

	var currentTRSx = document.getElementById("sx_row_middle"+hightedRowIndex);

	if (currentTRSx)
	{

		eval(currentTRSx.attributes['onclick'].value);
		hideSxList();
	}

}


function scrollVisibleList(arrowdirection){

	if (document.getElementById('div_sx_container') &&
		document.getElementById('div_sx_container').style.visibility == 'visible'){
		//document.getElementById('div_sx_container').focus();
	  //alert(hightedRowIndex+":"+ arrowdirection +":"+document.getElementById('quicksymptom_results') );
		 if (hightedRowIndex>-1)
				unhighliterow2(hightedRowIndex);

			if (arrowdirection == 'down'){
				highliterow2(hightedRowIndex+1);
				//hightedRowIndex = hightedRowIndex+1;
			}else if (arrowdirection == 'up'){
				if (hightedRowIndex>0){
					highliterow2(hightedRowIndex-1);
					//hightedRowIndex = hightedRowIndex-1;
				} else
					hightedRowIndex = -1;
			}


	}else if (document.getElementById('emdDrugPickComp') &&
	document.getElementById('emdDrugPickComp').style.visibility == 'visible'){
		 //document.getElementById('emdDrugPickComp').focus();
		 	if (hightedRowIndex>0)
				unhighliterow(hightedRowIndex);


			if (arrowdirection == 'down'){

				highliterow(hightedRowIndex+1);

				//alert(hightedRowIndex);
				//hightedRowIndex = hightedRowIndex+1;
			}else if (arrowdirection == 'up'){
				if (hightedRowIndex>0){

					highliterow(hightedRowIndex-1);
					//hightedRowIndex = hightedRowIndex-1;
				}else
					hightedRowIndex = -1;
			}


	}




}



/////////////////////////////////////////////////////////////
// ICD drug add list
/////////////////////////////////////////////////////////////
drugAddList.prototype = new _drugList;
drugAddList.prototype.constructor = drugAddList;

// ---------------------------------------------------------------------------------------------------------------------

drugAddList.prototype.getListById = function (val) {
    this.postUrl("pages/popups/DrugList.jsp", "listID=" + val + "&varName=" + this.varName);
};

// ---------------------------------------------------------------------------------------------------------------------

function drugAddList(varName, settings) {
    _drugList.call(this, varName, settings);
}

/////////////////////////////////////////////////////////////
// ICD symptom list
/////////////////////////////////////////////////////////////
sxAddList.prototype = new _sxList;
sxAddList.prototype.constructor = sxAddList;

function sxAddList(varName, settings) {
    _sxList.call(this, varName, settings);
}



var drugAdd ;
function makeDrugAdd()
{
    drugAdd = new drugAddList('drugAdd');
    return  ;
}

var sxAdd ;
function makeSxAdd()
{
    sxAdd = new sxAddList( 'sxAdd' ) ;
    return  ;
}

/////////////////////////////////////////////////////////////
// Condition list
/////////////////////////////////////////////////////////////
_conditionList.prototype = new _popupList;
_conditionList.prototype.constructor = _conditionList;

// need for support delete from list
var conditionsRegister = new Object();

function _conditionList(varName, settings) {
    this.settings = settings;
    if (!this.settings) this.settings = new Object();
    if (this.settings.persistanceOnlyRead == undefined) {
        this.settings.persistanceOnlyRead = true;
    }
    _popupList.call(this, varName);
    conditionsRegister[varName] = this;
    this.debug = false;
    // in up this property select in popup condition use event onAdd
    this.useAdd = false;
    this.inputId = (this.settings.custom ? varName : "") + "searchCondition";
    this.picklistDiv = (this.settings.custom ? varName : "") + "popupListBox";
    if (this.debug) alert(this.picklistDiv);
    this.itemsId = (this.settings.custom ? varName : "") + "conditionItems";
    this.itemIdPrefix = (this.settings.custom ? varName : "") + "conditionItem";
    this.trashGif = "./images/glythe/trash.gif";
    this.containerDiv = (this.settings.custom ? varName : "") + "condition_popup_container";
    this.rowDivName = "condition_row";
    this.restoreFromCookie();
}

_conditionList.prototype.saveToCookie = function () {
    if (!this.settings.persistanceCookieName) return;
    if (this.settings.persistanceOnlyRead) return;
    if (this.debug) alert("Start save to cookie!");
    var cookieValue = "";
    var idAndNames = this.getConditionIdAndNames();
    for (var i = 0; i < idAndNames.length; i++) {
        cookieValue += idAndNames[i].conditionId + ":" + idAndNames[i].conditionName + ";";
    }
    SetCookie(this.settings.persistanceCookieName, cookieValue);
    if (this.debug) alert("Setted cookie value: " + cookieValue);
}

_conditionList.prototype.restoreFromCookie = function () {
    if (!this.settings.persistanceCookieName) return;
    if (this.debug) alert("Start restore to cookie!");
    var cookieValue = "";
    var cookieValue = ReadCookie(this.settings.persistanceCookieName);
    if (this.debug) alert("Restore cookie value: " + cookieValue);
    if (cookieValue && cookieValue.length > 0) {
        var idAndNames = cookieValue.split(";");
        for (var i = 0; i < idAndNames.length; i++) {
            var idAndName = idAndNames[i].split(":");
            if (idAndName[0] > 0) {
                this.addCondition(idAndName[0], idAndName[1]);
            }
        }
    }
}

_conditionList.prototype.popupType = function () {
    return 'condition';
}

_conditionList.prototype.hidePopup = function () {
    hide(this.containerDiv);
}

_conditionList.prototype.getConditionIds = function () {
    var conditionItems = document.getElementById(this.itemsId);
    var conditionIds = new Array();
    for (var i = 0; i < conditionItems.childNodes.length; i++) {
        var conditionItem = conditionItems.childNodes[i];
        if (conditionItem.conditionId > 0) {
            conditionIds.push(conditionItem.conditionId);
        }
    }
    return conditionIds;
}

_conditionList.prototype.getConditionIdAndNames = function () {
    var conditionItems = document.getElementById(this.itemsId);
    var conditionIdAndNames = new Array();
    for (var i = 0; i < conditionItems.childNodes.length; i++) {
        var conditionItem = conditionItems.childNodes[i];
        if (conditionItem.conditionId > 0) {
            var conditionIdAndName = new Object();
            conditionIdAndName.conditionId = conditionItem.conditionId;
            conditionIdAndName.conditionName = conditionItem.conditionName;
            if (this.debug) alert(conditionItem.conditionId + " " + conditionItem.conditionName);
            conditionIdAndNames.push(conditionIdAndName);
        }
    }
    return conditionIdAndNames;
}

function removeConditionInternal(varName, conditionId) {
    conditionsRegister[varName].removeCondition(conditionId);
}

_conditionList.prototype.addCondition = function (conditionId, conditionName) {
    if (!conditionId || conditionId < 1) return;
    var conditionIds = this.getConditionIds();
    for (var i = 0; i < conditionIds.length; i++) {
        if (conditionId == conditionIds[i]) return;
    }
    var conditionItems = document.getElementById(this.itemsId);
    var conditionItem = document.createElement("div");
    conditionItem.conditionId = conditionId;
    conditionItem.id = this.itemIdPrefix + conditionId;
    conditionItem.conditionName = conditionName;
    conditionItem.innerHTML = "<span  onmouseover=\"Tip('Remove " + conditionName +
                              "', BALLOON, true)\" onmouseout=\"UnTip()\"><img class='cursorhand' src='"
            + this.trashGif + "' " + "onclick='UnTip(); removeConditionInternal(\"" + this.varName + "\", " + conditionId + ")'/></span>" + conditionName;
    conditionItems.appendChild(conditionItem);
    this.saveToCookie();
    if (this.onAdd) this.onAdd(conditionId, conditionName);
}

_conditionList.prototype.removeCondition = function (conditionId) {
    if (!conditionId || conditionId < 1) return;
    var conditionItem = document.getElementById(this.itemIdPrefix + conditionId);
    if (!conditionItem) return;
//    alert("delete " + conditionId);
    conditionItem.parentNode.removeChild(conditionItem);
    this.saveToCookie();
    if (this.onRemove) this.onRemove(conditionId);
}

_conditionList.prototype.showPopup = function (text) {
    if (this.debug) alert("Start show condition popup " + text);
    document.getElementById(this.picklistDiv).innerHTML = text;

    if (this.useAdd) {
        for (var i = 0; ; i++) {
            var conditionItem = document.getElementById(this.rowDivName + i);
            if (!conditionItem) break;
            if (this.debug) alert("Start find condition in popup!");
            conditionItem.conditionId = document.getElementById("conditionItemId" + i).value;
            conditionItem.conditionName = document.getElementById("conditionItemName" + i).value;
            if (this.debug) alert("Аdd condition " + conditionItem.conditionId + " in popup!");
            var conditions = this;
            conditionItem.onclick = function () {
                conditions.addCondition(this.conditionId, this.conditionName);
            }
        }
    }

    var searchInput = document.getElementById(this.inputId);
    var drugListDiv = document.getElementById(this.containerDiv);
    drugListDiv.style.width = 430 + "px";
    var obj1TopX = _getLeft(searchInput);
    var obj1TopY = _getTop(searchInput);
    var obj1BottomX = _getLeft(searchInput) + this.popupLeftOffset; //+searchInput.offsetWidth;
    obj1BottomX = calculateWindow(obj1BottomX, searchInput);
    var obj1BottomY = _getTop(searchInput) + searchInput.offsetHeight + 6;
    drugListDiv.style.left = obj1BottomX + "px";
    drugListDiv.style.top = obj1BottomY + "px";

    if (this.debug) alert("Start show " + this.picklistDiv);
    show(this.picklistDiv);
    if (this.debug) alert("Show " + this.picklistDiv);
    if (this.debug) alert("Start show " + this.containerDiv);
    show(this.containerDiv);
    if (this.debug) alert("Show " + this.containerDiv);
    this.highliterow(0);
}

_conditionList.prototype.getList = function (val) {
    if (this.debug) alert("Start get condition list!");
    var parameters = "name=" + val + "&varName=" + this.varName;
    this.postUrl("pages/popups/ConditionPick.jsp", parameters);
}
