var oAJAX	= clsAJAX;
var aDID	= new Array();
var aDN		= new Array();
var aDSV	= new Array();
var drugformid;
function getEID(id) {
	return(document.getElementById(id));
}
var cntb=-1;
var sublist=0;
function showBox(oParent, id, align) {
	
	var oBox = getEID(id);
	var oP=oParent;
	var curtop=19;
	oBox.style.left = setLeft(oParent) + 'px';
	oBox.style.wdith = setWidth(oParent) + 'px';
	if (oParent.offsetParent) {
		do {
			curtop += oParent.offsetTop;

		} while (oParent = oParent.offsetParent);
		
	}
	sublist=curtop;
	oBox.style.top=curtop+'px';
	oBox.style.display = 'block';
	oBox.style.width=oP.style.width;
	
	
}
function selectBox(v,uni)
{
	
	var d='did_'+v;
	var be_d=0;
	var oBox = getEID(d);
	if(v==0)
	{
		if(uni==38)
		{
			be_d='did_'+(v+1);
			var bBox=getEID(be_d);
			bBox.className='item';
		}
		oBox.className='item_h';
		
	}
	else
	{
		if(uni==40)
			 be_d='did_'+(v-1);
		else if(uni==38)
			 be_d='did_'+(v+1);
	
		var bBox=getEID(be_d);
		oBox.className='item_h';
		bBox.className='item';
		

	}
	var objDiv = document.getElementById("container");
	oBox.focus();
	
	if(drugformid==1)
	{
		getEID('drug_name').value = aDN[v];
		getEID('drug_id').value = aDID[v];
		getEID('savings').value = aDSV[v];
	}else if(drugformid==2)
	{
		getEID('drug_name2').value = aDN[v];
		getEID('drug_id2').value = aDID[v];
		getEID('savings2').value = aDSV[v];
	}
	var oBox2 = getEID(d);
	
	var curtop=0;
	var objDiv = document.getElementById("container");
	
	if (oBox2.offsetParent) {
		do {
			curtop += oBox2.offsetTop;

		} while (oBox2 = oBox2.offsetParent);
		
	}
	objDiv.scrollTop =curtop-sublist;
	

	
	
}


function hideBox(objID) {
	cntb=-1;
	var objDiv = document.getElementById("container");
	objDiv.scrollTop=0;
	var box = getEID(objID);
	if (box) {box.style.display = 'none';}
	
}

function classChange(obj, newClass){
	if (!obj.readOnly) obj.className = newClass;
}

function addDiv(id, className, content) {
	var oDiv = document.createElement("DIV");
	
	oDiv.id = id;
	oDiv.className = className;
	oDiv.innerHTML = content;
	
	oDiv.onmouseover	= function() {classChange(this, 'item_h');};
	oDiv.onmouseout 	= function() {classChange(this, 'item');};
	oDiv.onmousedown	= function() {setDrugName(this); hideBox('container');document.getElementById("first_name").focus();} ;
	
	
	return(oDiv);
}

function addBold(text, start, length) {
	text = text.replace(text.substr(start, length), '<b>' + text.substr(start, length) + '</b>');
	return(text);
}

function removeBold(text) {
	text = text.replace('<b>', '');
	text = text.replace('</b>', '');
	text = text.replace('<B>', '');
	text = text.replace('</B>', '');
	
	return(text);
}

function doDrugLookup(e,id) {
	
	var unicode=e.keyCode;
	drugformid=id;
	
	if(drugformid==1)
 	var drug_name	= getEID("drug_name").value;
	else if(drugformid==2)
		var drug_name	= getEID("drug_name2").value;
		
	var ajxURL		= "../common/ajxDrugLookup.php";
	var ajxForm		= 'action=getList&dn=' + drug_name;
 
	
	
		if (drug_name.length > 0)
		{
			if(unicode!=40 && unicode!=38 && unicode!=13 )
			{
				
				oAJAX.getData(ajxURL, ajxForm, buildDrugList, '', '');
				if(drugformid==1)
					document.getElementById("drug_name").focus();
				
				
			}
			else if(unicode==40)
			{
				
				
				cntb+=1;
				selectBox(cntb,unicode);
				if(drugformid==1)
					document.getElementById("drug_name").focus();

				
			}
			else if(unicode==38)
			{
				
				if(cntb!=0)
					cntb-=1;
				selectBox(cntb,unicode);
				if(drugformid==1)
					document.getElementById("drug_name").focus();
			}
			else if(unicode==13 )
			{		
				hideBox('container');
				if(drugformid==1)
					document.getElementById("first_name").focus();
				else if(drugformid==2)
					addCoupon();
			}
			
			
		}
	else {
		hideBox('container');
	}
	//onkeypress="showBox(this, 'container', 'left');"
	
}

function buildDrugList(data) {
	var oDrugList = data.parseJSON();
	var oContainer = getEID("container");
	
	if(drugformid==1)
		var oDrugName	= getEID('drug_name');
	else if(drugformid==2)
		var oDrugName	= getEID('drug_name2');
	
	
	//<div class="item" onmouseover="classChange(this, 'item_h');" onmouseout="classChange(this, 'item');">drug 0</div>

	oContainer.innerHTML = "";
		
	if (oDrugList.length > 0) {
		aDID	= [];
		aDN 	= [];
		aDSV	= [];

		for (var i=0;i<oDrugList.length;i++) {
			drug_id = oDrugList[i].id;
			drug_name = oDrugList[i].drug_name;
			savings = oDrugList[i].savings;
			
			aDID.push(drug_id); 
			aDN.push(drug_name);
			aDSV.push(savings);
			
			drug_name = addBold(drug_name, 0, oDrugName.value.length);
			var oDiv = addDiv('did_'+i, 'item', drug_name);
			oContainer.appendChild(oDiv);
		}
		//alert(oDrugList.length * 17)
		
		if (oDrugList.length * 17 < 136) oContainer.style.height = oDrugList.length * 17 + 'px';
		else oContainer.style.height = 136 + 'px';
		showBox(oDrugName, 'container', 'left');
		//alert(oContainer.style.height);
		//if (oContainer.style.display != "block") showBox(oDrugName, 'container', 'left');
	}
	else hideBox('container');
}

function setDrugName(div) {
	var id = div.id.replace('did_', '');
	
	if(drugformid==1)
	{
		getEID('drug_name').value = aDN[id];
		getEID('drug_id').value = aDID[id];
		getEID('savings').value = aDSV[id];
	}
	else if(drugformid==2)
	{
		getEID('drug_name2').value = aDN[id];
		getEID('drug_id2').value = aDID[id];
		getEID('savings2').value = aDSV[id];
	}
}


function drugInList(drug_name) {
	var dn = drug_name;
	var ldn = '';
	dn = dn.toLowerCase();

	for (var i=0;i<aDN.length;i++) {
		ldn = aDN[i];
		ldn = ldn.toLowerCase();
		if (ldn == dn) return(true);
	}
	return(false);
}

function showSignupForm() {

	var blanket		= getEID("blanket");
	var container	= getEID("form_container");

	if (debug != 1) blanket.style.display="block";
	container.style.top = document.body.scrollTop + 50+'px';
	container.style.display="block";
	
}



hideSignupForm = function() {
	var blanket		= getEID("blanket");
	var container	= getEID("form_container");
	//var submitted 	= document.getElementById("submitted");

	blanket.style.display="none";
	container.style.display="none";
}

function padStr(text, pad) {
	var len		= 0;
	var ntext	= '';
	
	len = pad - text.length;
	for (var i=0;i<len;i++) {
		ntext += '0';
	}
	return(ntext + text);
}

function tabNext(input) {
	if (input == 'zip_code') {
		if (getEID(input).value.length == 5) {
			getEID("area").focus();
		}
	}
}

function setWidth(obj) {
	return obj.offsetWidth;	
}

function setLeft(obj) {
	var curleft = 0;
	
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
		
	return curleft;
}

