// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value) {
    this.strings = new Array("");
    this.append(value);
};
// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function(value) {
    if (value) {
        this.strings.push(value);
    }
};
// Clears the string buffer
StringBuilder.prototype.clear = function() {
    this.strings.length = 1;
};
// Converts this instance to a String.
StringBuilder.prototype.toString = function() {
    return this.strings.join("");
};

/**
* The simple way for an Ajax request, use onRequest/onComplete/onFailure
* to do add your own Ajax depended code.
*/
function ajax(url, id) {
    new Request.JSON({
        url: url,
        onComplete: function(json, text) {
            //json = eval('('+text.cleanHTML()+')');
            $(id).set('html', text);
        }
    }).get()
};

//target _blank and main
function externallinks() {

    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    //alert(anchors.length);
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
            anchor.hidefocus = 'true';
            anchor.target = "_blank";
        }
        else if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "main") {
            anchor.target = "main";
        }
    }
}
function template(name) {
    return $(name).innerHTML.replace('<![CDATA[', '').replace(']]>', '').trim();
}
window.onload = externallinks;


function openWin(theURL, winName, features) {
    window.open(theURL, winName, features);
}

function locationWin(theURL, winName) {
    window.open(theURL, winName);
}

function getUrl() {
    var url = document.URL;
    var domain = document.location.host;
    var urlprotocol = document.location.protocol;
    //var urlport = document.location.port;
    var sitesecond = "";
    if (url.indexOf("9DiamondWeb") != -1) {
        sitesecond = "9DiamondWeb/";
    }
    if (url.indexOf("cs3") != -1) {
        sitesecond = "9DiamondWeb/";
    }
    var siteUrl = urlprotocol + "//" + domain + "/" + sitesecond;
    return siteUrl;
}
function getUrlAnchor() {
    var url = document.URL;
    var a = url.split('#');
    var retval = '';
    if (a.length > 1) {
        retval = '#' + a[1];
    }
    retval = '###';
    if (retval == '#')
        retval = '###';
    return retval;
}
//function getSkinPath() {
//    return getUrl() + 'Themes/default/';
//}


function getSkinPath() {
    return getUrl() + 'Themes/default/';
}

function getPicturePath(){
    return 'http://pic.9diamond.com/';
}

function getSwfPath(){
    return 'http://swf.9diamond.com/';
}

function getJsPath(){
    return 'http://js.9diamond.com/';
}

function setClipboard(maintext) {
    if (window.clipboardData) {
        return (window.clipboardData.setData("Text", maintext));
    }
    else {
        if (window.netscape) {
            try {
                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 = maintext;
                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 true;
            }
            catch (e) {
                return "您的firefox安全限制限制您进行剪贴板操作。";
                //return false;
            }
        }
    }
    return false;
}
var HideObject = [];


function alertMessage(text, url, url2) {
    //避免IE出现“Internet Explorer 无法打开Internet站点……,已终止操作”
    if (arguments.length == 1) {
        window.setTimeout(function() {
            new EMessage('', {
                alertText: text,
                closeImageUrl: getPicturePath() + 'ctrl/alert_close.gif'
            }).start();
        }, 200);
    } else if (arguments.length == 2) {
        window.setTimeout(function() {
            new EMessage('', {
                alertText: text,
                closeImageUrl: getPicturePath() + 'ctrl/alert_close.gif',
                createButton: { display: 'block', data: [
	            { buttonType: '确定', buttonUrl: url, buttonEvent: '', buttonStyle: '', buttonCss: '' }
	            ]
                }
            }).start()
        }, 200);
    }
}

//function makekeywords()	//根据页面的Mate标记中的Keywords ,用于生成广告单元链接对应的mk参数。
//{
//	var anchors = document.getElementsByTagName("meta");
//	var key='';
//	try{
//		for (var i=0; i<anchors.length; i++) {
//			var anchor = anchors[i];
//			if (anchor.getAttribute("name") == "Keywords"){
//				key = anchor.content;
//			}
//		}
//		key=key.replace(/[/| ，、　]/g,',');	//合关键字以逗号分隔。
//		if (key.length>200)
//		{
//			var tmpstr = key.substring(192,key.length-1);
//			var pos = tmpstr.indexOf(",");
//			key = key.substring(0, pos+192);
//		}
//	}catch(e){
//	}
//	return key;
//}
//function AddPanel()
//{
//	var title = document.title;
//	var url = document.URL;
//	var desc = makekeywords();
//	if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))//Gecko
//	{
//		window.sidebar.addPanel(title,url,desc);
//	}
//	else//IE
//	{
//		window.external.AddFavorite(url,title);
//	}
//}
//function AddMyHome(){
//	var url = document.URL;
//	obj = this;
//	//alert(obj+'--'+this);
//	try{
//		
//		obj.style.behavior='url(#default#homepage)';
//		obj.setHomePage(url);
//	}
//	catch(e){
//		if(window.netscape) {
//			try {
//				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
//			}  
//			catch (e)  { 
//				alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
//				return;
//			}
//			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
//			prefs.setCharPref('browser.startup.homepage',url);
//		}
//	}
//}

function stringToEntities(s) {
    var r = "", c;
    for (var i = 0; i < s.length; i++) {
        c = s.charCodeAt(i);
        r += (c < 32 || c == 38 || c > 127) ? ("&#" + c + ";") : s.charAt(i);
    }
    return r;
}
function stringToHex(s) {
    var r = new StringBuilder();
    r.append("0x");
    var hexes = new Array("A", "J", "E", "V", "0", "L", "E", "7", "8", "9", "A", "B", "C", "D", "E", "A");
    for (var i = 0; i < s.length; i++) {
        r.append(hexes[s.charCodeAt(i) >> 4] + hexes[s.charCodeAt(i) & 0xF]);
    }
    return r.toString();
}
function hexToString(h) {
    var r = new StringBuilder();
    for (var i = (h.substr(0, 2) == "0x") ? 2 : 0; i < h.length; i += 2) {
        r.append(String.fromCharCode(parseInt(h.substr(i, 2), 16)));
    }
    return r.toString();
}
function SignToHEX(s) {
    var r = new StringBuilder();
    var c;
    var regexp = new RegExp("^[a-zA-Z\u4e00-\u9fa5\u3040-\u309F\u30A0-\u30FF]+$");
    for (var i = 0; i < s.length; i++) {
        if (regexp.test(s.substr(i, 1)))
            r.append(s.substr(i, 1));
        else
            r.append(encodeURIComponent(s.substr(i, 1)));
    }
    return r.toString();
}
function controlImage(w, h, id) {
    function DrawImage(ImgD) {
        var image = new Image();
        image.src = ImgD.src;
        if (image.width > 0 && image.height > 0) {
            if (image.width < w && image.height < h) {
                ImgD.width = image.width;
                ImgD.height = image.height;
                ImgD.title = ImgD.width + "x" + ImgD.height;
            } else {
                var theScaleWidth = w / image.width;
                var theScaleHeight = h / image.height;
                if (theScaleWidth < theScaleHeight) {
                    var scale = theScaleWidth;
                } else {
                    var scale = theScaleHeight;
                }
                ImgD.width = image.width * scale;
                ImgD.height = image.height * scale;
                ImgD.title = ImgD.width + "x" + ImgD.height;
            }
        }
    };
    var t = null;
    try {
        t = $(id).getElements('img');
    } catch (e) {
        return;
    };
    for (i = 0; i < t.length; i++) {
        DrawImage(t[i]);
    }
}

function clearNoNum(obj) {
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g, "");
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g, "");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g, ".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}

function EasyOptions(obj) {
    this.Obj = obj;
    var tabs = this.Obj.getElement('div[class*=tabs]');
    var tabsLI = tabs.getElements('li');
    var box = this.Obj.getElement('div[class*=items]');
    var items = box.getElements('div[class^=item]');


    tabsLI.each(function(li, i) {
        li.addEvent('click', function(event) {
            if (event)
                event.stop();
            li.d = i;
            items.each(function(item, j) {
                item.setStyle('display', 'none');
                tabsLI[j].removeClass('on');
                if (li.d == j) {
                    item.setStyle('display', 'block');
                    tabsLI[j].addClass('on');
                }
            })

        })
    });
};
function AddJavaScript(src) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = src;
    document.getElementById('footerJS').appendChild(script);
}
