String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.replaceAll = function(s1, s2) {
	var s = this;
	if (s1 == s2)
		return s;
	
	while(s.indexOf(s1) != -1) {
		s = s.replace(s1, s2);
	}
	return s;	
}

function SimpleMap() {
	this.items = new Array();
	this.put = function put(key, value) {
		this.items.push([key, value]);
	}
	
	this.get = function get(key) {
		for (var i=0; i<this.items.length; i++) {
			if (this.items[i][0] == key) 
				return this.items[i][1];
		}
		return null;
	}
}

function doRequest(url) {
	var x = "";
	var uuUrl;
    loadingShow();
	if (url.indexOf("?") >=0)
		uuUrl = url + "&" + Math.random() + "&xcharset=中文";
	else
		uuUrl = url + "?" + Math.random() + "&xcharset=中文";
	try {
		if (window.XMLHttpRequest)
			xmlHttpReq = new XMLHttpRequest();
		else 
			try {
          		xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
       		} catch (e) {
          		try {
             		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
          		} catch (e) {
          		}
       		}
 		xmlHttpReq.open("GET", uuUrl, false);
       	xmlHttpReq.setRequestHeader("Pragma", "no-cache");
 		xmlHttpReq.send(null);
		x = xmlHttpReq.responseText;	
	}catch(e) {
		alert(e);
	}
	loadingHidden();
	
	if (x.trim() == "<errmsg>timeout<errmsg>") {
		alert("安全性提示：页面访问超时，请重新登陆！");
		document.location = "../../vi/potal/login.jsp";
		return null;
	}
	return x;
}

var popWindowZIndex = 1;
function popWindow(windowId, url) {	
	var windowDiv = _G(windowId);
	if (windowDiv == null) {
		return;
	}
	var x = doRequest(url);
	windowDiv.innerHTML = x;
	windowDiv.style.visibility = "visible";
	windowDiv.style.display = "block";
}

var cacheMap = new SimpleMap();
function load(id, url, isReload) {
	var obj = _G(id);
	var cache = cacheMap.get(id);
	if (cache == null || isReload) {
		var x = doRequest(url);
		cacheMap.put(id, x);
		obj.innerHTML = x;
	}
	else
		obj.innerHTML = cacheMap.get(id);
}

function openWindow(id, url, isReload) {
	var windowDiv = _G(id);
	if (windowDiv == null) {
		alert("Can not find " + id);
		return;
	}
	load(id, url, isReload);
	windowDiv.style.visibility = "visible";
	windowDiv.style.display = "block";
}

function switchDisplay(id) {
	var obj = _G(id);
	var isClose = ( obj.style.display == "none");
	if (isClose) 
		obj.style.display = "";
	else
		obj.style.display = "none";
}

var messageDiv = null;
function messageWindow(text) {
	messageDiv = _T_C("div", "messageDiv");
	
	window.top.document.body.appendChild(messageDiv);

	var h = document.body.scrollTop + 100;
	messageDiv.style.top = h + "px";
	var s = 	"<table width=100% cellpadding=0 cellspacing=0><tr><td align=center valign=middle height=90>"+text+"<input type=button value='确定' onclick='hiddenMessageWindow()'></td></tr><table>";
	messageDiv.innerHTML = s;

	setTimeout("_hiddenMessageWindow()", 2000);
}

function _hiddenMessageWindow() {
	try {
		window.top.document.body.removeChild(messageDiv);
	}catch(e) {
	}
}

function hiddenMessageWindow() {
	var messageDiv = document.getElementById('messageDiv');
	if (messageDiv != null)
		document.body.removeChild(messageDiv);
}

function hiddenWindow(id) {
	var obj = _G(id);
	obj.style.visibility="hidden";
	obj.style.display = "none";
}

function hiddenWindowP(id) {
	var obj = _PG(id);
	obj.style.visibility="hidden";
	obj.style.display = "none";
}

function showWindow(id) {
	var obj = _G(id);
	obj.style.visibility="visible";
	obj.style.display = "block";
}

function showWindowP(id) {
	var obj = _PG(id);
	obj.style.visibility="visible";
	obj.style.display = "block";
}

function hiddenRelativeWin(id){
	var obj = _G(id);
	obj.style.visibility="hidden";
	obj.style.position = "absolute";
}

function showRelativeWin(id) {
	var obj = _G(id);
	obj.style.visibility="visible";
	obj.style.position = "relative";
}

var show = 0;
var hid = 0;
function loadingShow() {
	show++;
	var loadingDiv = _G('loadingDiv');
	if (loadingDiv != null)
		loadingDiv.style.visibility = "visible";
}

function loadingHidden() {
	hid++;
	if(hid==show){
		var loadingDiv = _G('loadingDiv');
		if (loadingDiv != null) {
			loadingDiv.style.visibility = "hidden";
			show = 0;
			hid = 0;
		}
	}
}

function debugWin(text) {
	var debugTextArea = _G("debugTextArea");
	debugTextArea.value = text;
}

function debugWinForElement(id) {
	var obj = _G(id);
	debugTextArea.value = obj.innerHTML;
}

function _G(id) {
	return document.getElementById(id);
}

function _D_A(obj) {
	document.body.appendChild(obj);
}

function _D_R(obj) {
	document.body.removeChild(obj);
}

function _AC(obj1, obj2) {
	obj1.appendChild(obj2);
}

function _T_C(tag, id) {
	var v = window.top.document.createElement(tag);
	v.id = id;
	return v;
}

function _C(tag, id) {
	var v = document.createElement(tag);
	v.id = id;
	return v;
}

function _CIPT(id, type, name, value) {
	var v = _C("input", id);
	v.type = type;
	v.name = name;
	v.value = value;
	return v;
}

function _CCBOX(id, name, value, checked) {
	var v = _CIPT(id, "checkbox", name, value);
	v.checked = checked;
	return v;
}

function _PG(id) {
	return window.parent.document.getElementById(id);
}

//for check
function isNumber(value){
	var pattern = /\d/;
	for(i=0;i<value.length;i++)
		if(!pattern.test(value[i]))
			return false;
	return true;
}

function isChar(value){
	var pattern = /\W/;
	if(!pattern.test(value))
		return true;
	return false;
}

function isNull(value){
	if(value.trim()=="")
		return false;
	return true;
}

function getLength(value){
	var len = 0;
	for(i=0;i<value.length;i++){
		if((value.charCodeAt(i)>0)&&(value.charCodeAt(i)<128))
			len++;
		else
			len+=2;
	}
	return len;
}

function getHumanSize(size) {
	var g = Math.round(size / 1000000000);
	var m = Math.round(size / 1000000);
	var k = Math.round(size / 1000); 
	var s;

	if (size < 1000)
		s = size;
	else if (size < 1000000)
		s = k + "K";
	else if (size < 1000000000)
		s = 	m + "M";
	else 
		s = g + "G";
	return s;
}

function getFileExtName(name) {
	var ss = name.toLowerCase().split(".");
	if (ss.length == 1)
		return "";
	return ss[ss.length - 1];
}


