/**
 * Project:			unknow
 * Author:			qiujf
 * Company: 		9sky.com
 * Created Date:	2007-7-17
 * Description		通用库
 
 * Copyright @ 2007 9sky.com – Confidential and Proprietary
 * 
 * History:
 * ------------------------------------------------------------------------------
 * Date			|time		|Author	|Change Description		*/

//Start prototype extend
Array.prototype.remove = function(index)
{	
	if (isNaN(index) || index >= this.length)
		return false;
	this.splice(index, 1);
}

Array.prototype.foreach = function(handler)
{	
	for(var i=0; i<this.length; i++)
	{
		var obj = this[i];
		if (handler){ if ( handler(obj, i, this) ) { break; } }
	}
}

Array.prototype.peek = function()
{	
	if (this.length == 0){ return null;	}
	return this[this.length-1];
}

Array.prototype.insert = function(index, obj)
{	
	if (index > this.length) { return; }
	
	this.length++;
	for(var i=this.length-1; i>index; i--)
	{
		this[i] = this[i-1];
	}
	this[index] = obj;
}

Array.prototype.move = function move(s, t)
{
	try
	{
		if (s > this.length-1 || t > this.length-1) { return; }
		if (s > t)
		{
			var e = null;
			for(var i=s; i>t; i--)
			{
				if(s == i ) { e = this[i]; }
				if(i == t ) { this[i] = e; } else { this[i] = this[i-1]; }
			}
		}
		else
		{
			var e = null;
			for(var i=s; i<=t; i++)
			{
				if(s == i ) { e = this[i]; }
				if(i == t ) { this[i] = e; } else { this[i] = this[i+1]; }
			}
		}
	}
	catch (e)
	{
		Debug( "Array.move", e);
	}
}

Date.prototype.toGeneralString = function()
{
	var d = this;
	return d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
}

Date.prototype.addYears = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * (365 * 30 * 24 * 60 * 60 * 1000));
	return d;
}

Date.prototype.addMonths = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * (30 * 24 * 60 * 60 * 1000));
	return d;
}

Date.prototype.addDays = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * (24 * 60 * 60 * 1000));
	return d;
}

Date.prototype.addHours = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * (60 * 60 * 1000));
	return d;
}

Date.prototype.addMinutes = function(v)
{
	var d = this;
	d.setTime (d.getTime() + v * ( 60 * 1000));
	return d;
}

String.prototype.format = function()
{
	var number;
	var template = this;
	for (var i = 0; i < arguments.length; i++) 
	{
		number = "\{(" + i + ")\}";
		var reg = new RegExp(number, "ig");
		template = template.replace(reg, arguments[i]);
	}
   
	return template;
}

String.prototype.replaceAll = function(s,t)
{
	try
	{
		var self = this;
		var c = "(" + s + ")";
		var reg = new RegExp(c, "ig");
		return self.replace(reg, t);
	}
	catch(e)
	{
		Debug("String.replaceAll", e);
	}
}

String.prototype.clearChar = function(c)
{
	var s = this;
	var target = "\{[" + c + "]\}";
	var reg = new RegExp(target, "ig");
	
	return s.replace(reg, "");
}

String.prototype.trim = function()
{
	var s = this;
	var reg = /(^\s*)|(\s*$)/g;  
	return s.replace(reg, "");   
}

String.prototype.rightTrim = function()
{
	var s = this;
	while (s.substring(s.length-1, s.length) == ' '){
		s = s.substring(0, s.length-1);
	}
	return s;
}


String.prototype.leftTrim = function()
{
	var s = this;
	while (s.substring(0, 1) == " "){
		s = s.substring(1, s.length);
	}
	return s;
}


String.prototype.endsWith = function(s)
{
	var sLen = s.length;
	var start = this.length - sLen;
	var newString = this.substring(start, this.length);
	if (newString != s) { return null; } else { return this.substring(0, start); }	
}

//end

//Hashtable ***
function Hashtable()
{
	this.name = "Hashtable";
	this.keys = new Array();
	this.objs = new Array();
}	

Hashtable.prototype.add = function(key, obj)
{
	try
	{		
		if ( this.isExist(key) ) { return; }
		
		this.keys.push(key);
		this.objs.push(obj);
	}
	catch (e)
	{
		Debug("Hashtable.add", e);
	}
}

Hashtable.prototype.remove = function(key)
{
	var index = this.getIndex(key);
	
	this.keys.remove( index );
	this.objs.remove( index );
}

Hashtable.prototype.isExist = function(key)
{
	try
	{	
		if (this.getIndex(key) != UNDEFINE) { return true; }
		return false;
	}
	catch (e)
	{
		Debug( "Hashtable.isExist", e);
	}
}

Hashtable.prototype.getIndex = function(key)
{
	try
	{	
		var i = UNDEFINE;
		this.keys.foreach(	function (obj, index)
							{
								if (key == obj) { i = index; }
							}
					);
		return i;
	}
	catch (e)
	{
		Debug( "Hashtable.getIndex", e);
	}
}

Hashtable.prototype.getObject = function(key)
{
	try
	{	
		var index = this.getIndex(key);
		return this.objs[index];	
	}
	catch (e)
	{
		Debug("Hashtable.getObject");
	}
}

Hashtable.prototype.getCount = function()
{
	return this.objs.length;	
}

Hashtable.prototype.foreach = function(hFn)
{
	for(var i=0; i<this.keys.length; i++)
	{
		if (hFn)
		{
			if (hFn(this.keys[i], this.objs[i], i)) { break; }
		}
	}
}

Hashtable.prototype.clear = function()
{
	var length = this.getCount()-1;
	
	return this.keys.splice(0, length);	
	return this.objs.splice(0, length);	
}

Hashtable.prototype.dispose = function()
{
	this.clear();
}

//字符写入器 ***
function TextWriter()
{
	this.m_container = new Array();
}

TextWriter.prototype.m_container = null;

TextWriter.prototype.write = function(s)
{
	this.m_container.push(s);
}

TextWriter.prototype.writeLine = function(s)
{
	this.write(s);
	this.write("\r");
}

TextWriter.prototype.writeFullBeginTag = function(tagName)
{
	this.write("<");
	this.write(tagName);
	this.write(">");
}

TextWriter.prototype.writeBeginTag = function(tagName)
{
	this.write("<");
	this.write(tagName);
}

TextWriter.prototype.writeEndTag = function(tagName)
{
	this.write("</");
	this.write(tagName);
	this.write(">");
}

TextWriter.prototype.writeAttribute = function(name, value)
{
	this.write( " {0}=\"{1}\"".format(name, value) );
}

TextWriter.prototype.writeStyleValue = function(name, value)
{
	this.write( "{0}:{1};".format(name, value) );
}

TextWriter.prototype.toString = function()
{
	return this.m_container.join("");
}


function OEvent(e)
{
	this.m_event = e;
	this.srcElement = (ie ? this.m_event.srcElement : this.m_event.target);
}

function System()
{
	this.handlerList = new EventHandlerList();
}

System.prototype.event = function(e)
{
	return new OEvent(e);
}

System.prototype.handlerList = null;
System.prototype.openWindow = function(url, t)
{
	var self = this;
	this.createElement("A", "_OpenWindow_",	function(obj) { obj.href = url; obj.target = t; obj.style.display = "none"; self.body.appendChild(obj); obj.click(); });
}

System.prototype.setLoad = function(fn)
{
	window.onload = fn;
}

System.prototype.setUnload = function(fn)
{
	window.onunload = fn;
}

//对某个对象增加onmouseOVer回调(个数不限)
System.prototype.addMouseOver = function(hObj, hFn)
{
	this.addHandler(hObj, "onmouseover", hFn);
}

System.prototype.addMouseOut = function(hObj, hFn)
{
	this.addHandler(hObj, "onmouseout", hFn);
}

System.prototype.addClick = function(hObj, hFn)
{
	this.addHandler(hObj, "onclick", hFn);
}

System.prototype.addHandler = function(hObj, type, hFn)
{
	try
	{
		var self = this;
		eval( "hObj.{0} = function() { self.eventCallBack(hObj); }".format( type.toLowerCase() ) );
		this.handlerList.addEvent(hObj, type, hFn);
	}
	catch(e)
	{
		Debug("System.addHandler MSG:{0}", e);
	}
}

System.prototype.eventCallBack = function(hObj)
{
	var type = "on{0}".format(event.type);
	this.handlerList.invoke(hObj, type);
}

System.prototype.createElement = function(tagName, objID, hFn)
{
	var obj	= document.getElementById(objID);
	if (obj == null)
	{
		obj = document.createElement(tagName);
		obj.id = objID;
	}
	if (hFn) { hFn(obj); }
	
	return obj;
}

System.prototype.createDIV = function(objID, hFn)
{
	return this.createElement("div", objID, hFn);
}

System.prototype.getScrollLeft = function()
{
	var scrollPos; 
	if (typeof window.pageXOffset != 'undefined') { 
	   scrollPos = window.pageXOffset; 
	} 
	else if (typeof document.compatMode != 'undefined' && 
		 document.compatMode != 'BackCompat') { 
	   scrollPos = document.documentElement.scrollLeft; 
	} 
	else if (typeof document.body != 'undefined') { 
	   scrollPos = document.body.scrollLeft; 
	} 
	return scrollPos;
}


System.prototype.getScrollTop = function()
{
	var scrollPos; 
	if (typeof window.pageYOffset != 'undefined') { 
	   scrollPos = window.pageYOffset; 
	} 
	else if (typeof document.compatMode != 'undefined' && 
		 document.compatMode != 'BackCompat') { 
	   scrollPos = document.documentElement.scrollTop; 
	} 
	else if (typeof document.body != 'undefined') { 
	   scrollPos = document.body.scrollTop; 
	} 
	return scrollPos;
}

System.prototype.setScrollTop = function(v)
{
	if (typeof window.pageYOffset != 'undefined') { 
	   window.pageYOffset = v; 
	} 
	else if (typeof document.compatMode != 'undefined' && 
		 document.compatMode != 'BackCompat') { 
	   document.documentElement.scrollTop = v; 
	} 
	else if (typeof document.body != 'undefined') { 
	   document.body.scrollTop = v; 
	} 
}


System.prototype.getClientWidth = function()
{
	if(document.documentElement && document.documentElement.clientWidth>0)
	{
		return document.documentElement.clientWidth;
	}
	else
	{
		return document.body.clientWidth;
	}
}


System.prototype.getClientHeight = function()
{
	if(document.documentElement && document.documentElement.clientHeight>0)
	{
		return document.documentElement.clientHeight;
	}
	else
	{
		return document.body.clientHeight;
	}
}

System.prototype.getScrollWidth = function()
{
	if(document.documentElement && document.documentElement.scrollWidth>0)
	{
		return document.documentElement.scrollWidth;
	}
	else
	{
		return document.body.scrollWidth;
	}
}

System.prototype.getScrollHeight = function()
{
	if(document.documentElement && document.documentElement.scrollHeight > 0)
	{
		return document.documentElement.scrollHeight;
	}
	else
	{
		return document.body.scrollHeight;
	}
}

//获取字符串字节长度,中文计2个字节
//参数[字符串]
System.prototype.getByteLength = function(s)
{
	var j=0; 
	for(var i=0;i<str.length;i++)
	{
		if(str.charCodeAt(i)>127 || str.charCodeAt(i)==94)
			j=j+2;
		else
			j=j+1 
	}
	return j;
}

System.prototype.removeElement = function(id)
{
	try
	{
		var ctl = document.getElementById(id);
		var parent = ctl.parentElement;
		if (parent)	{ parent.removeChild(ctl); }
	}catch(e){}
}

System.prototype.forCtlChild = function(obj, hFn, index)
{
	if (obj != null)
	{
		if (hFn) 
		{
			if (hFn(obj, this, index)) { return true; } 
		}

		var arr = obj.childNodes;
		if (arr)
		{
			for(var i=0; i<arr.length; i++)
			{
				var c = arr[i];
				this.forCtlChild(c, hFn, i)
			}
		}
	}
}

System.prototype.getParentAttribute = function(ctl, attrName)
{
	var ret	= null;
	if (ctl) 
	{	
		if (ctl.getAttribute(attrName))
		{
			ret = ctl.getAttribute(attrName); 
		}
		else if (ctl.parentElement)
		{
			ret = this.getParentAttribute(ctl.parentElement, attrName); 
		}
	}
	return ret;
}

System.prototype.appendChild = function(container, obj)
{
	if (container && container.appendChild) { container.appendChild(obj); };
}

System.prototype.elementMoveTo = function(container, index, obj)
{
	var html = obj.innerHTML;
	container.appendChild(obj);
	var arr = container.childNodes;
	
	obj.innerHTML = html;
	for(var i=0; i<arr.length - index - 1; i++)
	{
		container.appendChild(arr[index]);
	}
}

System.prototype.foreach = function(array, hFn)
{
	for(var i=0; i<array.length; i++)
	{
		var obj = array[i];
		if (hFn)
		{ 
			if ( hFn(obj, i) ) { break; }
		}
	}
}

System.prototype.setHTML = function(ctl, v)
{
	if (ctl) { ctl.innerHTML = v; }
}

System.prototype.joinChar = function()
{
	var len = arguments.length;
	if (len < 0) { return ""; }
	
	var ret = "";
	var c	= arguments[len-1];
	for (var i = 0; i < arguments.length; i++) 
	{
		if (i == len-1) { break; }
		
		ret += arguments[i];
		if (i < len-2) { ret += c; }
	}
	return ret;
}

System.prototype.filterElement = function(nodes, tagName)
{
	var newArr	= new Array();
	if (nodes == null) { return newArr; }
	for (var i = 0; i < nodes.length; i++) 
	{
		var node = nodes[i];
		if (node && node.tagName == tagName.toUpperCase()) { newArr.push(node); }
	}
	return newArr;
}

System.prototype.getOffsetXByID = function(id)
{
	var k = $E(id);
	return this.getOffsetX(k);
}

System.prototype.getOffsetYByID = function(id)
{
	var k = $E(id);
	return this.getOffsetY(k);
}

System.prototype.getOffsetX = function(obj)
{
	var left=obj.offsetLeft;
	while(obj=obj.offsetParent){
		left+=obj.offsetLeft;
	}
	return left;
}

System.prototype.getOffsetY = function(obj)
{
	var top=obj.offsetTop;
	while(obj=obj.offsetParent){
		top+=obj.offsetTop;
	}
	return top;
}


//设置cookie值
//参数[cookie名,cookie值,有效期,有效域]
//var expdate = new Date();//设置有效期方法
//expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1Day
System.prototype.setCookie = function(name, value)
{
	var argv = this.setCookie.arguments;
	var argc = this.setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var domain = (argc > 3) ? argv[3] : null;
	var path = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + encodeURI(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((secure == true) ? "; secure" : "");
}

//设置cookie键值
//参数[cookie名,cookie子键名,cookie子键值,有效期,有效域]
System.prototype.setCookies = function(name, key, value)
{
	var argv = this.setCookies.arguments;
	var argc = this.setCookies.arguments.length;
	var expires = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var path = (argc > 5) ? argv[5] : null;
	var secure = (argc > 6) ? argv[6] : false;
	var becookie = this.getCookie(name);
	if(becookie == "")
		value = key + "=" + encodeURI(value);
	else{
		var re = new RegExp("(" + key + "=)[^&]*", "gi");
		if(re.test(becookie))
			value = becookie.replace(re, "$1" + encodeURI(value));
		else
			value = becookie + "&" + key + "=" + encodeURI(value);
	}
	document.cookie = name + "=" + value +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((secure == true) ? "; secure" : "");
}



//获取cookie值
//参数[cookie名,cookie子键名]
System.prototype.getCookie = function(name, key)
{
	var arg = name + "=";
	var i = document.cookie.indexOf(arg);
	if(i==-1) return '';
	var str = document.cookie.substring(i+arg.length);
	var j=str.indexOf(";");
	if(j>-1) str=str.substring(0,j);
	if(key&&key.length>0){
		arg=key+"=";
		i=str.indexOf(arg);
		if(i==-1)return '';
		str=str.substring(i+arg.length);
		j=str.indexOf("&");
		if(j>-1)str=str.substring(0,j);
	}
	return decodeURI(str);
}

//获取cookie值
//参数[cookie名,cookie子键名]
System.prototype.getHost = function(uri)
{
	var pattern=/:\/\/([^\/]+)/i;
    if(uri.indexOf("://")==-1) pattern=/[^\/]+/i;
    var host=uri.match(pattern);
    if(host!=null)
        if(host.length>1)return host[1];
        else return host[0];
    else return uri;
}

//设置url地址的hash值
//参数[hash参数名,对应值]
System.prototype.setUrlHash = function(uri)
{
	var hash=location.hash;
    if(hash.indexOf(name)>-1){
        var re=new RegExp("("+name+"=)([^;]*)","gi"); 
        hash=hash.replace(re,'$1'+val);
    }
    else if(hash.length>0) hash+=";"+name+"="+val;
    else hash=name+"="+val;
    location.hash=hash;
}

//获取url地址的hash值
//参数[hash参数名]
System.prototype.getUrlHash = function(name)
{
    var hash=location.hash;
    if(hash.indexOf(name+"=")>-1){
        hash=hash.substr(hash.indexOf(name)+name.length+1);
        hash=hash.split(";")[0];
        return hash;
    }
    return "";
}

//设置url地址参数
//参数[url地址,Array(参数名,值),....]
System.prototype.setUrlParam = function(url)
{
    if(arguments.length>1){
        var hash=url.substr(url.indexOf("#")-1);
        for(var i=1;i<arguments.length;i++){
            if(arguments[i].length==2){
                var arg=arguments[i];
                var re = new RegExp("("+arg[0]+"=)[^&]*","gi");
                if(re.test(url))
                    url=url.replace(re,"$1"+arg[1]);
                else
                    url+="&"+arg[0]+"="+arg[1];
            }
        }
        if(url.indexOf("?")<0) url.replace("&","?");
        if(hash.length>0) url+=hash;
    }
    return url;
}

System.prototype.setTimeout = function(fn, s, max, i)
{
	if (fn && this.isFunction(fn))
	{
		if (i == null)
		{
			i = 1;
		}
		
		var self = this;
		setTimeout( function()
					{
						try
						{
							fn(); 
						}
						catch(e)
						{
							if (i < max)
							{
								i++;
								self.setTimeout(fn, s, max, i);
							}
						}
					}, s);
	}	
}

System.prototype.isFunction = function(fn)
{
	return (typeof(fn) == "function" ? true : false);
}

//四舍五入
System.prototype.adv_format = function(value,num)
{
	var a_str = this.formatnumber(value,num);
	var a_int = parseFloat(a_str);
	if (value.toString().length>a_str.length)
	{
		var b_str = value.toString().substring(a_str.length,a_str.length+1)
		var b_int = parseFloat(b_str);
		if (b_int<5)
		{
		return a_str
		}
		else
		{
		var bonus_str,bonus_int;
		if (num==0)
		{
			bonus_int = 1;
		}
		else
		{
			bonus_str = "0."
			for (var i=1; i<num; i++)
				bonus_str+="0";
				bonus_str+="1";
				bonus_int = parseFloat(bonus_str);
			}
			a_str = this.formatnumber(a_int + bonus_int, num)
		}
	}
	return a_str
}

System.prototype.formatnumber = function(value,num)
{
	var a,b,c,i
	a = value.toString();
	b = a.indexOf('.');
	c = a.length;
	if (num==0)
	{
		if (b!=-1)
		a = a.substring(0,b);
	}
	else
	{
		if (b==-1)
		{
			a = a + ".";
			for (i=1;i<=num;i++)
			a = a + "0";
		}
		else
		{
			a = a.substring(0,b+num+1);
			for (i=c;i<=b+num;i++)
			a = a + "0";
		}
	}
	return a
}


System.prototype.loadScript = function(url)
{
	document.write("<script src=\"{0}\"><\/script>".format(url));
}

System.prototype.closeAD = function(obj, h)
{
	try
	{					
		var self = this;	
		obj.style.overflow = "hidden";
		obj.style.height = h;

		if (h>0)
		{
			h=h-5;
			setTimeout(function() { self.closeAD(obj, h); }, 10);
		}
		else
		{
			this.hideControl(obj);
		}
	}
	catch (e)
	{
		this.hideControl(obj);
	}
}

System.prototype.openAD = function(obj, h, i)
{
	try
	{	
		if (i == null)
		{
			i = 0;
		}
		
		var self = this;	
		
		obj.style.overflow = "hidden";
		obj.style.height = i;
		
		this.showControl(obj);

		if (i < h)
		{
			i+=10;
			setTimeout(function() { self.openAD(obj, h, i); }, 10);
		}
	}
	catch (e)
	{
		this.showControl(obj);
	}
}



System.prototype.hideControl = function(obj)
{
	if (obj) { obj.style.display = "none"; }
}

System.prototype.showControl = function(obj)
{
	if (obj) { obj.style.display = "block"; }
}

// <EventHandlerList>
function EventHandlerList()
{
	this.events = new Array();
}

EventHandlerList.prototype.events = null;
EventHandlerList.prototype.addEvent = function(hObj, type, hFn)
{
	try
	{
		var arg = this.find(hObj, type);
		if (arg == null)
		{
			arg = new EventArgs(hObj, type);
			this.events.push(arg);
		}
		arg.addHandler(hFn);
	}
	catch(e)
	{
		Debug("EventHandlerList.addEvent MSG:{0}".format(e.message));
	}	
}

EventHandlerList.prototype.find = function(hObj, type)
{
	try
	{
		var retArg = null;
		this.events.foreach(
								function(arg, index)
								{
									if (arg.m_handle == hObj && arg.m_type == type) { retArg = arg; }
								}
							);						
		return retArg;
	}
	catch(e)
	{
		Debug("EventHandlerList.find", e);
	}
}

EventHandlerList.prototype.getIndex = function(hObj, type)
{
	try
	{
		var retIndex = null;
		this.events.foreach( 
								function(arg, index)
								{
									if (arg.m_handle == hObj && arg.m_type == type) { retIndex = index; } 
								}
							);
		return retIndex;
	}
	catch(e)
	{
		Debug("EventHandlerList.getIndex", e);
	}
}

EventHandlerList.prototype.exist = function(hObj, type)
{
	try
	{
		var arg = this.find(hObj, type);
		if (arg == null) { return false; } else { return true; }
	}
	catch(e)
	{
		Debug("EventHandlerList.exist", e);
	}
}
 
EventHandlerList.prototype.clear = function(hObj, type)
{
	try
	{
		var index = this.getIndex(hObj, type);	
		if (index != null)
		{
			this.events.remove(index);
		}
	}
	catch(e)
	{
		Debug("EventHandlerList.clear", e);
	}
}

EventHandlerList.prototype.remove = function(hObj, type, hFn)
{
	try
	{
		var arg = this.find(hObj, type);	
		if (arg != null)
		{
			arg.remove(hFn);
		}
	}
	catch(e)
	{
		Debug("EventHandlerList.remove", e);
	}
}

EventHandlerList.prototype.invoke = function(hObj, type)
{
	try
	{
		var arg = this.find(hObj, type);
		if (arg != null) { arg.invoke(); }
	}
	catch(e)
	{
		Debug("EventHandlerList.invoke", e);
	}
}
// </EventHandlerList>

function EventArgs(hObj, type)
{
	this.m_handle	= hObj;
	this.m_type		= type;
	this.m_handler	= new Array();
}

EventArgs.prototype.addHandler = function(hFn)
{
	if ( !this.exist(hFn) ) { this.m_handler.push(hFn) }
}

EventArgs.prototype.exist = function(fn)
{
	var exist = false;
	this.m_handler.foreach( 
							function(hFn, index)
							{
								if (hFn == fn) { exist = true; }
							}
						);
	return exist;
}

EventArgs.prototype.remove = function(fn)
{
	this.m_handler.foreach( 
							function(hFn, index, sender)
							{
								if (hFn == fn) { sender.remove(index) }
							}
						);
}

EventArgs.prototype.invoke = function()
{
	this.m_handler.foreach( 
							function(hFn, index)
							{ 
								if (hFn && hFn != null) { hFn(); }
							}
						);
}

//函数列表
function FunctionList()
{
	var self = this;
	this.list = new Array();
	system.foreach(arguments,	function(fn, index)
								{
									self.add(fn);
								}
						);
}

FunctionList.prototype.invokeAt = function(index)
{
	if (this.list.length > index) { this.list[index](); }
}

FunctionList.prototype.add = function(fn)
{
	this.list.push(fn);
}

FunctionList.prototype.invoke = function(t)
{
	this.list.foreach(	function(obj, i)
						{
							if (obj)
							{ 
								if (t) { setTimeout(function(){ obj(); }, t*(i+1)); } else { obj(); }
							}
						}
				);
}


//End prototype extend
function CreateAjax(url, method, body, onsucces, onfail)
{	
	$.ajax(
			{
				url:url,
				type:method,
				dataType:"html",
				data:body,
				error:onfail,
				success:onsucces
			});
}

function CreateAjaxByGet(url, param, onsucces, onfail)
{
	if (param) { url = "{0}?{1}".format(url, param); }
	
	CreateAjax(url, "get", null, onsucces, onfail);
}

function CreateAjaxByPost(url, param, onsucces, onfail)
{	
	CreateAjax(url, "post", param, onsucces, onfail);
}

function AjaxControlInvoker(rece, mode)
{
	this.m_receiver		= rece;
	this.m_mode			= mode;
	this.m_files		= new Array();
	this.m_parameters	= new Hashtable();
}

AjaxControlInvoker.prototype.addParam = function(name, value)
{
	this.m_parameters.add(name, value);
}

AjaxControlInvoker.prototype.addModule = function(fileName, path)
{
	if (path == null) { path = g_ajaxControlPath; }
	this.m_files.push(path + fileName);
}

AjaxControlInvoker.prototype.buildParam = function()
{
	var fileCount = this.m_files.length;
	var writer = new TextWriter();
	writer.write("fn={0}".format( escape(this.m_files.join(",")) ));
	this.m_parameters.foreach( function(name, value) { writer.write( "&{0}={1}".format(name, escape(value)) ); } );
	return writer;
}

AjaxControlInvoker.prototype.invoke = function(onsucces, onfail)
{
	var param = this.buildParam();
	switch(this.m_mode)
	{
		case "post":			
			CreateAjaxByPost(this.m_receiver, param.toString(), onsucces, onfail);
			break;
		
		case "get":
			CreateAjaxByGet(this.m_receiver, param.toString(), onsucces, onfail);
			break;
	}
	
	this.m_parameters.dispose();
}

function CreateAjaxPlusControlInvoker(rece, mode)
{
	if (rece == null) { rece = g_ajaxControlReceiver }
	if (mode == null) { mode = "post"; }
	return new AjaxControlInvoker(rece, mode);
}

function CreateAjaxControlInvoker(rece, mode)
{
	if (rece == null) { rece = g_ajaxControlReceiver; }
	if (mode == null) { mode = "post"; }
	return new AjaxControlInvoker(rece, mode);
}

function AjaxInvoker(clas, method, rece, mode)
{
	this.m_class		= clas;
	this.m_method		= method;
	this.m_receiver		= rece;
	this.m_mode			= mode;
	this.m_parameters	= new Hashtable();
}

AjaxInvoker.prototype.addParam = function(name, value)
{
	this.m_parameters.add(name, value);
}

AjaxInvoker.prototype.buildParam = function()
{
	try
	{
		var writer = new TextWriter();
		writer.write("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
		writer.write("<Ajax class=\"{0}\" method=\"{1}\">".format(this.m_class, this.m_method));
		this.m_parameters.foreach(	function(name, value)
									{
										writer.write("<Parameters name=\"{0}\"><![CDATA[{1}]]></Parameters>".format(name, value));
									}
				);
		writer.write("</Ajax>");
		return writer;
	}
	catch(e)
	{
		Debug("AjaxInvoker.buildParam", e);
	}
}

AjaxInvoker.prototype.invoke = function(onsucces)
{
	var param = "xml={0}".format(escape(this.buildParam()));
	
	switch(this.m_mode)
	{
		case "post":
			CreateAjaxByPost(this.m_receiver, param, onsucces);
			break;
		
		case "get":
			CreateAjaxByGet(this.m_receiver, param, onsucces);
			break;
	}
	this.m_parameters.dispose();
}

function CreateAjaxInvoker(clas, method, rece, mode)
{
	return new AjaxInvoker(clas, method, rece, mode);
}


function ShowDebugLog()
{
	if (debuger != EMPTY) { alert(debuger);	}
}


//弹窗
function winOpen(url,name,width,height,scroll,resize){
	if(!scroll||scroll=="") scroll="yes";
	if(!resize||resize=="") resize="yes";
	window.open(url,name,'width='+width+',height='+height+',location=no,toolbar=no,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no');
}

//获取用户的唯一标识
function getUserID(){
	var passid=system.getCookie("9Sky_PassID");
	var arr=passid.split("\r");
	if(passid.length>0 && !/[^\d]/.test(arr[0])) return arr[0];
	return 0;
}
//获取用户当前或上次登录帐号
function getUserName(){
	var passid = system.getCookie("9Sky_PassID");
	var arr=passid.split("\r");
	if(arr.length>1) 
	{
		return decodeURI(arr[1].replace("%40", "@"));
	}
	return "";
}

function getLoginState()
{
	var v = system.getCookie("9Sky_Passporta");
	if (v == "" || v == null) { v = system.getCookie("9Sky_Passportb"); }
	if (v == "" || v == null) { return false; } else { return true; }
}


//调试器 ***
function Debug(objName, e)
{
	try
	{	
		if (DEBUG) 
		{
			var error = objName;
			if (e)
			{
				error = "FN:{0}  MSG:{1}  LINE:{2}".format(objName, e.message, e.number);
			}
			debuger.write(error + "\r"); 
		}
	}
	catch (e){ alert("Debug"); }
}

//随机数
function rand(num)
{
   return Math.floor(Math.random()*num)+1;
}

function $E(id)
{
	return document.getElementById(id);
}

//快速排序
function sort(x, icompare)
{
	var n = arr.length;
	for(i=0; i<n-1; i++)                              
	{
		var k = i;
		for(j=i+1; j<n ;j++)
		{
		   if (icompare)
		   {
				if( icompare(x[k], x[j]) )
					k=j;
		   }
		   else
		   {
				throw "error";
		   }
		}
		if(k != i)
		{
			var t=x[i];
			x[i]=x[k];
			x[k]=t;
		}
	}
}

function AjaxScript(url, varName, params, onsuccess)
{	
	var arr = document.getElementsByTagName("script");
	var url =url.toLowerCase();
	var isLoad = true;
	if (arr)
	{
		for(i=0; i<arr.length; i++)
		{
			var src = arr[i].src;
			if (src) 
			{
				if (src.toLowerCase() == url) { isLoad = false; }
			}
		}
	}
	
	if (isLoad)
	{
		var s = document.createElement("script");
		var params = (params ? params : "") + (params && varName ? "&" : "") + (varName ? "varName=" + varName : "")
		if (url.indexOf("?") >= 0)
		{
			url += (params && params != "" ? "&" + params  : "");
		}
		else
		{
			url += (params && params != "" ? "?" + params : "");
		}
		
		s.src = url;
		s.type = "text/javascript";
		s.onreadystatechange = function()
		{	
			switch(this.readyState)
			{
				case "complete":
				case "loaded":
					var cmd = "try {	if (onsuccess) { onsuccess({0});  {1} }	}catch(e){}".format((varName != null ?  varName : ""), (varName != null ? "delete {0}".format(varName) : "") )
					eval(cmd);
					break;
					
				case "loading":
					break;
			}
		}
		s.onload = function() { 
					var cmd = "try {	if (onsuccess) { onsuccess({0});  {1} }	}catch(e){}".format((varName != null ?  varName : ""), (varName != null ? "delete {0}".format(varName) : "") )
					eval(cmd);
				}
		var arr = document.getElementsByTagName("head");
		var h= arr[0];
		document.body.appendChild(s);
	}
	else
	{
		eval("if (onsuccess) { onsuccess({0}); }".format( varName != null ?  varName : "" ));
	}
}

function LoadScript(url)
{	
	var arr = document.getElementsByTagName("script");
	var isLoad = true;
	if (arr)
	{
		for(i=0; i<arr.length; i++)
		{
			var src = arr[i].src;
			if (src) 
			{
				if (src.toLowerCase() == url.toLowerCase()) { isLoad = false; }
			}
		}
	}
	
	if (isLoad)
	{
		document.writeln("<script src=\"" + url + "\" language=\"javascript\" type=\"text\/javascript\"><\/script>");
	}
}

function LoadCSS(url)
{
	var arr = document.getElementsByTagName("link");
	var isLoad = true;
	
	if (arr)
	{
		for(i=0; i<arr.length; i++)
		{
			var type = arr[i].type;
			if (type && type.toLowerCase() == "text/css")
			{
				var href = arr[i].href;
				if (href) 
				{
					if (href.toLowerCase() == url.toLowerCase()) { isLoad = false; }
				}
			}
		}
	}
	if (isLoad)
	{
		document.writeln("<link  href=\"" + url + "\" type=\"text\/css\" rel=\"stylesheet\"\/>");
	}
}

function foreachCheckBox(name, hFn, hFnFilter)
{
	var arrCtl = document.body.getElementsByTagName("input");
	var arrBox = new Array();
	system.foreach(arrCtl,	function(obj, i)
							{ 
								if (obj.type == "checkbox" && obj.name == name) 
								{
									if(hFnFilter) 
									{ 
										if (hFnFilter(obj))
										{
											arrBox.push(obj); 
										}
									} 
									else 
									{ 
										arrBox.push(obj); 
									}
								}
							});
	
	arrBox.foreach(	function(obj, i) { if (hFn) { hFn(obj, i, arrBox.length); } });
	return arrBox;
}

//选择CheckBox
function chooseCheckBox(name, v)
{
	foreachCheckBox(name, function(obj,i) { obj.checked = v; });
}

//反选
function checkReverse(name)
{
	foreachCheckBox(name, function(obj,i) { obj.checked = !obj.checked; });
}

function getCheckBoxValue(name)
{
	var trkIDs = "";
	var arrBox = foreachCheckBox(name, null,	function(box) { return box.checked; } );
	arrBox.foreach(	function(box,i)
					{
						if (i == 0)
						{
							trkIDs = box.value;
						}
						else
						{
							trkIDs += "," + box.value;
						}
					});
	return trkIDs;
}

function ayTrace()
{
	var args = ayTrace.arguments
	for(i=0; i<args.length; i++)
	{
		var id = args[i];
		eval("newImage('http://smarttrade.allyes.com/main/adftrack?db=smarttrade&point={0}&js=on&cache={1}&pre={2}');".format(id, (new Date()).getTime(), escape(document.referrer)) );
	}
}

function newImage(url)
{
	var img = new Image(1,1);
	img.src= url;
	img.onload=function() { return function() { return; } }
}

function pageStateTrace(p, s)
{
	try
	{
		eval("newImage('http://trace.9sky.com/klik/pagestate.aspx?p={0}&s={1}&ts={2}');".format(p, s, (new Date()).getTime()));
	}catch(e){}
}

function aim9(n)
{
  var a9=new Image(1,1);
  a9.src='http://zzz.9sky.com/target/aim.aspx?targed=2413&type=208&aim='+n+'&cache='+(new Date()).getTime()+'&pre='+escape(document.referrer);
  a9.onload=function() { return function(){return;} }
}
//trace



//系统实例
var system = new System();

//调试器实例
var debuger = new TextWriter();



//环境变量声明
var NULL		=	0;
var EMPTY		=	"";
var TRUE		=	1;
var FALSE		=	0;
var UNDEFINE	=	-1;
var DEBUG		=   true;

var g_domain	= "http://www.9sky.com";
var g_static	= "http://static.9eye.cn/s3";
var g_siteID	= 135;
var g_hwnd		= 0;
var g_islogin	= getLoginState();
var g_ajaxControlReceiver	= "{0}/ajax/d/ajaxUserControl.aspx".format(g_domain);
var g_ajaxControlPath = "Skin/Soin/Music/";


document.domain = "9sky.com";
var ie = (navigator.appName == "Microsoft Internet Explorer" ? true : false);


LoadScript("{0}/js/library/jquery.js".format(g_static));