// JavaScript Document

var main = new Object();
var field = new Object();



main.checkboxClick = function(elementName) {
	if (document.getElementById(elementName).checked) document.getElementById(elementName).checked = false;
	else document.getElementById(elementName).checked = true;
};

main.topMenuOver = function(curr, menuId, menuWidth, menuX, menuY, splitNum) {
	if (!main.topMenu[menuId].length) return;
	clearTimeout(main.topMenuTimer);
	try {
		document.getElementById('showBG').removeChild(main.menuDiv);
	} catch(error) {
	}
	if (!splitNum) splitNum = 3;
	main.menuDiv = document.createElement('div');
	main.menuDiv.style.position = "absolute";
	main.menuDiv.style.zIndex = 12;
	var pos = main.getObjectPos(curr);
	main.menuDiv.style.left = pos[0]+menuX+"px";
	main.menuDiv.style.top = pos[1]+menuY+"px";
	var menuTable = document.createElement('table');
	menuTable.className = "topMenuBg";
	menuTable.width = menuWidth;
	if (window.addEventListener) {
		menuTable.addEventListener("mouseover", main.topMenuBgOver, false);
		menuTable.addEventListener("mouseout", main.topMenuBgOut, false);
	} else {
		menuTable.attachEvent("onmouseover", main.topMenuBgOver);
		menuTable.attachEvent("onmouseout", main.topMenuBgOut);
	}
	var menuTbody = document.createElement('tbody');
	var menuTr = document.createElement('tr');

	itemCount = 0;
	for(menuKey in main.topMenu[menuId]) {
		//menuId = main.topMenu[menuId][menuKey]['dataId'];
		if ((itemCount > 0) && ((itemCount % splitNum) == 0)) {
			menuTbody.appendChild(menuTr);
			var menuTr = document.createElement('tr');
			var menuTd = document.createElement('td');
			menuTd.colSpan = splitNum;
			var menuHr = document.createElement('hr');
			menuHr.className = "topMenuHr";
			menuTd.appendChild(menuHr);
			menuTr.appendChild(menuTd);
			menuTbody.appendChild(menuTr);
			var menuTr = document.createElement('tr');
		}
		var menuTd = document.createElement('td');
		var menuFont = document.createElement('font');
		menuFont.className = "topMenuFont";
		menuFont.style.cursor = "pointer";
		menuFont.innerHTML = main.topMenu[menuId][menuKey]['name'];
		menuFont.sendUrl = main.topMenu[menuId][menuKey]['url'];
		if (window.addEventListener) {
			menuFont.addEventListener("mouseover", main.topMenuItemOver, false);
			menuFont.addEventListener("mouseout", main.topMenuItemOut, false);
			menuFont.addEventListener("click", main.topMenuItemClick, false);
		} else {
			menuFont.attachEvent("onmouseover", main.topMenuItemOver);
			menuFont.attachEvent("onmouseout", main.topMenuItemOut);
			menuFont.attachEvent("onclick", main.topMenuItemClick);
		}
		menuTd.appendChild(menuFont);
		menuTr.appendChild(menuTd);
		itemCount++;
	}
	menuTbody.appendChild(menuTr);
	menuTable.appendChild(menuTbody);
	main.menuDiv.appendChild(menuTable);
	document.getElementById('showBG').appendChild(main.menuDiv);
};

main.topMenuOut = function() {
	clearTimeout(main.topMenuTimer);
	main.topMenuTimer = setTimeout("main.topMenuRemove()", 500);
};

main.topMenuRemove = function() {
	try {
		document.getElementById('showBG').removeChild(main.menuDiv);
	} catch(error) {
	}
}

main.topMenuBgOver = function(e) {
	var evn = e.target || e.srcElement;
	clearTimeout(main.topMenuTimer);
};

main.topMenuBgOut = function(e) {
	var evn = e.target || e.srcElement;
	main.topMenuOut();
};

main.topMenuItemOver = function(e) {
	var evn = e.target || e.srcElement;
	evn.className = "topMenuFontOver";
	clearTimeout(main.topMenuTimer);
};

main.topMenuItemOut = function(e) {
	var evn = e.target || e.srcElement;
	evn.className = "topMenuFont";
	main.topMenuOut();
};

main.topMenuItemClick = function(e) {
	var evn = e.target || e.srcElement;
	
	if (evn.sendUrl) {
		location.href = evn.sendUrl;
	}
};

main.getObjectPos = function(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
};


main.convertStringToXml = function(string) {  
	if (window.ActiveXObject) {  
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  
		xmlDoc.async="false";  
		xmlDoc.loadXML(string);
		return xmlDoc;
	} else {  
		parser=new DOMParser();  
		xmlDoc=parser.parseFromString(string,"text/xml");
		return xmlDoc;  
	}  
};

main.buttonClick = function(e) {
	var evn = e.target || e.srcElement;
	if (evn.sendUrl) eval(evn.sendUrl);
};

main.firstFocus = function(elementId) {
	main.firstFocusTimes = 0;
	main.focusObject = document.getElementById(elementId);
	main.focusObject.hasFocus = false;
	if (window.addEventListener) {
		main.focusObject.addEventListener("focus", main.fieldCheckFocus, false);
	} else {
		main.focusObject.attachEvent("onfocus", main.fieldCheckFocus);
	}
	setTimeout("main.firstFocusTimer()", 100);
};

main.firstFocusTimer = function() {
	if (!main.focusObject.hasFocus) {
		main.focusObject.focus();
		if (main.firstFocusTimes > 10) return;
		setTimeout("main.firstFocusTimer()", 500);
		main.firstFocusTimes++;
	}
};

main.fieldCheckFocus = function(e) {
	var evn = e.target || e.srcElement;
	evn.hasFocus = true;
};

main.windowClose = function(windowId) {
	var windowDiv = document.getElementById(windowId);
	windowDiv.style.display = "none";
};

main.windowOpen = function(windowTitle, getPath) {
	windowControl.windowOpen('selectCat');
	var oldTable = document.getElementById('selectCatTable');
	if (oldTable) document.getElementById('selectCat').removeChild(oldTable);

	var tableBg = document.createElement('table'); // create backgroud table
	tableBg.setAttribute('id','selectCatTable');
	tableBg.className = "windowBg";
	tableBg.width = 680;

	var theadBg = document.createElement('thead');
	var trBg = document.createElement('tr');
	var tdBg = document.createElement('td');
	tdBg.width = 680;
	tdBg.height = 30;
	trBg.appendChild(tdBg);
	theadBg.appendChild(trBg);
	tableBg.appendChild(theadBg);  // create backgroud table *** end

	var tableTitle = document.createElement('table'); // create title and icons table
	tableTitle.width = 650;
	tableTitle.align = "right";
	tableTitle.className = "windowTitleTable";
	var theadTitle = document.createElement('thead');
	var trTitle = document.createElement('tr');
	var tdTitle = document.createElement('td');
	tdTitle.width = 650 - 68;
	tdTitle.height = 30;
	//tdTitle.setAttribute('background', "image/layout_page_en/popupIconTitle.png");
	//tdTitle.style.paddingLeft = "60px";
	if (windowTitle) tdTitle.innerHTML = windowTitle;
	tdTitle.className = "windowTitleFont";
	tdTitle.windowId = 'selectCat';
	tdTitle.style.cursor = "move";
	if (window.addEventListener) {
		tdTitle.addEventListener("mousedown", windowControl.dragStart, false);
	} else {
		tdTitle.attachEvent("onmousedown", windowControl.dragStart);
	}
	trTitle.appendChild(tdTitle);
	tdTitle = document.createElement('td');
	tdTitle.width = 68;
	tdTitle.style.textAlign = "right";
	tdTitle.style.paddingRight = "10px";
	//tdTitle.setAttribute('background', "image/icon/closeIndex.gif");
	var buttonClose = document.createElement('img');
	buttonClose.src = "image/icon/closeIndex.gif";
	buttonClose.border = 0;
	buttonClose.sendUrl = "main.windowClose('selectCat')";
	buttonClose.style.cursor = "pointer";
	if (window.addEventListener) {
		buttonClose.addEventListener("click", main.buttonClick, false);
	} else {
		buttonClose.attachEvent("onclick", main.buttonClick);
	}
	tdTitle.appendChild(buttonClose);
	trTitle.appendChild(tdTitle);
	theadTitle.appendChild(trTitle);
	tableTitle.appendChild(theadTitle);
	tdBg.appendChild(tableTitle);

	trBg = document.createElement('tr');
	tdBg = document.createElement('td');
	trBg.appendChild(tdBg);
	theadBg.appendChild(trBg);
	
	trBg = document.createElement('tr');
	tdBg = document.createElement('td');
	tdBg.setAttribute('id', 'selectCatDiv');
	trBg.appendChild(tdBg);
	theadBg.appendChild(trBg);

	trBg = document.createElement('tr');
	tdBg = document.createElement('td');
	tdBg.innerHTML = "&nbsp;";
	trBg.appendChild(tdBg);
	theadBg.appendChild(trBg);

	document.getElementById('selectCat').appendChild(tableBg);
	if (getPath) XMLHttp.sendReq('POST', getPath, '', '', "selectCatDiv", 'send', 0, 0);
};

main.setDivLocation = function(curr, img, top, left) {
	if (!top) top = 0
	if (!left) left = 0;
	var pos = main.getObjectPos(document.getElementById(curr));
	if (pos[0] == 0) {
		setTimeout("main.setDivLocation('"+curr+"', '"+img+"', "+top+", "+left+")", 100);
		return;
	}
	var winDiv = document.createElement('div');
	winDiv.style.position = "absolute";
	winDiv.style.top = pos[1]+top+"px";
	winDiv.style.left = pos[0]-left+"px";
	var winImg = document.createElement('img');
	winImg.src = img;
	winImg.border = 0;
	winDiv.appendChild(winImg);
	document.body.appendChild(winDiv);
};




