// JavaScript Document
//functions for H-Central


//Globala variabler för hcentral

var activeHC = -1; //hid
//var HCreg = new Array(); //hid som nyckel till arrayer med egenskaper, status etc ANVÄNDS EJ ?!!


//alt 2, spara hela aktiva diven globalt
var HCE;

//var testtop=40;
//var testleft=40;

//var tord = new Array("no","first","second","third","fourth");
var tord = new Array("no","1:st","2:nd","3:rd","4:th");

function openHC(phid,hid,uniint){
	openHC2(phid,hid,uniint,"");
}


function openHC2(phid,hid,uniint,mode){
	
	//alert("openHC phid:"+phid+", hid:"+hid+", uniint:"+uniint);
	//Kolla allra först om vi redan har en hc för detta h
	var hc;
	//var elemid = "hcdiv_"+hid;
	//hc=document.getElementById("hcdiv_"+hid);
//alert("elemid="+elemid+", hc="+hc);
	
	if((hc=document.getElementById("hctab_"+hid))!=null){
		/*
			if(hc.style.zIndex != highz){
				highz = highz+1;
				hc.style.zIndex	= highz;
			}
		*/
		focusHC(hid);
		
		hc.style.visibility = "visible";
		activeHC = hid;
		//alert("oldHC z="+hc.style.zIndex);
		closehcmenu2(phid);
		return;
	}
	
	if(typeof ae != "object"){
		ae = new Ace.Engine();
	}
	var url = incpath+"hanzicentral/hcentral.php?act=hc";
	
	if(uniint!=""){
		url+="&uniint="+uniint;
	}if(hid!=""){
		url+="&hid="+hid;
	}if(phid!=""){
		url+="&phid="+phid;
	}
	if(mode==""){
		mode="A"; //normal mode, B=strokes animation
	}
	//alert("url="+url);
	var args = new Array();
	args[0] = mode;
	var req = new Ace.Request(Ace.Method.Get, url, null, null,createHC, args, Ace.CallbackOption.ReadyStateComplete,120);
	ae.invoke(req,600); //Response stored in cache for 10 minutes
	
	//Stäng en eventuell hc_menu om vi har phid
	closehcmenu2(phid);
}

function openHCList(hid,p1,type,mode,p2,p3){
	//alert("openHCList hid:"+hid+", p1:"+p1+", type:"+type+", mode:"+mode+", p2:"+p2+", p3:"+p3);
	
	
	//Kolla om vi redan har en hclist för detta h
	if(mode=="0" && (type=="ph" || type=="r" || type=="s")){
		if(document.getElementById("hcl_"+hid+"_"+p1+"_ph_0")!=null || document.getElementById("hcl_"+hid+"_"+p1+"_r_0")!=null || document.getElementById("hcl_"+hid+"_"+p1+"_s_0")!=null){
			closehcmenu2(hid);
			return;
		}
	}else if(document.getElementById("hcl_"+hid+"_"+p1+"_"+type+"_"+mode)!=null){
		closehcmenu2(hid);
		return;
	}
	
	var hcl;

	if(typeof ae != "object"){
		ae = new Ace.Engine();
	}
	var url = incpath+"hanzicentral/hcentral.php?act=hcl";;
	
//	if(p1!=""){
//		url+="&p1="+p1;
//	}
	if(hid!=""){
		url+="&hid="+hid;
	}
	if(type!=""){
		url+="&type="+type;
	}
	if(mode!=""){
		url+="&mode="+mode;
	}
	if(type=="p"){
		if(p2!=""){
			url+="&pin="+p2;
		}
		if(p3!=""){
			url+="&tone="+p3;
		}
	}else{
		if(p1!=""){
			url+="&uniint="+p1;
		}
	}
	if(type=="s"){
		if(p2!=""){
			url+="&dt="+p2;
		}
		if(p3!=""){
			url+="&partsix="+p3;
		}
	}
	//alert("url="+url);
	var args = [hid,p1,type,mode,p2,p3];
	var req = new Ace.Request(Ace.Method.Get, url, null, null,createHCList, args, Ace.CallbackOption.ReadyStateComplete,120);
	ae.invoke(req,600); //Response stored in cache for 10 minutes
	
	//Stäng en eventuell hc_menu
	closehcmenu2(hid);
}

//Callback function
function createHC(resp,args){
	
	//Try to do this as simple as possible!!!
	//alert(resp.text);	
	//Vi får hid från data
	var mode = args[0];

	//alert("createHC, hid="+hid);
	var data = parseResp(resp);
	
	/*
	data is an array:
	data['status']= 0/1
	data['phid'] = '' / parent hid
	data['ui']=uniint
	data['hid']=hid
	data['pin']=pinyin
	data['tone']=tone
	data['fp']=html full pinyin
	data['def']=definition
	data['strks']=number of strokes
	data['hasstro'] 0/1 strokes animation
	data['hassound'] 0/1 
	data['freq']=frequency //gör något av detta som är meningsfullt för anv. Klasser??
	data['divt']=divtype
	data['parts'][0]=23456; data['parts'][1]=33456; etc
	data['trad'][0]=23456; data['trad'][1]=33456;  etc
	data['vari']  -""-
	data['wrivar']  -""-
	data['radi']  -""-
	data['phon']  -""-
	data['info'] //list of text rows
	data['subh'][0][0]=hid; data['subh'][0][1]=pinyin; data['subh'][0][2]=tone; data['subh'][0][3]=fullpin; data['subh'][0][4]=def
	data['subh'][1][0]=hid; data['subh'][1][1]=pinyin; data['subh'][1][2]=tone; data['subh'][1][3]=fullpin; data['subh'][1][4]=def
	*/
	//alert("createHC, phid="+data['phid']);
	//if(data['phid']!="" && document.getElementById("hcmenu_"+data['phid'])!=null){ //We open HC from another HC where we have an open hc_menu
		//closehcmenu2(data['phid']);
	//}
	
	if(data['status']== "0"){
		
		//Something more fancy than an alert if we want, like an empty HC
		alert("This hanzi or part is not in the database.");
		return;
	}
	
	var hid = data["hid"];
	var phid = data["phid"]; //hid for parent HC when we open HC from HC
	
	//We must check for an open HC here too, even if we also check directly on openHC to avoid the ajax request
	//This is for cases when we open HC from uniint, e.g from a part in another HC - we don't have hid until here
	if((hc=document.getElementById("hctab_"+hid))!=null){
		focusHC(hid);
		hc.style.visibility = "visible";
		activeHC = hid;
		return;
	}
	
	var th, tb, td, row, elem;
	
	var ourWidth = 250;
	
	//Calculate position to place the div
	var xoffs = 275;
	var yoffs = -50;
	var left=(clickX-parseInt(ourWidth)+xoffs)+"px";
    var top=(clickY+yoffs)+"px"; 
	
	highz = highz+1;
	
	var toptab = document.createElement("TABLE");
	toptab.id = "hctab_"+hid;
	//alert("createHC, toptab.id="+toptab.id);
	toptab.style.top = top;
	toptab.style.left = left;
	toptab.style.width = ourWidth+"px";
	toptab.style.zIndex = highz;
	//toptab.style.backgroundColor = "#f9f9f9";
	toptab.style.backgroundColor = "#ffffff";
	
	toptab.className = "hctab";
	toptab.setAttribute("border","0");
	toptab.setAttribute("cellSpacing","0");
	toptab.setAttribute("cellPadding","0");
	
	th = document.createElement("THEAD");
	row = document.createElement("TR");
	
	td = document.createElement("TD");
	td.id = "hctit_"+hid;
	td.className = "hctitletd";
	td.title = "Drag me!";
	
	//Ev testa att skippa dessa ilayer & layer, för N4 mfl äldre browsers??
	var ilay = document.createElement("ILAYER");
	ilay.setAttribute("width","100%");
	ilay.onselectstart="return false";
	var lay = document.createElement("LAYER");
	lay.setAttribute("width","100%");
	lay.onmouseover="drag=true";
	lay.onmouseout="drag=false";
	lay.appendChild(document.createTextNode("Hanzi Central"));
	
	ilay.appendChild(lay);
	td.appendChild(ilay);
	
	row.appendChild(td);
	
	td = document.createElement("TD");
	td.id = "hcclose_"+hid;
	td.className = "hcclosetd";
	
	elem = document.createElement("A");
	elem.setAttribute("href","javascript:closeHC("+hid+")");
	//elem.onclick = "closeHC("+hid+");return false";
	elem.appendChild(document.createTextNode("X"));
	
	td.appendChild(elem);
	row.appendChild(td);
	
	th.appendChild(row);
	toptab.appendChild(th);
	
	tb = document.createElement("TBODY");
	tb.id = "hctb_"+hid;
	row = document.createElement("TR");
	
	td = document.createElement("TD");
	td.id = "hccont_"+hid;
	td.className = "hccontenttd";
	td.setAttribute("colSpan","2");
	td.appendChild(hc_createContentTab(data,mode));
	
	row.appendChild(td);
	tb.appendChild(row);
	toptab.appendChild(tb);
	
	document.body.appendChild(toptab);

	//swfobject.embedSWF(incpath+"flash/emff_silk_button.swf", data['pin']+data['tone'], "18", "18", "7",false, {src:incpath+"sound/"+data['pin']+data['tone']+".mp3"}, {movie:incpath+"flash/emff_silk_button.swf"}, {title:"Listen to "+data['pin']+data['tone']},false);
	swfobject.embedSWF(incpath+"flash/emff_silk_button.swf", data['pin']+data['tone'], "18", "18", "7",false, {src:incpath+"sound/"+data['pin']+data['tone']+".mp3"}, {movie:incpath+"flash/emff_silk_button.swf"}, false,false);
}

function hc_createContentTab(data,mode){
	
	var i, tab, tb, td, row, elem, elem2, elem3;
	
	var conttab = document.createElement("TABLE");
	
	//conttab.setAttribute("width","100%");
	conttab.setAttribute("border","0");
	//Cellspacing=2
	conttab.setAttribute("cellSpacing","2");
	conttab.setAttribute("cellPadding","0");
	var conttb = document.createElement("TBODY");
	conttb.id = "hcconttb_"+data["hid"];
	conttab.appendChild(conttb);
	row = document.createElement("TR");
	conttb.appendChild(row);
	td = document.createElement("TD");
	td.setAttribute("colSpan","2");
	row.appendChild(td);
	
	tab = document.createElement("TABLE");
	tb = document.createElement("TBODY");
	tab.appendChild(tb);
	tab.setAttribute("width","100%");
	tab.setAttribute("border","0");
	tab.setAttribute("cellSpacing","0");
	tab.setAttribute("cellPadding","0");
	td.appendChild(tab);
	
	row = document.createElement("TR");
	tb.appendChild(row);
		
	td = document.createElement("TD");
	td.className = "hc_himgmain";
	//Main himg
	elem = document.createElement("IMG");
	elem.setAttribute("src",incpath+"images/72/"+data["ui"]+".gif");
	
	//elem.setAttribute("onmousedown","hc_menu("+data["hid"]+","+data["ui"]+",'g')");	
	elem.onmouseover = hc_movhimg;
	elem.onmouseout = hc_mouhimg;
	elem.onmousedown = hc_mdhimg;
	//elem.name = "g_"+data["hid"]+"_"+data["ui"]+"_"+data["pin"]+"_"+data["tone"];
	elem.name = "m_"+data["hid"]+"_"+data["ui"];
	td.appendChild(elem);
	if(data["strks"]!==""){
		td.appendChild(document.createElement("BR"));
		td.appendChild(document.createTextNode(data["strks"]+" strokes"));
	}
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);
	
	//modified below
	elem = document.createElement("DIV");
	elem.className = "hc_pinmain";
	elem.onmouseover = hc_movhimg;
	elem.onmouseout = hc_mouhimg;
	elem.onmousedown = hc_mdhimg;
	elem.name = "p_"+data["hid"]+"_"+data["pin"]+"_"+data["tone"]+"_"+data["fp"]+"_"+data["hid"];
	elem.appendChild(document.createTextNode(decodeFP(data["fp"])));
	td.appendChild(elem);
	
	if(data['hassound']==1 || data['hasstro']==1){
		elem = document.createElement("DIV");
		elem.className = "hc_ctrl";
		td.appendChild(elem);
		if(data['hasstro']==1){
			elem2 = document.createElement("IMG");
			elem2.id = "hcpenimg_"+data["hid"];
			elem2.setAttribute("src",incpath+"images/icon/pagebrush.png");
			elem2.setAttribute("title","Stroke order");
			elem2.setAttribute("border","0");
			
			elem3 = document.createElement("A");
			elem3.id = "hcpena_"+data["hid"];
			elem3.setAttribute("href","javascript:hcToModeB('"+data["ui"]+"','"+data["hid"]+"')");
			elem3.appendChild(elem2);		
			elem.appendChild(elem3);

			if(data['hassound']==1){
				elem.appendChild(document.createTextNode("  "));
			}
		}
		if(data['hassound']==1){
			elem2 = document.createElement("DIV");
			elem2.setAttribute("id",data["pin"]+data["tone"]);
			elem3 = document.createElement("SPAN");
			elem3.setAttribute("title","No flash player");
			elem3.appendChild(document.createTextNode("--"));
			elem2.appendChild(elem3);
			elem.appendChild(elem2);
					
		}
	}
	elem = document.createElement("DIV");
	elem.className = "hc_defmain";
	elem.appendChild(document.createTextNode(data["def"]));
	td.appendChild(elem);

//Översta raden, colspan=2, med himg och def klar, dags för ny rad 
	if(mode=="A"){
		conttb.appendChild(hc_createLowerRowA(data));
	}else if(mode=="B"){
		conttb.appendChild(hc_createLowerRowB(data["ui"],data["hid"]));
	}
	
   	
	if(data["info"].length > 0){
		//Sista raden, med övrig info
		row = document.createElement("TR");
		row.id = "hcinforow_"+data["hid"];
		conttb.appendChild(row);
		td = document.createElement("TD");
		td.className = "hc_info";
		td.setAttribute("colSpan","2");
		row.appendChild(td);
		for(i=0;i<data["info"].length;i++){
			if(i>0){
				td.appendChild(document.createElement("BR"));
			}
			td.appendChild(document.createTextNode(data["info"][i]));
		}
	}	
	return conttab;
}

//Lower row A: normal view
function hc_createLowerRowA(data){
	
	var td,td0,td1,tab,tb,elem;
	
	var mainrow = document.createElement("TR");
	mainrow.id = "hclowrowA_"+data["hid"];
	
	//td0 is left column td, td1 is right col td
	td0 = document.createElement("TD");
	td0.setAttribute("width","80%");
	td1 = document.createElement("TD");
	mainrow.appendChild(td0);
	mainrow.appendChild(td1);
	
	//if(data["subh"].length > 0 || data["divt"]!="-1"){ //Has subh or structure
		//tab now additional meanings tab
	tab = document.createElement("TABLE");
	tb = document.createElement("TBODY");
	tab.appendChild(tb);
	//tab.setAttribute("width","100%");
	tab.setAttribute("border","0");
	tab.setAttribute("cellSpacing","0");
	tab.setAttribute("cellPadding","0");
	tab.setAttribute("width","100%");
	tab.className = "hc_subh";
	td0.appendChild(tab);
	//}
	if(data["subh"].length > 0){ //Has subh
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		td.setAttribute("colSpan","2");
		td.className = "hctitle2";
		row.appendChild(td);
		//if(data["subh"].length == 1){
		td.appendChild(document.createTextNode("Also"));
		//td.appendChild(document.createTextNode("Other pinyin:"));
		//}else{
			//td.appendChild(document.createTextNode("Other meanings:"));
		//}
		
		//Loop through all subindex
		//Use hid, subh[i][0], in a link to hc for each subh
	//alert("subh.length="+data["subh"].length);
		for(i=0;i<data["subh"].length;i++){
			row = document.createElement("TR");
			tb.appendChild(row);
			td = document.createElement("TD");
			td.className = "subhpin";
			row.appendChild(td);
			
			elem = document.createElement("SPAN");
			td.appendChild(elem);
			//elem.className = "hc_pinmain";
			elem.onmouseover = hc_movhimg;
			elem.onmouseout = hc_mouhimg;
			elem.onmousedown = hc_mdhimg;
			//elem.name = "p_"+data["hid"]+"_"+data["subh"][i][1]+"_"+data["subh"][i][2]+"_"+data["subh"][i][3];
			elem.name = "p_"+data["hid"]+"_"+data["subh"][i][1]+"_"+data["subh"][i][2]+"_"+data["subh"][i][3]+"_"+data["subh"][i][0];
			
			elem.appendChild(document.createTextNode(decodeFP(data["subh"][i][3])));
	
			//td.appendChild(document.createTextNode(decodeFP(data["subh"][i][3])));
			td = document.createElement("TD");
			td.className = "subhdef";
			row.appendChild(td);
			td.appendChild(document.createTextNode(data["subh"][i][4]));
		}
	} //End has subh
	

	//if(data["divt"]!="-1"){ //Has structure
	//Structure
	row = document.createElement("TR");
	tb.appendChild(row);
	td = document.createElement("TD");
	td.className = "hctitle2";
	td.setAttribute("colSpan","2");
	row.appendChild(td);
	td.appendChild(document.createTextNode("Structure"));
	elem = document.createElement("DIV");
	elem.setAttribute("align","center");
	td0.appendChild(elem);	
	elem.appendChild(hc_createStructureTab(data));
		
	//}

	
	//Second column, charvar - tab
	tab = document.createElement("TABLE");
	tb = document.createElement("TBODY");
	tab.appendChild(tb);
	//tab.setAttribute("width","100%");
	tab.setAttribute("border","0");
	tab.setAttribute("cellSpacing","0");
	tab.setAttribute("cellPadding","0");
	tab.className = "hc_charvar";
	
	td1.appendChild(tab);
	
	//Maybe check that we at least have one variant etc that will create one row
	
	//Traditional
	if(data["trad"].length > 0){
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		td.appendChild(document.createTextNode("Traditional"));
		td.appendChild(document.createElement("BR"));
		//Maybe put different tradvars in separate rows
		//Maybe we need br after each?
		for(i=0;i<data["trad"].length;i++){
			if(i>0){
				td.appendChild(document.createElement("BR"));
			}
			elem = document.createElement("IMG");
			elem.setAttribute("src",incpath+"images/30/"+data["trad"][i]+".gif");
			//elem.onmouseover = hc_movhimg;
			//elem.onmouseout = hc_mouhimg;
			//We don't have many trad hanzi in db, so for now no HC link here
			//elem.onmousedown = hc_mdhimg;
			elem.name = "g_"+data["hid"]+"_"+data["trad"][i];
			td.appendChild(elem);
		}	
	}
	//Variant
	if(data["vari"].length > 0){
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		td.appendChild(document.createTextNode("Variant"));
		td.appendChild(document.createElement("BR"));
		//Maybe put different tradvars in separate rows
		//Maybe we need br after each?
		for(i=0;i<data["vari"].length;i++){
			if(i>0){
				td.appendChild(document.createElement("BR"));
			}
			elem = document.createElement("IMG");
			elem.setAttribute("src",incpath+"images/30/"+data["vari"][i]+".gif");
			elem.onmouseover = hc_movhimg;
			elem.onmouseout = hc_mouhimg;
			elem.onmousedown = hc_mdhimg;
			elem.name = "g_"+data["hid"]+"_"+data["vari"][i];
			td.appendChild(elem);
		}	
	}
	//Writevar
	if(data["wrivar"].length > 0){
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		td.appendChild(document.createTextNode("Writevar"));
		td.appendChild(document.createElement("BR"));
		//Maybe put different tradvars in separate rows
		//Maybe we need br after each?
		for(i=0;i<data["wrivar"].length;i++){
			if(i>0){
				td.appendChild(document.createElement("BR"));
			}
			elem = document.createElement("IMG");
			elem.setAttribute("src",incpath+"images/30/"+data["wrivar"][i]+".gif");
			elem.onmousedown = hc_mdhimg;
			elem.name = "g_"+data["hid"]+"_"+data["wrivar"][i];
			elem.onmouseover = hc_movhimg;
			elem.onmouseout = hc_mouhimg;
			td.appendChild(elem);
		}
	}
	//Radical
	if(data["radi"].length > 0){
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		if(data["radi"].length == 1 || data["radi"][1]==""){
			td.appendChild(document.createTextNode("Radical"));
		}else{
			td.appendChild(document.createTextNode("Radicals"));	
		}
		td.appendChild(document.createElement("BR"));
		//Maybe put different tradvars in separate rows
		//Maybe we need br after each?
		var n=0;
		for(i=0;i<data["radi"].length;i++){
			if(data["radi"][i]==""){
				continue;
			}
			if(n>0){
				td.appendChild(document.createElement("BR"));
			}
			elem = document.createElement("IMG");
			elem.setAttribute("src",incpath+"images/30/"+data["radi"][i]+".gif");
			elem.onmousedown = hc_mdhimg;
			elem.name = "r_"+data["hid"]+"_"+data["radi"][i];
			elem.onmouseover = hc_movhimg;
			elem.onmouseout = hc_mouhimg;
			td.appendChild(elem);
			n++;
		}
	}
	//Phoneticum
	if(data["phon"].length > 0){
		row = document.createElement("TR");
		tb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		td.appendChild(document.createTextNode("Phoneticum"));
		td.appendChild(document.createElement("BR"));
		//Maybe put different tradvars in separate rows
		//Maybe we need br after each?
		for(i=0;i<data["phon"].length;i++){
			if(i>0){
				td.appendChild(document.createElement("BR"));
			}
			elem = document.createElement("IMG");
			elem.setAttribute("src",incpath+"images/30/"+data["phon"][i]+".gif");
			elem.onmousedown = hc_mdhimg;
			elem.name = "ph_"+data["hid"]+"_"+data["phon"][i];
			elem.onmouseover = hc_movhimg;
			elem.onmouseout = hc_mouhimg;
			td.appendChild(elem);
		}
	}
	return mainrow;	
}

//Lower row B: show strokes animation
function hc_createLowerRowB(uniint,hid){
	//alert("hc_createLowerRowB");
	var td,elem,elem2;
	
	var mainrow = document.createElement("TR");
	mainrow.id = "hclowrowB_"+hid;
	
	td = document.createElement("TD");
	td.className = "hcstrkleft";
	mainrow.appendChild(td);
	
	elem = document.createElement("IMG");
	elem.id = "strkanim_"+hid;
	//elem.setAttribute("src","");	
	elem.setAttribute("src",incpath+"images/strokes/"+uniint+".gif");	
	elem.setAttribute("width","180");
	elem.setAttribute("height","180");
	td.appendChild(elem);
	
	td = document.createElement("TD");
	td.className = "hcstrkright";
	mainrow.appendChild(td);
	
	elem = document.createElement("IMG");
	elem.setAttribute("src",incpath+"images/icon/cross.png");
	elem.setAttribute("title","Normal view");
	elem.setAttribute("border","0");
			
	elem2 = document.createElement("A");
	elem2.setAttribute("href","javascript:hcToModeA('"+uniint+"','"+hid+"')");
	elem2.appendChild(elem);		
	td.appendChild(elem2);

	td.appendChild(document.createElement("BR"));
	
	elem = document.createElement("IMG");
	elem.setAttribute("src",incpath+"images/icon/refresh.png");
	elem.setAttribute("title","Show again");
	elem.setAttribute("border","0");
			
	elem2 = document.createElement("A");
	elem2.setAttribute("href","javascript:hcToModeB('"+uniint+"','"+hid+"')");
	elem2.appendChild(elem);		
	td.appendChild(elem2);
	
	//td.appendChild(document.createTextNode(uniint));
		
	return mainrow;	
}

function hc_createStructureTab(data) {

	var td, row, elem;
	var structtab = document.createElement("TABLE");

	//structtab.setAttribute("width","100%");
	structtab.setAttribute("border","0");
	structtab.setAttribute("cellSpacing","0");
	structtab.setAttribute("cellPadding","0");
	structtab.id = "hcstruct_"+data["hid"];
	structtab.className = "hcstructtab";
	
	var structtb = document.createElement("TBODY");
	structtab.appendChild(structtb);
	
	//***** Kod beroende av divtyp *****
	
	//Alla har en första rad och en första cell
	row = document.createElement("TR");
	structtb.appendChild(row);
	td = document.createElement("TD");
	row.appendChild(td);
	
	//td.appendChild(document.createTextNode("divt:"+data["divt"]));
	
	//##### Divtype -1 no structure information ######
	if(data["divt"]==-1){
		td.className = "smalltxt2";
		td.appendChild(document.createTextNode("Structure information coming soon."));
	}
	
	//##### Divtype 0 ######
	if(data["divt"]==0){
		td.className = "smalltxt2";
		td.appendChild(document.createTextNode("Can not be divided into parts."));
	}
	//##### Divtype 1, 3, 6, 7, 8 ######
	else if(data["divt"]==1 || data["divt"]==3 || data["divt"]==6 || data["divt"]==7 || data["divt"]==8){
	
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";		
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 2, 4, 5, 9 ######
	else if(data["divt"]==2 || data["divt"]==4 || data["divt"]==5 || data["divt"]==9){
		
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	  	row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 10 ######
	else if(data["divt"]==10){
		
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	  	td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 11 ######
	else if(data["divt"]==11){
	
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 12 ######
	else if(data["divt"]==12){
		
		td.setAttribute("rowSpan","2");
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);	
	}
	//##### Divtype 13 ######
	else if(data["divt"]==13){
		
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
		td.setAttribute("rowSpan","2");
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);	
	  	row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 14 ######
	else if(data["divt"]==14){
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	  	row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		td.setAttribute("colSpan","2");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 15 ######
	else if(data["divt"]==15){
		
		td.setAttribute("colSpan","2");
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}
	//##### Divtype 16 ######
	else if(data["divt"]==16){
		
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][0]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][0]+"_"+data["divt"]+"_0";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][1]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][1]+"_"+data["divt"]+"_1";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		row = document.createElement("TR");
		structtb.appendChild(row);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][2]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][2]+"_"+data["divt"]+"_2";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
		td = document.createElement("TD");
		row.appendChild(td);
	  	elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+data["parts"][3]+".gif");
		elem.className = "structpartimg";
		elem.name = "s_"+data["hid"]+"_"+data["parts"][3]+"_"+data["divt"]+"_3";
		elem.onmouseover = hc_movhimg;
		elem.onmouseout = hc_mouhimg;
		elem.onmousedown = hc_mdhimg;
		td.appendChild(elem);
	}

  return structtab;

}


function parseResp(resp){
	
	var data = new Array();
	
	//Three types of values simple values and 1- and 2-dim arrays
	//All arrays below data are indexed by numbers
	//When we read the data we know how to interpret each field
	//Array-typer, med tom sträng som tredje fält skall bli tom array
	
	/* data example	
	name || type(0=single value,1= onedim. array, 2= twodim. array) || value1 [|| value2, ..] @@
	name || type(0=single value,1= onedim. array, 2= twodim. array) || value1 [|| value2, ..] @@
	
	ui || 0 || 123456 @@
	subh || 2 || fullp0 || def0 @@	
	subh || 2 || fullp1 || def1 @@
	tradv || 1 || 123456 || 123467 || 123456 || 123467 @@
	*/
	//alert(resp.text);
	var a = resp.text.replace(/^\s+|\s+$/g, '').split("@@");

	for(var i=0;i<a.length;i++){
		a[i] = a[i].split("||");
		
		//Simple name value pairs
		if(a[i][1]==0){
			data[a[i][0]]=a[i][2];
		}
		//1-dimensional array type, create in data here
		else if(a[i][1]==1){
			if(a[i][2].replace(/^\s+|\s+$/g, '') == ""){
				data[a[i][0]] = new Array();	 
			}else{
				data[a[i][0]]= a[i].slice(2);
			}
		}
		//2-dimensional array type, create in data if not exist, and add second dimension
		else{
			if(typeof data[a[i][0]] == "undefined"){
				data[a[i][0]] = new Array(); 
			}
			//Ingenting i andradimensionen för tomma värden
//alert("2:nd dim, name="+a[i][0]+", value='"+a[i][2]+"'");
			if(a[i][2] != ""){
//alert("value add to subh");
				data[a[i][0]][data[a[i][0]].length]= a[i].slice(2);
			}
		}
	}
	return data;
}

function decodeFP(inp){

	//alert("decodeFP");
	//alert("inp:"+inp);

	var fp;
	var i0 = inp.indexOf("&#");
	if(i0 > -1){
		var i1 = inp.indexOf(";");
		fp = inp.substring(0,i0)+String.fromCharCode(inp.substring(i0+2,i1))+inp.substr(i1+1);
	}else{
		fp = inp;
	}
	return fp;
}

function closeHC(hid){
	document.getElementById("hctab_"+hid).style.visibility = "hidden";
}

function focusHC(hid){
	var hc = document.getElementById("hctab_"+hid);
	if(hc.style.zIndex != highz){
		highz = highz+1;
		hc.style.zIndex	= highz;
	}
}

//Callback function
function createHCList(resp,args){
	
	//args = [hid,p1,type,mode,p2,p3];
	//For type = p: p1 = fp, p2 = pinyin, p3 = tone
	//For type = g,g,r,ph: p1 = uniint, p2 = -, p3 = -
	
	var phid = args[0];
	var p1 = args[1];
	var type = args[2]; 
	var mode = args[3];
	var pin = args[4];
	var tone = args[5];
	
	pin = pin.replace(/v/,"ü");
	
	//alert("createHCList   phid:"+phid+", p1:"+p1+", type:"+type+", mode:"+mode+", pin:"+pin+", tone:"+tone);
	
	//Stäng hcmenu
	//closehcmenu2(phid);
	
	//Redan kollat detta - bör inte behöva läggas här på response istället?
	//if(mode=="0" && (type=="ph" || type=="r" || type=="s")){
		//if(document.getElementById("hcl_"+phid+"_"+p1+"_ph_0")!=null || document.getElementById("hcl_"+phid+"_"+p1+"_r_0")!=null || document.getElementById("hcl_"+phid+"_"+p1+"_s_0")!=null){
			//return;
		//}
	//}else if(document.getElementById("hcl_"+phid+"_"+p1+"_"+type+"_"+mode)!=null){
		//return;
	//}
	
	if(document.getElementById("hctab_"+phid)==null){
		return;	
	}
	
	
	//alert("createHCList, resp.text="+resp.text);
	var data = parseResp(resp);
	
	/*
	data is an array:
	data['hanzi'][0][0]=hid; data['hanzi'][0][1]=uniint; data['hanzi'][0][2]=fullpin; data['hanzi'][0][3]=def
	data['hanzi'][1][0]=hid; data['hanzi'][1][1]=uniint; data['hanzi'][1][2]=fullpin; data['hanzi'][1][3]=def
	*/
	
	if(data['status']== "0"){
		//Something more fancy than an alert if we want, like an empty HC
		alert("There is no relation for this query.");
		return;
	}
	
	var row,td,elem;
	
	var hctb = document.getElementById("hctb_"+phid);
	
	//Every hclist gets a new row with a table
	//Containing row and cell
	var hclrow = document.createElement("TR");
	hclrow.id = "hcl_"+phid+"_"+p1+"_"+type+"_"+mode;
	td = document.createElement("TD");
	td.setAttribute("colSpan","2");
	hclrow.appendChild(td);
	
	//Table for this hclist
	var hcltab = document.createElement("TABLE");
	td.appendChild(hcltab);
	
	//structtab.setAttribute("width","100%");
	hcltab.setAttribute("border","0");
	hcltab.setAttribute("cellSpacing","0");
	hcltab.setAttribute("cellPadding","0");
	hcltab.className = "hcltab";
	
	var hcltb = document.createElement("TBODY");
	hcltab.appendChild(hcltb);
	
	/* title row with close */
	row = document.createElement("TR");
	hcltb.appendChild(row);
	td = document.createElement("TD");
	row.appendChild(td);
	/* title depending on type and mode, style from hcmenu */
	td.className = "hcmenutit";
	if(type=="m" || type=="g" || type=="r" || type=="s" || type=="ph"){
		//td.appendChild(document.createTextNode("Hanzi which has  "));	
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+p1+".gif");
		td.appendChild(elem);
		if(mode=="0"){
			td.appendChild(document.createTextNode(" as a part ("+data["hanzi"].length+")"));	
		}
		else if(mode=="1"){
			if(type=="r"){
				td.appendChild(document.createTextNode(" as radical ("+data["hanzi"].length+")"));
			}else if(type=="ph"){
				td.appendChild(document.createTextNode(" as phoneticum ("+data["hanzi"].length+")"));
			}else if(type=="s"){
				td.appendChild(document.createTextNode(" as a part, same position ("+data["hanzi"].length+")"));
			}
		}
	}
	else if(type=="p"){
		//td.appendChild(document.createTextNode("Hanzi with pinyin "));
		td.appendChild(document.createTextNode("Pinyin "));
		if(mode=="0"){
			//td.appendChild(document.createTextNode(decodeFP(p1)));
			td.appendChild(document.createTextNode(decodeFP(p1)+" - "+tord[tone]+" tone ("+data["hanzi"].length+")"));
		}else if(mode=="1"){
			td.appendChild(document.createTextNode(pin+" - any tone ("+data["hanzi"].length+")"));
		}
	}
	
	/* close cell, style from hcmenu */
	td = document.createElement("TD");
	row.appendChild(td);
	td.className = "hcmenuclose";
	elem = document.createElement("A");
	elem.setAttribute("href","javascript:closehcl("+phid+",'"+p1+"','"+type+"',"+mode+")");
	elem.appendChild(document.createTextNode("Close"));
	td.appendChild(elem);
	
	/* the list */
	row = document.createElement("TR");
	hcltb.appendChild(row);
	td = document.createElement("TD");
	row.appendChild(td);
	td.setAttribute("colSpan","2");
	
	var hcldiv = document.createElement("DIV");
	td.appendChild(hcldiv);
	hcldiv.className = "hcldiv";
	
	if(typeof data["hanzi"]=="undefined"){
		hcldiv.appendChild(document.createTextNode("No hanzi found"));
	}else{
		for(i=0;i<data["hanzi"].length;i++){
			elem = document.createElement("A");
			elem.setAttribute("href","javascript:openHC('"+phid+"','"+data["hanzi"][i][0]+"',"+data["hanzi"][i][1]+")");
			elem1 = document.createElement("IMG");
			elem.appendChild(elem1);
			elem1.setAttribute("title",data["hanzi"][i][3]);
			elem1.setAttribute("border","0");
			elem1.setAttribute("src",incpath+"images/30/"+data["hanzi"][i][1]+".gif");
			elem1.setAttribute("id","openhc_"+data["hanzi"][i][0]);
			elem1.setAttribute("onmouseover","hc_mark(this)");
			elem1.setAttribute("onmouseout","hc_umark(this)");
			hcldiv.appendChild(elem);
			if(type!="p" || mode!="0"){ //show no pinyin for pin+tone match
				hcldiv.appendChild(document.createTextNode(" "+decodeFP(data["hanzi"][i][2])+"  "));
			}else{
				hcldiv.appendChild(document.createTextNode("  "));
			}
		}
		//hcldiv.appendChild(document.createTextNode(" "));
	}
	
	hctb.appendChild(hclrow);
	
}

function hc_menu(inpars){
	
	//alert("hc_menu("+inpars+")");
	var params = inpars.split("_");
	var type = params[0];
	var phid = params[1];
	
	//Params
	//type=g,r,ph, (hanzi)
	//p_phid_uniint
	
	//type=s (hanzi-structure)
	//p_phid_uniint_dt_partsix
	
	//type=p, pinyin
	//p_phid_pin_tone_fp_hid
	
	
	if(document.getElementById("hcmenu_"+phid)!=null){
		return;	
	}
	
	var td,row,tb,elem,elem1;
	
	var xoffs = -10;
	var yoffs = -10;
    
	var left=(clickX+xoffs)+"px"; 
	var top=(clickY+yoffs)+"px"; 
	
	//alert("l:"+left+", t:"+top);
	
	highz = highz+1;
	//var hcmenudiv = document.createElement("DIV");
	
	var hcmenutab = document.createElement("TABLE");
	hcmenutab.onmouseout = closehcmenu;
	//hcmenutab.setAttribute("onmouseout","closehcmenu()");
	hcmenutab.id = "hcmenu_"+phid;
	hcmenutab.style.top = top;
	hcmenutab.style.left = left;
	//hcmenutab.style.width = ourWidth+"px";
	hcmenutab.style.zIndex = highz;
	//hcmenutab.style.backgroundColor = "#f9f9f9";
	hcmenutab.style.backgroundColor = "#ffffff";
	
	hcmenutab.className = "hcmenu";
	hcmenutab.setAttribute("border","0");
	hcmenutab.setAttribute("cellSpacing","0");
	hcmenutab.setAttribute("cellPadding","0");
		
	tb = document.createElement("TBODY");
	
	//First row for character other than main - this HC hanzi
	if(type=="g" || type=="s" || type=="r" || type=="ph" || (type=="p" && params[5]!=phid)){
		row = document.createElement("TR");
		td = document.createElement("TD");
		//td.className = "hcmenutit";
		td.className = "hcmenurow";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		elem = document.createElement("A");
		if(type=="p" && params[5]!=phid){ //Open HC on pinyin for subh
			elem.setAttribute("href","javascript:openHC('"+phid+"','"+params[5]+"','')");
		}else{
			elem.setAttribute("href","javascript:openHC('"+phid+"','',"+params[2]+")");
		}
		elem.appendChild(document.createTextNode("Open Hanzi Central for  "));
		if(type=="p" && params[5]!=phid){ //Open HC on pinyin for subh
			elem.appendChild(document.createTextNode(decodeFP(params[4])));
		}else{
			elem1 = document.createElement("IMG");
			elem.appendChild(elem1);
			elem1.setAttribute("src",incpath+"images/30/"+params[2]+".gif");
			elem1.setAttribute("border","0");
		}
		td.appendChild(elem);
		row.appendChild(td);
		tb.appendChild(row);
	}
	
	//Title row for character
	if(type=="m" || type=="g" || type=="s" || type=="r" || type=="ph"){
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenutit";
		td.appendChild(document.createTextNode("Show hanzi which has  "));
		
		elem = document.createElement("IMG");
		elem.setAttribute("src",incpath+"images/30/"+params[2]+".gif");
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
			
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+","+params[2]+",'"+type+"','0','-','-')");
		//td.appendChild(document.createTextNode("as a part"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+","+params[2]+",'"+type+"','0','-','-')");
		elem.appendChild(document.createTextNode("as a part"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
		
	}
	//Title and first rows for pinyin
	else if(type=="p"){
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenutit";
		td.appendChild(document.createTextNode("Show hanzi with pinyin"));
		row.appendChild(td);
		tb.appendChild(row);
		
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+",'"+params[4]+"','"+type+"','0','"+params[2]+"','"+params[3]+"')");
		//td.appendChild(document.createTextNode(decodeFP(params[4])+" - "+tord[params[3]]+" tone"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+",'"+params[4]+"','"+type+"','0','"+params[2]+"','"+params[3]+"')");
		elem.appendChild(document.createTextNode(decodeFP(params[4])+" - "+tord[params[3]]+" tone"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
		
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+",'"+params[2]+"','"+type+"','1','"+params[2]+"','"+params[3]+"')");
		//td.appendChild(document.createTextNode(params[2]+" - any tone"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+",'"+params[2]+"','"+type+"','1','"+params[2]+"','"+params[3]+"')");
		elem.appendChild(document.createTextNode(params[2].replace(/v/,"ü")+" - any tone"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);		
	}
	
	if(type=="r" || type=="m"){
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+","+params[2]+",'r','1','-','-')");
		//td.appendChild(document.createTextNode("as radical"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+","+params[2]+",'r','1','-','-')");
		elem.appendChild(document.createTextNode("as radical"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
	}
	
	if(type=="ph" || type=="m"){
		row = document.createElement("TR");
		td = document.createElement("TD");	
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+","+params[2]+",'ph','1','-','-')");
		//td.appendChild(document.createTextNode("as phoneticum"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+","+params[2]+",'ph','1','-','-')");
		elem.appendChild(document.createTextNode("as phoneticum"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
	}
	
	else if(type=="s"){	
		row = document.createElement("TR");
		td = document.createElement("TD");
		td.className = "hcmenurow";
		td.name = "hc_menu";
		td.setAttribute("onmouseover","hc_mark(this)");
		td.setAttribute("onmouseout","hc_umark(this)");
		//td.setAttribute("onmousedown","openHCList("+phid+","+params[2]+",'"+type+"','1','"+params[3]+"','"+params[4]+"')");
		//td.appendChild(document.createTextNode("as a part in this position"));
		
		elem = document.createElement("A");
		elem.setAttribute("href","javascript:openHCList("+phid+","+params[2]+",'"+type+"','1','"+params[3]+"','"+params[4]+"')");
		elem.appendChild(document.createTextNode("as a part in this position"));
		td.appendChild(elem);
		
		row.appendChild(td);
		tb.appendChild(row);
	}
	
	row = document.createElement("TR");
	tb.appendChild(row);
	td = document.createElement("TD");
	row.appendChild(td);
	td.className = "hcmenuclose";
	elem = document.createElement("A");
	elem.setAttribute("href","javascript:closehcmenu2("+phid+")");
	elem.appendChild(document.createTextNode("Close"));
	td.appendChild(elem);
	
	hcmenutab.appendChild(tb);
	document.body.appendChild(hcmenutab);

}

function hcToModeB(ui,hid){
	//alert("hcToModeB, ui:"+ui+" hid:"+hid);
	//Creates / Opens / Reloads strokes animation
	var Brow,strkanim;
	var tb = document.getElementById("hcconttb_"+hid);
	
	if(document.getElementById("hclowrowB_"+hid)!=null){
		Brow = document.getElementById("hclowrowB_"+hid);
	}else{
		Brow = hc_createLowerRowB(ui,hid);
		tb.insertBefore(Brow,document.getElementById("hcinforow_"+hid));
	}
	if(document.getElementById("hclowrowA_"+hid)!=null){
		document.getElementById("hclowrowA_"+hid).style.display="none";
	}
	Brow.style.display="block";	
	//Set img src for animation
	strkanim = document.getElementById("strkanim_"+hid);	
	//If we run into cache problems
	//var d = new Date();
	//var s = incpath+"images/strokes/"+ui+".gif?"+d.getTime();
	//strkanim.setAttribute("src",s);
	strkanim.setAttribute("src",incpath+"images/strokes/"+ui+".gif");
}

function hcToModeA(ui,hid){
	//alert("hcToModeA, ui:"+ui+" hid:"+hid);
	
	var Arow,penimg,pena;
	if(document.getElementById("hclowrowA_"+hid)!=null){
		Arow = document.getElementById("hclowrowA_"+hid);
	}else{
		Arow = hc_createLowerRowA(ui,hid);
	}
	if(document.getElementById("hclowrowB_"+hid)!=null){
		document.getElementById("hclowrowB_"+hid).style.display="none";
	}
	Arow.style.display="block";	
/*	
	pena = document.getElementById("hcpena_"+hid);	
	pena.setAttribute("href","javascript:hcToModeB('"+ui+"','"+hid+"')");
	penimg = document.getElementById("hcpenimg_"+hid);	
	penimg.setAttribute("src",hc_path+"images/icon/pageedit.png");
*/
}
/* mouseover for general himg in hc
function hc_movhimg_g(e){
	//alert("hc_movhimg_g");
  var thisE=isIE ? event.srcElement : e.target;
  thisE.style.backgroundColor="#cccccc";
  //thisE.style.border="#000 3px solid";
  //thisE.onmousedown=hc_mdhimg_g;
}
 */
/* mouseover for himg in hc */
function hc_movhimg(e){
	//alert("hc_movhimg_g");
  var thisE=isIE ? event.srcElement : e.target;
  thisE.style.backgroundColor="#ff6";
  //thisE.style.backgroundColor="#cccccc";
  //thisE.style.border="#000 3px solid";
  //thisE.onmousedown=hc_mdhimg_s;
}

/* mouseout for general himg and structure himg in hc */
function hc_mouhimg(e){
  var thisE=isIE ? event.srcElement : e.target;
  thisE.style.backgroundColor="";
  //thisE.style.border="none";
  //thisE.onmousedown="";
}
function closehcmenu(e){
		
  var thisE=isIE ? event.srcElement : e.target;
  
  if(thisE.tagName=="TABLE"){
  	removehcmenu(thisE);
  }
  //par.removeChild(thisE);
  //thisE.style.border="none";
  //thisE.onmousedown="";
}

function closehcmenu2(hid){
	//alert("closehcmenu2("+hid+")");
	//Stäng en eventuell hc_menu
	if(document.getElementById("hcmenu_"+hid)!=null){
		document.body.removeChild(document.getElementById("hcmenu_"+hid));
	}
}

function removehcmenu(tab){
	document.body.removeChild(tab);	
}

function closehcl(hid,p1,type,mode){
	document.getElementById("hctb_"+hid).removeChild(document.getElementById("hcl_"+hid+"_"+p1+"_"+type+"_"+mode));
}

/* mousedown for himg in hc */
function hc_mdhimg(e){
	var thisE=isIE ? event.srcElement : e.target;
	clickX=isIE ? event.x : window.pageXOffset+e.clientX;
    clickY=isIE ? event.y : window.pageYOffset+e.clientY;
	//Open menu
	hc_menu(thisE.name);
}
/* mousedown for structure himg in hc
function hc_mdhimg_s(e){
	var thisE=isIE ? event.srcElement : e.target;
	//Open menu
	hc_menu(thisE,"s");
}
 */


