﻿/*
ajax读取内容后显示在指定的div里

*/

var xmlHttp;
var div_id;
function createXMLHttpRequest()
{

	//如果是IE，用activexobject
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	//如果其它浏览器就用XMLHttpRequest
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

//开始函数

function startRequest(method, url, param, div)
{
	createXMLHttpRequest();
	div_id = div;

	//指定当readyState属性改变时执行的函数
	xmlHttp.onreadystatechange = handleStateChange;
	//创建一个新的http请求，并指定此请求的方法、URL以及验证信息
	xmlHttp.open(method, url, true);

	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	//发送请求到http服务器并接收回应
	if(method == 'GET')
	{
		xmlHttp.send(null);
	}
	else
	{
		xmlHttp.send(param);
	}
	

}

function handleStateChange()
{
	//4数据接收完毕
	if(xmlHttp.readyState == 4)
	{
	//200返回请求状态为OK　
		if(xmlHttp.status == 200)
		{
		//弹出对话框，并输入simpleResponse.xml文件的文本内容
			//alert("The server replied with: " + xmlHttp.responseText);
			document.getElementById(div_id).innerHTML = xmlHttp.responseText;
			//alert(div_id);
			//alert(xmlHttp.responseText);
		}
	}

}

function get_form_elements(form)
{
	str = '';
	for(i=0;i<form.elements.length;i++)
	{
		c = form.elements[i];
		type = c.type ;
		if(type != 'button' && type != 'reset' && type != 'submit' && type != 'img' && c.value != '')
		{
			if(type == 'radio')
			{
				if(c.checked == true)
				{
					str += c.name + '=' + c.value + '&';
				}
			}
			else
			{
					str += c.name + '=' + c.value + '&';
			}
		}
	}
	str = str.substring(0, str.length - 1);
	return str;
}