/**
 * CONTACT コンタクト ページスクリプト
 **/

function contactLoad(){
	var param = {
		type : "get",
		dataType : "html",
		contentType : "text/html;charset=utf-8"
	};
	$("#maintext").ajaxStart(function(){
		//読み込み前準備処理
		$("#maintitle").html("CONTACT");
	}).load("./contact/text.html",null,function(data,stat){
		//読み込み処理
		this.innerHTML = data;
		textBox("contact-list");
		mainShow();
	});
};


function createXMLHttpRequest(funcP){
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if(funcP && XMLhttpObject)XMLhttpObject.onreadystatechange = funcP;
	return XMLhttpObject;
}
var messageObj = null;
var Message = function(){
	this.init();
};
var messageppp=null;
Message.prototype = {
	id  : null,
	chk : null,
	init: function(){
		if(messageObj){
			if(messageObj != this){
				messageObj.init();
				return null;
			}
			if(this.chkParam()){
				$("#massage_res").html("同一内容でのメッセージ送信はご遠慮ください。");
				return null;
			}
		}else{
			messageObj = this;
		}
		//返信flagセット
		this.setFlg();
		//設定
		this.setup();
		this.accessOpen();
	},
	chkParam : function(){
		if( this.chk == this.getFlg() && this.textAddr == this.getMailAddr() && this.textMsg == this.getText()){
			return true;
		}
		return false;
	},
	//返信チェックの格納
	chkid : 'res_chk',
	maru : 'o',
	batu : 'x',
	getFlg : function(){
		var flg = document.getElementById(this.chkid);
		if(flg)
			flg = flg.checked?this.maru:this.batu;
		else
			flg = "x";
		return flg;
	},
	setFlg : function(){
		this.chk = this.getFlg();
	},
	//メールアドレスの格納
	mailid : 'mailaddr',
	textAddr : null,
	getMailAddr:function(){
		var addr = document.getElementById(this.mailid);
		if(addr)
			addr = addr.value;
		else
			addr = "???";
		return addr;
	},
	setMailAddr:function(){
		this.textAddr = this.getMailAddr();
	},
	//URLの格納
	urlid : 'url',
	textUrl : null,
	getUrl:function(){
		var url = document.getElementById(this.urlid);
		if(url)
			url = url.value;
		else
			url = "???";
		return url;
	},
	setUrl:function(){
		this.textUrl = this.getUrl();
	},
	//メッセージの格納
	textid : 'massagetext',
	textMsg: null,
	getText : function(){
		var text = document.getElementById(this.textid);
		if(text)
			text = text.value;
		else
			text = "???";
		return text;
	},
	setText : function(){
		this.textMsg = this.getText();
	},
	access : null,
	method : 'POST',
	accessPath : './Massage.php',
	accessContentType : 'Content-Type',
	accessOption : 'application/x-www-form-urlencoded; charset=UTF-8',
	accessOpen : function(){
		if(!messageppp)return null;
		if(this.chkText(this.textMsg)){
			$("#massage_res").html("メッセージを入力してください。");
			return null;
		}
		messageppp.open(this.method, this.accessPath, true);
		messageppp.setRequestHeader(this.accessContentType, this.accessOption);
		messageppp.send(
			"chk="+ encodeURI(this.chk) +
			"&textAddr="+ encodeURI(this.textAddr) +
			"&textMsg="+ encodeURI(this.textMsg) +
			"&textUrl="+ encodeURI(this.textUrl)
		);
	},
	//テキストチェック
	chkText:function(str){
		if(str=="" || str.match(/^\s*$/i))return true;;
	},
	//非同期通信設定処理
	setup : function(func){
		messageppp = createXMLHttpRequest((func?func:this.dispFunc));
		this.setText();
		this.setMailAddr();
		this.setUrl();
	},
	dispFunc : function(){
		if(messageppp.readyState == 1){
			$("#massage_res").html("Sending...");
		}else if((messageppp.readyState == 4) && (messageppp.status == 200)){
			//成功時
			var t = "";
			switch(messageppp.responseText){
				case "o":
					t = "Success";
					break;
				case "x":
					t = "ServerError:時間を置いてもう一度メッセージ送信してください。";
					break;
				case "x1":
					t = "ClientError:送信情報に不正を発見しました。CLAPからご報告お願いいたします。";
					break;
				default:
			}
			$("#massage_res").html(t);
		}else if ((messageppp.readyState == 4) && (messageppp.status == 404)){
			//失敗時
			$("#massage_res").html("Error!!");
		}
	}
};

