﻿/* ASP Snail JavaScript Framework, Version 0.0.3
 *  (c) 2005 Kittow <kittow@263.net>
 *
 *  Snail is freely distributable under the terms of an MIT-style license.
 *
 *  For details, see the Snail web site: http://snail.skyhe.com
 *
/*--------------------------------------------------------------------------*/

/*
xmlHttpRequest 对象方法
方法									描述 
abort()								停止当前请求 
getAllResponseHeaders()				作为字符串返问完整的headers 
getResponseHeader("headerLabel")	作为字符串返问单个的header标签 
open("method","URL"[,asyncFlag[,"userName"[, "password"]]]) 设置未决的请求的目标 URL, 方法, 和其他参数 
send(content)						发送请求 
setRequestHeader("label", "value")	设置header并和请求一起发送 

xmlHttpRequest 对象属性
属性								描述 
onreadystatechange				状态改变的事件触发器 
readyState						对象状态(integer):
	0 = 未初始化
	1 = 读取中
	2 = 已读取
	3 = 交互中
	4 = 完成 
responseText					服务器进程返回数据的文本版本 
responseXML						服务器进程返回数据的兼容DOM的XML文档对象 
status							服务器返回的状态码, 如：404 = "文件末找到" 、200 ="成功" 
statusText						服务器返回的状态文本信息 
/*--------------------------------------------------------------------------*/

/*	建立XMLHTTPRequest组件对象的实例Ajax
 *	MicroSoft IE方式为 var Ajax=new ActiveXObject("microsoft.XMLHTTP");
 *  下面采用try方法，兼容Microsoft IE 5.5+、Mozilla 1.0+、NetScape 7+、Operav7.6x+等浏览器
/*--------------------------------------------------------------------------*/
var Ajax = false;
/* --- 开始初始化XMLHttpRequest对象 --- Begin --- */
if(window.XMLHttpRequest) {
	// Mozilla 浏览器
	Ajax = new XMLHttpRequest();
	if (Ajax.overrideMimeType) {
		// 设置MiME类别
		Ajax.overrideMimeType('text/xml');
	}
}
else if (window.ActiveXObject) { // IE浏览器
	try {
		Ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) {
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {}
	}
}
if (!Ajax) { // 异常，创建对象实例失败
	window.alert("不能创建XMLHttpRequest对象实例.");
}
/* --- 初始化XMLHttpRequest对象 --- End --- */

/*
function link_to_remote
用途：远程链接处理函数
url - the controller page url
		控制器页面的地址
action - QUERY_STRING
		请求查询的字符串，action操作
update - the html tag to update
		要更新的html元素标识
*/
function link_to_remote(url,action,update)
{
	var url = url + '?' + action;
	/*方法一：通过eval执行语句重建一个html对象 - 要更新的元素id*/
	//eval('var updateTag=document.all."+update+";');
	/*方法二：通过getElementById属性得到该html对象*/
	updateTag=document.getElementById( update );
	Ajax.open("GET", url, false);
	Ajax.send(null);
	if (Ajax.readyState == 4){
		// everything is good, the response is received
	} else {
		// still not ready
	}
	//alert(url);
	if (Ajax.status == 200) {
		// perfect!
		updateTag.outerHTML = '当前时间为:' + Ajax.responseText + '<br />' + updateTag.outerHTML;
		//alert('ok!');
	} else {
		// there was a problem with the request,
		// for example the response may be a 404 (Not Found)
		// or 500 (Internal Server Error) response codes
		updateTag.innerHTML = '失败，请重新选择试试吧！！';
		return false;
	}
}

/*
function form_remote_tag
用途：远程表单处理函数
url - the controller page url
		控制器页面的地址
action - QUERY_STRING
		请求查询的字符串，action操作
update - the html tag to update
		更新参数：要更新的html元素标识
position - the position of the html tag
		位置参数：插入到html元素顶部或底部的返回HTML片断
		(top - 顶部或前面，bottom - 底部或后面,replace - 替换原内容)
xmlContent - the XML content which you want to update this tag
		内容参数：要更新标识的html内容
//funcName - the function which will run after readystate changed
		响应函数：当服务器返回信息变化后需要执行的函数名
*/
function form_remote_tag(url,action,update,position,xmlContent)
{
	var result = false;
	var url = url + '?' + action;
	// 通过getElementById属性得到该html对象
	updateTag=document.getElementById( update );
	// 处理返回信息的函数
	Ajax.onReadystateChange = function()
	{
		if (Ajax.readyState == 4 && Ajax.status == 200) {
			//alert(unescape(Ajax.responseText));
			// 完成全部操作
			if (position == "top") {
				updateTag.outerHTML = updateTag.outerHTML + unescape(Ajax.responseText);
			}
			else if (position == "bottom") {
				updateTag.outerHTML = unescape(Ajax.responseText) + updateTag.outerHTML;
			}
			else {
				updateTag.innerHTML = unescape(Ajax.responseText);
			}
			//alert('ok!');
			result = true;
		} else {
			// there was a problem with the request,
			// for example the response may be a 404 (Not Found)
			// or 500 (Internal Server Error) response codes	
			result = false;
		}
	}
	// 确定发送请求的方式和URL以及是否异步执行下段代码
	Ajax.open("POST", url, false);
	// 设置头部信息，让它用utf-8发送，不然中文可能会乱码
	Ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	// 发送http请求并获取http响应
	//alert(xmlContent);
	Ajax.send(xmlContent);
	Ajax.abort();
	return result;
}
