var domain = window.location.hostname.split('.').slice(-2).join('.');
var main_url = 'http://www.'+domain;
//try{document.domain=domain;}catch(e){};

var G = {
	$: function(id){
		if(typeof id == 'string')
			return document.getElementById(id);
		return id;
	},
	trim: function(str){
		return str.replace(/^\s+|\s+$/g,'');
	},
	time: function (){
		return new Date().getTime();
	},
	bind: function(obj, eventHandler,fun){
		if(obj.attachEvent)
			obj.attachEvent('on'+eventHandler,fun);
		else if(obj.addEventListener)
			obj.addEventListener(eventHandler,fun,false);
	},
	ready: function(f){
		if (this.ready.done) 
			return f(); 
		if (this.ready.timer){  
			this.ready.readys.push(f); 
		} else {
			if(window.addEventListener)
				window.addEventListener('DOMContentLoaded',function(){_this.ready.readied=true;checkReady();},false);
			this.bind(window, "load", function(){_this.ready.readied=true;checkReady();}); 
			this.ready.readys = [f];  
			this.ready.timer = setInterval(function(){checkReady();}, 13); 
		}
		var _this = this;
		function checkReady(){
			if (_this.ready.done) 
				return false;
			if(!_this.ready.readied&&document.documentElement.doScroll){
				try{document.documentElement.doScroll('left');_this.ready.readied=true;}catch(e){}
			}
			if (_this.ready.readied){
				clearInterval(_this.ready.timer);  
				_this.ready.timer = null;   
				_this.ready.done = true;
				_this.ready.readied = null;
				for (var i=0; i<_this.ready.readys.length; i++)  
					_this.ready.readys[i](); 
				_this.ready.readys = null; 
			}
		};
	},
	loadJs: function(url, func, remove){
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = url;	
		
		if(typeof func != 'undefined' || typeof remove != 'undefined' && remove){
			script.onload = function(){
				if(typeof func == 'function') func();
				if(remove) script.parentNode.removeChild(script);
			};
			script.onreadystatechange = function(){
				if (this.readyState == 'complete' || this.readyState == 'loaded') {
					if(typeof func == 'function') func(); 
					if(remove) script.removeNode(true);
				}
			};
		}
		head.appendChild(script);
	},
	loadIframe: function(name_id, url, func, remove){
		try{
			var iframe = document.createElement('<iframe name="'+name_id+'"></iframe>');
		}catch(e){
			var iframe = document.createElement('iframe');
			iframe.name = name_id;
		}
		iframe.id = name_id;
		iframe.src = url;
		
		if(typeof func != 'undefined' || typeof remove != 'undefined' && remove){
			iframe.onload = function(){
				if(typeof func == 'function') func();
				if(remove) iframe.parentNode.removeChild(iframe);
			};
			iframe.onreadystatechange = function(){
				if (this.readyState == 'complete' || this.readyState == 'loaded') {
					if(typeof func == 'function') func(); 
					if(remove) iframe.removeNode(true);
				}
			};
		}
		document.body.appendChild(iframe);
	},
	goIframe: function(name_id, url, referer){
		window.frames[name_id].location = url;
		if(typeof referer != 'undefined'){
			var iframe = document.getElementById(name);
			iframe.onload = function (){
				iframe.onload=null;
				window.frames.asyn.location = referer;
			};
			iframe.onreadystatechange = function() {
				if (this.readyState == 'complete' || this.readyState == 'loaded') {
					iframe.onreadystatechange = null;
					window.frames.asyn.location = referer;
				}
			};
		}
	},
	setCookie: function (name, value, seconds, path, domain, secure) {
		document.cookie = escape(name) + '=' + escape(value)
			+ (seconds ? '; expires=' + new Date(new Date().getTime() + seconds * 1000).toGMTString() : '')
			+ (path ? '; path=' + path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : '');
	},
	getCookie: function (name) {
		var start = document.cookie.indexOf(name);
		var end = document.cookie.indexOf(";", start);
		if(start != -1){
			return unescape(document.cookie.substring(start + name.length + 1, (end != -1 ? end : document.cookie.length)));
		}
		return null;
	},
	copy: function (text, note){
		if (window.clipboardData){
			window.clipboardData.setData("Text", text);
			if(note){
				alert(note);
			}
		}else{
			alert("\u62b1\u6b49\uff0c\u8be5\u529f\u80fd\u76ee\u524d\u53ea\u652f\u6301ie\u6d4f\u89c8\u5668\u3002\u8bf7Ctrl+C\u6216\u53f3\u952e\u590d\u5236\u3002");
		}
		return false;
	},
	urlencode: function(text){
		return encodeURIComponent(text);
	},
	urldecode: function(text){
		return decodeURIComponent(text);
	},
	addslashes: function(text){
		return text.replace(/\\/g,'\\\\').replace(/'/g, "\\'").replace(/"/g, '\\"');
	},
	htmlspecialchars: function(text){
		return text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g,'&#039;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	},
	click: function(obj){
		if(obj.click){
			obj.click();
		}else{
			var evt = document.createEvent("MouseEvents");   
    		evt.initEvent("click", true, true);   
    		obj.dispatchEvent(evt);
		}
	},
	rand: function(len, type, cases){//type: 1 数字 2 字母 3 数字和字母 case: 1 小写 2 大写 3 大小写
		var rand_arr;
		var num = [0,1,2,3,4,5,6,7,8,9];
		if(type == 1){
			rand_arr = num;
		}else if(type == 2 || type == 3){
			var low = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
			var up = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
			if(cases == 1){
				rand_arr = (type == 3 ? num.concat(low) : low);
			}else if(cases == 2){
				rand_arr = (type == 3 ? num.concat(up) : up);
			}else if(cases == 3){
				rand_arr = (type == 3 ? num.concat(low, up) : low.concat(up));
			}
		}
		var rand = '';
		for(var i=0; i<len; i++){
			rand += rand_arr[Math.round(Math.random()*(rand_arr.length-1))];
		}
		
		return rand;
	}
}
