/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function redirectTo(targetpage){
	window.location=targetpage;
}


function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('login_response').innerHTML = "Loading..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var username = encodeURI(document.getElementById('login_username').value);
var password = encodeURI(document.getElementById('login_password').value);
var scode = encodeURI(document.getElementById('code').value);

// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'login_access.php?username='+username+'&password='+password+'&scode='+scode+'&nocache='+nocache);
http.onreadystatechange = loginReply;
http.send(null);
}
function loginReply() {
if(http.readyState == 4){
var responseb = http.responseText; // รับส่วนตอบกลับจาก server
var da = responseb.split( '|' ); // แบ่งออกเป็นสองส่วน
var respStatus = da[0]; // ค่าส่วนแรกเก็บเข้าตัวแปร
var respMessage = da[1]; // ค่าส่วนที่สองเก็บเข้าตัวแปร
var resUrl = da[2]; // ค่าส่วนที่สามเก็บเข้าตัวแปร

if(respStatus == "success") {

	document.getElementById('login_response').innerHTML = respMessage;
	setTimeout("redirectTo('" + resUrl + "')",2000);

}else{
	document.getElementById('login_response').innerHTML = respMessage;
	document.getElementById('login_username').value='';
	document.getElementById('login_password').value='';
	document.getElementById('code').value='';
	document.getElementById('captcha_iamge').src = 'library/captcha/code.php?'+Math.random();
	
	
	}
	 
}


}

