function get_tag(id, w) {
	if (!w) {
		w = this;
	}

	if (w.document.getElementById) {
		c = eval("w.document.getElementById('" + id + "')");
	}   
	else {
		c = eval("w.document.all." + id);
	}
	return c;
}

function createXMLHttp() {
	try{return new ActiveXObject("Msxml2.XMLHTTP");} catch(e){}
	try{return new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}
	try{return new XMLHttpRequest();} catch(e){}

	alert("XMLHttpRequest Object not existed!!!");

	return null;
}

function httpXML(url, data, response) {
	http = createXMLHttp();

	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) {
			response(http.responseXML);
		}
	};

	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", data.length);
	http.setRequestHeader("Connection", "close");
	http.send(data);
}

function HttpHTMLClass() {
}

HttpHTMLClass.prototype.readystatechange = function(o) {
	if (o.http.readyState == 4) {
		if (o.http.status == 200) {
			if (o.type == "string" || o.type == "object") {
				var txt = o.http.responseText;

				var js_begin_tag = '<script type="text/javascript">\r\n<!--\r\n';
				var js_end_tag = '//-->\r\n</script>';

				var htm = "";
				var js = "";
				var idx = 0;

				while (true) {
					var js_begin_idx = txt.indexOf(js_begin_tag, idx)	;
					if (js_begin_idx == -1) {
						htm += txt.substr(idx)
						break;
					}
					else {
						htm += txt.substring(idx, js_begin_idx);
						var js_end_idx = txt.indexOf(js_end_tag, js_begin_idx);
						js += txt.substring(js_begin_idx + js_begin_tag.length, js_end_idx);

						idx = js_end_idx + js_end_tag.length;
						if (idx >= (txt.length - 1)) {
							break;
						}
					}
				}

				o.block.removeAttribute("disabled");

				if (this.color != 0) {
					o.block.style.color = this.color;
				}

				o.block.innerHTML = htm;
				eval(js);
			}
			else if (this.type == "function") {
				o.response(o.http.responseText);
			}
		}
		else {
			o.block.innerHTML = o.http.responseText;
			alert('Error: ' + o.http.status);
		}
	}
};

function htm_js(txt) {
	var js_begin_tag = '<script type="text/javascript">\r\n<!--\r\n';
	var js_end_tag = '//-->\r\n</script>';

	var htm = "";
	var js = "";
	var idx = 0;

	while (true) {
		var js_begin_idx = txt.indexOf(js_begin_tag, idx)	;
		if (js_begin_idx == -1) {
			htm += txt.substr(idx)
			break;
		}
		else {
			htm += txt.substring(idx, js_begin_idx);
			var js_end_idx = txt.indexOf(js_end_tag, js_begin_idx);
			js += txt.substring(js_begin_idx + js_begin_tag.length, js_end_idx);

			idx = js_end_idx + js_end_tag.length;
			if (idx >= (txt.length - 1)) {
				break;
			}
		}
	}

	return new Array(htm, js);
}

HttpHTMLClass.prototype.httpHTMLExecute = function(url, data, response, reloadx, toggle, show_loading) {
	this.url = url;
	this.data = data;
	this.response = response;
	this.reloadx = reloadx;
	this.toggle = toggle;

	this.type = typeof(this.response);
	if (this.type == "string" || this.type == "object") {
		if (this.type == "string") {
			this.block = get_tag(this.response);
		}
		else {
			this.block = this.response;
		}

		if (this.toggle) {
			if (this.block.style.display != "none") {
				this.block.style.display = "none";
				return;
			}
		}

		if (this.block.style.display == "none") {
			this.block.style.display = "inline";
		}

		if (this.block.innerHTML != "") {
			if (!this.reloadx) {
				return;
			}
		}

		if (show_loading || show_loading == null) {
			this.block.innerHTML = this.block.innerHTML + "<img src='/img/loading.gif' alt='Loading...' />";
		}

		if (this.block.style) {
			if (this.block.style.color != null) {
				this.color = this.block.style.color;
				if (this.color == "") {
						this.color = "#000000";
				}
			}
			else {
				this.color = 0;
			}
		}
		else {
			this.color = 0;
		}

		tag_disable(this.block);
	}

	this.http = createXMLHttp();
	var main = this;

	this.http.onreadystatechange = function() {
		main.readystatechange(main);
	};

	this.http.open("POST", this.url, true);
	this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this.http.setRequestHeader("Content-length", this.data.length);
	this.http.setRequestHeader("Connection", "close");
	this.http.send(this.data);
}

function httpHTML(url, data, response, reloadx, toggle, show_loading) {
	if (data == null) {
		data = "";
	}
	obj = new HttpHTMLClass;
	obj.httpHTMLExecute(url, data, response, reloadx, toggle, show_loading);
}

function open_tag(tag) {
	var tag_obj;
	if (typeof(tag) == "string") {
		tag_obj = get_tag(tag);
	}
	else {
		tag_obj = tag;
	}

	tag_obj.style.display = "inline";
}

function close_tag(tag) {
	var tag_obj;
	if (typeof(tag) == "string") {
		tag_obj = get_tag(tag);
	}
	else {
		tag_obj = tag;
	}

	tag_obj.style.display = "none";
}

function toggle_tag(tag) {
	var tag_obj;
	if (typeof(tag) == "string") {
		tag_obj = get_tag(tag);
	}
	else {
		tag_obj = tag;
	}

	var display = tag_obj.style.display;

	if (display == "none") {
		display = "";
	}
	else if (display.length == 0) {
		display = "none";
	}
	else if (display == "inline") {
		display = "none";
	}
	else {
		alert("style.display = '" + display + "'");
	}

	tag_obj.style.display = display;

	return !(display == "none");
}


function form_data(form) {
	var data = "";

	var params = form_params(form);
	
	for(var key in params){
		if (data.length > 0) {
			data += "&";
		}
		data += key + "=" + encodeURIComponent(params[key]);
	}

	return data;
}

function form_params( form )
{
    var params = new Array()
    var length = form.elements.length
    for( var i = 0; i < length; i++ )
    {
        element = form.elements[i]

        if(element.tagName == 'TEXTAREA' )
        {
                params[element.name] = element.value
        }
        else if( element.tagName == 'INPUT' )
        {
                if( element.type == 'text' || element.type == 'hidden' || element.type == 'password')
                {
                        params[element.name] = element.value
                }
                else if( element.type == 'radio' && element.checked )
                {
                        if( !element.value )
                                params[element.name] = "on"
                        else
                                params[element.name] = element.value

                }
                else if( element.type == 'checkbox' && element.checked )
                {
                        if( !element.value )
                                params[element.name] = "on"
                        else
                                params[element.name] = element.value
                }
        }
        else if( element.tagName == 'SELECT' ) {
					params[element.name] = element.value;
				}
    }
    return params;
}

function tag_disable(tag) {
	if (tag.childNodes && tag.childNodes.length > 0) {
		for (var i = 0; i < tag.childNodes.length; i++) {
			tag_disable(tag.childNodes[i]);
		}
	}

	if (tag.style != null) {
		if (tag.style.color != null) {
				tag.style.color = "#CCCCCC";
		}
/*
		if (tag.style.backgroundColor != null) {
			tag.style.backgroundColor = "#FFFFFF";
		}
*/
	}

	if (tag.href != null) {
		tag.removeAttribute("href");
	}

	if (tag.disabled != null) {
		tag.disabled = true;
	}
}
