var ajax_request_type = "GET";
var ajax_target_id = "";
var uri = "gettargetgroup.asp";
function update_info() {
	ajax_target_id = "target_group";
	document.getElementById(ajax_target_id).innerHTML = "<font class=normalbold>Loading ......</font>";
	var username = "test";
	uri = update_info.arguments[0];
	x_update_info(username, check_handle);
}
function x_update_info() {
	ajax_do_call("update_info",
		x_update_info.arguments);
}
function check_handle(result) {
	if(result[0] == 'yes') {
		document.getElementById('not_available').style.display = 'none';
		document.getElementById('available').style.display = 'block';
	}
	else {
		document.getElementById('available').style.display = 'none';
		document.getElementById('not_available').style.display = 'block';
		var str = 'Sorry that username is not available try these <br />';
		for(i = 1; i < result[1]; i++) {
			str += "<input type=\"radio\" name=\"try\" onclick=\"switch_username('"+result[i+1]+"')\"/>" + result[i+1] + "<br />";
		}
		document.getElementById('not_available').innerHTML = str;
	}
}

function ajax_do_call(func_name, args) {
	var i, x, n;
	var post_data;
	var target_id;
	target_id = ajax_target_id;
	if (ajax_request_type == "") 
		ajax_request_type = "GET";

	if (ajax_request_type == "GET") {

		if (uri.indexOf("?") == -1) 
			uri += "?rs=" + escape(func_name);
		else
			uri += "&rs=" + escape(func_name);
		uri += "&rst=" + escape(ajax_target_id);
		uri += "&rsrnd=" + new Date().getTime();

		for (i = 0; i < args.length-1; i++) 
			uri += "&rsargs[]=" + escape(args[i]);

		post_data = null;
	} 
	else if (ajax_request_type == "POST") {
		post_data = "rs=" + escape(func_name);
		post_data += "&rst=" + escape(ajax_target_id);
		post_data += "&rsrnd=" + new Date().getTime();

		for (i = 0; i < args.length-1; i++) 
			post_data = post_data + "&rsargs[]=" + escape(args[i]);
	}
	else {
		alert("Illegal request type: " + ajax_request_type);
	}

	x = ajax_init_object();
	x.open(ajax_request_type, uri, true);

	if (ajax_request_type == "POST") {
		x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
		x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}

	x.onreadystatechange = function() {
		if (x.readyState != 4) 
			return;

		var status;
		var data;
		var txt = x.responseText.replace(/^\s*|\s*$/g,"");
		document.getElementById(target_id).innerHTML = txt;
		if(target_id=="loginstatus")
		{
			if (txt=="ERROR")
			{
				document.getElementById("errRow").style.display="block";
			}
			else
			{
				document.getElementById("loginBox").style.display="none";
				//document.getElementById("comment").focus();
				if (txt=="OK")
					window.location.reload();
				else if (txt=="OK1")
					document.location.href = "Page.asp?id=1";
				else
					document.location.href = "activity.asp";
			}
		}
	}
	x.send(post_data);
	delete x;
	return true;
}
function ajax_init_object() {
	var A;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			A=null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();
	if (!A)
	{}
	return A;
}

function showNetworkContent(url,ctype,targetCtrl)
{
	ajax_target_id = targetCtrl;
	document.getElementById(ajax_target_id).innerHTML = "<font class=normalbold>Loading ......</font>";
	var username = "test";
	uri = url;
	x_update_info(username, check_handle);
}
function showContent(url,controlId)
{
	ajax_target_id = controlId;
	document.getElementById(ajax_target_id).innerHTML = "<font class=normalbold>Loading ......</font>";
	var username = "test";
	uri = url;
	x_update_info(username, check_handle);
}
