var xForm = Class.create();

xForm.prototype = {

/****************************************


need is
	・prototype.js(by v1.4)
	・all element id (form,input,textarea...)
	・no naming(form,input,textarea...)
	・form send button's  id name is "form_send"
	・form id name is class argument's page + "_js"
		(this situation is class argument's page = html filename) 
		
		
****************************************
Constructor
****************************************/
initialize: function(page) {
	Form_root = this;
	//正常入力状態の要素数を管理
	this.value_obj={};
	this.target = page+"_js";
	//値初期化
	this.xReset();
	this.array = this.xMakeArray();
	this.check_array = this.array[0];
	this.clear_array = this.array[1];
	this.null_array = this.array[2];
},
/****************************************
Value Reset
****************************************/
xReset : function(){
	//id名をname名に割り当て
	var form_array = Form.getElements(Form_root.target);
	for(var j=0;j<form_array.length;j++) form_array[j].name = form_array[j].id;
	//値リセット
	$(Form_root.target).reset();
	textarea_array = document.getElementsByTagName("textarea");
	for(var i=0;i<textarea_array.length;i++){
		textarea_array[i].value="";
	}
},
/****************************************
Element Action
****************************************/
xMakeArray : function(){
//id設定
	var mail = "mail";
	var null_array = [];
	var clear_array = [];
	var check_array = [];
	switch(page){
		case "form_contact" :
			 null_array = ["name","kana","tel","comment"];
			break;
		case "form_feed" :
			null_array = ["name","kana","subject","comment"];
			break;
		case "form_invitation" : 
			null_array = ["date1","people","name","kana","tel"];
			clear_array = ["date1"];
			break;
		case "form_special" :
			null_array = ["name","kana","tel"];
		default :
			break;
	}
//入力値をチェックすべきid名(送信時に使用)
	if(null_array.length) check_array = null_array.slice(0);
	check_array.push("mail");
//挙動設定
	$(mail).onblur = Form_root.xMailCheck;
	if(clear_array.length) for(var i=0;i<clear_array.length;i++) $(clear_array[i]).onfocus = Form_root.xValueClear;
	if(null_array.length) for(var i=0;i<null_array.length;i++) $(null_array[i]).onblur = Form_root.xNullCheck;
	$("form_send").onclick = Form_root.xSendCheck;
	$("form_send").onmouseover = function(){this.style.backgroundPosition="0 -23px";};
	$("form_send").onmouseout = function(){this.style.backgroundPosition="0 0";};	
//返値設定
	var array = [check_array,clear_array,null_array];
	return array;
},
/****************************************
Error
****************************************/
xError : function(obj,b){
//入力エラー時の挙動
	var errorObj = obj.parentNode.getElementsByTagName("p").item(0);
	if(b){
		errorObj.style.display = "none";
		Form_root.value_obj[obj.id]=true;
	}else{
		errorObj.style.display = "block";
		delete Form_root.value_obj[obj.id];
	}

},
/****************************************
Value Clear
****************************************/
xValueClear : function(obj){
	obj = (typeof obj == "string") ? $(obj) : this;
	if(Form_root.value_obj[obj.id] == undefined) obj.value = "";
},
/****************************************
Mail check
****************************************/
xMailCheck : function(obj){
	obj = (typeof obj == "string") ? $(obj) : this;
	var b =(obj.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) == null) ? false : true;
	Form_root.xError(obj,b);
},
/****************************************
Null check
****************************************/
xNullCheck : function(obj){
	obj = (typeof obj == "string") ? $(obj) : this;
	var b =(obj.value.match(/\S/) == null) ? false : true;
	Form_root.xError(obj,b);
},
/****************************************
All check
****************************************/
xAllCheck : function(obj){
	if(Form_root.clear_array.length) for(i=0;i<Form_root.clear_array.length;i++) Form_root.xValueClear(Form_root.clear_array[i]);
	if(Form_root.null_array.length) for(i=0;i<Form_root.null_array.length;i++) Form_root.xNullCheck(Form_root.null_array[i]);
	Form_root.xMailCheck("mail");
},
/****************************************
SendCheck
****************************************/
xSendCheck : function(){
	//入力された値の数と入力必要数を照合
	var i=0;
	for(key in Form_root.value_obj) i++;
	if(Form_root.check_array.length == i){
		//送信
		Form_root.xSend();
	}else{
		//値チェック
		Form_root.xAllCheck();
	}
},
/****************************************
Send
****************************************/
xSendAction : function(){
	//in share.js
	xSetResult(Form_root.target);
},
/****************************************
Send
****************************************/
xSend : function(){
	if(confirm("入力内容を確定して送信します。")){
		//$(Form_root.target).submit() : "";
		new Ajax.Request("./contents/form.html",{
			method : "post",
			//パラメーターにページ要素を追加
			parameters : "page="+page+"&"+Form.serialize(Form_root.target),
			onComplete : function(httpobj){
				Form_root.xSendAction();
				//$("result").innerHTML = httpobj.responseText;
				if(httpobj.responseText !=11) $("result").innerHTML = "サーバーエラーにより送信が出来ませんでした。<br />恐れ入りますが、<a href='mailto:163@ichirosan-printkobo.com' title='お問い合わせ'>163@ichirosan-printkobo.com</a>へ直接お問い合わせ頂くか、しばらく経ってから再度送信下さいますようお願い致します。";
			}
		});
	}
}



};

