/**
 * Utils
 */

///////////////////////////////////////////////////////////////////////////////
// ??????????Щ???

/**
 * ???????????? ?????Щ????????????????????.
 * @author ????
 */
var XDefaultEventHandler = {
	/**	????????????????? */
	onContextMenu : function () {
		var event = window.event;
		var ctrlKey = event.ctrlKey;
		 
		if (event.srcElement.tagName == "INPUT") {
			var typeName = event.srcElement.getAttribute("type")
			typeName = typeName.toString().toLowerCase();

			if (typeName == "button" || typeName == "	" || typeName == "reset") {
				return ctrlKey;
			}
			return true;

		} else if (event.srcElement.tagName == "TEXTAREA") {
			return true;
		} 
		return false;
	},

	/* ???????????????????? */
	onSelectStart : function () {
		var event = window.event;
		var ctrlKey = event.ctrlKey;
		if (event.srcElement.tagName == "INPUT") {
			var typeName = event.srcElement.getAttribute("type")
			typeName = typeName.toString().toLowerCase();

			if (typeName == "button" || typeName == "sumbit" || typeName == "reset") {
				return ctrlKey;
			}
			return true;

		} else if (event.srcElement.tagName == "TEXTAREA") {
			return true;
		}else{
			return false;
		}
	},

	/** ??????. */
	onDragStart : function () {
		var event = window.event;
		var ctrlKey = event.ctrlKey;
		if (event.srcElement.tagName == "INPUT") {
			var typeName = event.srcElement.getAttribute("type")
			typeName = typeName.toString().toLowerCase();

			if (typeName == "button" || typeName == "sumbit" || typeName == "reset") {
				return ctrlKey;
			}
			return true;

		} else if (event.srcElement.tagName == "TEXTAREA") {
			return true;
		}
		return ctrlKey;
	},

	init : function () {
		document.oncontextmenu = this.onContextMenu;
		document.onselectstart = this.onSelectStart;
		document.ondragstart   = this.onDragStart;
	}
};

XDefaultEventHandler.init();

///////////////////////////////////////////////////////////////////////////////
// Lite javascript lib

var XVision = function(selector, context) {
	
};

XVision.fn = XVision.prototype = {
	version : "3.1",
	all : function(selector) {
		if (!selector) {
			return null;
		}
		return document.getElementById(selector);
	},
	empty : function(node) {
		
	}
}



///////////////////////////////////////////////////////////////////////////////
// PopupWindow class


var XWindowManager = {
	curDragPopWindow : null,
	curDragOffX : 0,
	curDragOffY : 0,
	dragPopWindow : function () {
		if (this.curDragPopWindow) {
			this.curDragPopWindow.style.pixelLeft = Math.max(0, event.clientX - this.curDragOffX);
			this.curDragPopWindow.style.pixelTop  = Math.max(0, event.clientY - this.curDragOffY);
		} else {
			return false;
		}
	},
	startDragPopWindow : function(dlgItem) {

		var mainFrame = this.findPopWindowFrame(dlgItem);
		if (!mainFrame) {
			return;
		}

		if (window.captureEvents) {
			window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP)

			document.onmousemove = function () {
				XWindowManager.dragPopWindow();
			}

			document.onmouseup = function() {
				XWindowManager.stopDragPopWindow();
			}
		} else {
			event.srcElement.setCapture();
		}

		this.curDragOffX = event.clientX - mainFrame.style.pixelLeft;
		this.curDragOffY = event.clientY - mainFrame.style.pixelTop;
		this.curDragPopWindow = mainFrame;

	},
	stopDragPopWindow : function() {
		if (window.releaseEvents) {
			window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP)
		} else {
			event.srcElement.releaseCapture();
		}

		this.curDragPopWindow = null;
		this.curDragOffX = 0;
		this.curDragOffY = 0;
	},
	findPopWindowFrame : function (dlgItem) {
		while (dlgItem) {
			if (dlgItem.className == "popupWindow") {
				 return dlgItem;
			}
			dlgItem = dlgItem.parentElement || dlgItem.parentNode;
		}
		return null;
	},
	closePopWindow : function (dlgItem) {
		var mainFrame = this.findPopWindowFrame(dlgItem);
		if (mainFrame) {
			mainFrame.style.display = "none";
		}
	}
};

function initPopWindow(wnd) {
	if (wnd) {
		wnd.style.pixelLeft = 0;
		wnd.style.pixelTop = 0;
	}
}

function dragPopWindow() {
	XWindowManager.dragPopWindow();
}

function startDragPopWindow(dlgItem) {
	XWindowManager.startDragPopWindow(dlgItem);
}

function stopDragPopWindow() {
	XWindowManager.stopDragPopWindow();
}

function closePopWindow(dlgItem) {
	XWindowManager.closePopWindow(dlgItem);
	pageNormal();
}

function showPopWindow(wnd, left, top, width, height) {
	if (!wnd || wnd.className != "popupWindow") {
		return false;
	}
	
	if (width) {
		wnd.style.width = width + "px";
	}
	
	if (height) {
		wnd.style.height = height + "px";
	}
	
	var xMax = document.body.clientWidth; // screen.width;
	var yMax = screen.height; // document.body.clientHeight;

	var xOffset = Math.max(0, (xMax - width) / 2);
	var yOffset = Math.max(0, (yMax - height) / 2);

	if (left) {
		wnd.style.left = xOffset + "px";
	}
	
	if (top) {
		wnd.style.top = yOffset + document.documentElement.scrollTop - 80 + "px";
		
		var os = getOs();
		if (os != "Firefox" && os != "MSIE")
		{
			wnd.style.top = yOffset + document.body.scrollTop - 80 + "px";
		}
	}

	wnd.style.display = "block";
	pageDisabled();
	return true;
}

function pageDisabled()
{
	var topDiv = document.getElementById("topDiv");
	if (!topDiv)
	{
		var body = document.body;
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id", "topDiv");
		newDiv.className = "topDiv";
		newDiv.style.width = document.body.clientWidth + "px";
		newDiv.style.height = document.body.clientHeight + "px";
		body.appendChild(newDiv);
		return;
	}
	topDiv.style.display = "block";
}

function pageNormal()
{
	var topDiv = document.getElementById("topDiv");
	if (!topDiv)
		return;

	topDiv.style.display = "none";
}

///////////////////////////////////////////////////////////////////////////////
// ????????????????

function showInfo(info) 
{
	if (!info || info == "") {
		hideLoading();
		return;
	}

	var infobar = document.getElementById("infobar");
	if (!infobar) {
		infobar = document.createElement("DIV");
		infobar.id = "infobar";
		infobar.style.display = "none";
		document.body.appendChild(infobar);
		
		infobar.style.left = document.body.offsetWidth / 2 - 150 + "px";
		infobar.style.top = document.body.offsetHeight / 2 - 10 + "px";
	}

	if (infobar) {
		infobar.innerHTML = "<div class='info-text'>" + info + "</div>";
		infobar.style.display = "";
	}
}

function showLoadding() {
	showLoading();
}

function showLoading() {
	if (loading)
		showInfo(loading);
	else
		showInfo("正在处理您的请求，请稍候...");
}

function hideLoading() {
	var infobar = document.getElementById("infobar");
	if (infobar) {
		infobar.innerHTML = "";
		infobar.style.display = "none";
	}
}

function showLoadPanel()
{
	var loadPanel = document.getElementById("load-panel");
	var loadbar = document.getElementById("loadbar");
	if (loadbar)
	{
		loadPanel.removeChild(loadbar);
	}
	loadbar = document.createElement("DIV");
	loadbar.id = "loadbar";
	loadbar.className = "loadbar";
	var img = document.createElement("IMG");
	img.src = "../images/load.gif";
	loadbar.appendChild(img);
	loadPanel.appendChild(loadbar);
}

function hiddenLoadPanel()
{
	var loadPanel = document.getElementById("load-panel");
	var loadbar = document.getElementById("loadbar");
	if (loadbar)
	{
		loadPanel.removeChild(loadbar);
	}
}

var errorTimeoutCounter = 0;

function showError(errorText)
{
	var errorbar = document.getElementById("errorbar");
	if (!errorbar) {
		errorbar = document.createElement("DIV");
		errorbar.id = "errorbar";
		errorbar.style.display = "none";
		document.body.appendChild(errorbar);
	}
	
	if (!errorText || errorText == "") {
		errorbar.innerHTML = "";
		errorbar.style.display = "none";

	} else {
		var closeButton = '<div class="closeButton" title="Close"><a href="#" onclick="showError(null);">X</a></div>';
		errorbar.innerHTML = closeButton + errorText;
		errorbar.style.display = "";
		var timeoutId = errorTimeoutCounter++;
		errorbar.timeoutId = timeoutId;
		setTimeout(function () { if (errorbar.timeoutId == timeoutId) errorbar.style.display = "none"; },  25000);
	}
}

///////////////////////////////////////////////////////////////////////////////


var VisionLangManager = new Object() ;

VisionLangManager.VisionLang = null;


/** ????????????. */
VisionLangManager.AvailableLanguages = 
{
	'en'		: 'English',
	'zh-tw'		: 'Chinese Traditional',
	'zh-cn'		: 'Chinese Simplified'
}

VisionLangManager.HTMLEncode = function(text)
{
	if (!text) {
		return '';
	}

	text = text.replace(/&/g, '&amp;');
	text = text.replace(/</g, '&lt;');
	text = text.replace(/>/g, '&gt;');

	return text;
}

/** ????????????????????. */
VisionLangManager.GetActiveLanguage = function()
{
	if (this.AutoDetectLanguage ) {
		var userLang;
		
		// IE accepts "navigator.userLanguage" while Gecko "navigator.language".
		if (navigator.userLanguage ) {
			userLang = navigator.userLanguage.toLowerCase();

		} else if (navigator.language) {
			userLang = navigator.language.toLowerCase();

		} else {
			// Firefox 1.0 PR has a bug: it doens't support the "language" property.
			return VisionConfig.DefaultLanguage;
		}
		
		// Some language codes are set in 5 characters, 
		// like "pt-br" for Brasilian Portuguese.
		if (userLang.length >= 5) {
			userLang = userLang.substr(0, 5) ;
			if (this.AvailableLanguages[userLang]) return userLang;
		}
		
		// If the user's browser is set to, for example, "pt-br" but only the 
		// "pt" language file is available then get that file.
		if (userLang.length >= 2) {
			userLang = userLang.substr(0, 2) ;
			if (this.AvailableLanguages[userLang]) return userLang;
		}
	}
	
	return this.DefaultLanguage;
}

/** ????????????. */
VisionLangManager.TranslateElements = function(targetDocument, tag, propertyToSet, encode)
{
	var elements = targetDocument.getElementsByTagName(tag);
	var langKey, langValue;
	for (var i = 0; i < elements.length; i++) {
		/*if (langKey = elements[i].getAttribute('langid')) {
			if (langValue = VisionLangManager.VisionLang[langKey]) {
				if (encode)	langValue = this.HTMLEncode(langValue);
				eval('elements[i].' + propertyToSet + ' = langValue');
			}
		}*/

		langKey = elements[i].className;
		if (!langKey) {
			continue;
		}
		var keys = langKey.split(":");
		if (keys[0] == "langid") {
			langKey = keys[1]; 
			if (langValue = this.VisionLang[langKey]) { 
				if (encode)	{
					langValue = this.HTMLEncode(langValue);
				}
				langValue = unescape(langValue);
				eval('elements[i].' + propertyToSet + ' = langValue');
			}
		}
	}
}

/** ????????????. */
VisionLangManager.TranslatePage = function(targetDocument)
{
	if (!this.VisionLang) {
		return;
	}

	this.TranslateElements(targetDocument, 'INPUT',  'value');
	this.TranslateElements(targetDocument, 'SPAN',	 'innerHTML');
	this.TranslateElements(targetDocument, 'LABEL',  'innerHTML');
	this.TranslateElements(targetDocument, 'OPTION', 'innerHTML', true ) ;
}

/** ?????. */
VisionLangManager.Initialize = function()
{
	this.DefaultLanguage = 'en';
	this.AutoDetectLanguage = "true";

	var langName = GetCookie("lang"); 
	if (!langName) {
		langName = this.GetActiveLanguage();
	}

	this.ActiveLanguage = new Object() ;
	this.ActiveLanguage.Code = langName;
	this.ActiveLanguage.Name = this.AvailableLanguages[this.ActiveLanguage.Code];

	if (this.ActiveLanguage.Code == "zh-cn" && typeof VisionLangZhCn != "undefined")  {
		this.VisionLang = VisionLangZhCn;

	} else if (this.ActiveLanguage.Code == "zh-tw" && typeof VisionLangZhTw != "undefined") {
		this.VisionLang = VisionLangZhTw;

	} else {
		this.VisionLang = null;
	}	
}

VisionLangManager.ChangeLanguage = function(langName)
{
	if (langName) {
		SetCookie("lang", langName,"2150-1-1");
		LoadLanguage();
      
		if (window.parent) {
			window.parent.location.reload(true);
		} else {
			window.location.reload(true);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////

function loadLanguageFile(lang) {
	
	if (!lang || lang == "undefined") {
		// IE accepts "navigator.userLanguage" while Gecko "navigator.language".
		if (navigator.userLanguage) {
			lang = navigator.userLanguage.toLowerCase();

		} else if (navigator.language) {
			lang = navigator.language.toLowerCase();

		} else {
			// Firefox 1.0 PR has a bug: it doens't support the "language" property.
			lang = "en";
		}
		
		// Some language codes are set in 5 characters, 
		// like "pt-br" for Brasilian Portuguese.
		if (lang.length >= 5) {
			lang = lang.substr(0, 5) ;
		}
	}
	if (lang == "zh_CN"){
		lang = "zh-cn"
	}else if(lang == "zh_TW"){
		lang = "zh-tw";
	}
	return lang;
}

function LoadLanguage()
{    
	// TranslatePage current page
	VisionLangManager.Initialize();
	VisionLangManager.TranslatePage(document);

	if (document.getElementById["langSelect"]) {
		try {
			document.getElementById["langSelect"].value = VisionLangManager.ActiveLanguage.Code;
		} catch (e) {
		}
	}
}

function T(key, defaultValue) {
	 
	if (!defaultValue) {
		defaultValue = key;
	}

	if (!VisionLangManager.VisionLang) {
		return defaultValue;
	}

	if (!key || key == "") {
		return defaultValue;
	}

	key = key.replace(/[ \t]+/g, "");
	return  unescape(VisionLangManager.VisionLang[key] || defaultValue);
}

///////////////////////////////////////////////////////////////////////////////

var XVisionUtils = {};

/** 显示分页导航条 */
XVisionUtils.showPager = function(pagerContainer, curPage, pageSize, totalCount, imgPath)
{
	var div = document.getElementById(pagerContainer);
	if (div) {
		div.innerHTML = "";
	}
	
	if (!totalCount || totalCount <= 0) {
		totalCount = 0;
		startIndex = endIndex = 0;

	} else {
		var startIndex	= (curPage - 1) * pageSize + 1;
		var endIndex	= startIndex + pageSize - 1;
		var pageCount	= parseInt((totalCount - 1) / pageSize) + 1;
		if (curPage == pageCount) {
			endIndex = totalCount;
		}
	}
	
	div.innerHTML += "<div id='total'>"+startIndex+" - "+endIndex +" / "+totalCount+"</div>";

	var html = "";
	if (pageCount > 0) {
		html += "<a href='javascript:onGotoPage(1, " + pageSize + ");'>" + 
				"<img src='"+((imgPath) ? imgPath : "")+"images/first1.gif' align='absmiddle' /></a>";
		
		var cp = curPage - 1;
		if (cp < 1)
		{
			cp = 1;
		}
		html += "<a href='javascript:onGotoPage("+cp+", "+pageSize+")'>" + 
				"<img src='"+((imgPath) ? imgPath : "")+"images/pre1.gif' align='absmiddle'/></a>";
	}

	var offset = 5;
	var list = [];

	// 计算要显示的页码号. 一般显示当前页前后五页的链接.
	if (curPage - offset > 10) {
		list.push(curPage - offset - 10);
	}

 	for (var k = curPage - offset; k < curPage + offset - 1; k++) {
		if (k >= 0 && k < pageCount) {
			list.push(k);
		}
	}

	if (curPage + offset + 8 < pageCount) {
		list.push(curPage + offset + 8);
	}

	var length = list.length;
	var page = 0;

	for (var k = 0; k < length; k++) {
		page = list[k] + 1;
		if (page == curPage)
		{
			html += "<a class='curPageNumber' href='javascript:onGotoPage(" + page + ", " + pageSize + ");'>" + page + "</a>";
		}
		else
		{
			html += "<a href='javascript:onGotoPage(" + page + ", " + pageSize + ");'>" + page + "</a>";
		}
	}

	// 转到最后一页
	if (pageCount > 0)
	{
		var cp = curPage + 1;
		if (cp > pageCount)
		{
			cp = pageCount;
		}
		html += "<a href='javascript:onGotoPage("+cp+", " + pageSize + ")'>" + 
				"<img src='"+((imgPath) ? imgPath : "")+"images/next1.gif' align='absmiddle' /></a>";
		
		html += "<a href='javascript:onGotoPage(" + pageCount + ", " + pageSize + ");'>" + 
				"<img src='"+((imgPath) ? imgPath : "")+"images/last1.gif' align='absmiddle' /></a>";
	}
	
	div.innerHTML += "<div id='pages'>"+html+"</div>";
}

String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function setCurFocus(index)
{
	var list = document.getElementsByName("li");
	if (list && list.length)
	{
		for (var i = 0; i < list.length; i++)
		{
			list[i].className = (i == index) ? "curFocus" : "";
		}
	}
}

function getObjFromResult(result, inx)
{
	if (!result) {
		return null;
	}

	if (inx >= 0 && inx < result.length) {			
		return result[inx];
	}
	
	return null;
}

function getOs() 
{ 
	var OsObject = ""; 
	if(navigator.userAgent.indexOf("MSIE")>0) { 
		return "MSIE"; 
	}
	
	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0) { 
		return "Firefox"; 
	}
	
	if(isSafari=navigator.userAgent.indexOf("Safari")>0) { 
		return "Safari"; 
	}
	
	if(isCamino=navigator.userAgent.indexOf("Camino")>0) { 
		return "Camino"; 
	}
	
	if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0) { 
		return "Gecko"; 
	} 
}

Array.prototype.append = function(obj) {
	var appendObj = new Array(obj);
	return this.concat(appendObj);
}

Array.prototype.remove = function(obj) {
	var removeObj = new Array();
	for(i = 0; i < this.length; i++) {
		if(this[i] != obj) 
			removeObj = removeObj.append(this[i]);
	}
	return removeObj;
}

