var	form		=	{
	button_pending	:	'<IMG SRC="2009/pages/images/loading.gif" ALIGN="absmiddle">作業中...' ,
	button_submit	:	$(':button[name=CMDsubmit]').html()
} ;

$(document).ready(function(){
	$('#apply_form').ajaxForm({
		target		:	'#apply_form_output',
		beforeSend	:	function(){
			$(':button[name=CMDsubmit]').attr('disabled',true).html(form['button_pending']) ;
			$('.ajax_report').css('display','none') ;
			$('.error_message').remove() ;
			form['target']			=	$(this).attr('target') ;
			form['message_queue']	=	new Array() ;
			form['opcode']			=	1 ;
		},
		success		:	function(data) {
			try	{
				data			=	ajax_output(form['target']) ;
				form['opcode']	*=	data['opcode'] ;
				if	(data['opcode'] == 1) {
					alert(data['message']) ;
					$('#apply_form :hidden[name=UID]').val(data['UID']) ;
					top.location.href	=	$('base').attr('href') + 'application-confirm' ;
					return	false ;
				}
				else	{
					if	(data['verified'] != undefined) {
						for	(var i in data['verified']) {
							$('<P CLASS="error_message">' + data['verified'][i] + '</P>').appendTo('TR[field=' + i + '] TD.verified') ;
						}
					}
					alert(data['message']) ;
					$(':button[name=CMDsubmit]').attr('disabled',false).html(form['button_submit']) ;
				}
			} catch(e) {
				$('.ajax_report').css('display','inline') ;
				$(':button[name=CMDsubmit]').attr('disabled',false).html(form['button_submit']) ;
				alert(e.message) ;
			}
		}
	}); 
});
function	ajax_output(id) {
	if	($(id + ' > PRE').size())	return	eval('('+ $(id + ' > PRE').html() + ')') ;
	else							return	eval('('+ $(id).html() + ')') ;
}
function	submit_apply() {
	var	f	=	document.apply_form ;
	var	err	=	new	Array() ;
	if	(f.EMAIL.value == '')				err.push('‧註冊帳號') ; //	f.EMAIL.focus() ;
	if	(f.PWD.value == '')					err.push('‧密碼') ; //	f.PWD.focus() ;
	if	(f.PWD_2.value == '')				err.push('‧密碼確認') ; //	f.PWD_2.focus() ;
	if	(f.profile_NAME.value == '')		err.push('‧姓名') ; //	f.profile_NAME.focus() ;
	if	(f.profile_MOBILE.value == '')		err.push('‧手機') ; //	f.profile_MOBILE.focus() ;
	if	(f.profile_SCHOOL.value == '')		err.push('‧任教資訊 - 學校') ; //	f.profile_SCHOOL.focus() ;
	if	(f.profile_DEPARTMENT.value == '')	err.push('‧任教資訊 - 科系') ; //	f.profile_DEPARTMENT.focus() ;
	if	(f.profile_TEL_SCHOOL.value == '')	err.push('‧聯絡電話 - 學校') ; //	f.profile_TEL_SCHOOL.focus() ;
	if	(f.AUTH_CODE.value == '')			err.push('‧認證代碼') ; //	f.AUTH_CODE.focus() ;
	if	($('#apply_form :input[name=AGREEMENT]:checked').size() == 0)		err.push('‧已閱讀並同意遵守會員條款') ; //	f.AUTH_CODE.focus() ;
	if	(err.length > 0)	{
		alert(err.join('\n') + '\n' + '－－－－－－－－－－－－－－－－－\n以上欄位為必填，請確實填寫，謝謝！') ;
		return	false ;
	}
	else	$('#apply_form').submit() ;
	return	false ;
}
