/**
 * @(#)zkenet.js
 * @author wang.li
 * creation 2002-10-23 16:17:57
 * $Id$
 */


// define the HTTP class
function HTTP() {

	// cache the connection
	this.cache = new Array();
	
	// http GET method
	this.requestGetURL = function (url, method) {
		var conn = "";
		if(arguments.length == 2) {
			conn = this.getHttpConnection();
		}else {
			conn = this.getHttpConnection(arguments[2]);
		}
		
		if(typeof(conn) == "string") {
            // 返回连接错误提示
            return conn;
		}else if(typeof(conn) == "object") {
			// force IE refresh
			if(url.indexOf("?") >= 0) {
				url = url + "&rnd=" + Math.random();
			}else {
				url = url + "?rnd=" + Math.random();
			}
			// check the callback method
			if(typeof(method) != "function") {
				alert("Error: the callback method is not correctly.");
				return;
			}
    		conn.open("GET", url, true);
			conn.onreadystatechange = function () {
				if(conn.readyState == 4) {
					if(conn.status == 200) {
						method(conn.responseText);
					}else {
						alert("Error: the response status(" + conn.status + ")");
					}
        		}
			}
    		conn.send(null);
            return "数据正在加载, 请稍候";
		}
	}
	
	// http POST method
	this.requestPostURL = function (form, method) {
		form.enctype = form.enctype ? form.enctype : "application/x-www-form-urlencoded";
		if(form.enctype == "multipart/form-data") {
			alert("Error: multipart/form-data is not supported.");
			return;
		}
		
		var conn = "";
		if(arguments.length == 2) {
			conn = this.getHttpConnection();
		}else {
			conn = this.getHttpConnection(arguments[2]);
		}
		
		if(typeof(conn) == "string") {
            // 返回连接错误提示
            return conn;
		}else if(typeof(conn) == "object") {
			var url = form.action ? form.action : document.location.href;
			// force IE refresh
			if(url.indexOf("?") >= 0) {
				url = url + "&rnd=" + Math.random();
			}else {
				url = url + "?rnd=" + Math.random();
			}
			// check the callback method
			if(typeof(method) != "function") {
				alert("Error: the callback method is not correctly.");
				return;
			}
    		conn.open("POST", url, true);
			conn.onreadystatechange = function () {
				if(conn.readyState == 4) {
					if(conn.status == 200) {
						method(conn.responseText);
					}else {
						alert("Error: the response status(" + conn.status + ")");
					}
        		}
			}			
			conn.setRequestHeader("content-type", form.enctype);
    		conn.send(this.getPostData(this.getFormValues(form), form.enctype));
            return "数据正在加载, 请稍候";
		}
	}
	
	this.createHttpConnection = function () {
		var conn = "Error: cannot initialize xmlhttp.";
		if(window.XMLHttpRequest) { // Mozilla, Safari
			try {
				conn = new XMLHttpRequest();
				if(conn.overrideMimeType) {
					// some of the Mozilla Browers will occurs errors if the header without mime-type
					conn.overrideMimeType("text/xml");
				}
			}catch(e) { }
		}else if(window.ActiveXObject) { // IE
			try {
	        	conn = new ActiveXObject("Msxml2.XMLHTTP");
	        }catch(e) {
	        	try {
	        		conn = new ActiveXObject("Microsoft.XMLHTTP");
	        	}catch(ex) { }
	        }
		}else {
			conn = "Error: this browser is not supported.";
		}
		return conn;
	}
	
	this.getHttpConnection = function () {
		if(arguments.length == 0) {
			for(var i = 0; i < this.cache.length; i++) {
				if(this.cache[i][0].readyState == 0 || this.cache[i][0].readyState == 4) {
					return this.cache[i][0];
				}
			}
            // too many connections which has no response
            if(this.cache.length >= 6) return "数据交换中, 请稍候再试";
            // create new connection
			var obj = new Array(this.createHttpConnection(), "");
			this.cache[this.cache.length] = obj;
			return obj[0];
		}else {
			for(var i = 0; i < this.cache.length; i++) {
                if(this.cache[i][1] != arguments[0]) continue;
				if(this.cache[i][0].readyState == 0 || this.cache[i][0].readyState == 4) {
					return this.cache[i][0];
				}else {
					return "数据交换中, 请稍候再试";
				}
			}
			var obj = new Array(this.createHttpConnection(), arguments[0]);
			this.cache[this.cache.length] = obj;
			return obj[0];
		}
	}
	
	this.getPostData = function (formValues, contentType) {
		var sendObj = "";
		switch(contentType) {
			case "multipart/form-data":
				break;
			//application/x-www-form-urlencoded
			default:
				var sendArray = new Array();
				for(var i = 0; i < formValues.length; i++) {
					sendArray[sendArray.length] = formValues[i].name + "=" + encodeURIComponent(formValues[i].value);
				}
				sendObj = sendArray.join("&");
				break;
		}
		return sendObj;
	}
	
	this.getFormValues = function (form) {
		var formValue = new Array();
		
		function createElementObject(name, type, value) {
			return { "name" : name, "type" : type, "value" : value };
		}
		
		for(var i = 0; i < form.elements.length; i++) {
			var obj = form.elements[i];
			var name = obj.name ? obj.name : "";
			if(!name || name == "rnd") continue;    // 去掉防缓存的随机数
            
			switch(obj.type ? obj.type : "other") {
				case "radio":
				case "checkbox":
					if(obj.checked) {
						formValue[formValue.length] = createElementObject(name, obj.type, obj.value);
					}
					break;
				case "select-one":
				case "select-multiple":
					for(var j = 0; j < obj.options.length; j++) {
						if(obj.options[j].selected) {
							formValue[formValue.length] = createElementObject(name, obj.type, obj.options[j].value);
						}
					}
					break;
				default:
					if(obj.value != undefined) {
						formValue[formValue.length] = createElementObject(name, obj.type, obj.value);
					}
					break;
			}
		}
		return formValue;
	}
	
}
