﻿//返回一个随机数
function rObjId(){
	var d=new Date();
	var objId=d.getTime().toString()+(parseInt(Math.random()*100000)).toString();	
	return objId;
}
//标签名称,目标对象,标签样式,标签值
function t_cElement(strTagName,toObj,strClassName,strCssText,strValue,colSpan,rowSpan){
	var cObj=null;
	var r=new RegExp("\\S+","gi");
	switch(strTagName){
		case "div":{
			cObj=document.createElement("div");
			if(typeof(toObj)=="object"){toObj.appendChild(cObj);}
			if(r.test(strClassName)){cObj.className=strClassName;}
			r.compile("\\S+","gi");			
			if(r.test(strCssText)&&typeof(strCssText)!="undefined"){cObj.style.cssText=strCssText;}		
			if(r.test(strClassName)&&typeof(strClassName)!="undefined"){cObj.className=strClassName;}
			if(typeof(strValue)=="object"){
				cObj.appendChild(strValue);
			}else{
				r.compile("\\S+","gi");
				if(r.test(strValue)&&typeof(strValue)!="undefined"){
					cObj.innerHTML=strValue;
				}
			}			
		};break;
		case "text":{
			cObj=document.createElement("input");
			cObj.setAttribute("type","text");
			if(typeof(toObj)=="object"){toObj.appendChild(cObj);}
			if(r.test(strClassName)){cObj.className=strClassName;}
			r.compile("\\S+","gi");			
			if(r.test(strCssText)&&typeof(strCssText)!="undefined"){cObj.style.cssText=strCssText;}		
			if(r.test(strClassName)&&typeof(strClassName)!="undefined"){cObj.className=strClassName;}
			if(typeof(strValue)=="object"){
				cObj.appendChild(strValue);
			}else{
				r.compile("\\S+","gi");
				if(r.test(strValue)&&typeof(strValue)!="undefined"){
					cObj.value=strValue;
				}
			}			
		};break;
		case "button":{
			cObj=document.createElement("input");
			cObj.setAttribute("type","button");
			if(typeof(toObj)=="object"){toObj.appendChild(cObj);}
			if(r.test(strClassName)){cObj.className=strClassName;}
			r.compile("\\S+","gi");			
			if(r.test(strCssText)&&typeof(strCssText)!="undefined"){cObj.style.cssText=strCssText;}		
			if(r.test(strClassName)&&typeof(strClassName)!="undefined"){cObj.className=strClassName;}
			if(typeof(strValue)=="object"){
				cObj.appendChild(strValue);
			}else{
				r.compile("\\S+","gi");
				if(r.test(strValue)&&typeof(strValue)!="undefined"){
					cObj.value=strValue;
				}
			}			
		};break;
		case "span":{
			cObj=document.createElement("span");
			if(typeof(toObj)=="object"){toObj.appendChild(cObj);}
			if(r.test(strClassName)){cObj.className=strClassName;}
			r.compile("\\S+","gi");			
			if(r.test(strCssText)&&typeof(strCssText)!="undefined"){cObj.style.cssText=strCssText;}		
			if(r.test(strClassName)&&typeof(strClassName)!="undefined"){cObj.className=strClassName;}
			if(typeof(strValue)=="object"){
				cObj.appendChild(strValue);
			}else{
				r.compile("\\S+","gi");
				if(r.test(strValue)&&typeof(strValue)!="undefined"){
					cObj.innerHTML=strValue;
				}
			}			
		};break;
	}
	return cObj;
}

function t_cDivDigBody(title,w,h,isMax){
	var objId=rObjId();
	if(parseInt(w)=="NaN"||typeof(w)=="undefined"){w=400;}
	if(parseInt(h)=="NaN"||typeof(h)=="undefined"){h=300;}
	var cHeight=document.body.scrollHeight;
	var cWidth=t_clientWidth();
	var clientHei=t_clientHeight();
	var cBg=t_cElement("div",document.body,"t_cDivDigBgCss","width:"+cWidth+"px;height:"+cHeight+"px;");
	cBg.setAttribute("objId",objId);	
	cBg.oncontextmenu=function(){return false;}
	var cBody=t_cElement("div",document.body,"t_cDivDigBodyCss","width:"+w+"px;height:"+h+"px;left:"+((cWidth-w)/2)+"px;top:"+((clientHei-h)/2)+"px;");
	cBody.oncontextmenu=function(){return false;}
	cBody.setAttribute("objId",objId);	
	cBody.setAttribute("lp",((cWidth-w)/2));	
	cBody.setAttribute("rp",((cHeight-h)/2));	
	cBody.setAttribute("wp",w);	
	cBody.setAttribute("hp",h);	
	
	var cTitle=t_cElement("div",cBody,"t_cDivDigTitleCss");
	t_cElement("div",cTitle,"t_cLeftTitleDivCss","width:"+(w-30)+"px",title);	
	var cClose=t_cElement("div",cTitle,"t_cCloseDivCss");
	cClose.setAttribute("title","关闭");
	addEventHander(cClose,"mouseover",function(e){(e.srcElement||e.target).className="t_cCloseDivCss_1";});
	addEventHander(cClose,"mouseout",function(e){(e.srcElement||e.target).className="t_cCloseDivCss";});
	cClose.setAttribute("objId",objId);
	addEventHander(cClose,"click",closeDig);

	if(typeof(closeUse)=="object"){addEventHander(cClose,"click",closeUse);}		
	var cContextBody=t_cElement("div",cBody,"t_cDivDigContextBodyCss","width:"+w+"px;height:"+(h-23)+"px;");
	cContextBody.setAttribute("objId",objId);
	return cContextBody;
}
//关闭一个弹出框
function closeDig(e){	
	var objId=(e.srcElement||e.target).getAttribute("objId");
	var objDiv=document.getElementsByTagName("div");
	for(var i=0;i<objDiv.length;i++){
		if(objDiv[i].getAttribute("objId")==objId){				
			document.body.removeChild(objDiv[i]);	
			i--;
		}
	}
}

function addEventHander(obj,objEvent,objExec){
	if(window.addEventListener){
		obj.addEventListener(objEvent,objExec,false);
	}else if(window.attachEvent){
		obj.attachEvent("on"+objEvent,objExec);
	}else{
		obj["on"+objEvent]=objExec;
	}
}
function removeEventHander(obj,objEvent,objExec){
	if(window.removeEventListener){
		obj.removeEventListener(objEvent,objExec,false);
	}else if(window.detachEvent){
		obj.detachEvent("on"+objEvent,objExec);
	}else{
		obj["on"+objEvent]=null;
	}
}
function t_clientHeight(){
	try{
		var h=0;
		if(document.compatMode=="CSS1Compat"){
			h=document.documentElement.clientHeight;
		}else{
			h=document.body.clientHeight;
		}
		return h;
	}catch(e){
	}
}
function t_clientWidth(){
	try{
		var w=0;
		if(document.compatMode=="CSS1Compat"){
			w=document.documentElement.clientWidth;
		}else{
			w=document.body.clientWidth;
		}
		return w;
	}catch(e){
	}
}

