function ProcedureFinder()
{
	this.sReqBA = this.sReqBP = this.sSymp = null;
	this.oBP = {};
	this.oSymp = {};
	this.oProcs = {};
	this.oSrchProg = new HTMLLayer('srch-prog');
	this.oSrchRes = new HTMLLayer('srch-res');
	this.oSrchTxt = new HTML('pf_srch');
	this.sResRow = '<div class="srch-row"><div class="title"><a href="<#1#>"><#2#></a></div><div class="desc"><#3#></div></div>';
	
	if (this.oSrchTxt.elem)
	{
		this.oSrchTxt.elem.onkeyup = function(evt)
		{
			var e = window.event || evt;
			if (e.keyCode == 13)
			{
				oPF.searchProcs();
			}
		};
	}
	
	this.getBodyParts = function(ba)
	{
		var oUL;
		if (this.sReqBA)
		{
			oUL = document.getElementById('baul_'+this.sReqBA);
			if (oUL)
				oUL.style.display = 'none';
		}
		this.sReqBA = ba;
		oUL = document.getElementById('baul_'+this.sReqBA);
		if (this.oBP['ba_'+this.sReqBA] == 'Y' && oUL)
			oUL.style.display = 'block';
		else
		{
			var u = baseUrl+'procedurefinder/getBodyParts?ba='+ba,
			oAjax = new Ajax('procFind',u,true,['oPF.postBodyParts']);
			oAjax.send();
			return false;
		}
	};

	this.postBodyParts = function(resp)
	{
		var oD = resp.oJSON, len = oD ? oD.length : 0, s ='';
		if (len > 0)
		{
			var oUL = this.getUL('baul_' + this.sReqBA);

			for (var i=0;i<len;i++)
			{
				s += '<li id="bpli_' + oD[i].bp_id + '"><a href="#" onclick="oPF.getSymptoms(\'' + oD[i].bp_id + '\');return false;">' + oD[i].bp_text + '</a></li>';
			}

			this.oBP['ba_'+this.sReqBA] = 'Y';
			oUL.innerHTML = s;
			var oLI = document.getElementById('bali_'+this.sReqBA);
			if (oLI)
				oLI.appendChild(oUL);
			
		}
	};

	this.getSymptoms = function(bp)
	{
		var oUL;
		if (this.sReqBP)
		{
			oUL = document.getElementById('bpul_'+this.sReqBP);
			if (oUL)
				oUL.style.display = 'none';
		}
		this.sReqBP = bp;
		oUL = document.getElementById('bpul_'+this.sReqBP);
		if (this.oSymp['bp_'+this.sReqBP] == 'Y' && oUL)
			oUL.style.display = 'block';
		else
		{
			var u = baseUrl+'procedurefinder/getSymptoms?bp='+bp,
			oAjax = new Ajax('procFind',u,true,['oPF.postSymptoms']);
			oAjax.send();
			return false;
		}
	};

	this.postSymptoms = function(resp)
	{
		var oD = resp.oJSON, len = oD ? oD.length : 0, s ='';
		if (len > 0)
		{
			var oUL = this.getUL('bpul_' + this.sReqBP);

			for (var i=0;i<len;i++)
			{
				s += '<li id="sympli_' + oD[i].symp_id + '"><a href="#" onclick="oPF.getProcedures(\'' + oD[i].symp_id + '\');return false;">' + oD[i].symp_text + '</a></li>';
			}

			this.oSymp['bp_'+this.sReqBP] = 'Y';
			oUL.innerHTML = s;
			var oLI = document.getElementById('bpli_'+this.sReqBP);
			if (oLI)
				oLI.appendChild(oUL);
		}
	};

	this.getProcedures = function(sympid)
	{
		var oUL;
		if (this.sSymp)
		{
			oUL = document.getElementById('sympul_'+this.sSymp);
			if (oUL)
				oUL.style.display = 'none';
		}
		this.sSymp = sympid;
		oUL = document.getElementById('sympul_'+this.sSymp);
		if (this.oProcs['symp_'+this.sSymp] == 'Y' && oUL)
			oUL.style.display = 'block';
		else
		{
			var u = baseUrl+'procedurefinder/getProcedures?symp='+sympid,
			oAjax = new Ajax('procFind',u,true,['oPF.postProcedures']);
			oAjax.send();
			return false;
		}
	};

	this.postProcedures = function(resp)
	{
		var oD = resp.oJSON, len = oD ? oD.length : 0, s ='';
		if (len > 0)
		{
			var oUL = this.getUL('sympul_' + this.sSymp);

			for (var i=0;i<len;i++)
			{
				var u = this.getProcUrl(oD[i].proc_id, oD[i].proc_text);
				s += '<li id="procli_' + oD[i].proc_id + '"><a href="'+u+'">' + oD[i].proc_text + '</a></li>';
			}

			this.oProcs['symp_'+this.sSymp] = 'Y';
			oUL.innerHTML = s;
			var oLI = document.getElementById('sympli_'+this.sSymp);
			if (oLI)
				oLI.appendChild(oUL);
		}
	};
	
	this.searchProcs = function()
	{
		this.oSrchRes.setValue('');
		this.oSrchProg.show(true);
		var v = document.getElementById('pf_srch').value;
		if (v && v.length > 0)
		{
			var u = baseUrl+'procedurefinder/searchProcedures?kw='+v,
			oAjax = new Ajax('procSrach',u,true,['oPF.postProcSrch']);
			oAjax.send();
		}
		return false;
	};
	
	this.postProcSrch = function(resp)
	{
		this.oSrchProg.show(false);
		var oD = resp.oJSON, len = oD ? oD.length : 0, s = "";
		if (len > 0)
		{
			s += '<div class="m10">We found the following procedures for the above keyword</div>';
			for (var i=0; i<len;i++)
			{
				var d = oD[i], u = this.getProcUrl(d.proc_id, d.proc_text);
				s += this.sResRow.replaceTokens(u, d.proc_text, d.proc_desc);
			}
		}
		else
			s += '<div class="m10">No procedures found for the above keyword, please refine your keywords or browse by symptoms below.</div>';
		
		this.oSrchRes.setValue(s);
	};

	this.getUL = function(id)
	{
		var oUL = document.createElement('ul');
		oUL.id = id;
		oUL.className = 'proc-ul';
		return oUL;
	};
	
	this.getProcUrl = function(pId, pText)
	{
		var u = baseUrl + "medical-procedures-", n = pText;
		
		n = n.toLowerCase(); // lowercase
	    n = n.replace(/^\s+|\s+$/g, ''); // remove leading and trailing whitespaces
	    n = n.replace(/\s+/g, '-'); // convert (continuous) whitespaces to one -
	    n = n.replace(/[^a-z-]/g, '');
		
		u += n + "/"+pId;
		
		return u;
	};
};
var oPF = new ProcedureFinder();
