// JavaScript Document
var xmlHttp;

function GetXmlHttpObject()
{
	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");
    	}
  	}
	return xmlHttp;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
function chkLogin()
{ 
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var choice_time = document.getElementById("choice_time").value;
	if(!username){
		document.getElementById("msgLogin").innerHTML = 'กรุณาใส่ชื่อสมาชิก/Please enter your username !!';
		document.getElementById("username").focus();
		return false;
	}else if(!password){
		document.getElementById("msgLogin").innerHTML = 'กรุณาใส่รหัสผ่าน/Please enter your password !!';
		document.getElementById("password").focus();		
		return false;
	}else{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="login_parlex-src.php";
		url=url+"?username="+username;
		url=url+"&password="+password;	
		url=url+"&choice_time="+choice_time;	
		xmlHttp.onreadystatechange=function(){
			if (xmlHttp.readyState==4){ 																		
				//alert("xmlHttp.responseText : " +xmlHttp.responseText);
				if(xmlHttp.responseText==""){		
					if(choice_time!="0"){
						window.location.href = "autologin_warning.php?t="+choice_time;
					}else{
						window.location.href = "translations.php";
					}
				}else{
					document.getElementById("msgLogin").innerHTML = xmlHttp.responseText;
					return false;
				}				
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function chkusername()
{ 
	var username = document.getElementById("username").value;
	msg = "";
	if(!username){
		document.getElementById("msgUsername").innerHTML = 'กรุณาใส่ชื่อสมาชิก(username) !!';
		document.getElementById("username").focus();
		return false;
	}else{
		
		if ((username.length >20) || (username.length <4)){
			msg = "ชื่อสมาชิก(อย่างน้อย4ตัวอักษร แต่ไม่เกิน20ตัวอักษร)!!";	
		}else{
			symbol = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ_0123456789";
			for(i=0; i<username.length; i++){
				r = symbol.indexOf(username.charAt(i));
				if( symbol.indexOf(username.charAt(i))==-1 ){
					msg = "ชื่อสมาชิก(ต้องเป็นa-z, A-Z, 0-9)!!";						
					break;
				}
			}
		}
		if(msg != ""){
			document.getElementById("msgUsername").innerHTML = msg;
			document.getElementById("username").focus();
			return false;
		}else{
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null){
				alert ("Your browser does not support AJAX!");
				return;
			} 
			var url="checkusername-src.php";
			url=url+"?username="+username;
			xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 																		
						document.getElementById("msgUsername").innerHTML = xmlHttp.responseText;
				}
			}
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
	}
}