var tl={
	procfile: "tlproc.php",
	tl_memkey:'', 	//memory for keypresses on treelist
	tl_memkeytimer:null, 	//timer for keypresses on treelist
	tl_memobj:null, //memory for object highlighted by keypress
	tl_maindiv:null,
	picdd_memdoc:null,
	sendreq : function(strin){
		//alert(strin);
		if (this.tl_maindiv) strin+="&tl_maindiv="+this.tl_maindiv;
		scmain.sendreq(strin, this.procfile);
	},
	picdd_click : function(strid, event){
		if (!event) event=window.event;
		if (tmpobj=document.getElementById("picdd_trlist_"+strid)){
			if (tmpobj){
				if (tmpobj.style.display=="block"){
					tmpobj.style.display="none";
					document.onclick='';
				}else{
					tmpobj.style.display="block";
					document.onclick=function(event){tl.picdd_click(strid);};
					/*if (tmpobj.getAttribute("selectedindex")){
						
					}*/
					funcs.cancelbubble(event);
				}
			}
		}
	},
	picdd_iclick : function (strid,ctr,event){
		document.onclick='';
		if (tmpobj=document.getElementById("picdd_trlist_"+strid)){
			if (tmpobj) tmpobj.style.display="none";
		}
		if (listitem=document.getElementById("picdd_item_"+strid+"_"+ctr)){
			if (listitem){
				if (tritem=document.getElementById("picdd_item_"+strid)){
					if (tritem&&tritem.getAttribute("typeid")){
						sendstr="&tlclick="+tritem.getAttribute("typeid")+"&value="+listitem.getAttribute("dbid");
						if (tritem.getAttribute("subkey")) sendstr+="&subkey="+tritem.getAttribute("subkey");
						this.sendreq(sendstr);
						tritem.setAttribute("selectedindex",ctr);
						tritem.innerHTML=listitem.innerHTML;
					}
				}
			}
		}
	},
	picdd_mover : function(strid){
		if (tmpobj=document.getElementById("picdd_ddarrow_"+strid)){
			if (tmpobj){
				tmpobj.style.backgroundPosition="0px -18px";
			}
		}
	},
	picdd_mout : function (strid){
		if (tmpobj=document.getElementById("picdd_ddarrow_"+strid)){
			if (tmpobj){
				tmpobj.style.backgroundPosition="0px 0px";
			}
		}
	},
	picdd_imover : function(objin){
		if (objin){
			objin.style.backgroundColor="#3399FF";
		}
	},
	picdd_imout : function (objin){
		if (objin){
			objin.style.backgroundColor="";
		}
	},
	tl_mover : function(objin){
		objin.style.backgroundColor="#AAA";
	},
	tl_mout : function(objin){
		objin.style.backgroundColor="";
	},
	tl_click : function(objin){
		this.tl_clearkeymem();
		scmain.listtype=1;
		this.tl_removeeditfield();
		if ((objin.getAttribute("dbid"))&&(objin.getAttribute("typeid"))){
			sendstr="&tlclick="+objin.getAttribute("typeid")+"&value="+objin.getAttribute("dbid");
			if (objin.getAttribute("subkey")) sendstr+="&subkey="+objin.getAttribute("subkey");
			this.sendreq(sendstr);
		}
		else alert("No dbid or typeid on item (js tl.tl_click)");
	},
	tl_changeDD: function(objin){
		scmain.listtype=2;
		if(objin.getAttribute("typeid")){
			if (!(objin.value)) val=0;
			else val=objin.value;
			sendstr="&tlclick="+objin.getAttribute("typeid")+"&value="+val;
			if (objin.getAttribute("subkey")) sendstr+="&subkey="+objin.getAttribute("subkey");
			this.sendreq(sendstr);
		}
	},
	tl_clickb : function (typeid, subkey){
		this.tl_removeeditfield();
		this.tl_clearkeymem();
		if (typeid>0) this.sendreq("&tlclickb="+typeid+"&subkey="+subkey);
	},
	tl_rc: function (event, objin){
		event.cancelBubble=true;
		event.returnValue=false;
		if (!(objin.getAttribute("typeid"))){ alert("No typeid found in this element (js tl.tl_rc)"); return;}
		typeid=objin.getAttribute("typeid");
		coordX=event.clientX+document.documentElement.scrollLeft;
		coordY=event.clientY+document.documentElement.scrollTop;
		strbuttons="";
		//showtext was old function
		if (objin.getAttribute("dbid")) dbid=objin.getAttribute("dbid");
		else dbid=0;
		if (objin.getAttribute("typename")) typename=objin.getAttribute("typename");
		else typename="";
		if (objin.getAttribute("pfid")) pfid=objin.getAttribute("pfid");
		else pfid=0;
		if (objin.getAttribute("subkey")) subkey=objin.getAttribute("subkey");
		else subkey=0;
		strbuttons+="<div style='background-color:#254545;color:#FFFFFF;font-size:7pt;' oncontextmenu='return false;'>"+typename+"</div>";
		if (objin.getAttribute("item")){
			myitem=objin.getAttribute("item");
			myitem=myitem.replace(/\"/g,'\\"');
		}else myitem="";
		if (objin.getAttribute("addable")){
			strbuttons+="<input type=button value='Add' class='ddbut' onclick='DD.DBye();tl.tl_editfield("+typeid+", 0, \""+typename+"\", "+coordX+", "+coordY+", \"\", "+subkey+", \""+pfid+"\");'>";
		}
		if (objin.getAttribute("dbid")&&objin.getAttribute("changeable")) strbuttons+="<input type=button value='Change' class='ddbut' onclick='DD.DBye();tl.tl_editfield("+typeid+", "+dbid+", \""+typename+"\", "+coordX+", "+coordY+", \""+myitem+"\", "+subkey+", \""+pfid+"\");'>";;
		if (objin.getAttribute("toggleable")){
			if (objin.getAttribute("intdis")&&objin.getAttribute("intdis")>0){
				if (parseInt(objin.getAttribute("intdis"))==1) strbuttons+="<input type='button' class='ddbut' value='Enable' title='Enables current Location' onclick='tl.tl_togleaf("+typeid+", "+dbid+");'>";
				if (parseInt(objin.getAttribute("intdis"))==2) strbuttons+="<input type='button' class='ddbut' value='Enable' title='Enables current Fit' onclick='tl.tl_togleaf("+typeid+", "+dbid+");'>";
				if (parseInt(objin.getAttribute("intdis"))==3) strbuttons+="<input type='button' class='ddbut' value='Enable' disabled=true title=\"Cannot Enable Fit (Fabric isn't available in this Fit)\">";
			}else strbuttons+="<input type='button' class='ddbut' value='Disable' title='Disables current selection' onclick='tl.tl_togleaf("+typeid+", "+dbid+");'>";
		}
		if (objin.getAttribute("removable")) strbuttons+="<input type='button' class='ddbut' value='Remove' onclick='tl.tl_delfield("+typeid+", "+dbid+", \""+myitem+"\");'>";
		
		DD.createDD(strbuttons, coordX, coordY);
		return false;
	},
	tl_togleaf: function(typeid, dbid){
		this.sendreq("&tl_togleaf="+typeid+"&dbid="+dbid);
	},
	tl_delfield: function(typeid, dbid, item){
		if (confirm("Are you sure you want to remove "+item+"?")) this.sendreq("&tl_delfield=1&typeid="+typeid+"&dbid="+dbid);
	},
	tl_extraclick : function (event, objin, intid){
		rightclick=null;
		if (!(event)) event=window.event;
		if (event.which) rightclick = (event.which == 3);
		else if (event.button) rightclick = (event.button == 2);
		if (rightclick){
			this.sendreq("&tl_etoggle="+intid);
			if (objin.style.textDecoration=="line-through") objin.style.textDecoration="none";
			else objin.style.textDecoration="line-through";
		}
		return false;
	},
	tl_editfield: function(typeid, dbid, strtype, coordX, coordY, item, subkey, pfid){
		extrastr="";
		this.tl_removeeditfield();
		this.proto=document.createElement("div");
		this.proto.id="floatydiv";
		this.proto.style.position="absolute";
		this.proto.style.zIndex=100;
		this.proto.style.top=(coordY+parseInt(document.body.scrollTop))+"px";
		this.proto.style.left=(coordX+parseInt(document.body.scrollLeft))+"px";
		this.proto.style.background="#FFF";
		this.proto.style.border="1px solid #777";
		//this.proto.style.filter="progid:DXImageTransform.Microsoft.Shadow(Color='#bbb', Direction='135', Strength='3')";
		if (dbid==0) startstr="Add";
		else startstr="Change";
		if (subkey!=null&&subkey!=0) extrastr+=" subkey='"+subkey+"'";
		if (pfid!=null&&pfid!=0) extrastr+=" pfid='"+pfid+"'";
		this.proto.innerHTML="<table cellspacing='0' cellpadding='0' style='table-layout:fixed;border-collapse:collapse;'><tr><td style='border:1px solid #AAA;width:290px;cursor:move;background:#335577;color:#FFF;font:bold 8pt arial;' onmousedown=\"ezdrag.start(event, document.getElementById('floatydiv'));\">"+startstr+" "+strtype+":</td><td style='width:11px;cursor:pointer;border-bottom:1px solid #AAA;border:1px solid #AAA;text-align:center;' onclick='tl.tl_removeeditfield();'>X</td></tr></table><table cellspacing='0' cellpadding='3'><tr><td><input type='text' dbid='"+dbid+"' id='floatytxtbox' typeid='"+typeid+"'"+extrastr+" onkeypress='tl.tl_chkeditfield(event, this);' value=\""+item+"\" style='width:300px;'></td></tr></table>";
		document.body.appendChild(this.proto);
		//remove jump to key so they can type in stuff into the edit field.
		document.onkeydown='';
		setTimeout("document.getElementById('floatytxtbox').focus();", 100);
	},
	tl_chngpt: function(objin){
		newpt=objin.getAttribute("priceid");
		plist=document.getElementsByTagName("input");
		for(i in plist){
			if (plist[i].className=="tl_price"){
				if (plist[i].getAttribute("pval"+newpt)) plist[i].value=plist[i].getAttribute("pval"+newpt);
			}
		}
	},
	tl_submitcover: function(){
		intid=0;
		retstr="&tl_submitcover=";
		if (tmpobj=document.getElementById("tl_detailid")){
			if (tmpobj) intid=tmpobj.value;
		}
		if (intid){
			retstr+=intid;
		}else retstr+="0";
		inputlist=document.getElementsByTagName("INPUT"); 
		for (i=0;i<inputlist.length;i++){
			if (inputlist[i].className=='tl_qty'){
				if(inputlist[i].name=="tl_eqty"&&inputlist[i].value>0){
					if (!(inputlist[i].getAttribute("dbid"))){
						alert("No dbid found for inputlist #: "+i);
						return;
					}
					retstr+="&eqty["+inputlist[i].getAttribute("dbid")+"]="+inputlist[i].value;
					if (tmpobj=document.getElementById("tl_eprice"+inputlist[i].getAttribute("dbid"))){
						if (tmpobj)retstr+="&eprice["+inputlist[i].getAttribute("dbid")+"]="+tmpobj.value;
					}else alert("(js) submitcover: No price field found for qty "+i+" with id:"+inputlist[i].getAttribute("dbid"));
				}
			}
		}
		if (tmpobj=document.getElementById("tl_txtcqty")) retstr+="&cqty="+tmpobj.value;
		if (tmpobj=document.getElementById("tl_txtcprice")) retstr+="&cprice="+tmpobj.value;
		if (tmpobj=document.getElementById("tl_chktax")) if(tmpobj.checked) retstr+="&taxable=1";
		if (tmpobj=document.getElementById("tl_itemnotes")) retstr+="&notes="+escape(tmpobj.value);
		if (tmpobj=document.getElementById("tl_submitbutton")){ tmpobj.disabled=true;tmpobj.value='Loading..';}
		priceid=2;
		if(radiolist=document.radiopriceid.tl_cprice){
			for(i=0;i<radiolist.length;i++){
				if (radiolist[i].checked==true)priceid=radiolist[i].getAttribute("priceid");
			}
		}
		retstr+="&priceid="+priceid;
		this.sendreq(retstr);
	},
	tl_removeeditfield: function(){
		if (tmpobj=document.getElementById("floatydiv")){
			if (tmpobj){
				tmpobj.parentNode.removeChild(tmpobj);
				if (tmpobj=document.getElementById("invItemWin")){
					//if itemwin is still here, set the jump key function again
					if (tmpobj) this.tl_setkey();
				}
			}
		}
	},
	tl_chkeditfield: function(event, objin){
		funcs.cancelbubble(event);
		//esc
		if (event.keyCode==27) this.removeeditfield();
		//enter
		if (event.keyCode==13){
			this.tl_removeeditfield();
			extrastr="";
			if (objin.getAttribute("subkey")) extrastr+="&subkey="+objin.getAttribute("subkey");
			if (objin.getAttribute("dbid")) extrastr+="&changeid="+objin.getAttribute("dbid");
			this.sendreq("&tl_savefield=1&typeid="+objin.getAttribute("typeid")+"&value="+escape(objin.value)+extrastr);
		}
	},
	tl_setkey:function(){
		document.onkeydown=this.tl_chkkey;
	},
	tl_chkkey:function(event){
		if (!(event)) event=window.event;
		var k;
		document.all ? k =event.keyCode : k = event.which;
		if (k==13){
			//enter
			if (tl.tl_memobj) tl.tl_click(tl.tl_memobj);
			return;
		}
		
		var character = String.fromCharCode(k);
		if (tl.tl_memkey) mystr=tl.tl_memkey+character;
		else mystr=character;
		//funcs.msgbox(mystr);
		yoff=0;
		tl.tl_memkey=mystr;
		mylist=funcs.mygebn("tl_item", "tr");
		for(i=0;i<mylist.length;i++){
			if (mylist[i].getAttribute("item")){
				thisstr=mylist[i].getAttribute("item");
				if (thisstr.substr(0,mystr.length).toUpperCase()==mystr.toUpperCase()){
					if (tl.tl_memobj) tl.tl_memobj.style.backgroundColor='';
					tl.tl_memobj=mylist[i];
					tl.tl_memobj.style.backgroundColor="#7799AA";
					yoff=mylist[i].offsetTop;
					document.getElementById("tl_treelist").scrollTop=yoff;
					break;
				}
			}
		}
		clearTimeout(tl.tl_memkeytimer);
		tl.tl_memkeytimer=setTimeout("tl.tl_clearkeymem();", 1500);
	},
	tl_clearkeymem:function(){
		clearTimeout(this.tl_memkeytimer);
		if (this.tl_memobj) this.tl_memobj.style.backgroundColor="";
		this.tl_memkey='';
		this.tl_memobj=null;
	},
	tl_DDconvert:function(){
		$(".tl_picdd").msDropDown();
	}
};
