var lastID
var defaultLabelWindowX = 0, defaultLabelWindowY = 0

function xescape(str) {
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/"/g, "&quot;") // "
	str = str.replace(/'/g, "&#039;") // '
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");

	return str;
}

function lmo(id){
	// find template div or span
	var templates = grabTemplateNames(window.event.srcElement)
	setStatus(templates, id)
	lastID = id
	window.event.srcElement.focus()
	var aElement = findAElement(window.event.srcElement)
	if (aElement!=null) {
		// check if there is Label dialog open
		if (labelWindow==null || labelWindow.closed) {
			// focus the link
			aElement.onkeypress=lkp
			aElement.onmouseover=lamo
			aElement.focus()
		}
	}
	window.event.returnValue = true
}
function grabTemplateNames(elem) {
	var templates = ""
	while (elem!=null && elem.tagName.toUpperCase()!='BODY') {
		if (elem.tagName.toUpperCase()=='DIV' || elem.tagName.toUpperCase()=='SPAN' || elem.tagName.toUpperCase()=='TBODY') {
			if (elem.id!=null && elem.id.indexOf('.tpl')>0) {
				templates = elem.id + " > " + templates 
			}
		}	
		elem = elem.parentElement
	}
	return templates
}
function setStatus(templates, id) {
	window.status = templates+id 
}

function findAElement(elem) {
	while ((elem.parentElement.tagName.toUpperCase()!='A' || elem.parentElement.href=='') && elem.parentElement.tagName.toUpperCase()!='BODY') {
		elem = elem.parentElement
	}
	if (elem.parentElement.tagName.toUpperCase()!='A') {
		return null
	}
	return elem.parentElement
}

function lamo(){
	setStatus (grabTemplateNames(window.event.srcElement), lastID)
}

function lmu(id){
	lastID = window.status=''
	window.event.returnValue = true
}
function lkp(){
	if (window.event.keyCode==69 || window.event.keyCode==101) { // 'E', 'e'
		if (lastID!='') showLabelForm(lastID)
		window.event.returnValue = true
	} else 	if (window.event.keyCode==67 || window.event.keyCode==99) { // 'C', 'c'
		clipboardData.setData('Text', lastID)
	}

}
function lmc(id){
	if (findAElement(window.event.srcElement)!=null) {
		return // inside <a>
	}
	showLabelForm(id)
	window.event.returnValue = true
}
function setLabel(){
	// find all spans having onMouseOver=lom('labelWindow.lf.val.value')
	allElem = document.body.getElementsByTagName("SPAN")
	for (a=0; a < allElem.length; a++) {
		if (allElem[a].onmouseover!=null) {
			if (allElem[a].id == labelID) {
				allElem[a].innerHTML = labelWindow.lf.val.value
				lng_labels[labelID] = xescape(labelWindow.lf.val.value);
			}
		}
	}	
	// document.all[labelWindow.lf.name.value].innerHTML = labelWindow.lf.val.value
}

var labelWindow
var labelID
var initialValue
function showLabelForm(id){
	if (labelWindow!=null && !labelWindow.closed) {
		labelWindow.close()
	}
	if (defaultLabelWindowX==0) {
		defaultLabelWindowX = window.screenLeft+100
		defaultLabelWindowY = window.screenTop+100
	}
	labelWindow = window.open("","labelWnd","width=450, height=60, left="+defaultLabelWindowX+", top="+defaultLabelWindowY)
	labelID = id
	setTimeout("fillLabelWindow()",100)
}
function fillLabelWindow(){
	var labelText = "";
	var content = "<HTML><HEAD><TITLE>Label Dialog</TITLE><META http-equiv='Content-Type' content='text/html; charset="+page_charset+"'></HEAD><body style='margin:5px' bgcolor=\"#FFFBD3\">"
	content += "<form name=lf action=\""+catalogs.admin+"/set_label.php\" method=post onSubmit=\"window.opener.setLabel()\" accept-charset=\""+page_charset+"\">"
	content += "<table cellspacing=0 cellpadding=0 id=tbl><tr style=\"font-size:12px;font-weight:bold\"><td>name</td><td>value</td></tr><tr valign=top><td style=\"font-size:12px\">"
	allElem = document.body.getElementsByTagName("SPAN")
	labelText = lng_labels[labelID];
	initialValue = labelText
	content += labelID+"&nbsp;&nbsp;</td><td>"
	if (labelText.length>40 || labelText.match(/[\n\r]/)) {
		content += "<textarea name=\"val\" cols=\"50\" rows=\"5\">"+labelText+"</textarea>"
	} else {
		content += "<input type=\"text\" name=\"val\" value=\""+labelText+"\" size=\"50\">"
	}
	content += "</td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" name=\"name\" value='"+labelID+"'><input type=\"hidden\" name=\"lang\" value='"+store_language+"'>"
	content += "<a href=\"javascript:window.opener.setLabel()\"><img align=\"middle\" border=\"0\" width=\"23\" height=\"22\" src=\""+catalogs.images+"/preview.gif\">&nbsp;preview</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:window.opener.setLabel();document.lf.submit()\"><img align=\"middle\" border=\"0\" width=\"23\" height=\"22\" src=\""+catalogs.images+"/save.gif\">&nbsp;save</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:clipboardData.setData('Text', '"+labelID+"');wndClose()\"><img align=\"middle\" border=\"0\" width=\"23\" height=\"22\" src=\""+catalogs.images+"/copy.gif\">&nbsp;copy name</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:restoreLabel();wndClose()\"><img align=\"middle\" border=\"0\" width=\"23\" height=\"22\" src=\""+catalogs.images+"/cancel.gif\">&nbsp;cancel</a>"
	content += "<script>function wndClose(){rememberXY();window.close()}</script>";
	content += "<script>function rememberXY() { window.opener.defaultLabelWindowX = window.screenLeft; window.opener.defaultLabelWindowY = window.screenTop; }</script>"
    content += "<script>function restoreLabel() { document.forms['lf'].val.value=window.opener.initialValue; window.opener.setLabel() }</script>"
	content += "</td></tr></table></form>";
	content += "<script language=\"javascript\">resizeTo(tbl.clientWidth+30, tbl.clientHeight+60);</script>";
	content += "</body></html>"

	labelWindow.document.write(content)
	labelWindow.document.charset=page_charset;
	labelWindow.document.close()
	labelWindow.document.lf.val.focus()
	labelWindow.document.lf.val.select()
}

function copyName() {
	clipboardData.setData("Text", labelID)
	window.close()
}
function dmo() {
	if (window.status=='') {
		window.status = grabTemplateNames(window.event.srcElement)
	}
}
function dmu() {
	window.status = ''
}
// functions to run from debug console

// make a border around the template
function markTemplate(tmplt, remove){
	// find all spans and divs having name=tmplt
	var tags = new Array()
	tags[0] = "SPAN"
	tags[1] = "DIV"
	tags[2] = "TBODY"
	var i
	for(i=0; i<tags.length; i++){
	var allelem = document.body.getElementsByTagName(tags[i])
	for (a=0; a < allelem.length; a++) {
		if (allelem[a].id!=null && allelem[a].id==tmplt) {
			if (allelem[a].id==tmplt) {
				if (tags[i]=='TBODY') {
					if (remove) {
						borderStyle = "0pt none black"
					} else {
						borderStyle = "1pt solid black"
					}	
					var firstRow = allelem[a].children[0]
					for (col=0;col<firstRow.children.length;col++) firstRow.children[col].style.borderTop = borderStyle
					var lastRow = allelem[a].children[allelem[a].children.length-1]
					for (col=0;col<lastRow.children.length;col++) lastRow.children[col].style.borderBottom = borderStyle

					lastRow.borderBottom = borderStyle
					var j
					for (j=0; j<allelem[a].children.length; j++) {
						var child = allelem[a].children[j]
						child.children[0].style.borderLeft = borderStyle
						child.children[child.children.length-1].style.borderRight = borderStyle
					}
				} else {	
					markElement(allelem[a], remove)
				}
			}
		}
	}
	}
}

function markElement(elem, remove) {
	if (remove) {
		elem.style.border = 'none'
		elem.style.borderWidth="0pt"
	} else {
		elem.style.border = 'solid'
		elem.style.borderWidth="1pt"
		elem.style.borderColor='black'
	}
}

function tmo(tmplt) {
	markTemplate(tmplt, 0)
}
function tmu(tmplt) {
	markTemplate(tmplt, 1)
}

