var rule_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var __Website		= "quiz2.eluhome.com";
var __AjaxPath	= "http://" + __Website + "/Index_Login.htm";

$(document).ready(function() {
	SetFocus();
});


function SetFocus() {
	if($.browser.safari) {
		$("#Account").focus();		
	} else {
		$("#Account").keyup( function (e) { SendAnswer(e); } ).focus();
		$("#Password").keyup( function (e) { SendAnswer(e); } );
	}
}


function CheckValue(name) {
	var fieldName = "#" + name;
	var hintName = "#Required_" + name;
	var length = $(fieldName).val();
	if(length.length <= 0) {
		$(hintName).html('必填');		
		$(hintName).fadeIn();		
		return false;
	} else {
		if(name == 'Email') {		
			if(rule_email.test($(fieldName).val())) {
				$(hintName).fadeOut();
				return true;
			} else {				
				$(hintName).html('錯誤');		
				$(hintName).fadeIn();
				return false;
			}
		}
		$(hintName).fadeOut();
		return true;		
	}
}


function CheckField(frmName) {
	var chk01 = CheckValue('Account');
	var chk02 = CheckValue('Password');
	
	if(chk01 && chk02) {
		$("#mode").val("Send");
		eval("document." + frmName + ".submit();");
	}
}


function CheckAccount(acc) {
	if(acc.length > 0) {
		$.post( __AjaxPath, 
						{ mode: "CheckAccount", Account: acc }, 
						function(data) {
							if(data != '') {
								var obj = eval("("+data+")");
								if(obj.Result) {
									$("#Required_Account").html('已使用');
									$("#Required_Account").fadeIn();
								} else {
									$("#Required_Account").html('可使用');
									$("#Required_Account").fadeIn();								
								}
							}
						} 
		);
	}
}



function CheckEmail(email) {
	if(email.length > 0) {
		$.post( __AjaxPath, 
						{ mode: "CheckEmail", Email: email }, 
						function(data) {
							if(data != '') {
								var obj = eval("("+data+")");
								if(obj.Result) {
									$("#Required_Email").html('已使用');
									$("#Required_Email").fadeIn();
								} else {
									$("#Required_Email").html('可使用');
									$("#Required_Email").fadeIn();								
								}
							}
						} 
		);		
	}
}


/*
 * onkeyup="SendAnswer(event);"
 */
function SendAnswer(e) {
	var key = (window.event)? e.keyCode : e.which;
		switch (key) {
			case 13:
				CheckField('frmLogin');
			break;
		}
	
}