var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var _P = new Array();

_P['style'] = 'style/';
_P['script'] = 'script/';
_P['image'] = 'images/';
_P['file'] = 'file/';

function _G(id)
{
	try
	{
		return document.getElementById(id);
	}
	catch (e)
	{
		return null;
	}
}

function show(id)
{
	if (_G(id) && _G(id).style.display != "block")
	{
		_G(id).style.display = "block";
	}
}

function hide(id)
{
	if (_G(id) && _G(id).style.display != "none")
	{
		_G(id).style.display = "none";
	}
}

//È¥×óÓÒ¿Õ°×
function trim(strvalue)
{
	ptntrim = /(^\s*)|(\s*$)/g;
	return strvalue.replace(ptntrim,"");
}

//È¥×ó¿Õ°×
function ltrim(strvalue)
{
	ptnltrim = /^\s*/g;
	return strvalue.replace(ptnltrim,"");
}

//È¥ÓÒ¿Õ°×
function ltrim(strvalue)
{
	ptnrtrim = /\s$/g;
	return strvalue.replace(ptnrtrim,"");
}

function checkall(id)
{
	var obj = document.getElementsByName(id);
	if (!obj || obj.length == 0)
	{
		return;
	}

	for (i=0; i<obj.length; i++)
	{
		if (obj[i].type == "checkbox")
		{
			obj[i].checked = true;
		}
	}
}

function checkval(id, val)
{
	var obj = document.getElementsByName(id);
	if (!obj || obj.length == 0)
	{
		return;
	}

	if (!val || val.length == 0)
	{
		return;
	}

	var arr = val.split(',');

	for (i=0; i<obj.length; i++)
	{
		if (obj[i].type == "checkbox")
		{
			for (j=0; j<arr.length; j++)
			{
				if (obj[i].value == arr[j])
				{
					obj[i].checked = true;
				}
			}
		}
	}
}

function clearall(id)
{
	var obj = document.getElementsByName(id);
	if (!obj || obj.length == 0)
	{
		return;
	}

	for (i=0; i<obj.length; i++)
	{
		if (obj[i].type == "checkbox")
		{
			obj[i].checked = false;
		}
	}
}

function selectval(id, val)
{
	var obj = _G(id);
	if (!obj)
	{
		return;
	}

	if (!val || val.length == 0)
	{
		return;
	}

	for (i=0; i<obj.length; i++)
	{
		if (obj.options[i].value == val)
		{
			obj.options[i].selected = true;
		}
	}
}

function popup_open(id)
{
	try
	{
		var obj = _G(id);
		if (obj)
		{
			viewport.getAll();
			obj.style.display = 'block';
			obj.style.left = ((viewport.width - obj.offsetWidth) / 2) + viewport.scrollX + "px";
			obj.style.top = ((viewport.height - obj.offsetHeight) / 2) + viewport.scrollY + "px";
		}
	}
	catch (e)
	{
		alert(e.message);
	}
}

function popup_close(id)
{
	try
	{
		_G(id).style.display = 'none';
	}
	catch (e)
	{
		alert(e.message);
	}
}

function preview(id, path)
{
	if (!path || path.length == 0)
	{
		return;
	}

	var patn = /\.jpg$|\.jpeg$|\.gif$|\.bmp$|\.png$/i;
	if (patn.test(path))
	{
		var img = '<img src="' + _P['file'] + path + '" height="80" width="100" alt="preview" />';
		var val = '<input type="hidden" id="' + id + '" name="' + id + '" value="' + path + '" />';
		var div = id + "_div";
		if (_G(div))
		{
			_G(div).innerHTML = img + val;
		}
	}
	else
	{
		alert("please select a image.");
	}
}

var viewport =
{
	getWinWidth: function ()
	{
		this.width = 0;
		if (window.innerWidth)
		{
			this.width = window.innerWidth - 18;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			this.width = document.documentElement.clientWidth;
		}
		else if (document.body && document.body.clientWidth)
		{
			this.width = document.body.clientWidth;
		}
	},

	getWinHeight: function ()
	{
		this.height = 0;
		if (window.innerHeight)
		{
			this.height = window.innerHeight - 18;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			this.height = document.documentElement.clientHeight;
		}
		else if (document.body && document.body.clientHeight)
		{
			this.height = document.body.clientHeight;
		}
	},

	getScrollX: function ()
	{
		this.scrollX = 0;
		if (typeof window.pageXOffset == "number")
		{
			this.scrollX = window.pageXOffset;
		}
		else if (document.documentElement && document.documentElement.scrollLeft)
		{
			this.scrollX = document.documentElement.scrollLeft;
		}
		else if (document.body && document.body.scrollLeft)
		{
			this.scrollX = document.body.scrollLeft;
		}
		else if (window.scrollX)
		{
			this.scrollX = window.scrollX;
		}
	},

	getScrollY: function ()
	{
		this.scrollY = 0;
		if (typeof window.pageYOffset == "number")
		{
			this.scrollY = window.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			this.scrollY = document.documentElement.scrollTop;
		}
		else if (document.body && document.body.scrollTop)
		{
			this.scrollY = document.body.scrollTop;
		}
		else if (window.scrollY)
		{
			this.scrollY = window.scrollY;
		}
	},

	getAll: function ()
	{
		this.getWinWidth();
		this.getWinHeight();
		this.getScrollX();
		this.getScrollY();
	}
}
