function System1(){}

String.prototype.format=function(){
    var str=this;
    var matchs=str.match(/\{\d\}/ig);
    for(var i=0;i<arguments.length;i++){
        str=str.replace(eval('/\\{'+i+'\\}/ig'),arguments[i]);
    }
    return str;
};

System1.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" : "");
}

System1.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" : "");
}

System1.prototype.getCookie = function(name, key) {
    return this.cookie(name);
};

System1.prototype.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

System1.prototype.canClear = function() {
    var index1 = document.cookie.indexOf("9Sky_PassID=");

    if (index1 < 0) //如果没有找到,则不需要清
        return false;
    else {
        var index2 = document.cookie.indexOf("9Sky_PassID=", index1+1);

        if (index2 > 0) //说明有2个KEY,要清掉
            return true;
        else {  //只有一个的时间要判断,是不是有9SKY
            var temp1 = this.getCookie("9Sky_PassID");

            if (temp1.indexOf("9SKY") < 0) //发果没有9SKY,说明是临时Key
                return true;
            else
                return false;
        }
    }

    return false;
};


var system1 = new System1();

if (system1.canClear()) {
    system1.cookie("9Sky_PassID", null, { domain: "group.9sky.com", expires: -1 });
    system1.cookie("9Sky_Passporta", null, { domain: "group.9sky.com", expires: -1 });
    system1.cookie("9Sky_Passportb", null, { domain: "group.9sky.com", expires: -1 });
    system1.cookie("9Sky_Passportc", null, { domain: "group.9sky.com", expires: -1 });
    system1.cookie("9Sky_STack", null, { domain: "group.9sky.com", expires: -1 });
    system1.cookie("9Sky_Tack", null, { domain: "group.9sky.com", expires: -1 });
}

//登录
function login()
{
	window.location.href="http://home.9sky.com/passport/login.aspx?return="+escape(document.URL.toString());
}


//注册
function regist()
{	
	window.location.href="http://home.9sky.com/passport/register.aspx?return="+escape(document.URL);
}


var LoginOutHandler = function()
{
    document.getElementById("sUnLogin").style.display = "";
    document.getElementById("sLogin").style.display = "none";
   
    location.href = location.href.split('#')[0];
}
//退出
function loginOut()
{
	if(confirm("您确定要退出登录吗?"))
	{
		var url = "http://home.9sky.com/passport/inc/postlogout.aspx";
		var ifr = document.createElement("iframe");
		ifr.src = url;
		ifr.width = 0;
		ifr.height = 0;
		ifr.onreadystatechange = function()
		{
			switch(this.readyState)
			{
				case "complete":
				case "loaded":
					if (LoginOutHandler){LoginOutHandler();}
					break;
			}
		}
		document.body.appendChild(ifr);		
	}
}

//获取登录ID
function getUserID(){
	var passid=system1.getCookie("9Sky_PassID");
	if(passid=="" || passid == 0)
	{
		while(true)
		{
			passid=Math.random();
			passid=parseInt(passid.toString().substr(passid.toString().length-10,10));
			if (passid>1000000000 && passid<2000000000) break;
		}
		document.cookie="9Sky_PassID="+encodeURI(passid)+"; expires="+(new Date(9999,12,31)).toGMTString()+"; domain=9sky.com";
		return passid;
	}
	
	var arr=passid.split("\r");
	if(passid.length>0 && !/[^\d]/.test(arr[0])) return arr[0];
	return 0;
}

//获取登录Name
function getUserName(){
	var passid = system1.getCookie("9Sky_PassID");
	var arr=passid.split("\r");
	if(arr.length>1) 
	{
		return decodeURI(arr[1]);
	}
	return "";
}

//获取登录状态
function getLoginState()
{
	var v = system1.getCookie("9Sky_Passporta");
	if (v == "" || v == null) { v = system1.getCookie("9Sky_Passportb"); }
	if (v == "" || v == null) { return false; } else { return true; }
}

//是否已登录
function isLogin()
{
    return getLoginState();
}

//非成员提示
function nonMemberAlert()
{
    if(isLogin() == false)
        alert("请登录或注册!");
    else
        alert("此功能仅对圈内成员开放!");
    
    return false;
}
function copy_clip(copy){
    if (window.clipboardData){
    window.clipboardData.setData("Text", copy);}
    else if (window.netscape){
    try {        
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");        
    } catch (e){        
               alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");        
    }
          
    //netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
    if (!clip) return;
    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    if (!trans) return;
    trans.addDataFlavor('text/unicode');
    var str = new Object();
    var len = new Object();
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    var copytext=copy;
    str.data=copytext;
    trans.setTransferData("text/unicode",str,copytext.length*2);
    var clipid=Components.interfaces.nsIClipboard;
    if (!clip) return false;
    clip.setData(trans,null,clipid.kGlobalClipboard);}
    return false;
}
