var xmlhttp;
var xmlhttp_func;

function ajax_init() {
	if(navigator.appName == "Microsoft Internet Explorer") {
  		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
  		xmlhttp = new XMLHttpRequest();
	}
}
function ajax_get(url)
{
	ajax_init();
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}
function ajax_post(url,para)
{
	ajax_init();
	if (xmlhttp) {
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.send(para);
		return xmlhttp.responseText;
	} else {
		return "POST FAILED";
	}
	return "OBJECT NOTHING";
}
function ajax_posta(url,para,func)
{
	xmlhttp_func=func;
	ajax_init();
	if (xmlhttp) {
		xmlhttp.open("POST",url,true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				xmlhttp_func(xmlhttp.responseText);
			}
		};
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.send(para);
		return "OK";
	} else {
		return "POST FAILED";
	}
	return "OBJECT NOTHING";
}
function ajax_post_xml(url,para)
{
	ajax_init();
	if (xmlhttp) {
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.send(para);
		xmldoc=xmlhttp.responseXML.documentElement;
		if (xmldoc==null) {
			return "XML INVALID";
		} else {
			return xmldoc;
		}
	} else {
		return "POST FAILED";
	}
	return "OBJECT NOTHING";
}
function ajax_check(sql,table,cond,key) {
	var para="sql="+sql;
	if (sql=="") {
		para="table="+table+"&cond="+cond+"&key="+key;
	}
	var response=ajax_post_xml(ajax_url_root()+ "check.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	if (result==0) {
		if (response.getElementsByTagName("msg")[0].firstChild!=null) {
			var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
			alert(msg);
		}
		return 0;
	}
	return 1;
}
function ajax_duplicate(table,fields,values) {
	para="table="+table+"&fields="+fields+"&values="+values;
	var response=ajax_post_xml(ajax_url_root()+ "check.php",para);
	if (typeof response != "object") {
		return -1;
	}
	return response.getElementsByTagName("result")[0].firstChild.nodeValue.valueOf();
}
// msg_mode=1:display the message from the server
function ajax_alogin(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin800(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin800.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin3000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin3000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}

function ajax_alogin8000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin8000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin10000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin10000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin50000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin50000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin350000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin350000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_alogin500000(email,password,next_page,msg_mode) {
	var para="email="+email+"&password="+password+"&next_page="+next_page;
	var url_root=ajax_url_root();
	var response=ajax_post_xml(url_root+ "alogin500000.php",para);
	if (typeof response != "object") {
		return -1;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	var ms_id=0;
	if (result==0) {
		if (msg_mode==1) {
			if (response.getElementsByTagName("msg")[0].firstChild!=null) {
				var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
				alert(msg);
			}
		}
	} else {
		if (response.getElementsByTagName("id")[0].firstChild!=null) {
			ms_id=response.getElementsByTagName("id")[0].firstChild.nodeValue;
		}
	}
	return ms_id;

}
function ajax_get_data(sql,table,field,cond,sort) {
	var para="sql="+sql;
	if (sql=="") {
		para="table="+table+"&cond="+cond+"&sort="+sort;
	}
	var response=ajax_post_xml(ajax_url_root() + "get_data.php",para);
	if (typeof response != "object") {
		return false;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	if (response.getElementsByTagName("msg")[0].firstChild!=null) {
		var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
		alert(msg);
	}
	return response;
}
function ajax_url_root() {
	var loc=window.location.toString();
	var end_slash=loc.lastIndexOf("/");
	return loc.substring(0,end_slash) + "/ajax/";
}

function ajax_send_mail(from,to,cc,bcc,subject,contents) {
	var para="from="+from+"&to="+to+"&cc="+cc+"&bcc="+bcc+
			"&subject="+subject+"&contents="+contents;
	var response=ajax_post_xml(ajax_url_root() + "send_mail.php",para);
	if (typeof response != "object") {
		return false;
	}
	var result=response.getElementsByTagName("result")[0].firstChild.nodeValue;
	if (response.getElementsByTagName("msg")[0].firstChild!=null) {
		var msg=response.getElementsByTagName("msg")[0].firstChild.nodeValue;
		alert(msg);
	}
	return result;

}
