/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
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 = "<img src='images/loader.gif' width=''> Loading..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var account = encodeURI(document.getElementById('account').value);
var passwd = encodeURI(document.getElementById('pwd').value);
var sn = encodeURI(document.getElementById('sn').value);

if(account==''||passwd==''||sn=='')
{
	alert('欄位請勿空白');
	return;
}

http.open('get', 'checksn.php?account='+account+'&passwd='+passwd+'&sn='+sn+"&dummy=" + new Date().getTime());
http.onreadystatechange = loginReply;
http.send(null);
}
function loginReply()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
	//      alert(response);
		
			if(response == 1)
			{
			// if login fails
				//document.getElementById('login_response').innerHTML = '帳號或密碼錯誤!';
				alert('帳號輸入錯誤!');
				//window.document.location="http://www.therichroad.com/event/eventcode/index.htm";
				return;
			// else if login is ok show a message: "Welcome + the user name".
			} 
			else
			{
				if(response == 2)
				{
					/*document.getElementById('login_response').innerHTML = '驗證碼錯誤!';
					window.frames['VerifyFrameObject'].vLoad();
					document.getElementById('validate').value = "";*/
					alert('兌換序號或密碼錯誤');
					//window.document.location="http://www.therichroad.com/event/eventcode/index.htm";
					return;
				}
				if(response == 3)
				{
					/*document.getElementById('login_response').innerHTML = '驗證碼錯誤!';
					window.frames['VerifyFrameObject'].vLoad();
					document.getElementById('validate').value = "";*/
					alert('序號已使用過囉');
					//window.document.location="http://www.therichroad.com/event/eventcode/index.htm";
					return;
				}
				if(response == 4)
				{
					//window.document.location.reload();
					alert('序號獎品已匯入您的帳號中囉^_^');
					//window.document.location="http://www.therichroad.com/event/eventcode/index.htm";				
					return;
				}
				if(response == 5)
				{
					alert('一個遊戲帳號只能兌換一次喔');
					//window.document.location="http://www.therichroad.com/event/eventcode/index.htm";
					return;
				}
					
			}
		}	
}
