/*
 *本js集合了 常用的ajax的函数
 * 以下是常用的ajax常用处理函数
 * sendRequest( url , processtype ,functionName) 
 */

var xmlhttp_request ;

function sendRequest(url,processtype,functionName){
	
	xmlhttp_request = false ;

	if ( window.ActiveXObject ) {
		//IE 浏览器 
		//由于在不同Internet Explorer浏览器中XMLHTTP版本可能不一致，为了更好的兼容不同版本的Internet
		//Explorer浏览器，因此我们需要根据不同版本的Internet Explorer浏览器来创建XMLHttpRequest类
		try {
			xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	} else if (window.XMLHttpRequest ) {
		//非IE浏览器
		xmlhttp_request = new XMLHttpRequest();
		if (xmlhttp_request.overrideMimeType) {
			xmlhttp_request.overrideMimeType('text/xml');
		}
	}

	if (!xmlhttp_request) { // 异常，创建对象实例失败
		window.alert("不能创建XMLHttpRequest 对象实例.");
		return false;
	}

	//指定处理函数
	var processtypeGroup = new Array() ;
	//显示信息
	processtypeGroup[0] = "alertMessage" ;
	//修改HTML元素
	processtypeGroup[1] = "changeElement" ;
	//翻页时候的 前一页函数
	processtypeGroup[2] = "changeElement" ;
	//翻页时候的 后一页函数
	processtypeGroup[3] = "changeElement" ;

	//在过滤器上显示过滤器的结果
	processtypeGroup[4] = "xyc_showFilterResult" ;


	if ( processtype == null ) {
		//默认处理 
		xmlhttp_request.onreadystatechange = processRequest ;
	} else if ( processtype > 0 ){
		xmlhttp_request.onreadystatechange = eval( processtypeGroup[processtype-1] ) ;
	} else {
		xmlhttp_request.onreadystatechange = eval( functionName ) ;		
	}
	/*
	在定义了如何处理响应后，就要发送请求了。可以调用HTTP请求类的open()和send()方法，如下所示：
	xmlhttp_request.open('GET', URL, true);
	xmlhttp_request.send(null);
	open()的第一个参数是HTTP请求方式—GET，POST或任何服务器所支持的您想调用的方式。 按照HTTP规范，该参数要大写；否则，某些浏览器(如Firefox)可能无法处理请求。 
	第二个参数是请求页面的URL。
	第三个参数设置请求是否为异步模式。如果是TRUE(默认为true),JavaScript函数将继续执行，而不等待服务器响应。这就是"AJAX"中的"A"。
	*/
	// 确定发送请求的方式和URL 以及是否同步执行下段代码
	
	xmlhttp_request.open("POST", url, true);
	xmlhttp_request.send(null);

 }

 /**
  *默认的ajax处理函数！
  *在调用sendRequest函数的时候，如果没有指定 processtype,functionName
  *默认的函数就是 processRequest函数
  */
 function processRequest() {
	if (xmlhttp_request.readyState == 4) { // 判断对象状态
		if (xmlhttp_request.status == 200) { // 信息已经成功返回，开始处理信息
			showDivMessage(xmlhttp_request.responseText);
		} else { //页面不正常
			alert("[系统提醒]您所请求的页面有异常.");
		}
	}
 }
/**
 * 用showDivMessage()来代替alert，这样比较人性化
 */
  	
function zshowDivMessage(text) {
	var oPopup = window.createPopup(); 
	var obj = document.createElement("<div></div>") ;
	obj.innerHTML = text ;
	
	obj.innerHTML = text ;
	with (oPopup.document.body) {
		style.backgroundColor="#D4DDE6";
		style.border="solid #CCCCCC 2px";
		style.fontSize="12px" ;
		style.position="absolute" ;
		style.overflow="auto" ;
		style.overflowX="hidden" ;
		var windowTitle = "<table width=\"100%\" height=\"28\" border=1 align=center cellspacing=0 bordercolor=#ffffff bordercolorlight=#c3c3c3 bgcolor=#FFFFFF><tr><td bgcolor=\"#778899\" background=\"/LKKCHEF/images/graytitle.gif\"><span style=\"font-size: 12px;color: 000000;\">&nbsp;系统提示</span></td></tr></table>" ;	
		innerHTML= windowTitle + obj.innerHTML ;
	}
	//设置位置
	var width = 320 ;
	var height = 150 ;
	var x = 0 , y = 0 ; 
	if ( parseInt( navigator.appVersion ) >= 4 ) {
		x = (screen.width - width) / 2 ;
		y = (screen.height - height) /2  - 100 ;
	}
	oPopup.show(x,y, width, height, document.body);
}


/**
 * 用来动态改变innerHTML，例如用在翻页中动态改变页面
 * 这个版本是默认的
 */
function changeElement(){
	if (xmlhttp_request.readyState == 4) { // 判断对象状态
		if (xmlhttp_request.status == 200) { // 信息已经成功返回，开始处理信息
			//update by xycleo 2006-03-15 增加处理 javascript 的功能 
			var index = xmlhttp_request.responseText.indexOf("<xycleo_script###>") ;
			if ( index > 0 ) {
				document.all["idDIV"].innerHTML = xmlhttp_request.responseText.substring(0, index) ;
				//执行jaavscript
				eval( xmlhttp_request.responseText.substring(index + 28 , xmlhttp_request.responseText.length-9) );
			} else {
				document.all["idDIV"].innerHTML = xmlhttp_request.responseText ;
			}
		} else { //页面不正常
			alert("[系统提醒]您所请求的页面有异常.");
		}
	}	
}


function xyc_showFilterResult() {
	if (xmlhttp_request.readyState == 4) { // 判断对象状态
		if (xmlhttp_request.status == 200) { // 信息已经成功返回，开始处理信息
			var index = xmlhttp_request.responseText.indexOf("<xycleo_script###>") ;
			if ( index > 0 ) {
				document.all["xycFilterDropDownPanel"].innerHTML = xmlhttp_request.responseText.substring(0, index) ;
				//执行jaavscript
				eval( xmlhttp_request.responseText.substring(index + 26 , xmlhttp_request.responseText.length-9) );
			} else {
				document.all["xycFilterDropDownPanel"].innerHTML = xmlhttp_request.responseText ;
			}
		} else { //页面不正常
			alert("[系统提醒]您所请求的页面有异常.");
		}
	}
}
/**
 *
 */
function changeElementByObject(obj) {
	if (xmlhttp_request.readyState == 4) { // 判断对象状态
		if (xmlhttp_request.status == 200) { // 信息已经成功返回，开始处理信息
			obj.innerHTML = xmlhttp_request.responseText ;
		} else { //页面不正常
			alert("[系统提醒]您所请求的页面有异常.");
		}
	}	
}

function showPopMessage(text,x,y) {
	var oPopup = window.createPopup(); 
	var obj = document.createElement("<div></div>") ;
	obj.innerHTML = text ;
	obj.innerHTML = text ;
	with (oPopup.document.body) {
		style.backgroundColor="#D4DDE6";
		style.border="solid #CCCCCC 2px";
		style.fontSize="12px" ;
		style.position="absolute" ;
		style.overflow="auto" ;
		style.overflowX="hidden" ;
		var windowTitle = "<table width=\"100%\" height=\"28\" border=1 align=center cellspacing=0 bordercolor=#ffffff bordercolorlight=#c3c3c3 bgcolor=#FFFFFF><tr><td bgcolor=\"#778899\" background=\"/lg/Images/graytitle.gif\"><span style=\"font-size: 12px;color: 000000;\">&nbsp;</span></td></tr></table>" ;	
		innerHTML= windowTitle + obj.innerHTML ;
	}
	//设置位置
	var width = 320 ;
	var height = 150 ;
	/*
	var x = 0 , y = 0 ; 
	if ( parseInt( navigator.appVersion ) >= 4 ) {
		x = (screen.width - width) / 2 ;
		y = (screen.height - height) /2  - 100 ;
	}*/
	//x = event.clientX;
	//y = event.clientY;
	x = x - 335 ;
	y = y + 350 ;
	oPopup.show(x,y, width, height, document.body);

}