var event_src;
var evElement;
var targetList = new Array('mail','entryJob','name','kana','phone','cellphone','postcode1','postcode2','prefecture','address','bill','year','month','day','old','sei','season','wish','PR','results','others','mailconf');
var obj = new Array();
var colorFlg = 0;
var count = 0;
function execValidateAllElement(event)
{
	for (var elm in obj) {
		if (obj[elm] instanceof Class_FormCheck) {
			if (obj[elm].executing == false) {
				if (obj[elm].checkTarget == "year" || obj[elm].checkTarget == "month" || obj[elm].checkTarget == "day") {
					count+=1;
					if (count < 40){
						colorFlg = 0;
					}
				}
				obj[elm].ngColor = (colorFlg == 1) ? "#CC3300" : "gray";
				obj[elm].execCheck();
			}
		}
	}
	for (var elm in obj) {
		if (obj[elm] instanceof Class_FormCheck) {
			if(obj[elm].checkResult == false) {
					window.status = targetList[i];
					document.input_form.sub.disabled = true;
					return;
			}
		}
	}
	document.input_form.sub.disabled = false;
}

function onLoadFunc(event)
{
	document.input_form.sub.disabled = true;

	for(i=0; i < targetList.length ; i++){
		if ($(targetList[i])) {

			obj[targetList[i]] = new Class_FormCheck();
			obj[targetList[i]].checkTarget = targetList[i];
			obj[targetList[i]].targetUrl   = 'check.php';
			obj[targetList[i]].formName    = 'input_form';
		} else {
			var j=1;
			while (true) {
				var targetName = targetList[i]+j;
				if ($(targetName)){
					obj[targetName] 	= new Class_FormCheck();
					obj[targetName].checkTarget	= targetName;
					obj[targetName].targetUrl		= 'check.php';
					obj[targetName].dispArea		= targetList[i]
					obj[targetName].formName		= 'input_form';

				} else {
					break;
				}
				j++;
			}
		}
		if(targetList[i] == "mail"){
			obj[targetList[i]] .execCheck = function() {
				if($(this.checkTarget).value != this.beforeValue || $('mailconf').value != obj['mailconf'].beforeValue) {
					this.beforeValue = $(this.checkTarget).value;
					this.comm.setTargetUrl(this.targetUrl + '?type=' + this.checkTarget);
					this.comm.setFormName(this.formName);
					this.comm.setCallback(this.onComplete_execCheck.bindAsEventListener(this));
					this.comm.exec(null);

					obj['mailconf'].comm.exec(null);

				}
				return;

			}
		}
		if(targetList[i] == "year" || targetList[i] == "month" || targetList[i] == "day") {
			obj[targetList[i]] .execCheck = function() {
				if($(this.checkTarget).value != this.beforeValue || $('month').value != obj['month'].beforeValue || $('day').value != obj['day'].beforeValue) {
					this.beforeValue = $(this.checkTarget).value;
					this.comm.setTargetUrl(this.targetUrl + '?type=' + this.checkTarget);
					this.comm.setFormName(this.formName);
					this.comm.setCallback(this.onComplete_execCheck.bindAsEventListener(this));
					this.comm.exec(null);

					obj['year'].comm.exec(null);
					obj['month'].comm.exec(null);
					obj['day'].comm.exec(null);

				}
				return;

			}
		}
	}
	setInterval(execValidateAllElement,200);
}

function Class_FormCheck()
{
	this.executing = false;
	this.ngColor = "#CC3300";
	this.okColor = "#009966";
	this.dispArea = null
	this.checkTarget = null;
	this.beforeValue = null;
	this.comm        = new Class_Communicator;
	this.targetUrl   = null;
	this.formName    = null;
	this.checkResult = false;
}

Class_FormCheck.prototype.getDispAreaName = function() {
	if (this.dispArea == null) {
		return 'msg_' + this.checkTarget;
	} else {
		return 'msg_' + this.dispArea;
	}
}

Class_FormCheck.prototype.execCheck = function() {

	if (this.executing == true) {
		return;
	}
	if ((this.checkTarget != 'sei1') && (this.checkTarget != 'sei2')) {
		if($(this.checkTarget).value == this.beforeValue) {
			colorFlg = 1;
			return;
		}
	} else {
		if(colorFlg != 1) {
			return;
		}
	}
	this.executing = true;
	this.beforeValue = $(this.checkTarget).value;

	this.comm.setTargetUrl(this.targetUrl + '?type=' + this.checkTarget);
	this.comm.setFormName(this.formName);
	this.comm.setCallback(this.onComplete_execCheck.bindAsEventListener(this));
	this.comm.exec(null);
}

Class_FormCheck.prototype.onComplete_execCheck = function(reqObj) {
	resdata = reqObj.responseText;
	resdata = resdata.replace(/^\s+|\s+$/g, "");

	if (resdata == 'OK') {
		this.checkResult = true;
		$(this.getDispAreaName()).style.color = this.okColor;

	} else if (resdata == "n1" || resdata == "n2" || resdata == "k1" || resdata == "k2") {
		if (resdata == "n1") {
			resdata = '名前も入力してください';
		} else if (resdata == "n2") {
			resdata = "名字も入力してください";
		} else if (resdata == "k1") {
			resdata = "名字のフリガナも入力してください";
		} else {
			resdata = "名前のフリガナも入力してください";
		}
		this.checkResult = true;
		$(this.getDispAreaName()).style.color = this.ngColor;
	//エラーの場合
	} else {
		resdata = this.getName(this.checkTarget)+this.getErrorMessage(resdata);
		this.checkResult = false;
		$(this.getDispAreaName()).style.color = this.ngColor;
	}
		$(this.getDispAreaName()).innerHTML = resdata;
		this.executing = false;
}

Class_FormCheck.prototype.getName = function(mode) {

	$ret = null;
	switch(mode){
		case "entryJob":
			ret = "応募職種";
			break;
		case "name1":
			ret = "名字";
			break;
		case "name2":
			ret = "名前";
			break;
		case "kana1":
			ret = "名字のフリガナ";
			break;
		case "kana2":
			ret = "名前のフリガナ";
			break;
		case "mail":
			ret = "メールアドレス";
			break;
		case "mailconf":
			ret = "確認用メールアドレス";
			break;
		case "phone":
			ret = "電話番号";
			break;
		case "cellphone":
			ret = "携帯電話";
			break;
		case "postcode1":
			ret = "郵便番号１";
			break;
		case "postcode2":
			ret = "郵便番号２";
			break;
		case "prefecture":
			ret = "都道府県";
			break;
		case "address":
			ret = "住所";
			break;
		case "bill":
			ret = "マンション・ビル名";
			break;
		case "year":
			ret = "年";
			break;
		case "month":
			ret = "月";
			break;
		case "day":
			ret = "日";
			break;
		case "old":
			ret = "年齢";
			break;
		case "sei1":
			ret = "<font color=gray>性別";
			break;
		case "sei2":
			ret = "<font color=gray>性別";
			break;
		case "season":
			ret = "転職希望時期";
			break;
		case "wish":
			ret = "志望動機";
			break;
		case "PR":
			ret = "自己ＰＲ";
			break;
		case "results":
			ret = "実績・作品";
			break;
		case "others":
			ret = "その他 お問い合わせ";
			break;
		default:
			ret = "error";
			break;
	}
	return ret;
}
Class_FormCheck.prototype.getErrorMessage = function(res) {

	switch(res){
		case "notInput":
			ret = "を入力してください";
			break;
		case "noMoreThan":
			ret = "字以内でお願いします";
			break;
		case "correctForm":
			ret = "を正しく入力してください";
			break;

		case "notAgree":
			ret = "とメールアドレスが一致しません";
			break;

		case "inputKana":
			ret = "は全角カタカナで入力してください";
			break;
		case "inputZen":
			ret = "は全角で入力してください";
			break;
		case "selectBox":
			ret = "を選んで下さい";
			break;
		case "notThisDate":
			ret = "その日付はありません";
			break;
		case "sei":
			ret = "を選んでください</font>";
			break;
		default:
			var a = res.replace("noMoreThan","");
			ret = "は"+a+"字以内でお願いします";
			break;
	}
	return ret;
}