/*******************************************************************************

 各画面共通の動き

 @auther kawamata@team-lab

 確認ブラウザ
  WindowsXP
    InternetExplorer 7.0.5730.11IC
    Opera 9.1.0
    Firefox 2.0.0.4
    Safari 3.0.3
    Netscape 7.1

 *******************************************************************************/

//
// 画面ロード時の処理
//---------------------------------------------------------
Event.observe(window, 'load', function(){

// === ページトップへの動き ===
var pageTop = $("to_pagetop");
if(!!pageTop){
	Event.observe(pageTop, 'click', function(event){
		var top = $('pagetop');
		if(!top) return;

		Event.stop(event);

		Leopalace.ScrollManager.moveTo(Position.cumulativeOffset(top)[1]);
	});
}

});

/**
 * バリデーション用のエラーメッセージオブジェクト
 */
Leopalace.Validator.ErrorObjects = new (Class.create({

	/**
	 * コンストラクタ
	 */
	initialize : function(){
		this.errors = {
			"error.invalid"       : this.createErrorBox("https://www.leopalace21.com/leo/joken/img/search/02/img/obj_fom_error01.gif", "入力内容に誤りがあります", 158, 22),
			"error.required"      : this.createErrorBox("https://www.leopalace21.com/leo/joken/img/search/02/img/obj_fom_error03.gif", "入力されていません", 158, 22),
			"error.select"        : this.createErrorBox("https://www.leopalace21.com/leo/joken/img/search/02/img/obj_fom_error02.gif", "項目を選択してください", 158, 22),
			"error.overselect"    : this.createErrorBox("https://www.leopalace21.com/leo/joken/img/search/02/img/obj_fom_error04.gif", "上限を超えて選択されています", 158, 22)
		};
	},

	/**
	 * エラーボックスを作成します
	 * @param imgSrc 画像URL
	 * @param imgAlt 画像のALT
	 * @param imgWidth 画像の幅
	 * @param imgHeight 画像の高さ
	 */
	createErrorBox : function(imgSrc, imgAlt, imgWidth, imgHeight){

		var img = document.createElement('img');
		img.src    = imgSrc;
		img.alt    = imgAlt
		img.width  = imgWidth;
		img.height = imgHeight;
		img.border = 0;

		return img;
	},

	/**
	 * エラーを見つけます
	 * @param type Leopalace.Validator.Checkerが返すエラーコード
	 */
	findError : function(type){
		return (this.errors[type] || this.errors["error.invalid"]).cloneNode(true);
	}
}))();

/**
 * バリデーショングループ
 */
Leopalace.Validator.BasicGroupA = Class.create(Leopalace.Validator.Group, {

	/**
	 * @see Leopalace.Validator.Group#init
	 */
	init : function(params){
		this.chipError = $(params.chip); // pをつける
		this.dlElement = this.chipError.parentNode.parentNode; // dlに来るはず
		this.head = this.dlElement.parentNode; // tdに来るはず
	},

	/**
	 * グループの頭のY座標を取得します
	 */
	getHead : function(){
		return (!!this.head) ? Position.cumulativeOffset(this.head)[1] : 0;
	},

	/**
	 * @see Leopalace.Validator.Group#onInit
	 */
	onInit : function(){
		Element.removeClassName(this.dlElement, 'ok');
		Element.removeClassName(this.dlElement, 'ng');
	},

	/**
	 * @see Leopalace.Validator.Group#onValid
	 */
	onValid : function(){
		Element.addClassName(this.dlElement, 'ok');
		Element.removeClassName(this.dlElement, 'ng');
	},

	/**
	 * @see Leopalace.Validator.Group#onInvalid
	 */
	onInvalid : function(errorCode){
		Element.addClassName(this.dlElement, 'ng');
		Element.removeClassName(this.dlElement, 'ok');

		$A(this.chipError.childNodes).each(function(e){
			e.parentNode.removeChild(e);
		});

		this.chipError.appendChild(Leopalace.Validator.ErrorObjects.findError(errorCode));
	}
});

Leopalace.Validator.BasicGroupB = Class.create(Leopalace.Validator.Group, {

	/**
	 * @see Leopalace.Validator.Group#init
	 */
	init : function(params){
		this.chipError = $(params.chip); // pをつける
		this.dlElement = this.chipError.parentNode; // divに来るはず
		this.head = this.dlElement.parentNode; // tdに来るはず
		this.cond = params.cond || function(){return true};
	},

	isCheckEnabled : function(){
		return this.cond.apply(this);
	},

	/**
	 * グループの頭のY座標を取得します
	 */
	getHead : function(){
		return (!!this.head) ? Position.cumulativeOffset(this.head)[1] : 0;
	},

	/**
	 * @see Leopalace.Validator.Group#onInit
	 */
	onInit : function(){
		Element.removeClassName(this.dlElement, 'ok');
		Element.removeClassName(this.dlElement, 'ng');
	},

	/**
	 * @see Leopalace.Validator.Group#onValid
	 */
	onValid : function(){
		Element.addClassName(this.dlElement, 'ok');
		Element.removeClassName(this.dlElement, 'ng');
	},

	/**
	 * @see Leopalace.Validator.Group#onInvalid
	 */
	onInvalid : function(errorCode){
		Element.addClassName(this.dlElement, 'ng');
		Element.removeClassName(this.dlElement, 'ok');

		$A(this.chipError.childNodes).each(function(e){
			e.parentNode.removeChild(e);
		});

		this.chipError.appendChild(Leopalace.Validator.ErrorObjects.findError(errorCode));
	}
});

Leopalace.Validator.BasicGroupC = Class.create(Leopalace.Validator.Group, {

	/**
	 * @see Leopalace.Validator.Group#init
	 */
	init : function(params){

		var arr = [];
		$A(params.chips).each(function(chip){
			arr.push($(chip));
		});

		this.chipErrors = $A(arr).compact(); // pをつける
		this.dlElement = this.chipErrors.first().parentNode; // divに来るはず
		this.head = this.chipErrors.first().parentNode.parentNode; // tdに来るはず
	},

	/**
	 * グループの頭のY座標を取得します
	 */
	getHead : function(){
		return (!!this.head) ? Position.cumulativeOffset(this.head)[1] : 0;
	},

	/**
	 * @see Leopalace.Validator.Group#onInit
	 */
	onInit : function(){
		this.chipErrors.each(function(chip){
			Element.removeClassName(chip.parentNode, 'ok');
			Element.removeClassName(chip.parentNode, 'ng');
		});
	},

	/**
	 * @see Leopalace.Validator.Group#onValid
	 */
	onValid : function(){
		this.chipErrors.each(function(chip){
			Element.addClassName(chip.parentNode, 'ok');
			Element.removeClassName(chip.parentNode, 'ng');
		});
	},

	/**
	 * @see Leopalace.Validator.Group#onInvalid
	 */
	onInvalid : function(errorCode){

		this.chipErrors.each(function(chip){

			Element.addClassName(chip.parentNode, 'ng');
			Element.removeClassName(chip.parentNode, 'ok');

			$A(chip.childNodes).each(function(e){
				e.parentNode.removeChild(e);
			});

			chip.appendChild(Leopalace.Validator.ErrorObjects.findError(errorCode));

		});

	}
});

/**
 * どれか必須のチェッカー
 */
Leopalace.Validator.RequiredAnyChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		this.list = $A(params.list);
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		var list = this.list;
		if(!list.detect(function(element){
			return !!$F(element);
		})) return "error.required";
	}
});

/**
 * 排他チェックのチェッカー
 */
Leopalace.Validator.RequiredXorChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		this.list = $A(params.list);
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		var list = this.list;
		var result = false;
		list.each(function(element){
			result ^= !!$F(element);
		});

		if(!result) return "error.invalid";

	}
});

/**
 * XX個以内チェックのバリデータ
 */
Leopalace.Validator.RequiredLimitChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		this.list = $A(params.list).compact();
		this.max = params.max;
		this.min = params.min || 0;
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){

		var cnt = 0;
		var list = this.list;
		list.each(function(element){
			if(element.options){
				$A(element.options || []).each(function(opt){
					cnt += (opt.selected || opt.checked || false) ? 1 : 0;
				});
			}else{
				cnt += (element.selected || element.checked || false) ? 1 : 0;
			}
		});

		if(cnt < this.min) return "error.select";
		if(cnt > this.max) return "error.overselect";
		return null;
	}
});

/**
 * テキストボックスのフォーマットチェッカー
 */
Leopalace.Validator.AbstractTextFormatChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		var target = $(params.target || "");
		if(!target) return;

		this.target = target;
		this.defaultValue = params.defaultValue || "";
		this.initMore(params);
	},

	/**
	 * 更なる初期化を行います
	 * @param params パラメータ
	 */
	initMore : function(params){
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		if(!this.target) return false;

		var value = $F(this.target) || "";
		if(value == this.defaultValue){
			value = "";
		}
		return this.checkFormat(value);
	},

	/**
	 * フォーマットをチェックします
	 * これの返り値がそのままcheck()の返り値となります
	 * @see Leopalace.Validator.Checker#check
	 *
	 * @param value チェックする値
	 */
	checkFormat : function(value){
	}
});

/**
 * 日付の妥当性チェッカー
 */
Leopalace.Validator.DateFormatChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		this.year = $(params.year);
		this.month = $(params.month);
		this.day = $(params.day);
		this.birthday = params.birthday || false;
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		var year = $F(this.year);
		var month = $F(this.month);
		var day = $F(this.day);

		if(!year && !month && !day) return;
		if(!year.match(/^[1-9]{1}[0-9]{3}$/)) return "error.format";
		if(!month.match(/^(0[1-9]|1[0-2])$/)) return "error.format";
		if(!day.match(/^(0[1-9]|1[0-9]|2[0-9]|3[01])$/)) return "error.format";
		var lastDay = new Date(parseInt(year), parseInt(month), 1);
		lastDay.setTime(lastDay.getTime() - 1000);
		if(lastDay.getDate() < parseInt(day)) return "error.format";

		if(this.birthday){
			if(parseInt(year) < 1910) return "error.format";
			if(new Date(parseInt(year), parseInt(month)-1, parseInt(day)).getTime() > new Date().getTime()) return "error.format";
		}
	}
});

/**
 * 郵便番号の妥当性チェッカー
 */
Leopalace.Validator.ZipChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		this.upper = $(params.upper);
		this.lower = $(params.lower);
	},

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		var upper = $F(this.upper);
		var lower = $F(this.lower);

		if(!upper || !lower) return;

		if(!upper.match(/^[0-9]{3}$/)) return "error.format";
		if(!lower.match(/^[0-9]{4}$/)) return "error.format";
	}
});

/**
 * 必須チェッカー
 */
Leopalace.Validator.RequiredTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value) return "error.required";
	}
});

/**
 * 半角英数字のチェッカー
 */
Leopalace.Validator.HalfAplhaNumberTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value.match(/^[0-9a-z]*$/i)) return "error.halfwidth";
	}
});

/**
 * 英数字のチェッカー
 * 全角：半角は区別しません
 */
Leopalace.Validator.AplhaNumberTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value.match(/^[0-9０１２３４５６７８９a-zA-Z]*$/i)) return "error.halfwidth";
	}
});

/**
 * 半角数値のチェッカー
 */
Leopalace.Validator.HalfNumberTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value.match(/^[0123456789]*$/)) return "error.halfwidth";
	}
});

/**
 * 数値のチェッカー
 * 全角：半角は区別しません
 */
Leopalace.Validator.NumberTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value.match(/^[0123456789０１２３４５６７８９]*$/)) return "error.format";
	}
});

/**
 * 全角チェッカー
 */
Leopalace.Validator.FullWidthTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(value.match(/[\x00-\x7f]/)) return "error.fullwidth";
		if(value.match(/[ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝﾞﾟｬｭｮｯ ]/)) return "error.fullwidth";
	}
});

/**
 * 全角ひらがな漢字チェッカー
 */
Leopalace.Validator.HiraganaKanjiTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(value.match(/[\x00-\x7f]/)) return "error.hiraganakanji";
		if(value.match(/[ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝﾞﾟｬｭｮｯ ]/)) return "error.format";
	}
});

/**
 * メールアドレスチェッカー
 */
Leopalace.Validator.MailAddressTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(value.match(/[^\x00-\x7fｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝﾞﾟｬｭｮｯ ]/)) return "error.halfwidth";
		if(!!value && !value.match(/^[\x00-\x7f]+@([\w\d_-]+\.)+[\w]{2,4}$/)) return "error.format";
	}
});

/**
 * 電話番号チェッカー
 */
Leopalace.Validator.TelnumberTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!!value && !value.match(/^[0123456789０１２３４５６７８９]+-[0123456789０１２３４５６７８９]+-[0123456789０１２３４５６７８９]+$/)) return "error.format";
	}
});

/**
 * ひらがなチェッカー
 */
Leopalace.Validator.HiraganaTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(!value.match(/^[あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをんがぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽぁぃぅぇぉゃゅょっゑゐ　 ]*$/)) return "error.hiragana";
	}
});

/**
 * チェックボックス／ラジオボタンがチェックされているときに必須となるチェッカー
 */
Leopalace.Validator.RequiredIfTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#initMore
	 */
	initMore : function(params){
		this.ref = $(params.ref || "");
		this.defaultValue = params.defaultValue || "";
	},

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(this.ref.selected || this.ref.checked || !!$F(this.ref) || false){
			if(!value || value == this.defaultValue) return "error.required";
		}
	}
});

/**
 * チェックボックス／ラジオボタンがチェックされていないときに空でなくてはならないチェッカー
 */
Leopalace.Validator.EmptyNotCheckedTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#initMore
	 */
	initMore : function(params){
		this.ref = $(params.ref || "");
		this.defaultValue = params.defaultValue || "";
	},

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(this.ref.selected || this.ref.checked || false) return;
		if(!!value && value != this.defaultValue) return "error.invalid";
	}
});

/**
 * 最大桁数チェッカー
 */
Leopalace.Validator.MaxlengthTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#initMore
	 */
	initMore : function(params){
		this.length = parseInt(params.length || 0);
	},

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(value.length > this.length) return "error.maxlength";
	}
});
/**
 * 最小桁数チェッカー
 */
Leopalace.Validator.MinlengthTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#initMore
	 */
	initMore : function(params){
		this.length = parseInt(params.length || 0);
	},

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(0 < value.length && value.length < this.length) return "error.minlength";
	}
});

/**
 * 同値チェッカー
 */
Leopalace.Validator.SameValueTextChecker = Class.create(Leopalace.Validator.AbstractTextFormatChecker, {

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#initMore
	 */
	initMore : function(params){
		this.ref = $(params.ref);
	},

	/**
	 * @see Leopalace.Validator.AbstractTextFormatChecker#checkFormat
	 */
	checkFormat : function(value){
		if(value != $F(this.ref)) return "error.format";
	}
});

/**
 * チェックボックスのチェッカー
 */
Leopalace.Validator.AbstractCheckboxChecker = Class.create(Leopalace.Validator.Checker, {

	/**
	 * @see Leopalace.Validator.Checker#init
	 */
	init : function(params){
		var name = params.name || "";
		if(!name) return;
		var elements = this.group.manager.formElement[name];
		if(elements.length){
			this.checkboxes = $A(elements);
		}else{
			this.checkboxes = $A([elements]);
		}
	}
});

/**
 * チェックボックスの必須チェッカー
 */
Leopalace.Validator.CheckboxRequiredChecker = Class.create(Leopalace.Validator.AbstractCheckboxChecker, {

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		if(!this.checkboxes.detect(function(checkbox){
			return checkbox.selected || checkbox.checked || false;
		})) return "error.select";
	}
});

/**
 * 全員選択チェックボックスの必須チェッカー
 */
Leopalace.Validator.ZenninnCheckboxRequiredChecker = Class.create(Leopalace.Validator.AbstractCheckboxChecker, {

	/**
	 * @see Leopalace.Validator.Checker#check
	 */
	check : function(){
		if(!this.checkboxes.detect(function(checkbox){
			return checkbox.selected || checkbox.checked || false;
		})) return "error.zenninn";
	}
});

