//form
var AjaxForm = {
	init: function() {
		var file = location.pathname;
		
		if(file.indexOf('contact.php') > -1) {
			page = 'contact';
			ajaxURL = 'form/contact_send.php';
		};
		if(file.indexOf('request.php') > -1) {
			page = 'request';
			ajaxURL = 'form/request_send.php';
		}
		
		//console.log(page);
		
		this.content = $('formContent');
		this.firstProcess();
	},
	
	
	// 処理分岐
	firstProcess: function() {
		switch(this.status) {
			case 'submit':
				this.caseSubmit();
				break;
			case 'send':
				this.caseSend();
				break;
			default:
				this.firstView();
		};
	},
	
	
	// ページロード：DOM取得
	caseSubmit: function() {
		this.formElement = $(page+'Form');
		this.submitBtn = $('submitBtn');
		this.resetBtn = $('resetBtn');
		
		if(page=='contact'){
			this.submitBtn.onclick = this.contactVali.bind(this);
		}else if(page=='request'){
			this.submitBtn.onclick = this.requestVali.bind(this);
		}
		this.resetBtn.onclick = this.reset.bind(this);
	},
	caseSend: function() {
		this.sendBtn = $('sendBtn');
		this.backBtn = $('backBtn');
		
		this.sendBtn.onclick = this.submit.bind(this);
		this.backBtn.onclick = this.firstView.bind(this);
	},
	
	
	// サブミット処理
	submit: function() {
		// サブミット時
		if(this.status == 'submit') {
				var url = ajaxURL + '?status=2';
				this.formElement.action = url;
				this.formElement.send({
					update: this.content,
					onSuccess: this.successProcess.bind(this,2),
					onComplete: function(response) {
						AjaxForm.firstProcess();
					},
					onFailure: this.errorProcess.bind(this)
				});
		// メール送信時
		} else if(this.status == 'send') {
			var url = ajaxURL + '?status=3';
			new Ajax(url, {
				method: 'post',
				update: this.content,
				onSuccess: this.successProcess.bind(this,3),
				onFailure: this.errorProcess.bind(this)
			}).request();
		};
		return false;
	},
	
	// リセット時
	reset: function() {
		//this.formElement.reset();
		var elements =  this.formElement.getFormElements();
		elements.each(function(el) {
			el.value = '';
		});
		return false;
	},
	
	// 初期画面
	firstView: function() {
		var url = ajaxURL + '?status=1';
		new Ajax(url, {
			method: 'post',
			update: this.content,
			onSuccess: this.successProcess.bind(this,1),
			onComplete: this.caseSubmit.bind(this),
			onFailure: this.errorProcess.bind(this)
		}).request();
		return false;
	},
	
	// Ajax成功時処理
	successProcess: function(status) {
		Imagefader.init();
		switch(status) {
			case 1:
				this.status = 'submit';
				if(page == 'request') {
					$("zipSearchTrigger").addEvent('click',function(){searchZip('zipId')});
					$$(".age").each(function(ele){ele.addEvent('blur',getAge.bind(this))});
				};
				break;
			case 2: this.status = 'send'; break;
			case 3: this.status = 'submit'; break;
		}
	},
	
	// エラー時
	errorProcess: function() {
		this.content.setHTML('<p class="failure">データの通信に失敗しました</p>');
	},
	
	// contactのValidate
	contactVali: function(){
		var alert_message_ary = Array();
	
		var form_element = this.formElement;	

		if( !CheckForm( form_element, "text", "nameId" ) ){
			alert_message_ary.include( '"氏名" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", "kanaId" ) ){
			alert_message_ary.include( '"フリガナ" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", 'mailId' ) ){
			alert_message_ary.include( '"メールアドレス" を入力してください。' );
		}else if( !CheckForm( form_element, "mail", 'mailId' ) ){
			alert_message_ary.include( '"メールアドレス" を正しく入力してください。' );
		}
		if( !CheckForm( form_element, "text", "telId" ) ){
			alert_message_ary.include( '"お電話番号" を入力してください。' );
		}
		if( alert_message_ary.length == 0 ){
			this.submit();
		}else{
			var alert_message_str = alert_message_ary.join("\n");
			alert_message_str = "以下の項目を再確認してください。\n\n" + alert_message_str;
			alert( alert_message_str );
		}
		return false;
	},

	// requestのValidate
	requestVali: function(){
		var alert_message_ary = Array();
	
		var form_element = this.formElement;
		
		if( !CheckForm( form_element, "check", "siryo" ) ){
			alert_message_ary.include( '"ご希望の資料" を選択してください。' );
		}
		if( !CheckForm( form_element, "text", "nameId" ) ){
			alert_message_ary.include( '"氏名" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", "kanaId" ) ){
			alert_message_ary.include( '"フリガナ" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", 'mailId' ) ){
			alert_message_ary.include( '"メールアドレス" を入力してください。' );
		}else if( !CheckForm( form_element, "mail", 'mailId' ) ){
			alert_message_ary.include( '"メールアドレス" を正しく入力してください。' );
		}
		if( !CheckForm( form_element, "text", "zipId" ) ){
			alert_message_ary.include( '"郵便番号" を入力してください。' );
		}
		if( !CheckForm( form_element, "select", "jusyoId" ) ){
			alert_message_ary.include( '"住所(都道府県)" を選択してください。' );
		}
		if( !CheckForm( form_element, "text", "address01Id" )){
			alert_message_ary.include( '"住所" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", "address02Id" )){
			alert_message_ary.include( '"住所" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", "phoneId" ) ){
			alert_message_ary.include( '"お電話番号" を入力してください。' );
		}
		if( !CheckForm( form_element, "text", "schoolId" ) ){
			alert_message_ary.include( '"在籍校・出身校" を入力してください。' );
		}
		if( !CheckForm( form_element, "select", "divisionId" ) ){
			alert_message_ary.include( '"学年" を選択してください。' );
		}
		if( alert_message_ary.length == 0 ){
			this.submit();
		}else{
			var alert_message_str = alert_message_ary.join("\n");
			alert_message_str = "以下の項目を再確認してください。\n\n" + alert_message_str;
			alert( alert_message_str );
		}
		return false;
	}	
	
};



window.addEvent('domready', AjaxForm.init.bind(AjaxForm));