﻿/*********************************************
	- 自定义网页对话框
	- Modify By Mudoo 2008.1.28

	- 修改至
	  Flyingfish(会飞的鱼)
	  自定义网页对话框 v1.0 
*********************************************/
function popBox(initValues){
	//初始化常量
	this.config=initValues;	
	if(typeof(initValues)!="object"){alert("初始化窗口参数出错！");return;}
	if(typeof(this.config.windowType)=="undefined"){alert("窗口类型必须指定！");return;}
	if(typeof(this.config.obj)=="undefined"){alert("未指定与之绑定的对象名！");return;}
	this.config.width=this.config.width==undefined?540:this.config.width;
	this.config.height=this.config.height==undefined?350:this.config.height;
	this.config.hint=this.config.hint==undefined?"":this.config.hint;
	this.config.title=this.config.title==undefined?"系统提示":this.config.title;	
	this.config.speed=this.config.speed==undefined?25:this.config.speed;
	this.config.float=this.config.float==undefined?false:this.config.float;
	this.config.drag=this.config.drag==undefined?true:this.config.drag;
	this.btnsLabs=this.config.btnsLabs;
	if(typeof(this.btnsLabs)=="undefined") this.btnsLabs={};
	this.lab_btnOk=this.btnsLabs.lab_btnOk==undefined?'确 定':this.btnsLabs.lab_btnOk;
	this.lab_btnYes=this.btnsLabs.lab_btnYes==undefined?'是':this.btnsLabs.lab_btnYes;
	this.lab_btnNo=this.btnsLabs.lab_btnNo==undefined?'否':this.btnsLabs.lab_btnNo;
	this.lab_btnCancel=this.btnsLabs.lab_btnCancel==undefined?'取 消':this.btnsLabs.lab_btnCancel;
	//初始化界面
	this.init=function(){
		if($("popBox")==null){
			/*
			var pop_strSource =	"<div id=\"popBox\">" +
								"	<div class=\"pop_round\">" +
								"		<div class=\"pop_tl\"></div>" +
								"		<div class=\"pop_tr\"></div>" +
								"		<div class=\"pop_fl\"></div>" +
								"		<div class=\"pop_fr\"></div>" +
								"		<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
								"			<tr>" +
								"				<td id=\"pop_top\">" +
								"					<div id=\"pop_close\"></div>" +
								"					<div id=\"pop_title\"></div>" +
								"				</td>" +
								"			</tr>" +
								"			<tr>" +
								"				<td id=\"pop_main\" class=\"alert\">" +
								"					<div id=\"pop_icon\"><img src=\"Images/Empty.gif\" /></div>" +
								"					<div id=\"pop_cont\"></div>" +
								"				</td>" +
								"			</tr>" +
								"			<tr>" +
								"				<td colspan=\"2\" id=\"pop_foot\"></td>" +
								"			</tr>" +
								"		</table>" +
								"	</div>" +
								"</div>"+
								"<div id=\"pop_mask\"><\/div>";
			*/
			var pop_strSource =	"<div id=\"popBox\">" +
								"	<div class=\"pop_round\">" +
								"		<div class=\"pop_tl\"></div>" +
								"		<div class=\"pop_tr\"></div>" +
								"		<div class=\"pop_fl\"></div>" +
								"		<div class=\"pop_fr\"></div>" +
								"		<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
								"			<tr>" +
								"				<td id=\"pop_top\" colspan=\"2\">" +
								"					<div id=\"pop_close\"></div>" +
								"					<div id=\"pop_title\"></div>" +
								"				</td>" +
								"			</tr>" +
								"			<tr id=\"pop_main\" class=\"alert\">" +
								"				<td id=\"pop_icon\"><img src=\"Images/Empty.gif\" /></td>" +
								"				<td id=\"pop_cont\"></td>" +
								"			</tr>" +
								"			<tr>" +
								"				<td colspan=\"2\" id=\"pop_foot\"></td>" +
								"			</tr>" +
								"		</table>" +
								"	</div>" +
								"</div>"+
								"<img src=\"Images\/Empty.gif\" id=\"pop_mask\" \/>";
			var elem;
			elem = document.createElement("div"); 
			elem.innerHTML = pop_strSource;
			document.body.insertBefore(elem, document.body.firstChild); 
		}
		$("pop_title").innerHTML = this.config.title;
		$("pop_close").innerHTML = "<a href=\"javascript:"+this.config.obj+".pop_Method(\'close\')\" ><img src=\"Images\/Empty.gif\" alt=\"关闭窗口\" /><\/a>";
	}
	this.init();
	//判断窗口类型
	switch(this.config.windowType){
		//类型为只读窗口
		case "readonly":
			$("pop_main").className = "readonly";
			$("pop_close").innerHTML="";
			$("pop_foot").style.display="none";
			$("pop_cont").innerHTML=this.config.hint;
			break;
		//类型为警告窗口
		case "alert" :
			$("pop_main").className = "alert";
			$("pop_foot").style.display="";
			$("pop_cont").innerHTML=this.config.hint;
			$("pop_foot").innerHTML='<input name="pop_btnYes" type="button" class="button" value="'+ this.lab_btnOk +'" onclick='+this.config.obj+'.pop_Method("close") />'
			break;
		//类型为确认窗口
		case "confirm":
			$("pop_main").className = "confirm";
			$("pop_foot").style.display="";
			$("pop_cont").innerHTML=this.config.hint;
			$("pop_foot").innerHTML=	'<input name="pop_btnYes" type="button" class="button" value="'+ this.lab_btnYes +'" onclick='+this.config.obj+'.pop_Method("yes") />'+
										'<input name="pop_btnNo" type="button" class="button" value="'+ this.lab_btnNo +'" onclick='+this.config.obj+'.pop_Method("no") />'
			break;
		//类型为询问窗口
		case "ask":
			$("pop_main").className = "ask";
			$("pop_foot").style.display="";
			$("pop_cont").innerHTML=this.config.hint;
			$("pop_foot").innerHTML=	'<input name="pop_btnYes" type="button" class="button" value="'+this.lab_btnYes+'" onclick='+this.config.obj+'.pop_Method("yes") />'+
										'<input name="pop_btnNo" type="button" class="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.pop_Method("no") />'+
										'<input name="pop_btnCancel" type="button" class="button" value="'+this.lab_btnCancel+'" onclick='+this.config.obj+'.pop_Method("cancel") />'
			break;
		//类型为可允许输入窗口
		case "text" :
			var elem
			elem = document.createTextNode(this.config.hint)
			$("pop_cont").appendChild(elem)
			elem = document.createElement("br"); 
			$("pop_cont").appendChild(elem)
			elem = document.createElement("textarea"); 
			elem.id = "pop_textarea";
			$("pop_main").className = "text";
			$("pop_cont").appendChild(elem)
			$("pop_foot").style.display="";			
			$("pop_foot").innerHTML='<input name="pop_btnYes" type="button" class="button" value="'+this.lab_btnYes+'" onclick='+this.config.obj+'.pop_Method("yes") />'+
									  '<input name="pop_btnNo" type="button" class="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.pop_Method("no") />'
			break;
		//类型为模式窗口
		case "window" :
			$("pop_main").className = "window";
			$("pop_cont").innerHTML='<iframe name="pop_iframe" id="pop_iframe" allowTransparency="true" align="center" frameborder="0" height="'+
									  this.config.height+'" width="100%" scrolling="yes" src="'+this.config.url+'" ></iframe>'
			$("pop_foot").style.display="";
			$("pop_foot").innerHTML='<input name="pop_btnYes" type="button" class="button" value="'+ this.lab_btnYes +'" onclick='+this.config.obj+'.pop_Method("ok") />'+
									'<input name="pop_btnNo" type="button" class="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.pop_Method("no") />'
			break;
		default: 
			break;
	}			
	this.o = 0;
	this.opacity = 20;
	this.doAlpha;
	//打开popBox
	this.open=function(){
		//getBrowserDocument().style.overflow = "hidden";		
		var posLeft,posTop
		var winWidth = $('popBox').clientWidth;
		var winHeight = $('popBox').clientHeight;
		var pageScroll=getPageScroll();
		var pageSize=getPageSize();
		//解决firefox下滚动条会复位
		var scrollTop = getBrowserDocument().scrollTop; 
		var scrollLeft = getBrowserDocument().scrollLeft; 
		getBrowserDocument().scrollTop = scrollTop;   
		getBrowserDocument().scrollLeft = scrollLeft;
		//初始化背景大小
		$("pop_mask").style.width = pageSize[4] +"px";
		$("pop_mask").style.height = pageSize[1] +"px";
//		$("pop_mask").style.width = "100%";
//		$("pop_mask").style.height = "100%";
		//alert(pageSize);
		//初始化窗口位置
		posLeft=pageScroll[0]+(pageSize[2]-winWidth)/2;
		posTop=pageScroll[1]+(pageSize[3]-winHeight)/2;
		$('popBox').style.left = posLeft +"px";
		$('popBox').style.top = posTop + "px"; 
		$("popBox").style.visibility="visible";
		$("pop_mask").style.visibility="visible";
		//设置弹出框焦点
		var input = $("pop_foot").getElementsByTagName("input");
		if(input[input.length-1] != null) {
			input[input.length-1].focus();
		}else{
			$('popBox').focus();
		}
		//隐藏下拉框，显示背景
		displaySelect(true)
		//clearInterval(this.doAlpha)
		//this.doAlpha=setInterval(this.config.obj+".fadeIn()",this.config.speed)
		/* Modify */
		setOpacity($("pop_mask"), 10)
		//拖动窗口
		if(this.config.drag) {
			DragEle($('pop_top'), 'popBox', 0, 0, pageSize[4]-$('popBox').offsetWidth, pageSize[1]-$('popBox').offsetHeight);
		}
		//随页面滚动移动
		if(this.config.float) {
			clearInterval(this.doFloat)
			this.doFloat=setInterval(this.config.obj+".float()",100);
		}
	}
	this.float = function() {
		var winHeight = $('popBox').clientHeight;
		var pageScroll=getPageScroll();
		var pageSize=getPageSize();
		posTop=pageScroll[1]+(pageSize[3]-winHeight)/2;
		$('popBox').style.top = posTop + "px";
	}
	//关闭popBox
	this.close=function(){
		this.reset()
		getBrowserDocument().style.overflow = "";
		clearInterval(this.doFloat);
		//clearInterval(this.doAlpha);
		//this.doAlpha=setInterval(this.config.obj+".fadeOut()",this.config.speed);
		/* Modify */
		displaySelect(false);
		this.remove();
	}
	this.reset=function(){
		$("pop_title").innerHTML="";
		$("pop_cont").innerHTML="";
		$("pop_foot").innerHTML="";
		$("popBox").style.visibility="hidden";
	}
	this.remove=function(){
		$("popBox").parentNode.removeChild($("popBox"));
		$("pop_mask").parentNode.removeChild($("pop_mask"));
	}
	//获取输入值
	this.getValue=function(){
		if($("pop_textarea")==null) return false;
		else return ($("pop_textarea").value)
	}
	//设置输入框默认值
	this.setValue=function(value){
		if($("pop_textarea")!=null) $("pop_textarea").value=value;
	}
	//设置窗口URL
	this.getURL=function(url){
		if($("pop_iframe")!=null) $("pop_iframe").src=url;
	}
	//背景淡入
	this.fadeIn=function(){
		if(this.o<this.opacity){
			setOpacity($("pop_mask"),this.o)
			this.o+=5;
		}else{
			clearInterval(this.doAlpha);
		}
	}
	//背景淡出
	this.fadeOut=function(){
		if(this.o>0){
			setOpacity($("pop_mask"),this.o)
			this.o-=5;
		}else{
			clearInterval(this.doAlpha);
			$("pop_mask").style.visibility="hidden";
			displaySelect(false);
			this.remove();
		}
	}
	//按钮事件
	this.pop_Method=function(method){
		this.callback(method)
	}
	//回调函数
	this.callback=function(args){this.close()}
}

/*********************************************
	- 调用自定义弹出框
*********************************************/
function popMsg(type, p_title, p_hint, doMain){
	p_title = p_title=="" ? "系统提示" : p_title;
	switch(type){
		case "readonly" : 	
			msgBox1=new popBox({obj:"msgBox1", windowType:type, title:p_title, hint:p_hint});
			msgBox1.open();	
			break;	
		case "alert" : 	
			msgBox2=new popBox({obj:"msgBox2", windowType:type, title:p_title, hint:p_hint});	
			msgBox2.open();
			msgBox2.callback=function(args){
				try{
					eval(doMain);
				}catch(e){}
				msgBox2.close();
			}	
			break;
		case "confirm" : 
			msgBox3=new popBox({obj:"msgBox3",windowType:type, title:p_title, hint:p_hint, btnsLabs:{lab_btnYes:'确 认',lab_btnNo:'取 消'}});
			msgBox3.open()
			msgBox3.callback=function(args){
				msgBox3.close()
				if(args=="yes"){
					//try{
						eval(doMain);
					//}catch(e){}
				}
			}
			break;	
		case "ask": 
			msgBox4=new popBox({obj:"msgBox4",windowType:type,title:"询问对话框",hint:"确认操作，是，否，取消"});
			msgBox4.open()
			msgBox4.callback=function(args){
				if(args=="yes"){
					alert("你点击了接受按钮")
				}else if(args=="no"){
					alert("你点击了拒绝按钮")
				}else if(args=="cancel"){
					alert("你点击了取消按钮")
				}else{
					alert("你点击了关闭按钮")
				}
				msgBox4.close()
			}
			break;	
		case "text":
			msgBox5=new popBox({obj:"msgBox5",windowType:type,title:"输入对话框",hint:"你可在下面输入反馈内容",btnsLabs:{lab_btnYes:'确认',lab_btnNo:'取消'}});
			msgBox5.setValue("123456789845445121")
			msgBox5.open()
			msgBox5.callback=function(args){
				if(args=="yes"){
					alert("你点击了确定按钮,返回值："+msgBox5.getValue())
					$("result").value=(msgBox5.getValue())
				}else if(args=="no"){
					alert("你点击了取消按钮")
				}else{
					alert("你点击了关闭按钮")
				}
				msgBox5.close()
			}
			break;	
		 case "window":
			msgBox6=new popBox({obj:"msgBox6",width:450,height:300,windowType:type,title:'模式对话框'});
			msgBox6.getURL("http://www.931music.com")
			msgBox6.open()
			msgBox6.callback=function(args){
				msgBox6.close()
			}
			break;	
		default: break;
	}
}

