var ctx="/zx";

/*
自定义confirm
input:
	txt:提示信息，ok_func:点击确定运行的方法，notok_func：点击取消运行的方法
*/
function confirm_css(txt,ok_func,notok_func){
	addPanel("系统提示",txt,"2",350,100,ok_func,notok_func,null,'confirm','no');
}
/*
重载window.alert

window.alert = function(txt)
{
	addPanel("系统提示",txt,"1",350,100,null,null,null,'alert');
}
*/
function pageload_css(txt){
	addPanel("系统提示",txt,"3",350,100,null,null,null,'pageload','no');
}

/*
打开新窗口
*/
function openNewWindow(title,url){
	addPanel(title,'',"5",500,300,null,null,url,'openNewWindow','auto');
}
/*
txt:提示信息
type:类型，1为提示(alert),2为confirm，3为加载)
*/
function addPanel(title,txt,MsgType,width,height,ok_func,notok_func,url,p_type,scroll){
	var framess=top.frames;
	var mainDiv =document.createElement('Div');
	document.body.appendChild(mainDiv);
	mainDiv.id="mainDiv_"+p_type;
	var webwidth = document.body.clientWidth;
	var webheight = document.body.scrollHeight;
	var scrtop = document.documentElement.scrollTop;
	var scrleft = document.documentElement.scrollLeft;
	var strHtml="";
	strHtml +="<table width=\""+width+"\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"nobordertab\">";
	strHtml +="<tr>";
	strHtml +="<td class=\"message_left\"></td>";
	if(p_type=='openNewWindow')
		strHtml +="<td class=\"message_mid\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=28>&nbsp;&nbsp;"+title+"</td><td width=20><span class=\"message_close\" onclick=\"pageLoadok('"+p_type+"')\" onmouseover=\"this.className='message_close_onmouserver'\" onmouseout=\"this.className='message_close'\"></span></td></tr></table></td>";
	else
		strHtml +="<td class=\"message_mid\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=28>&nbsp;&nbsp;"+title+"</td></tr></table></td>";
	strHtml +="<td class=\"message_right\" /></td></tr>";
	strHtml +="<tr><td height=\""+height+"\" colspan=\"3\" valign=top style=\"border-top-width: 1px;border-right-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-top-style: none;border-right-style: solid;border-bottom-style: solid;border-left-style: solid;border-top-color: #7B9AB6;border-right-color: #7B9AB6;border-bottom-color: #7B9AB6;border-left-color: #7B9AB6;\">";
	strHtml +="<table class=\"nobordertab\" width=\""+(width-30)+"\" height=\""+height+"\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
	if(MsgType=="4"){
		strHtml +="<td align=\"center\" style=\"font-size: 12px;\">"+txt+"<br/><br/><input name='inputvalue' id='inputvalue'></input></td></tr><tr><td height=\"40\" align=\"center\">";
	}else if(MsgType=="5"){
		strHtml +="<td align=\"center\" style=\"font-size: 12px;\"><iframe id='ifm2' src='"+url+"' name='ifm2' frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0 scrolling="+scroll+" style='width:100%;height:100%'></iframe></td></tr><tr><td height=\"40\" align=\"center\">";
	}else{
		strHtml +="<td align=\"center\" style=\"font-size: 12px;\">"+txt+"</td></tr><tr><td height=\"40\" align=\"center\">";
	}
	if(MsgType=="1"){
		strHtml +="<span class=\"message_but_ok\" onclick=\"pageLoadok('"+p_type+"')\"></span>";
	}else if(MsgType=="2"){
		if(ok_func==null)
			strHtml +="<span class=\"message_but_ok\" onclick=\"pageLoadok('"+p_type+"')\"></span>";
		else
			strHtml +="<span class=\"message_but_ok\" onclick=\"parent."+ok_func+";pageLoadok('"+p_type+"')\"></span>";
		if(notok_func==null)
			strHtml +="<span class=\"message_but_canel\" onclick=\"pageLoadok('"+p_type+"')\"></span>";
		else
			strHtml +="<span class=\"message_but_canel\" onclick=\"parent."+notok_func+";pageLoadok('"+p_type+"')\"></span>";
	}else if(MsgType=="3"){
		strHtml +="<span class=\"message_but_canel\" onclick=\"pageLoadok('"+p_type+"')\"></span>";
	}else if(MsgType=="4"){
		strHtml +="<span class=\"message_but_ok\" onclick=\"prompt_css_ok('"+pathStr+"','"+ok_func+"');\"></span>";
		strHtml +="<span class=\"message_but_canel\" onclick=\"pageLoadok('"+p_type+"')\"></span>";
	}
	strHtml +="</td></tr></table></td></tr></table>";
	mainDiv.innerHTML="<div id=shield style=\"position:absolute; top:0px;left:0px;height:"+webheight+"px;width:"+webwidth+"px;background-color:#CCCCCC; text-align:center; z-index:10000; filter:alpha(opacity=40)\"></div><div id=alertFram style=\"position:absolute; left:50%; top:50%; margin-left:"+(parseInt(scrleft)-width/2)+"px; margin-top:"+(parseInt(scrtop)-(height+46)/2)+"px; width:"+width+"px; height:"+(height+46)+"px; text-align:center; line-height:145px; z-index:10001\"><iframe id='ifm1' name='ifm1' frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0 scrolling="+scroll+" style='width:100%;height:100%'></iframe></div>";
	if(MsgType=="4"){
		this.frames["ifm1"].document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\""+ctx+"/css/wwxt.css\" /><script src=\""+ctx+"/js/public.js\"></script><body leftmargin=0 topmargin=0><form name=\"LazyForm\" method=\"post\" action=\"\">"+strHtml+"</form><script type=\"text/javascript\">document.getElementById('inputvalue').focus();</script></body>");
	}else{
		this.frames["ifm1"].document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\""+ctx+"/css/wwxt.css\" /><script src=\""+ctx+"/js/public.js\"></script><body leftmargin=0 topmargin=0>"+strHtml+"</body>");
	}
	return true;
}
/*
var drag_=false;
function Move_obj(obj,t,l){
	return;
	//alert(obj);
	var x,y;
	var o=parent.document.getElementById(obj);
	var message_mid=document.getElementById("message_mid");
	var oevent=new Function('e','if (!e) e = parent.window.event;return e');
	message_mid.onmousedown=function(e){
		drag_=true;
		with(o){
			style.position="absolute";
			var temp1=offsetLeft+l;var temp2=offsetTop+t;
			x=window.event.clientX;y=window.event.clientY;
			document.onmousemove=function(e){
				if(!drag_)return false;
				with(o){
					style.left=temp1+oevent(e).clientX-x+"px";
					style.top=temp2+oevent(e).clientY-y+"px";
				}
			}
		}
		document.onmouseup=new Function("drag_=false");
	}

	var D=new Function('obj','return parent.document.getElementById(obj);')
	var oevent=new Function('e','if (!e) e = parent.window.event;return e')
	var x,y;
	
	D(obj).onmousedown=function(e){
		drag_=true;
		with(this){
			alert(offsetLeft);
			alert(offsetTop);
			style.position="absolute";var temp1=offsetLeft+l;var temp2=offsetTop+t;
			x=oevent(e).clientX;y=oevent(e).clientY;
			document.onmousemove=function(e){
				if(!drag_)return false;
				with(this){
					style.left=temp1+oevent(e).clientX-x+"px";
					style.top=temp2+oevent(e).clientY-y+"px";
				}
			}
		}
		document.onmouseup=new Function("drag_=false");
	}
}*/

/*
页面加载完成后移除DIV
*/
function pageLoadok(p_type){
	p_type=p_type||"pageload";
	var mainDiv=parent.document.getElementById("mainDiv_"+p_type);
	if(mainDiv!=null){
		if(mainDiv.length==undefined){
			parent.document.body.removeChild(mainDiv);
		}else{
			parent.document.body.removeChild(mainDiv[0]);
		}
	}
	if(mainDiv==null){
		var mainDiv=document.getElementById("mainDiv_"+p_type);
		if(mainDiv!=null){
			if(mainDiv.length==undefined){
				document.body.removeChild(mainDiv);
			}else{
				document.body.removeChild(mainDiv[0]);
			}
		}
	}
	
	return true;
}

/*
AJAX类
input:
	url:
*/
function AJAX(url){
	this.AjaxFailedAlert = "fff";//
	this.requestURL = url;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true; 
		}
	};
	
	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}
	
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.setForm = function(frmID){
        var item; // for each form's object
        var itemValue;// store each form object's value
		for(var i=0;i<frmID.length;i++ ) 
        {
			item = frmID[i];// get form's each object
			if( item.name!='' ){
				if( item.type == 'select-one' ){
					itemValue = item.options[item.selectedIndex].value;
				}else if ( item.type=='checkbox' || item.type=='radio'){
					if ( item.checked == false ){
						continue;
					}
					itemValue = item.value;
				}else if ( item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image'){// ignore this type
					continue;
				}else{
					itemValue = item.value;
				}
				//itemValue = encodeURIComponent(itemValue);
				this.setVar(item.name,itemValue);
			}
			
		}
	}
	
	this.alertString = function(){
		alert(this.URLString);
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){ 
			alert(this.AjaxFailedAlert); 
		} else {
			if (urlstring){ 
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring; 
				} else {
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			if (this.element) { this.elementObj = document.getElementById(this.element); }
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestURL + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					if(this.requestURL.indexOf("?")>-1){
						this.requestURL+="&charset=UTF-8";
					}else{
						this.requestURL+="?charset=UTF-8";
					}
					
					this.xmlhttp.open(this.method, this.requestURL, true);
				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
					} catch (e) {}
				}
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){self.runResponse(); }
							if (self.elementObj) {
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							self.URLString = "";
							return self.response;
						break;
					}
				};
				this.xmlhttp.send(this.URLString);
			}
		}
	};
this.createAJAX();
}
