﻿// JScript 文件

var XMLHttpReq; 
//创建XMLHttpRequest对象 
function createXMLHttpRequest() 
{ 
    if(window.XMLHttpRequest) 
    { //Mozilla 浏览器 
	    XMLHttpReq = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject) 
    { // IE浏览器 
	    try 
	    { 
		    XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    { 
		    try 
		    { 
				   XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e)
		    {
		    } 
	    } 
    } 
} 
    
function ajaxLoadPage(url,request,method,container)
{
	method=method.toUpperCase();
	var loading_msg='loading...';//the text shows on the container on loading.
	createXMLHttpRequest();
	var loader= XMLHttpReq;//require Cross-Browser XMLHttpRequest
	if (method=='GET')
	{
		urls=url.split("?");
		if (urls[1]=='' || typeof urls[1]=='undefined')
		{
			url=urls[0]+"?"+request;
		}
		else
		{
			url=urls[0]+"?"+urls[1]+"&"+request;
		}
		
		request=null;//for GET method,loader should send NULL
	}
	loader.open(method,url,true);
	if (method=="POST")
	{
		loader.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	loader.onreadystatechange=function(){
		    if (loader.readyState==1)
		    {
			    container.innerHTML=loading_msg;
    			
		    }
		    if (loader.readyState==4)
		    {
			    container.innerHTML=loader.responseText;
		    }
	    }
	loader.send(request);
}
//@desc    transform the elements of a form object and their values into request string( such as "action=1&name=surfchen")
//@param   form_obj          the form object
//@usage   formToRequestString(document.form1)
//@notice  this function can not be used to upload a file.if there is a file input element,the func will take it as a text input.
//         as I know,because of the security,in most of the browsers,we can not upload a file via xmlhttp.
//         a solution is iframe.
//@author  SurfChen <surfchen@gmail.com>
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/lgpl.html LGPL
function formToRequestString(form_obj)
{
    var query_string='';
    var and='';
    for (var i=0;i<form_obj.length ;i++ )
    {
        e=form_obj[i];
       
        if(e.id != "__VIEWSTATE" && e.id != "__EVENTVALIDATION")
        {
            if (e.id) {
                if (e.type=='select-one') {
                    element_value=e.options[e.selectedIndex].value;
                } else if (e.type=='select-multiple') {
                    for (var n=0;n<e.length;n++) {
                        var op=e.options[n];
                        if (op.selected) {
                            query_string+=and+e.id+'='+encodeURIComponent(op.value);
                            and="&"
                        }
                    }
                    continue;
                } else if (e.type=='checkbox' || e.type=='radio') {
                    if (e.checked==false) {   
                        continue;   
                    }   
                    element_value=e.value;
                } else if (typeof e.value != 'undefined') {
                    element_value=e.value;
                } else {
                    continue;
                }
                query_string+=and+e.id+'='+encodeURIComponent(element_value);
                and="&"
            }
        }
    }
    return query_string;
}
//@desc    no refresh submit(ajax) by using ajaxLoadPage and formToRequestString
//@param   form_obj          the form object
//@param   container          the container object,the loaded page will display in container.innerHTML
//@usage   ajaxFormSubmit(document.form1,document.getElementById('my_home'))
//@author  SurfChen <surfchen@gmail.com>
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/lgpl.html LGPL
function ajaxFormSubmit(url,form_obj,container)
{
	//ajaxLoadPage(url,formToRequestString(form_obj),"GET",container);
	//ajaxLoadPage(form_obj.getAttributeNode("action").value,formToRequestString(form_obj),form_obj.method,container);
}

function sendSubmit(url,form_obj)
{
    ShowMsgo.show("提交数据","<table align='center'><tr><td align='center'><br><br><br><img src='App_Themes/dearwheat/images/msg/i_animated_loading_32_2.gif'><br>正在提交数据，请稍候...</td></tr></table>",0);
    createXMLHttpRequest(); 
    var url = url + formToRequestString(form_obj);
    XMLHttpReq.open("GET", url, true);
    XMLHttpReq.onreadystatechange = doSubmit;//指定响应函数 
    XMLHttpReq.send(null);
}

function doSubmit()
{
    if (XMLHttpReq.readyState == 4) 
    { // 判断对象状态 
        if (XMLHttpReq.status == 200) 
        { // 信息已经成功返回，开始提示
          submitSuccess();
        } 
        else 
        { //页面不正常 
             ShowMsgo.show("提交数据出错!","提交数据出错!<br>请重新提交，<br>如果还碰到此问题，请联系网站管理人员。",2);
        } 
    } 
}
