<?php
ob_start();
session_start();

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

$str_pin = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
$_SESSION['pin'] = $str_pin;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="zh-tw" />
<meta name="robots" content="none" />
<link rel="stylesheet" type="text/css" href="admin.css" />
<title>登入</title>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
<!--
function do_login(element_form)
{
	if( element_form.account.value == '' )
	{
		alert('請輸入登入帳號');
		return false;
	}

	if( element_form.password.value == '' )
	{
		alert('請輸入登入密碼');
		return false;
	}

	if( element_form.valid_code.value == '' )
	{
		alert('請輸入圖文驗證碼');
		return false;
	}

	var regexp_valid_code =  /^\d{4}$/;
	if( !regexp_valid_code.test(element_form.valid_code.value) )
	{
		alert('請填寫正確的圖文驗證碼');
		return false;
	}

	var obj_form_data = new FormData(element_form);

	$('#button_submit').attr('disabled', true);

	$.ajax(
	{
		url: 'ajax_do_login.php',
		type: 'POST',
		data: obj_form_data,
		cache: false,
		processData: false,
		contentType: false,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ValidPin'] != '' )
				$('#valid_code').attr('src', 'generate_pass.php?pin=' + jdata['ValidPin']);

			$('#button_submit').attr('disabled', false);
		}
	});

	return false;
}
//-->
</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<div id="content_login">
<h1><img alt="三好文創" title="三好文創" src="images/logo.png" /></h1>
<form onsubmit="return do_login(this);">
<table>
<tr><td><input class="field" type="text" name="account" placeholder="帳號" maxlength="255" /></td></tr>
<tr><td><input class="field" type="password" name="password" placeholder="密碼" size="12" maxlength="12" /></td></tr>
<tr><td><input class="valid_code" type="text" name="valid_code" placeholder="驗證碼" size="5" maxlength="4" /><img id="valid_code" class="valid_code" src="generate_pass.php?pin=<?=$str_pin?>" /></td></tr>
</table>
<input class="button_submit" id="button_submit" type="submit" value="登入" />
</form>
</div>
</body>

</html>