// 简单Util库 封装必要功能
if( typeof Util == 'undefined' )Util = {};
Util.isIE = (navigator.appName.indexOf("Microsoft",0) != -1 ) ? true:false;
Util.getBodyHeight = function(){
		var Dim = Util.getBodyDim();
		return Dim.H;
};
Util.getBodyDim = function(){
	var Dim = {};
	// Try to get the "inner width" and "inner height" 
	
	if (window.innerWidth) 
	{ 
		Dim.W = window.innerWidth ;
		Dim.H = window.innerHeight ;
	} 
	else if (document.documentElement && document.documentElement.clientWidth) 
	{ 
		Dim.W = document.documentElement.clientWidth ;
		Dim.H = document.documentElement.clientHeight ;
	} 
	else if (document.body) 
	{ 
		Dim.W = document.body.clientWidth ;
		Dim.H = document.body.clientHeight; 
	}	
	return Dim;
};
Util.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;
};
Util.randomString = function(string_length, first_alpha){
	var alphachars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var chars = alphachars + "0123456789";
	var randomstring = '';
	for (var i=0; i < string_length ; i++) {
		
		if(first_alpha && i == 0){
			var rnum = Math.floor(Math.random() * alphachars.length);
			randomstring += alphachars.substring(rnum,rnum+1);
		}
		else {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
	}
	return randomstring;
};
Util.setCookie = function(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
};
Util.getCookie = function(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
};
Util.AddEvent = function(el, name, cb, useCapture) {
    if (el.addEventListener) {      
      el.addEventListener(name, cb, useCapture);
    } else if (el.attachEvent) {
      el.attachEvent('on' + name, cb);
    }
};
Util.RemoveEvent = function(el, name, cb, useCapture) {
    if (el.removeEventListener) {      
      el.removeEventListener(name, cb, useCapture);
    } else if (el.detachEvent) {
      el.detachEvent('on' + name, cb);
    }
};
Util.StrFmt = function() {
	var fmt = arguments[0],tS = fmt.split("%s"),rtn = new Array(); 
	for(var i = 0; i < tS.length; i++){
		rtn.push(tS[i]);
		if( i + 1 < arguments.length && i != tS.length - 1)
			rtn.push(arguments[i + 1]);
	}
	return rtn.join(''); 
};


Function.prototype._Bind = function() { 
  var __m = this, object = arguments[0], args = new Array(); 
  for(var i = 1; i < arguments.length; i++){
		args.push(arguments[i]);
	}
  return function() {
  	var c_args = [];
		for(var k = 0; k < arguments.length; k++){
			c_args.push(arguments[k]);
		} 
		return __m.apply(object, c_args.concat(args));
  } 
} 

Function.prototype._BindForEvent = function() { 
  var __m = this, object = arguments[0], args = new Array();
  for(var i = 1; i < arguments.length; i++){
		args.push(arguments[i]);
	} 
  return function(event) { 
    return __m.apply(object, [( event || window.event)].concat(args)); 
  } 
}
//From DOJO lang/common.js
Util.Extend = function(obj, props){
	var tobj = {};
  var robj = new Util.cloneObject(obj);
	
	for(var x in props){
		// the "tobj" condition avoid copying properties in "props"
		// inherited from Object.prototype.  For example, if obj has a custom
		// toString() method, don't overwrite it with the toString() method
		// that props inherited from Object.protoype
		if(typeof tobj[x] == "undefined" || tobj[x] != props[x]) {
			robj[x] = props[x];
		}
	}
	// IE doesn't recognize custom toStrings in for..in
	if(Util.isIE && typeof props["toString"] == 'function' && props["toString"] != robj["toString"]) {
		robj.toString = props.toString;
	}
	return robj;
}

Util.cloneObject = function(what) {
    for (i in what) {
        this[i] = what[i];
    }
}


