/*
 * @file:	base.js
 * @date:	2006-03-28
 *
 * 基本工具集
 *
 * @author waa
 */



// -----------------------------------------------------------------------------
// ------------------------------------------------------------ 需要修改的参数 -
// -----------------------------------------------------------------------------

var m_jsLib_path = "../share/";



// -----------------------------------------------------------------------------
// ---------------------------------------------------------- 扩展js原有的对象 -
// -----------------------------------------------------------------------------

if (!Array.prototype.push)
Array.prototype.push = function(obj)
{
	this[this.length] = obj;
};

/**
 * 判断数组中是否已经包含指定对象
 *
 * @param a		要判断的对象
 *
 * @return		-1:		表示数组不包含该对象;
 *				>= 0:	表示在数组中的位置
 */
Array.prototype.contain = function(a)
{
	var b = this;

	for (var i = 0; i < b.length; i++)
		if (isIdentity(a, b[i]))
			return i;

	return -1;
};

/**
 * 弹出数组中任意的对象
 *
 * @param a		要弹出的对象
 *
 * @return		-1:		表示数组不包含该对象;
 *				>= 0:	表示刚弹出的对象在数组中的序号（成功操作）
 */
Array.prototype.reject = function(a)
{
	var b = this;
	var c = b.contain(a); // 序号

	if (c >= 0)
	{
		var d = []; // 临时数组

		for (var e = b.length; e > c; e--)
			d.push(b.pop());

		d.pop();

		while (d.length > 0)
			b.push(d.pop());
	}

	return c;
};



// -----------------------------------------------------------------------------
// ------------------------------------------------------------ 关于导入的操作 -
// -----------------------------------------------------------------------------

var imported = [];

/**
 * 导入指定的js脚本文件
 *
 * @param a		js脚本文件名
 */
function importScript(a)
{
	if (imported.contain(a) < 0)
	{
		var b = "<script src=\"";
		b += m_jsLib_path;
		b += a;
		b += "\" type='text/javascript'></script>";

		document.write(b);
	}
}

/**
 * 设置指定的js脚本文件已经导入
 *
 * @param a		js脚本文件名
 */
function setScriptImported(a)
{
	if (!imported.contain(a))
		imported.push(a);
}



// -----------------------------------------------------------------------------
// ------------------------------------------------------------ 用于调试的工具 -
// -----------------------------------------------------------------------------

function debug()
{
}

debug.start = function()
{
	window.__debuging = true;
};

debug.end = function()
{
	window.__debuging = false;
};

debug.alert = function(msg)
{
	if (window.__debuging)
		window.alert("debug: [" + msg + "]");
};



// -----------------------------------------------------------------------------
// -------------------------------------------------------------- 运行时的工具 -
// -----------------------------------------------------------------------------

function runTime()
{
}

runTime.alertEx = function(msg, width, height)
{
	var features = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; scroll: no; status: no";
	var rs = window.showModalDialog('../share/alert_box.jsp', msg, features);

	return rs || false;
};

runTime.confirmEx = function(msg, width, height)
{
	var features = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; scroll: no; status: no";
	var rs = window.showModalDialog('../share/confirm_box.jsp', msg, features);

	return rs || false;
};

runTime.promptEx = function(msg, width, height)
{
	var features = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; scroll: no; status: no";
	var rs = window.showModalDialog('../share/prompt_box.jsp', msg, features);

	return rs;
};



// -----------------------------------------------------------------------------
// ------------------------------------------------------------------ 系统函数 -
// -----------------------------------------------------------------------------

/**
 * 继承
 *
 * @param a		继承类（衍生类）
 * @param b		基类
 */
_extends = function(a, b)
{
	var c = function() {};
	c.prototype = b.prototype;
	a.prototype = new c();
}

/**
 * 绑定类
 *
 * @param a		类名
 * @param b		类对象
 */
function bindClass(a, b)
{
	window[a] = b;
}

/**
 * 绑定对象
 *
 * @param a		对象名
 * @param b		对象实体
 */
function bindObject(a, b)
{
	window[a] = b;
}

/**
 * 检查是否存在指定的类
 *
 * @param a		类名
 */
function haveClass(a)
{
	return (window[a] != null);
}

/**
 * 检查是否存在指定的对象
 *
 * @param a		对象名
 */
function haveObject(a)
{
	return (window[a] != null);
}

/**
 * 绑定静态方法
 *
 * @param a		类对象
 * @param b		方法名
 * @param c		方法对象
 */
function bindStaticMethod(a, b, c)
{
	a[b] = c;
}

/**
 * 绑定类的方法
 *
 * @param a		类对象
 * @param b		方法名
 * @param c		方法对象
 */
function bindProtoType(a, b, c)
{
	a.prototype[b] = c;
}



// -----------------------------------------------------------------------------
// ------------------------------------------------------------ 关于数据的操作 -
// -----------------------------------------------------------------------------

/**
 * 将整型数的字符串转化为整型值
 *
 * @param a		整型值的字符串
 */
function parseInt(a)
{
	var a = a.replace(/\,/g, '');

	return a * 1;
}

/**
 * 判断两个变量是否引用同一对象
 *
 * @param a
 * @param b
 */
function isIdentity(a, b)
{
	var c = typeof(a);
	var d = typeof(b);

	if ("object" == c && "object" == d)
		return a === b;
	else if ("object" != c && "object" != d)
		return a == b;

	return false;
}



// -----------------------------------------------------------------------------
// -------------------------------------------------------- 关于html对象的操作 -
// -----------------------------------------------------------------------------

/**
 * 通过id取得html模型中的指定对象
 *
 * @param a		对象的id
 * @param b?	document对象，可选
 */
function idElement(a, b)
{
	var c = b || document;

	return c.getElementById(a);
}
