

//把函邦定到对象上
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!String.trim){
	String.prototype.trim  =function()
	{
		return this.replace(/(^\s*)|(\s*$)/g,"")
	}
}
if (!String.isMail){
	String.prototype.isMail=function()
	{
		return /^\w+([-+._]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this);
	}
}

if(!String.isDate){
	String.prototype.isDate=function(){
		var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
		 if(r==null){
			  return false;
		 } 
		 var d= new Date(r[1], r[3]-1, r[4]); 
		 if(!(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])){  
			  return false;
		 }
		 return true;	
	}
}
//继承
Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

if (!window.Element)  var Element = new Object();
Element.getOpacity = function(element){  
  var opacity;
  if (opacity = Element.getStyle(element, 'opacity'))   return parseFloat(opacity);  
  if (opacity = (Element.getStyle(element, 'filter') || '').match(/alpha\(opacity=(.*)\)/))  
    if(opacity[1]) return parseFloat(opacity[1]) / 100;  
  return 1.0;  
};

Element.setOpacity = function(element, value){  
  element= $(element);  
  if (value == 1){
    Element.setStyle(element, { opacity: 
      (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
      0.999999 : null });
    if(/MSIE/.test(navigator.userAgent))  
      Element.setStyle(element, {filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
  } else {  
    if(value < 0.00001) value = 0;  
    Element.setStyle(element, {opacity: value});
    if(/MSIE/.test(navigator.userAgent))  
     Element.setStyle(element, 
       { filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') +  'alpha(opacity='+value*100+')' });  
  };   
} ; 

if (!Element.makePositioned2){
  Element.makePositioned2=function(element) {
    	element = $(element);
	    element.style.position=(document.all)?'absolute':'fixed';	
        element._madePositioned = true;
	}	
};
//---------------------------------------------------------------------------------------------------------------------------------------
//原始类
var Class = {
  create: function() {
    return function() {//alert(arguments[0].easyClose)
      this.initialize.apply(this, arguments);
    }
  }
}

//根据ID限对像 ，相当于document.getElementById

if((typeof $) == undefined){
	var $ = function () {
	  var results = [], element;
	  for (var i = 0; i < arguments.length; i++) {
		element = arguments[i];	
		if (typeof element == 'string')
		  element = document.getElementById(element);
		results.push(element);		
	  }
	  return results.length < 2 ? results[0] : results;
	};
};
//转换成数组
function $A(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
};

//把{}结构转换成url字串
function escapeString(h){
	if(!h) return '';
	var resultValue = [];
	for(var param in h){
		resultValue.push(param + '=' + escape(h[param]));
	};
	return resultValue.join('&');
};
function transXslt(xml,xslt){
	//因为xml可能是由 createXmlDoc()函数建立的空XML,
	//所以无法用.documentElement.tagName来检查XML是否失败
	
	//检查是否XSL文件出错
	var xslError=false ;
	try{  
		var xsl=xslt.documentElement.tagName;
		if (!xsl) throw new Error(99997,"转换XSL文件失败");
	}catch(x){
		xslError=true;
	}
	//尝试转换，如果转换失败，则可判定XML失败
	
	try{
			if(window.ActiveXObject){	//alert(xml.xml);alert(xslt.xml)
				return xml.transformNode(xslt);	
			}
			else
			{
				var xsltProcessor = new XSLTProcessor();
				xsltProcessor.importStylesheet(xslt);
				var fragment=xsltProcessor.transformToFragment(xml,document);
				var tDiv = document.createElement('div');
				tDiv.appendChild(fragment);		
				var tHTML = tDiv.innerHTML;
				tDiv.innerHTML = '';
				var tDiv = null;
				xsltProcessor = null;
				return tHTML;
			}
	}catch(x){	
		if 	(pDefaultHtml) return pDefaultHtml;
		if  (xslError)   return "<p>读取XSL文件时失败,请检查XSL格式是否正确</p>";
		return "<p>读取XML数据时失败,请检查服务器返回数据是否正确<p>";
	}
};

//把一段html代码插入指定的对象最后面
function insertBottom(obj, content){
	var element = $(obj);	
	if(element.insertAdjacentHTML){			
		if(element.tagName == 'TABLE'){//alert(element.id)
			//在表格中不能直接以HTML的方式把一行插入进到， 所以上面的方法不行
			//try{
			var oDiv=document.createElement("div"),aRows;
			oDiv.innerHTML = "<table>"+content+"</table>";
			aRows= oDiv.childNodes[0].tBodies[0].rows;
			while(aRows.length>0){
				obj.tBodies[0].appendChild(aRows[0]);
			}
			//}catch(e){alert('e.description')}
		}else{
			element.insertAdjacentHTML('beforeEnd', content);	
		}
		aRows = null;
		oDiv = null;
	}else{
		var oRange = element.ownerDocument.createRange();				
  		initializeRange(oRange,element);				
		insertContent([oRange.createContextualFragment(content)],element);
		oRange = null;
	}
	function initializeRange(range,element){
		range.selectNodeContents(element);
		range.collapse(element);
	};
	
	function insertContent(fragments,element) {
		for(var i = 0; i < fragments.length; i++){
			element.appendChild(fragments[i]);
		};
	};
};






//取页面宽高
function getPageSize(){
	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	};
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	};
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	};

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	};

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) ;
	return arrayPageSize;
};
//检查身份证号码 
function checkId(pId){
    var arrVerifyCode = [1,0,"x",9,8,7,6,5,4,3,2];
    var Wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
    if(pId.length != 15 && pId.length != 18)    return "身份证号共有 15 码或18位";

    var Ai=pId.length==18 ?  pId.substring(0,17)   :   pId.slice(0,6)+"19"+pId.slice(6,16);

    if (!/^\d+$/.test(Ai))  return "身份证除最后一位外，必须为数字！";

    var yyyy=Ai.slice(6,10) ,  mm=Ai.slice(10,12)-1  ,  dd=Ai.slice(12,14);

    var d=new Date(yyyy,mm,dd) ,  now=new Date();
     var year=d.getFullYear() ,  mon=d.getMonth() , day=d.getDate();

    if (year!=yyyy || mon!=mm || day!=dd || d>now || year<1940) return "身份证输入错误！";

    for(var i=0,ret=0;i<17;i++)  ret+=Ai.charAt(i)*Wi[i];    
    Ai+=arrVerifyCode[ret %=11];     

    return pId.length ==18 && pId != Ai?"身份证输入错误！":pId;        
};
var Js_overLay = Class.create();
var mainOverLay=null;      //半透明的遮罩层对象
Js_overLay.prototype={
		_div:null,
		_iframe:null,
		initialize:function(options,aa){
			var arr=getPageSize();
	
			var options = Object.extend(
					{
		    		  easyClose : false,
				      opacity	: 0.6,
				      top		: 0,
				      left		: 0,
				      width		: arr[2],
				      height	: arr[1]
				    }, options || {});

			
			var wrap = document.createElement('iframe');
			var wrap2 = document.createElement('div');
			opacity=options.opacity;
			if (document.all){
				wrap.style.cssText="position:absolute;filter:Alpha(opacity=50);overflow:hidden;display:none;z-index:10; ";
				wrap2.style.cssText="position:absolute;filter:Alpha(opacity=50);overflow:hidden;display:none ;z-index:10;";		
			}else{
				wrap.style.cssText="position:fixed;-moz-opacity:0.5;overflow:hidden;display:none";
				wrap2.style.cssText="position:fixed;-moz-opacity: 0.5;overflow:hidden;display:none"	;	
			}
			//Element.setOpacity(wrap2,options.opacity);
		
			wrap.style.left=wrap2.style.left=options.left;
			wrap.style.top=wrap2.style.top=options.top;
			wrap.style.width=wrap2.style.width=options.width;
			wrap.style.height=wrap2.style.height=options.height;

			wrap.setAttribute("scrolling","no");
			wrap2.style.backgroundColor="#000000";		
			document.body.appendChild(wrap);
			document.body.appendChild(wrap2);
			if (options.easyClose) Event.observe(wrap2,'click',function(){wrap2.style.display=wrap.style.display="none"});		
			this._div=wrap2;
			this._iframe=wrap;
			return wrap2;
		},
		show:function(){
			
			this._div.style.display=this._iframe.style.display="";	
			},
		hide:function(){this._div.style.display=this._iframe.style.display="none"},
		remove:function(){this._div.parentNode.removeChild(this._div);this._iframe.parentNode.removeChild(this._iframe)}
};


function closeModelWin(obj,remove){
	var o=obj;
	while(!/divmenu/.test(o.id)) 	o=o.parentNode	;	
	if (remove){
		Element.remove(o);	
	}else{
		Element.hide(o);
	};
	if (mainOverLay && mainOverLay._div) {
		$(mainOverLay._div).remove();
		$(mainOverLay._iframe).remove();
		mainOverLay=null;
	};	
};

function createModelWin(id,options)  
{
	options=options || {}	;
	if (options.lock!="true") options.nooverlay=true;
	var arr=getPageSize();
	
	var win=null;

	var htmlFromAjax=function(html){			
		var root="divmenu"+id;

		if (!$(root))
		{
			var _div=document.createElement("div");	
			_div.id=root;
			_div.style.border="0px solid red";			
			document.body.appendChild(_div);
			Element.makePositioned2(_div);
			insertBottom(_div,html);	
			if (options.nooverlay)
			{
				var oFrame = document.createElement('iframe');
				oFrame.className="overlay";
				Element.setOpacity(oFrame,1.1);
				Element.makePositioned2(oFrame);
				_div.appendChild(oFrame);
				Position.clone(_div,oFrame);
				oFrame.style.top = '0px';
				oFrame.style.left = '0px';
				oFrame.style.zIndex = -1;
				//oFrame.style.borderStyle = 'none';
				oFrame.style.width="98%";
				oFrame.style.height="98%";		
			};
			//_div.onmousedown=function(){Block_toFront(_div)};			
		};
		win=$(root);
		setTimeout(function(){
			$(root).style.height=$(root).firstChild.offsetHeight+"px";
			$(root).style.width=$(root).firstChild.offsetWidth+"px";
		
			var w=options.width || win.offsetWidth;
			var h=options.height || win.offsetHeight;
		
			win.style.width=w+"px";
			win.style.height=h+"px";
		
			Position.prepare();	
			var l=options.left || (arr[2]-w)/2;
			var t=options.top || (arr[3]-h)/2+Position.deltaY;
			

			win.style.left=l+"px";
			//if(window.mainClip) win.style.top=mainClip.y1+10+"px"	
			//if (options.top)
			win.style.top=t+"px";
			//if (window.adsorbDragClass) win.style.top=(adsorbDragClass.getInfo($('mainTable')).top+10)+"px"	
		},100);
		
		if (!options.nooverlay) showOverlayDiv(win)	;	
		//zIndex
		win.style.zIndex = 100;
		win.style.display = '';
		if (options.afterCreate) options.afterCreate(win);
		try{
			var titleBar=_div.getElementsByTagName("TR")[0];
		}catch(x){var titleBar=_div};			
		if (typeof Drag == 'object' && options.lock!="true" || options.drag=="drag")	Drag.init(titleBar,_div,{minX:5,minY:5});					

	};
	
	if (options.html) return htmlFromAjax(options.html);	

	function showOverlayDiv(win)   //建立半透明的遮罩层
	{
		
		if (mainOverLay==null || mainOverLay._div==null) {
			if (options.opacity){
				mainOverLay=new Js_overLay({opacity:options.opacity});	
			}else{
				mainOverLay=new Js_overLay();
			}
		};
		mainOverLay.show();
		//Block_toFront(win)	;	
		if (options.easyClose) {
			mainOverLay._div.onclick=function(){closeModelWin(win)};
		}else{
			mainOverLay._div.onclick=null;
		}
	};	
			
	
};


//IE下彩旗内存
function free(){
	if(document.all)CollectGarbage();//释放内存
}




