
// Require prototype.js
// Require function.js

var User = Class.create();

User.prototype = {

	initialize  : function() {
		this.dict = {};
    },

	setDict : function(value) {
		this.dict = value;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//登録日時

	setCreate_tstmp : function(value) {
		this.dict['create_tstmp'] = trim(value);
	},
	getCreate_tstmp : function() {
		return this.dict['create_tstmp'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//更新日時

	setUpdate_tstmp : function(value) {
		this.dict['update_tstmp'] = trim(value);
	},
	getUpdate_tstmp : function() {
		return this.dict['update_tstmp'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ユーザＩＤ

	setId : function(value) {
		this.dict['id'] = trim(value);
	},
	getId : function() {
		return this.dict['id'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//有効区分

	setEnable : function(value) {
		this.dict['enable'] = trim(value);
	},
	getEnable : function() {
		return this.dict['enable'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//区分

	setUser_class : function(value) {
		this.dict['user_class'] = trim(value);
	},
	getUser_class : function() {
		return this.dict['user_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//メールアドレス(半角英数字)

	setMail : function(value) {
		this.dict['mail'] = trim(value);
	},
	getMail : function() {
		return this.dict['mail'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//メールアドレスの確認(半角英数字)

	setMail2 : function(value) {
		this.dict['mail2'] = trim(value);
	},
	getMail2 : function() {
		return this.dict['mail2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//携帯メールアドレス(半角英数字)

	setMobilemail : function(value) {
		this.dict['mobilemail'] = trim(value);
	},
	getMobilemail : function() {
		return this.dict['mobilemail'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//携帯メールアドレスの確認(半角英数字)

	setMobilemail2 : function(value) {
		this.dict['mobilemail2'] = trim(value);
	},
	getMobilemail2 : function() {
		return this.dict['mobilemail2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//パスワード(半角英数字)

	setPassword : function(value) {
		this.dict['password'] = trim(value);
	},
	getPassword : function() {
		return this.dict['password'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//パスワードの確認(半角英数字)

	setPassword2 : function(value) {
		this.dict['password2'] = trim(value);
	},
	getPassword2 : function() {
		return this.dict['password2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//お名前(全角)

	setName : function(value) {
		this.dict['name'] = trim(value);
	},
	getName : function() {
		return this.dict['name'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//お名前(全角かな)

	setKana : function(value) {
		this.dict['kana'] = trim(value);
	},
	getKana : function() {
		return this.dict['kana'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ニックネーム

	setNickname : function(value) {
		this.dict['nickname'] = trim(value);
	},
	getNickname : function() {
		return this.dict['nickname'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//英字名(半角英数字)

	setEname : function(value) {
		this.dict['ename'] = trim(value);
	},
	getEname : function() {
		return this.dict['ename'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//郵便番号(半角数字)

	setZip : function(value) {
		this.dict['zip'] = replaceAll(trim(value), "-", "");
	},
	getZip : function() {
		return this.dict['zip'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ご住所　都道府県市区(全角)

	setAdd1 : function(value) {
		this.dict['add1'] = trim(value);
	},
	getAdd1 : function() {
		return this.dict['add1'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//町村番地(全角)

	setAdd2 : function(value) {
		this.dict['add2'] = trim(value);
	},
	getAdd2 : function() {
		return this.dict['add2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//建物名

	setAdd3 : function(value) {
		this.dict['add3'] = trim(value);
	},
	getAdd3 : function() {
		return this.dict['add3'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//電話番号(半角数字)

	setTel : function(value) {
		if ( value ) {
			value = replaceAll( replaceAll( replaceAll(value, "-", ""), "(", ""), ")", "");
		}
		this.dict['tel'] = trim(value);
	},
	getTel : function() {
		return this.dict['tel'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//FAX番号(半角数字)

	setFax : function(value) {
		if ( value ) {
			value = replaceAll( replaceAll( replaceAll(value, "-", ""), "(", ""), ")", "");
		}
		this.dict['fax'] = trim(value);
	},
	getFax : function() {
		return this.dict['fax'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//性別

	setGender : function(value) {
		this.dict['gender'] = value;
	},
	getGender : function() {
		return this.dict['gender'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//緯度

	setLatitude : function(value) {
		this.dict['latitude'] = trim(value);
	},
	getLatitude : function() {
		return this.dict['latitude'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//経度

	setLongitude : function(value) {
		this.dict['longitude'] = trim(value);
	},
	getLongitude : function() {
		return this.dict['longitude'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//携帯ID

	setMobileid : function(value) {
		this.dict['mobileid'] = trim(value);
	},
	getMobileid : function() {
		return this.dict['mobileid'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//全国地方公共団体コード

	setArea_code : function(value) {
		value = trim(value);
		if (value) {
			this.dict['area_code1'] = value;
			if (value.length > 1) {
				this.dict['area_code2'] = value.substring(0,2);
			}
		}
	},
	getArea_code : function() {
		return this.dict['area_code1'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//全国地方公共団体コード１

	setArea_code1 : function(value) {
		this.dict['area_code1'] = trim(value);
	},
	getArea_code1 : function() {
		return this.dict['area_code1'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//全国地方公共団体コード２

	setArea_code2 : function(value) {
		this.dict['area_code2'] = trim(value);
	},
	getArea_code2 : function() {
		return this.dict['area_code2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//誕生日

	setBirthday : function(y, m, s) {
		if ((y) && (m) && (s)) {
			this.dict['birthday'] = y + "-" + m + "-" + s;
		} else {
			this.dict['birthday'] = "";
		}
	},
	getBirthday : function() {
		return this.dict['birthday'];
	},
	getBirthday_y : function() {
		if ( this.dict['birthday'] ) {
			return this.dict['birthday'].split("-")[0];
		}
		return '';
	},
	getBirthday_m : function() {
		if ( this.dict['birthday'] ) {
			return this.dict['birthday'].split("-")[1];
		}
		return '';
	},
	getBirthday_d : function() {
		if ( this.dict['birthday'] ) {
			return this.dict['birthday'].split("-")[2];
		}
		return '';
	},

	getBirthdayThisYear2Date : function() {
		if (! this.dict['birthday'])	return null;

		var today = new Date();
		var year = today.getYear();
		var checkdate = new Date(year, 1, 28);
		checkdate.setTime(checkdate.getTime() + 86400000);	//１日加算
		var uruu = (checkdate.getMonth() == 2);	//うるう年

		var word = this.dict['birthday'].split("-")
		var month = toInt( word[1] );
		var day = toInt( word[2] );

		if ((!uruu) && (month == 2) && (day == 29)) {
			month = 3;
			day = 1;
		}

		return new Date(year, month - 1, day);
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//お子様の有無

	setChild : function(value) {
		this.dict['child'] = value;
	},
	getChild : function() {
		return this.dict['child'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//お車の有無

	setCars : function(value) {
		this.dict['cars'] = value;
	},
	getCars : function() {
		return this.dict['cars'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ペットの有無

	setPet : function(value) {
		this.dict['pet'] = value;
	},
	getPet : function() {
		return this.dict['pet'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ご登録場所

	setPos_type : function(value) {
		this.dict['pos_type'] = value;
	},
	getPos_type : function() {
		return this.dict['pos_type'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//取得半径

	setScope : function(value) {
		this.dict['scope'] = value;
	},
	getScope : function() {
		return this.dict['scope'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//最終ログイン日時

	setLogin_tstmp : function(value) {
		this.dict['login_tstmp'] = value;
	},
	getLogin_tstmp : function() {
		return this.dict['login_tstmp'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ログイン回数

	setLogin_count : function(value) {
		this.dict['login_count'] = value;
	},
	getLogin_count : function() {
		return this.dict['login_count'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//入退会理由文字列

	setReason_text : function(value) {
		this.dict['reason_text'] = value;
	},
	getReason_text : function() {
		return this.dict['reason_text'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//セッションＩＤ

	setSSID : function(value) {
		this.dict['ssid'] = value;
	},
	getSSID : function() {
		return this.dict['ssid'];
	},

	// - - - - - - - - - - - - - - - - - - - - -

	validate : function() {

		var errmsg = "";

		//ユーザーＩＤ(半角英数字)
		if ( ! this.dict['id'] ) {
			errmsg += "・ユーザーＩＤを入力してください。\n";
		}
		else if ( ! checkHankaku(this.dict['id']) ) {
			errmsg += "・ユーザーＩＤを半角英数字で入力してください。\n";
		}

		//パスワード(半角英数字)
		if ( ! this.dict['password'] ) {
			errmsg += "・パスワードを入力してください。\n";
		}
		else if ( ! checkHankaku(this.dict['password']) ) {
			errmsg += "・パスワードを半角英数字で入力してください。\n";
		}

		//パスワードの確認(半角英数字)
		if ( ! this.dict['password2'] ) {
			errmsg += "・パスワードの確認入力を行ってください。\n";
		}
		else if ( ! checkHankaku(this.dict['password2']) ) {
			errmsg += "・パスワードを半角英数字で入力してください。\n";
		}
		else if ( this.dict['password2'].length < 3 ) {
			errmsg += "・パスワードは３文字以上１２文字以下の文字数で入力してください。\n";
		}
		if (this.dict['password'] != this.dict['password2']) {
			errmsg += "・パスワードと確認入力されたパスワードが違います。\n";
		}

		//お名前(全角)
		if (( this.dict['name'] ) && (! checkZenkaku(this.dict['name']))) {
			errmsg += "・お名前は全角文字で入力してください。\n";
		}

		//お名前(全角かな)
		if (( this.dict['namekana'] ) && (! checkZenkaku(this.dict['namekana']))) {
			errmsg += "・お名前(全角かな)は全角文字で入力してください。\n";
		}

		//英字名(半角英数字)
		if (( this.dict['ename'] ) && (! checkHankaku(this.dict['ename']))) {
			errmsg += "・英字名は半角英数字で入力してください。\n";
		}

		//ニックネーム
		if ( ! this.dict['nickname'] ) {
			errmsg += "・ニックネームを入力してください。\n";
		}

		//郵便番号(半角数字)
		if (( this.dict['zip'] ) && (! checkTel(this.dict['zip']) )) {
			errmsg += "・郵便番号は半角数字で入力してください。\n";
		}

		//ご住所　都道府県市区(全角)
		if ( ! this.dict['add1'] ) {
			errmsg += "・ご住所　都道府県市区を入力してください。\n";
		}
		else if ( ! checkZenkaku(this.dict['add1']) ) {
			errmsg += "・ご住所　都道府県市区を全角文字で入力してください。\n";
		}

		//町村番地(全角)
		if ( ! this.dict['add2'] ) {
			errmsg += "・ご住所　町村番地を入力してください。\n";
		}
		else if ( ! checkZenkaku(this.dict['add2']) ) {
			errmsg += "・ご住所　町村番地を全角文字で入力してください。\n";
		}

		//電話番号(半角数字)
		if (( this.dict['tel'] ) && (! checkNum(this.dict['tel']))) {
			errmsg += "・電話番号は半角数字で入力してください。\n";
		}

		//FAX番号(半角数字)
		if (( this.dict['fax'] ) && (! checkNum(this.dict['fax']))) {
			errmsg += "・FAX番号は半角数字で入力してください。\n";
		}

		//メールアドレス(半角英数字)
		if ((!this.dict['mail']) && (!this.dict['mobilemail'])) {
			errmsg += "・ＰＣメールアドレス、携帯メールアドレスのいずれかは必ず入力してください。\n";
		}

		if (this.dict['mail']) {
			if (!this.dict['mail2']) {
				errmsg += "・ＰＣメールアドレスの確認入力を入力してください。\n";
			}
			else if (this.dict['mail'] != this.dict['mail2']) {
				errmsg += "・ＰＣメールアドレスと確認入力されたメールアドレスが違います。\n";
			}
			if (! checkEmail(this.dict['mail'])) {
				errmsg += "・正しい形式のＰＣメールアドレスを入力してください。\n";
			}
			if (! checkEmail(this.dict['mail2'])) {
				errmsg += "・正しい形式のＰＣメールアドレスを入力してください。\n";
			}
		}

		if (this.dict['mobilemail']) {
			if (!this.dict['mobilemail2']) {
				errmsg += "・携帯メールアドレスの確認入力を入力してください。\n";
			}
			else if (this.dict['mobilemail'] != this.dict['mobilemail2']) {
				errmsg += "・携帯メールアドレスと確認入力されたメールアドレスが違います。\n";
			}
			if (! checkEmail(this.dict['mobilemail'])) {
				errmsg += "・正しい形式の携帯メールアドレスを入力してください。\n";
			}
			if (! checkEmail(this.dict['mobilemail2'])) {
				errmsg += "・正しい形式の携帯メールアドレスを入力してください。\n";
			}
		}


/*
		if ((this.dict['mail']) && (!this.dict['mail2'])) {
			errmsg += "・メールアドレスの確認入力を入力してください。\n";
		}
		else if ((!this.dict['mail']) && (this.dict['mail2'])) {
			errmsg += "・メールアドレスを入力してください。\n";
		}
		else if ((this.dict['mail']) && (this.dict['mail2']) && (this.dict['mail'] != this.dict['mail2'])) {
			errmsg += "・メールアドレスと確認入力されたメールアドレスが違います。\n";
		}
*/

		//住所存在確認
		if (( this.dict['add1'] ) && ( this.dict['add2'] )) {
			var ret = getPosFromAdd( this.dict['add1'] + this.dict['add2'] );
			if (( ret[0] ) && ( ret[1] )) {
				//折角なので代入
				this.dict['latitude'] = ret[0];
				this.dict['longitude'] = ret[1];
			} else {
				errmsg += "・ご入力頂きました住所は確認できません。町域番地等をご確認ください。\n";
			}

		}

		return errmsg;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//JSON

	toJSON : function() {
		this.dict['password2'] = null;
		this.dict['mail2'] = null;
		this.dict['mobilemail2'] = null;
		return hashToJSON(this.dict);
    }

};


function user_login(userid, userpassword) {

	var user = null;
//	var url = "https://" + location.host + "/user/info/" + userid;
//	var url = "https://" + location.host + "/login.php";
//	var url = "http://" + location.host + "/login.php";
	var url = "http://" + location.host + "/user/login";

	new Ajax.Request(
		url, 
		{
			"method": "get", 
			"parameters": "request_dummy=" + Math.random() + "&json", 
			"requestHeaders": ['X-WSSE', wsseHeader(userid, userpassword)],
			"asynchronous": false,
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json="+request.responseText); 

				var list= json.userResponse.userList.user;
				if (list) {
					user = new User();
					user.setDict( list[0] );
				}
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return user;
}


function user_logout() {

	//var url = "https://" + location.host + "/logout.php";
	var url = "http://" + location.host + "/logout.php";

	new Ajax.Request(
		url, 
		{
			"method": "get", 
			"asynchronous": false,
			onSuccess: function(request) {
				alert(request.responseText);
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 
}


function user_read(userid, userpassword) {

	var url = "http://" + location.host + "/user/info/" + userid;
//	var url = "https://" + location.host + "/user/info/" + userid;
	var user = null;
	new Ajax.Request(
		url, 
		{
			"method": "get", 
			"parameters": "request_dummy=" + Math.random() + "&json", 
			"requestHeaders": ['X-WSSE', wsseHeader(userid, userpassword)],
			"asynchronous": false,
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json="+request.responseText); 

				var list= json.userResponse.userList.user;
				if (list) {
					user = new User();
					user.setDict( list[0] );
				}
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return user;
}

function user_regist(user, isCreate) {

//	var url = "https://" + location.host;
	var url = "http://" + location.host;
	var method;
	var ret = false;

	if (isCreate) {
		//新規
		url += "/user/create";
		method = 'post';
	} else {
		//編集
		url += "/user/update/" + user.getId();
		method = 'put';
	}


	var body = "{ userRequest : { userList : { user : [" + user.toJSON() + "]} }}";
	//alert( url );
	//alert( body );

	new Ajax.Request(
		url,
		{
			"method": method, 
			"parameters": "",
			"postBody": body,
			"asynchronous": false,
			"requestHeaders": [	'X-WSSE', wsseHeader(user.getId(), user.getPassword()),
								'Content-Type', 'text/javascript;charset=UTF-8' ],
			onSuccess: function(request) {
				ret = true;
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return ret;
}




