if(window.addEventListener){FixPrototypeForGecko();}
function FixPrototypeForGecko(){
	HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);
	window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);
	Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);
}
function element_prototype_get_runtimeStyle(){
	//return style instead...
	return this.style;
}
function window_prototype_get_event(){
	return getEvent();
}
function event_prototype_get_srcElement(){
	return this.target;
}
function getEvent(){     //同时兼容ie和ff的写法
         if(document.all)    return window.event;        
         func=getEvent.caller;            
         while(func!=null){    
             var arg0=func.arguments[0];
             if(arg0){
                 if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
                     || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                     return arg0;
                 }
             }
             func=func.caller;
         }
         return null;
}

/**
 * 去空格
 */
String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
  }

function $N(name) {
  if (typeof name == 'string')
  		return  document.getElementsByName(name); //DOM Level 1
  else
  		return null;
}
/**
 * event
 */
function $E() {
  return event.srcElement ? event.srcElement : event.target;
}
//event code
function _event_code() {
  return event.keyCode ? event.keyCode : event.which;
}
//event.x
function _event_x() {
  return event.x ? event.x : event.pageX;
}
//event.y
function _event_y() {
  return event.y ? event.y : event.pageY;
}
//event.offsetX
function _offsetX() {
  return event.offsetX ? event.offsetX : event.layerX;
}
function _offsetY() {
  return event.offsetY ? event.offsetY : event.layerY;
}
function _innerText(obj) {
  return obj.innerText ? obj.innerText : obj.textContent;
}




/*
 *	judge object is null or blank character
 */
function isNull(obj){
	if (typeof(obj)=='undefined'||obj==null) return true;
	if (typeof obj == 'string') return obj.trim()=='';
	return false;
  } 

/*
 *	util is a tool
 *	
 */

// here defined a util;
if(typeof util=="undefined"){var util=new Object();}
util = {
isHTMLElement : function(ele, nodeName) {
  		if (ele == null || typeof ele != "object" || ele.nodeName == null) 
   			return false;
  		if (nodeName != null) {
    		var test = ele.nodeName.toLowerCase();
    	if (typeof nodeName == "string") { return test == nodeName.toLowerCase();}
			return false;
		}
  		return true;
		},
	
windowOpen : function (url,w,h){
		window.open(url,'name','width='+ w +',height='+ h +',resizable=no,scrollbars=no,modal=yes');
	},

/** 清除表单上的错误信息  **/
clearFormError : function (form){
		var _al=$(form).elements;
		for(var i=0,j=_al.length;i<j;i++){
		if (util._isInputOrSelect(_al[i])) $T(_al[i],'');}
	},

_isInputOrSelect : function (e){
	return ( util.isHTMLElement(e,"select") && e.type=='select-one')
		 || ( util.isHTMLElement(e,"input") && e.type=='text' );
	},
	
/**  使(表单)有效 或 失效  **/
disableForm : function(form,b){
	var _e=$(form).elements;_e.disabled=b;
	for(var i=0;i<_e.length;i++)
		if(util.isHTMLElement(_e[i],"select")||util.isHTMLElement(_e[i],"input"))
		if(_e[i].type=='text'||_e[i].type=='select-one')
				_e[i].disabled=b;	
},

/**  重置表单 [arguments[1]]参数是是否强制重置b 已被 disabled的表单项 
如果 第二个参数是 true 的话, 已被 disabled 的表单项 不会被重置清空
**/
clearForm : function (form){
	var _b = arguments.length > 1 && arguments[1] == true;
	if (!_b) $(form).reset();
	var _e=$(form).elements;//_e.disabled=false;
	for(var i=0;i<_e.length;i++)
		if(util.isHTMLElement(_e[i],"select")||util.isHTMLElement(_e[i],"input")||util.isHTMLElement(_e[i],"textarea"))
		{
			if ( _b && _e[i].disabled == true){ continue;}
			if(_e[i].type=='text')
				_e[i].value='';	
			if(_e[i].type=='hidden')
				_e[i].value='';	
			if(_e[i].type=='textarea')
				_e[i].value='';	
			if(_e[i].type=='select-one')
				_e[i].selectedIndex=0;
		}
},
/**	删除select 表单选项所有Option	**/
removeAllOptions : function(ele_id){
	$(ele_id).options.length = 0;
	},
/*	删除表格 或 某个项目的所有子元素,包括table,tr,select,etc.	*/
removeAllRows : function(ele_id){
	util.removeAllChilds(ele_id);
},
removeAllChilds : function (){
	$A(arguments).each(function(node){
		var s=$(node);
		var child = s.firstChild;
  		while (child != null) {
    		s.removeChild(child);
			child = s.firstChild;}});
},
addOption :function(obj,text,value,selected) {
	obj = $(obj);
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	obj.toggle().toggle();
	}

}//结束定义  

/*
 *	show and hide switch for basic and adv in search.jsp
 */
function showHide(){
	for(var i=0,l=arguments.length;i<l;i++)
		$(arguments[i]).toggle();
}
/*
 *	get date string as format 'yyyy-mm-dd'
 */
function getCurrentDate(){
	var date = new Date();
	return dateToStr(date);
}
/*
 *	initialize date as format 'yyyy-mm-dd' for input
 */
function initdate(inputName){  
	$(inputName).value=getCurrentDate();
	}

/*
 *	check all input according to chkall(click input name must be "chkall")
 */
function check_all(formName,chkall){
	var form = $(formName);
	var check_all = $(chkall);
	for (var i=0,j=form.elements.length;i<j;i++){
		var e = form.elements[i];
		if(e == check_all){continue;}
		if (e.name == check_all.name){
			e.checked = check_all.checked;
		}
	}
}
	
/*
 *	back
 */
function back(){ history.back(); }

function isIE(){ return (/MSIE/.test(navigator.userAgent) && !window.opera);}

//代替 IE 的 replaceAdjacentText
function $T(obj,str){
if (isIE()) $(obj).replaceAdjacentText('afterEnd',str);
else {	
	if ( $(obj).next() != undefined && $(obj).next().nodeName=='SPAN') $(obj).next().innerHTML = str ;
	else new Insertion.After($(obj),'<span class=errorMessage>' + str + '</span>');
		}
}

/* TODO 将num按小数位为dotLen来进行格式化  如无小数位参数则为2位小数
 *	format number 格式化数字
 *  @dLen dot length  小数位数
 */
function num_format(num,dotLen) {
    var dot=0;
    var num1=0;
    if (typeof dotLen=="undefined")
        dot=2;
    else
        dot=dotLen;
    if (isNaN(parseFloat(num)))
        return 0;
    else
        num1=parseFloat(num);
    var n1=Math.pow(10,dot);
    if (n1==0)
        var iValue= Math.round(num1);
    else
        var iValue=    Math.round(num1*n1)/n1;
  var sValue = iValue.toString();
  if (sValue.indexOf(".") == -1)
  {
      sValue = sValue + ".00";
  }
  else
  {
      if (sValue.indexOf(".") == sValue.length - 1)
      {
          sValue = sValue + "00";
      }
      else if (sValue.indexOf(".") == sValue.length - 2)
      {
          sValue = sValue + "0";
      }
  }
return sValue;
}

/*
 *  private method
 *	parseFloat and reserve the zero
 *  @n 
 *  return	2.00   	_float('2.00q')
 *  		2.   	_float('2.a')
 */
function _float(n){
	if (isNull(n)||n==0)return n;if (isNaN(parseFloat(n))) return 0;
    else {
		var s0,s=n.toString().trim().split('.');
		s0=parseInt(s[0],10);if (s.length>1) s0+='.'+_Int(s[1]);
		return s0;
	}
function _Int(v){
		if(isNull(v)||v==0)return v;if(isNaN(parseInt(v,10))) return '';
		var n=parseInt(v,10);return Math.pow(10,v.indexOf(n)).toString().substr(1)+n;
	}
}
/*
 *	check if num is a number
 *  @num  must be a string //判断是否是数字
 */
function isNum(num){
	  re = /^\d+$/; // if (typeof num == 'number') num = num.toString(); 
	  return !isNull(num)&&re.test(num.trim());
}

/*
 * params: yyyy-MM-dd
*/
function conventDate(cdate)
{
	if(cdate==null || cdate==''){return null;}
	var less1st = cdate.indexOf('-');
	var less2nd = cdate.lastIndexOf('-');
	var y = cdate.substring(0,less1st);
	var m = cdate.substring(less1st+1,less2nd);
	var d = cdate.substring(less2nd+1,cdate.length);
	return new Date(y,m-0-1,d);
}
function dateToStr(cdate)
{
	if(cdate==null || cdate==''){return null;}
	with(cdate){
		var year = getFullYear(),
			month = getMonth()+1,
			day = getDate();
	}
	if(month<10) month="0"+month;
	if(day<10) day="0"+day;
	return year+"-"+month+"-"+day;
}

