// JavaScript Document

function Login( str_admin_username_id , str_admin_password_id , str_verifying_code_id , type )
{
	var username = document.getElementById( str_admin_username_id ).value;
	var password = document.getElementById( str_admin_password_id ).value;
	if(str_verifying_code_id!=0)
		var verifying_code = document.getElementById( str_verifying_code_id ).value;

	request = createRequest();
	var url = "js/ajax/login.php";
	request.open( "POST" , url , true );
	request.onreadystatechange = function()
	{
		if( Response() )
		{			
			//alert(request.responseText);
			var obj_xmldoc = request.responseXML;
			
			var str_result = obj_xmldoc.getElementsByTagName( "result" ).item(0).childNodes[0].nodeValue;
			var str_message = obj_xmldoc.getElementsByTagName( "message" ).item(0).childNodes[0].nodeValue;
			alert(str_message);
			
			if( str_result == "1" )
			{
				//導向頁面
				location.reload();
				//location.href='home.php';
			}
			else if( str_result == "3" )
			{
				location.reload();
				//location.href='member_questionnaire.php';
				//showMask();showPopup('popup2');
				//window.onresize=function(){checkMaskRange();showPopup('popup2')};				
			}
			else
			{
				showMask();showPopup('popup');
				window.onresize=function(){checkMaskRange();showPopup('popup')};
				if( str_result == "2" )
				{
					RebuildVerifyingCode( document.getElementById( "img_verifying_code" ) );					
					
					document.getElementById( "verifying_code" ).value = "";
					document.getElementById( "verifying_code" ).focus();
				}
			}
		}
	};
	request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
	request.send(	"username=" + username + 
					"&password=" + password + 
					"&verifying_code=" + verifying_code + 
					"&type=" + type);
}

//登出
function Logout()
{
	request = createRequest();
	var url = "js/ajax/logout.php";
	request.open( "POST" , url , true );
	request.onreadystatechange = function()
	{
		if( Response() )
		{
			//導向頁面
			alert( request.responseText );
			location.reload();
		}
	};
	request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
	request.send( null );
}

//重新產生驗證碼
function RebuildVerifyingCode( obj_verifying_code_img )
{
	var verifying_code_url = obj_verifying_code_img.src.split( "?" );
	verifying_code_url = verifying_code_url[0];
	
	obj_verifying_code_img.src = verifying_code_url + "?" + Math.random();
}