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();
var nocache = 0;

function login() {
	document.getElementById('login_response').innerHTML = "Loading...";
	
	var email = encodeURI(document.getElementById('username').value);
	var psw = encodeURI(document.getElementById('password').value);
	
	nocache = Math.random();
	
	http.open('get', where+'login.php?MsgBy='+email+'&QPass='+psw+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}

function logout() {
	
	//document.getElementById('login_response').innerHTML = "Loading...";
	nocache = Math.random();
	
	http.open('get', where+'logout.php?nocache = '+nocache);
	http.onreadystatechange = logoutReply;
	http.send(null);
}


function loginReply() {
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == 0){
			msgBox("Login ล้มเหลว","กรุณาตรวจสอบดูความถูกต้องของ username และ password <br /> หากพบความผิดพลาดโปรดติดต่อ webmaster");
			document.getElementById('login_response').innerHTML = 'Login failed! Verify user and password';
		} else {
			document.getElementById('login').innerHTML = '<div class="bodyRight circular"><h2>สวัสดี คุณ '+response+' [สามารถ logout ได้<a href="#" onClick="javascript: logout()">ที่นี่</a>]</h2></div>';
			msgBox("Login ได้แล้ว","สวัสดีคุณ "+response+" <br />  ขอบคุณที่ทำการล๊อคอินก่อนโพสต์ข้อความ");
						var sPath = window.location.pathname;
			var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
			if(sPage == 'postq.php'){
					document.getElementById('email').innerHTML = '';
					document.getElementById('by').innerHTML = response;
			}
		}
	}
}


function logoutReply() {
	if(http.readyState == 4){
			document.getElementById('login').innerHTML = '	  <div id="login_response"></div>  <form action="javascript:login()" method="post">			<strong>Member Login - -</strong>            Username       	<input type="text" name="MsgBy" id="username"/>            Password           <input type="password" name="QPass"  id="password"/> 		    <button class="awesome" type="submit" name="submit">Sign in</button>       </form>';
			var sPath = window.location.pathname;
			var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
			if(sPage == 'postq.php'){
					document.getElementById('email').innerHTML = '<dt><label for="username"><strong>E-mail</strong></label></dt>        <dd>          <input type=text name="QEmail" size=35 maxlength=50>        </dd>';
					document.getElementById('by').innerHTML = '<input type=text name="QName" size=15 maxlength=50>        Password<font color=red>&nbsp;(สำหรับสมาชิก)</font>&nbsp;&nbsp; <input type=password name="QPass" size=15 maxlength=10>';
			}
			msgBox("Logout เรียบร้อยแล้ว", "ขอบคุณที่เข้ามาเยี่ยมชมเวปไซต์ครับ");
	}
}

function msgBox(headText,bodyText){
	//jquery function นะฮ๊าฟฟ
	$(document).ready(function() {
	$("#facebox").overlay({
		top: 260,
		mask: {
			color: '#fff',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: false,
		load: true
	});
	});
	
	document.getElementById('msgBoxH').innerHTML = headText;
	document.getElementById('msgBoxB').innerHTML = bodyText;
}




function msgBoxPage(headText,page){
	nocache = Math.random();
	
	http.open('get', page+'&nocache = '+nocache);
	http.onreadystatechange = msgBoxPageReply;
	http.send(null);
}

function msgBoxPageReply() {
	if(http.readyState == 4){
				
		document.getElementById('msgBoxH').innerHTML = "Profile";
		document.getElementById('msgBoxB').innerHTML = http.responseText;
		$("#facebox").overlay({
			top: 100,
			mask: {
				color: '#fff',
				loadSpeed: 200,
				opacity: 0.5
			},
			closeOnClick: false,
			load: true
		});

	}
}



