﻿String.prototype.trim = function()
{
    return this.replace(/(^[\s\u3000]*)|([\s\u3000]*$)/g, "");
}

window.__jsAlert = window.alert;
window.alert = function()
{
    var msg = arguments[0];
    for(var i = 1; i < arguments.length; i ++)
    {
        msg = msg.replace("%s", arguments[i]);
    }
    __jsAlert(msg);
};

function e(id)
{
    return document.getElementById(id);
}

function alertXp(msg)
{
    e("alertXpText").innerHTML = msg;
    e("alertXp").style.display = "block";
    e("overLayer").style.display = "block";
}

function alertXpClose()
{
    e("alertXp").style.display = "none";
    e("overLayer").style.display = "none";
}

function getWindowWidth()
{
    if (window.self && self.innerWidth)
        return self.innerWidth;
    if (document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    return 610;
}

function getWindowHeight()
{
    if (window.self && self.innerHeight)
        return self.innerHeight;
    if (document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;
    return 1002;
}

function queryString(urlParam)
{
	var uri=window.location.search;
	var re=new RegExp(urlParam+"\=([^\&\?]*)","ig");
	return ((uri.match(re))?(uri.match(re)[0].substr(urlParam.length+1)):null);
}

Object.extend = function(dest, source, replace)
{
	for(var prop in source)
	{
		if(replace == false && dest[prop] != null) continue;
		dest[prop] = source[prop];
	}
	return dest;
};
SortedList = function() { this.keys = []; }  //优化的可通加键读取数据的列表，key不能为数字
Object.extend(SortedList.prototype,
{
	add : function(k, v)
	{
	    if (false == isNaN(parseInt(k)))
	    {
	        alert("优化的可通加键读取数据的列表, key不能为数字");
	        return;
	    }
        if (this.containsKey(k))
        {
            this.keys[k] = v;
        }
        else
        {
            this.keys[k] = v;
	        this.keys.push(k);
		}
		return this.keys.length - 1;
	},
	remove : function(k)
	{
	    for(var i = 0; i < this.keys.length; i ++)
	    {
	        if (this.keys[i] == k)
	        {
	            this.removeByIndex(i);
	            break;
	        }
	    }
	},
	removeByIndex : function(index)
	{
	    this.keys[this.keys[index]] = null;
	    this.keys.splice(index,1);
	},
	containsKey : function(k)
	{
	    return this.keys[k] != undefined;
	},
	getKeys : function()
	{
	    return this.keys;
	},
	getValue : function(k) 
	{
	    if (!this.containsKey(k)) return null;
        return this.keys[k];
	},
	getValueByIndex : function(index)
	{
	    return this.getValue(this.keys[index]);
	},
	setValue : function(k, v)
	{
	    return this.add(k, v);
	},
	count : function()
	{
	    return this.keys.length;
	}
}, true);

var JsCookie = 
{
    //获得Cookie的原始值
	getCookie : function(name)
	{
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen)
		{
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
			return JsCookie.getCookieVal(j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
		return null;
	},
    //获得Cookie解码后的值
	getCookieVal : function(offset)
	{
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
			endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	},
    //设定Cookie值
	setCookie : function(name, value)
	{
		var expdate = new Date();
		var argv = JsCookie.setCookie.arguments;
		var argc = JsCookie.setCookie.arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		//var path = (argc > 3) ? argv[3] : null;
		var path = (argc > 3) ? argv[3] : "/";  //默认让不同目录的cookie共享
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		if(expires != null)
			expdate.setTime(expdate.getTime() + (expires * 1000));
		document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
			+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
			+((secure == true) ? "; secure" : "");
	},
	//删除Cookie
	delCookie : function(name)
	{
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = JsCookie.getCookie(name);
		document.cookie = name + "=" + cval + "; path=/; expires=" + exp.toGMTString();
	}
};

// 四舍五入并能格式化浮点数的小数位
/* formatNumber : 要格式化的数字，必选；
   digits : 要精确的小数位（四舍五入），可选，默认是2；
   isStrongFormat : 是否使用用强格式转换（如.9会转为0.90，3会转为3.00），可选，默认是false。
   注：本函数对于NaN、undefined、Infinity 均返回0
*/
/*	例子：formatFloat(2.1235) = 2.12; formatFloat(2.12512) = 2.13
		  formatFloat(2.1235, 3, true) = 2.124;  formatFloat(2.1, 2, true) = 2.10; format(.2, 4, true) = 0.2000
*/
function formatFloat(formatNumber, digits, isStrongFormat)
{
    var iDig=2, boolStrong=false;

    if(typeof(digits)=="number" && digits>0) {iDig=digits;}
    if(typeof(isStrongFormat)=="boolean") {boolStrong=isStrongFormat;}
    try{
        if(iDig>20) iDig=20;
        var sNum = formatNumber.toFixed(iDig);
        var sPreNum = "" + formatNumber;
        if(!boolStrong && sNum.length>sPreNum.length){
            sNum = sNum.substr(0,sPreNum.length);
        }
        return sNum;
    }catch(e){
        return "0";
    }
}