//
//	$Id: cookie functions for inquiry form ,v 1.0 2003/03/08 17:54:00 ymch Exp $
//



/*--------------------------------------------------------------------------------------------------*/
//	物件一覧画面で呼び出すファンクション
/*--------------------------------------------------------------------------------------------------*/

function PutNumber(number,url,winName,W,H) {
	//請求済みかどうかのチェック
	number_ref = getCookie("number");
	send_number = number;
	sn = new RegExp(send_number);
	if ( number_ref.match(sn) ) { alert("この物件は既に 問い合わせカゴ に入っています。"); }
	else {
		if ( number_ref == '' ){
			setCookie("number", send_number);
		} else {
			cook = number_ref + "\n" + send_number;
			setCookie("number", cook);
		}
	}
	//物件問い合わせカゴのウインドウをオープン
	subWin=window.open(url,winName,'scrollbars=1,width='+W+',height='+H+',resizable=1,status=1');
	//ウインドウが裏に回ると気付かない方もいらっしゃる様で…
	subWin.focus();
}



/*--------------------------------------------------------------------------------------------------*/
//	物件問い合わせカゴの画面で呼び出すファンクション
/*--------------------------------------------------------------------------------------------------*/

//問い合わせ物件リストの内容を取得
function putList() {
	number = getCookie("number");
	window.document.inquiry.Nos.value = number;
}

//フォームウインドウをオープン
function formWindow() {
	subWin=window.open('./form.html','','scrollbars=1,width=800,height=600,resizable=1,status=1');
	//ウインドウが裏に回ると気付かない方もいらっしゃる様で…
	subWin.focus();
}



/*--------------------------------------------------------------------------------------------------*/
//	あくまでもデバッグ用ファンクションです…(^_^ゞ
/*--------------------------------------------------------------------------------------------------*/

function loadCookies() {
	id2 = setTimeout("cookieGet()", 1500);
}



/*--------------------------------------------------------------------------------------------------*/
//	問い合わせフォーム画面で呼び出すファンクション
/*--------------------------------------------------------------------------------------------------*/

function win_open(url,winName,W,H) {
	subWin=window.open(url,winName,'scrollbars=1,width='+W+',height='+H+',resizable=1,status=0');
	subWin.focus();
}

//現在のセッションのcookieを得て、フォームに代入
function cookieGet() {
	Name         = getCookie("Name");
	Mail_Address = getCookie("Mail_Address");
	Tel_No       = getCookie("Tel_No");
	FAX_No       = getCookie("FAX_No");
	Pref         = getCookie("Pref");
	City         = getCookie("City");
	When         = getCookie("When");
	Why          = getCookie("Why");
	What1        = getCookie("What1");
	What2        = getCookie("What2");
	number_ref   = getCookie("number");
	Count        = getCookie("Count");
	window.document.inquiry.Name.value          = Name;
	window.document.inquiry.Mail_Address.value  = Mail_Address;
	window.document.inquiry.Tel_No.value        = Tel_No;
	window.document.inquiry.FAX_No.value        = FAX_No;
	window.document.inquiry.Pref.selectedIndex  = Pref;
	window.document.inquiry.City.value          = City;
	window.document.inquiry.When.selectedIndex  = When;
	window.document.inquiry.Why.selectedIndex   = Why;
	window.document.inquiry.What1.selectedIndex = What1;
	window.document.inquiry.What2.value         = What2;
	window.document.inquiry.Nos.value           = number_ref;
	window.document.inquiry.Count.value         = Count;
}

//フォーム未入力確認（中途送信防止）
function certify() {

	// 個人情報保護法施行に伴い…
	if(!(window.document.inquiry.pp_agree.checked)) {
		alert('当社のプライバシーポリシー（個人情報のお取り扱いについて）に同意いただく必要があります。');
		win_open('/PrivacyPolicy/index.html','PrivacyPolicy',600,400);
		return(false);
	}

	Name         = window.document.inquiry.Name.value;
	Mail_Address = window.document.inquiry.Mail_Address.value;
	Tel_No       = window.document.inquiry.Tel_No.value;
	FAX_No       = window.document.inquiry.FAX_No.value;
	Pref         = window.document.inquiry.Pref.value;
	City         = window.document.inquiry.City.value;
	When         = window.document.inquiry.When.selectedIndex;
	Why          = window.document.inquiry.Why.selectedIndex;
	What1        = window.document.inquiry.What1.selectedIndex;
	What2        = window.document.inquiry.What2.value;
	List         = window.document.inquiry.Nos.value;
	if (Name == "") {
		alert("お名前が未入力です。");
		window.document.inquiry.Name.focus();
		return(false);
	}
	if (Mail_Address == "" && Tel_No == "" && FAX_No == "") {
		alert("連絡先を入力して下さい。");
		window.document.inquiry.Mail_Address.focus();
		return(false);
	}
	if (Mail_Address != "" && window.document.inquiry.Mail_Address.value.lastIndexOf("@") == -1 ) {
		alert("メールアドレスが間違っています。");
		window.document.inquiry.Mail_Address.focus();
		return(false);
	}
	if ( ! hankaku_chk ( window.document.inquiry.Mail_Address.value )) {
		alert("メールアドレスは日本語入力をオフにしてご入力下さい。");
		window.document.inquiry.Mail_Address.focus();
		return(false);
	}
	if ( window.document.inquiry.Mail_Address.value.lastIndexOf(",") != -1 ) {
		alert("メールアドレスにカンマが混ざっています。");
		window.document.inquiry.Mail_Address.focus();
		return(false);
	}
	if (Pref == "") {
		alert("現住所（都道府県）を選択してください。");
		window.document.inquiry.Pref.focus();
		return(false);
	}
	if (City == "") {
		alert("現住所（市区町村）をご記入下さい。");
		window.document.inquiry.City.focus();
		return(false);
	}
	if (When == "") {
		alert("引越の時期を指定して下さい。");
		window.document.inquiry.When.focus();
		return(false);
	}
	if (Why == "") {
		alert("引越の動機を指定して下さい。");
		window.document.inquiry.Why.focus();
		return(false);
	}
	if (List == "") {
		alert("「問い合わせカゴ」が空っぽです。\n問い合わせカゴに物件を入れて下さい。");
		self.close();
		return(false);
	}
	if (What1 == "") {
		alert("当サイトをお知りになったきっかけを選んで下さい。");
		window.document.inquiry.What1.focus();
		return(false);
	}
	if (What1 == 8 && What2 == "") {
		alert("当サイトをお知りになったきっかけ（その他）をご記入下さい。");
		window.document.inquiry.What2.focus();
		return(false);
	}

	cookieSet();
	return(true);
}

//２バイト文字が入っているかどうか（メールアドレス）
function hankaku_chk ( values ) {
	rc = 1;
	for ( var i=0; i<values.length; i++ ) {
		if ( escape(values.substring(i,i+1)).lastIndexOf("%") != -1 ) {
			if ( escape(values.substring(i,i+1)) >= '%80' ) {
				rc = 0;
			}
		}
	}
	return ( rc );
}


//最新のcookieをセット
function cookieSet() {
	Name         = window.document.inquiry.Name.value;
	Mail_Address = window.document.inquiry.Mail_Address.value;
	Tel_No       = window.document.inquiry.Tel_No.value;
	FAX_No       = window.document.inquiry.FAX_No.value;
	Pref         = window.document.inquiry.Pref.selectedIndex;
	City         = window.document.inquiry.City.value;
	When         = window.document.inquiry.When.selectedIndex;
	Why          = window.document.inquiry.Why.selectedIndex;
	What1        = window.document.inquiry.What1.selectedIndex;
	What2        = window.document.inquiry.What2.value;
	Count        = window.document.inquiry.Count.value;

	Count = Count-0+1;

	// 「その他」非選択の場合は「その他の理由」を消去
	if (What1 != 8) {
		What2 = "";
	}

	setCookie("Name", Name);
	setCookie("Mail_Address", Mail_Address);
	setCookie("Tel_No", Tel_No);
	setCookie("FAX_No", FAX_No);
	setCookie("Pref", Pref);
	setCookie("City", City);
	setCookie("When", When);
	setCookie("Why", Why);
	setCookie("What1", What1);
	setCookie("What2", What2);
	setCookie("Count", Count);

	//問い合わせ済みの物件ナンバーは不要なので…
	clearNos();
}


	// オブジェクトの表示非表示切り替え
	function conv(objectId) {
		child = document.getElementById(objectId);
		if (child.className == "show")
			child.className = "hide";
		else
			child.className = "show";
	}

	// ページのロード時に非表示部分を隠す
	function collapse() {
		What1 = getCookie("What1");
		if(What1 != 8){
			conv('factor0');
		}
	}

	// 「その他」選択時の挙動
	function etc(objectId) {
		child = document.getElementById(objectId);
		if(window.document.inquiry.What1.selectedIndex==8){
			child.className = "show";
		} else {
			child.className = "hide";
		}
	}



/*--------------------------------------------------------------------------------------------------*/
//	共通処理
/*--------------------------------------------------------------------------------------------------*/

//cookieをゲットする
function getCookie(key,  tmp1, tmp2, xx1, xx2, xx3) {
	tmp1 = " " + window.document.cookie + ";";
	xx1 = xx2 = 0;
	len = tmp1.length;
	while (xx1 < len) {
		xx2 = tmp1.indexOf(";", xx1);
		tmp2 = tmp1.substring(xx1 + 1, xx2);
		xx3 = tmp2.indexOf("=");
		if (tmp2.substring(0, xx3) == key) {
			return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
		}
		xx1 = xx2 + 1;
	}
	return("");
}

function setCookie(key, val, tmp) {

	tmp = key + "=" + escape(val) + "; ";
	// tmp += "path=" + location.pathName + "; ";

	// cookieの賞味期限は1年とする
	date_cookie = new Date();
	date_cookie.setTime(date_cookie.getTime() + (365*24*3600*1000));

	tmp += "expires=" + date_cookie.toGMTString() + "; ";
	window.document.cookie = tmp;

}

//物件ナンバーのクリア
function clearNos() {
	setCookie("number", "");
}
