var IsInstallActiveX = 0;
var IsInstall = true;
function trim(str) { return str.replace(/^\s*|\s*$/g,""); }
function validate_form()
{
	var f = document.frm;
	if(trim(f.id.value)=="" || trim(f.id.value)=="Visa ID")  { alert("กรุณาระบุ ID เล่นเกม ของท่านให้ครบถ้วน"); f.id.focus(); return false; }
	if(trim(f.passwd.value)=="") { alert("กรุณาระบุ Password ของท่านให้ครบถ้วน"); f.passwd.focus(); return false; }	
	return true;
}
function login_submit(id, passwd) {
	var frm = document.getElementById("frm");
	id = trim(id); passwd = trim(passwd);
	if(id=="" || passwd=="") { alert("กรุณาใส่ Visa ID และ Password ให้ครบถ้วนค่ะ"); }
	else { frm.id.value=id; frm.passwd.value=passwd; frm.submit(); }
}
function logout() {
	if(confirm("ต้องการออกจากระบบ ใช่หรือไม่ ?")) { window.location = "http://pucca.ini3.co.th/startgame/logout.asp"; }
}
function startgame(id, pass_hash, key_hash) {
	var divActiveX = document.getElementById("ActiveXObject");
	var code;
	try { 
		var x = new ActiveXObject("PUCCALAUNCHER.PuccaLauncherCtrl.1");   
		IsInstallActiveX = 1;
		code = "<object classid='clsid:6ABF3915-10A2-4EDD-A0C0-7FF44B4F4FEE' ";
		code = code + "codebase='http://pucca.ini3.co.th/startgame/PuccaLauncher.ocx#version=1,0,0,1' ";
		code = code + "width='10' height='10' id='PuccaAX_Launcher' TYPE='application/x-oleobject' ";
		code = code + "onError=>";
		code = code + "<PARAM NAME='sUserName' VALUE='"+id+"'>";
		code = code + "<PARAM NAME='sPasswd' VALUE='"+pass_hash+"'>";
		code = code + "<PARAM NAME='sSerial' VALUE='"+key_hash+"'>";
		code = code + "</object>";
		divActiveX.innerHTML = code;
	}
	catch(e) { Msgbox_ActiveX_Control(); IsInstall=false; check_install(); }
}
function msgbox(msg) { alert("ไม่สามารถ Start game ได้"); }
function loginfirst() { 
	var this_location = window.location;
	alert("กรุณา login ก่อนเข้าเล่นเกมค่ะ");
	window.location=this_location;
}
function activate_first() { alert("ไอดีนี้ยังไม่ได้ทำการ Activate เกม PuccaRacing\nกรุณา Activate เกม PuccaRacing ก่อนค่ะ"); window.open("http://visa.ini3.co.th/"); }
function check_ActiveX() {
	try { 
		var x = new ActiveXObject("PUCCALAUNCHER.PuccaLauncherCtrl.1"); 
		setActiveX();  
		IsInstallActiveX = 1;
	}
	catch(e) { Msgbox_ActiveX_Control(); IsInstall=false; check_install(); }
}
function check_install() {
	try {
		if(!IsInstall) {
			var x = new ActiveXObject("PUCCALAUNCHER.PuccaLauncherCtrl.1"); 
			IsInstall=true;
			setTimeout("check_install();", 1000);
		}
		else {
			alert("Install PuccaRacing ActiveX เรียบร้อยแล้ว\nกดปุ่ม Start Game เพื่อเล่นเกมค่ะ");
			IsInstallActiveX=1
		}		
	}
	catch(e) { 
		IsInstall=false;
		setTimeout("check_install();", 1000);
	}
}
function setActiveX(){
	var divActiveX = document.getElementById("ActiveXObject");
	var code;
	code = "<object classid='clsid:6ABF3915-10A2-4EDD-A0C0-7FF44B4F4FEE' ";
	code = code + "codebase='http://pucca.ini3.co.th/startgame/PuccaLauncher.ocx#version=1,0,0,1' ";
	code = code + "width='10' height='10' id='PuccaAX_Launcher' TYPE='application/x-oleobject' ";
	code = code + "onError=>";
	code = code + "<PARAM NAME='sUserName' VALUE=''>";
	code = code + "<PARAM NAME='sPasswd' VALUE=''>";
	code = code + "<PARAM NAME='sSerial' VALUE=''>";
	code = code + "</object>";
	divActiveX.innerHTML = code;
}
//function Msgbox_ActiveX_Control(){ alert("ตัวควบคุม ActiveX ยังไม่ได้ทำการติดตั้ง \nกรุณาติดตั้งเพื่อที่จะเรียกเกมส์"); setActiveX(); }
function Msgbox_ActiveX_Control(){ 
	alert("ตัวควบคุม ActiveX ยังไม่ได้ทำการติดตั้ง \nกรุณาติดตั้งเพื่อที่จะเรียกเกมส์");
	window.location="http://pucca.ini3.co.th/download/activex.asp"; 
}
function startgame_authen() {
	//------------------------------------------------------
	var xmlHttp = null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) { // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}	
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return false;
	}
	//------------------------------------------------------
	var url="http://pucca.ini3.co.th/startgame/startgame_action.asp";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			startgame_param=xmlHttp.responseText;
			arr_param = startgame_param.split("&");
			funct = arr_param[6].replace(/f=/,"");
			if(funct=="startgame") {
				startgame(arr_param[3].replace(/id=/,""), arr_param[4].replace(/passwd=/,""), arr_param[5].replace(/serial_key=/,""));
			}
			else if(funct=="loginfirst") { loginfirst(); }
			else if(funct=="msgbox") { msgbox("msg"); }
			else if(funct=="activate_first") { activate_first(); }
		}
	};
	//------------------------------------------------------
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	//------------------------------------------------------
}
function chk_textbox(obj, value) {
	if(document.getElementById(obj).value==value) { document.getElementById(obj).value=""; }	
}