var event_src;
var moveFlg ={};
var evElement;
var targetList = new Array('mail','subject','phone','name','kana','body','corp','mailconf');
var obj = new Array();
var colorFlg = 0;

function execValidateAllElement(event)
{
	for (var elm in obj) {
		if (obj[elm] instanceof Class_FormCheck) {
			if (obj[elm].executing == false) {
				obj[elm].ngColor = (colorFlg == 1) ? "#CC3300" : "gray";
				obj[elm].execCheck();
			}
		}
	}
	for (var elm in obj) {
	//alert(obj[elm].checkTarget+obj[elm].checkResult);
		if (obj[elm].executing == false) {
			if (obj[elm] instanceof Class_FormCheck) {
				if(obj[elm].checkResult == false) {
					window.status = obj[elm].checkTarget;
					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);

				}
				if (this.checkTarget == 'mail') {
					this.checkResult =true
				}
				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).value == this.beforeValue) {
			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;
//--エラーの場合--------------------------------------------------
	//----2つ以上あるもの-----------------------------
	} 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{
		this.checkResult = false;
		$(this.getDispAreaName()).style.color = this.ngColor;
		resdata = this.getName(this.checkTarget)+this.getErrorMessage(resdata);
	}
	$(this.getDispAreaName()).innerHTML = resdata;
	this.executing = false;
}
Class_FormCheck.prototype.getName = function(mode) {
	var ret = null;
	switch(mode){
		case "subject":
			ret = "件名";
			break;
		case "mail":
			ret = "メールアドレス";
			break;
		case "mailconf":
			ret = "確認用メールアドレス";
			break;
		case "body":
			ret = "内容";
			break;
		case "corp":
			ret = "会社名";
			break;
		case "phone":
			ret = "電話番号";
			break;
		case "name1":
			ret = "名字";
			break;
		case "name2":
			ret = "名前";
			break;
		case "kana1":
			ret = "名字のフリガナ";
			break;
		case "kana2":
			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 "name2":
			ret = "名前";
			break;
		case "kana1":
			ret = "名字のフリガナ";
			break;
		case "kana2":
			ret = "名前のフリガナ";
			break;
		default:
			var a = res.replace("noMoreThan","");
			ret = "は"+a+"字以内でお願いします";
			break;
	}
	return ret;
}