// Require prototype.js
// Require function.js
// Require Condition.js
// Require Schedule.js

var Topic = Class.create();

Topic.prototype = {

	initialize  : function() {
		this.dict = {};
		this.conditionList = [];
		this.scheduleList = [];
    },

	setDict : function(value) {
		this.dict = value;

		//検索条件を分解
		if ( this.dict['conditionList'] ) {
			var list = this.dict['conditionList'].condition;
			if ( list ) {
				for(var idx=0;idx < list.length;idx++) {
					var condition = new Condition();
					condition.setDict( list[idx] );
					this.conditionList.push(condition);
				}
			}
		}

		//スケジュールを分解
		if ( this.dict['scheduleList'] ) {
			var list = this.dict['scheduleList'].schedule;
			if ( list ) {
				for(var idx=0;idx < list.length;idx++) {
					var schedule = new Schedule();
					schedule.setDict( list[idx] );
					this.scheduleList.push(schedule);
				}
			}
		}

	},

	getDict : function() {
		return this.dict;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//作成日時
	
	getCreate_tstmp : function() {
		return this.dict['create_tstmp'];
	},

	getCreate_time : function() {
		if ( this.dict['create_tstmp'] ) {
			return Date.parse( replaceAll(this.dict['create_tstmp'], "-", "/") );
		}
		return 0;
	},

	getCreate_date_short : function() {

		var month = tstmp_month( this.dict['create_tstmp'] );
		if ((month) && (month.length < 1)) {
			month = '0' + month;
		}

		var day = tstmp_day( this.dict['create_tstmp'] );
		if ((day) && (day.length < 1)) {
			day = '0' + day;
		}

		return month + '.' + day;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//有効日時

	_datechar : function(val) {

		if (val) {
			val = trim(val);
			if (val.length < 2) {
				val = '0' + val;
			}
		}

		return val;
	},

	_tstmpString : function(y, m, d, hh, mm, ss) {
		return trim(y) + '-' + this._datechar(m) + '-' + this._datechar(d) + 
				' ' + this._datechar(hh) + ':' + this._datechar(mm) + ':' + this._datechar(ss);
	},

	_tstmpStringByDate : function(date) {
//      2010-08-10 Suzuki Safari対応
//		return this._tstmpString(
//			String(date.getYear()), String(date.getMonth()+1), String(date.getDate()), 
//			String(date.getHours()), String(date.getMinutes()), String(date.getSeconds()) );
		return this._tstmpString(
			String(date.getFullYear()), String(date.getMonth()+1), String(date.getDate()), 
			String(date.getHours()), String(date.getMinutes()), String(date.getSeconds()) );
	},

	_dateShort : function(tstmpstr) {

		var month = tstmp_month( tstmpstr );
		if ((month) && (month.length < 1)) {
			month = '0' + month;
		}

		var day = tstmp_day( tstmpstr );
		if ((day) && (day.length < 1)) {
			day = '0' + day;
		}

		return month + '.' + day;
	},


	setStart_tstmp : function(y, m, d, hh, mm, ss) {
		this.dict['start_tstmp'] = this._tstmpString(y, m, d, hh, mm, ss);
	},

	setStarttoToday : function() {
		var date = new Date();

//      2010-08-10 Suzuki Safari対応
//		var today = new Date(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
//		this.dict['start_tstmp'] = this._tstmpStringByDate( today );
		var today = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
		this.dict['start_tstmp'] = this._tstmpStringByDate( today );
	},

	getStart_tstmp : function() {
		return this.dict['start_tstmp'];
	},

	getStart_tstmp_y : function() {
		return tstmp_year( this.dict['start_tstmp'] );
	},

	getStart_tstmp_m : function() {
		return tstmp_month( this.dict['start_tstmp'] );
	},

	getStart_tstmp_d : function() {
		return tstmp_day( this.dict['start_tstmp'] );
	},

	getStart_tstmp_h : function() {
		return tstmp_hour( this.dict['start_tstmp'] );
	},

	getStart_tstmp_mm : function() {
		return tstmp_minutes( this.dict['start_tstmp'] );
	},

	getStart_tstmp_s : function() {
		return tstmp_seconds( this.dict['start_tstmp'] );
	},

	getStart_date_short : function() {
		return this._dateShort( this.dict['start_tstmp'] );
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//終了日時

	setEnd_tstmp : function(y, m, d, hh, mm, ss) {
		this.dict['end_tstmp'] = this._tstmpString(y, m, d, hh, mm, ss);
	},

	setEndtoDays : function(days) {
		var date = new Date();
//      2010-08-10 Suzuki Safari対応
//		var today = new Date(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
		var today = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
		date.setTime(today.getTime() + (days * 86400000) - 1);
		this.dict['end_tstmp'] = this._tstmpStringByDate(date);
	},

	getEnd_tstmp : function() {
		return this.dict['end_tstmp'];
	},

	getEnd_tstmp_y : function() {
		return tstmp_year( this.dict['end_tstmp'] );
	},

	getEnd_tstmp_m : function() {
		return tstmp_month( this.dict['end_tstmp'] );
	},

	getEnd_tstmp_d : function() {
		return tstmp_day( this.dict['end_tstmp'] );
	},

	getEnd_tstmp_h : function() {
		return tstmp_hour( this.dict['end_tstmp'] );
	},

	getEnd_tstmp_mm : function() {
		return tstmp_minutes( this.dict['end_tstmp'] );
	},

	getEnd_tstmp_s : function() {
		return tstmp_seconds( this.dict['end_tstmp'] );
	},

	getEnd_date_short : function() {
		return this._dateShort( this.dict['end_tstmp'] );
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//更新区分	

	setUpdate_class : function(value) {
		this.dict['update_class'] = trim(value);
	},
	getUpdate_class : function() {
		return this.dict['update_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事区分	
	
	setTopic_class : function(value) {
		this.dict['topic_class'] = trim(value);
	},
	getTopic_class : function() {
		return this.dict['topic_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//携帯区分

	setMobile_class : function(value) {
		this.dict['mobile_class'] = trim(value);
	},
	getMobile_class : function() {
		return this.dict['mobile_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事ＩＤ
	
	setId : function(value) {
		this.dict['id'] = trim(value);
	},
	getId : function() {
		return this.dict['id'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事カテゴリ１
	
	setCategory1 : function(value) {
		this.dict['category1'] = trim(value);
	},
	getCategory1 : function() {
		return this.dict['category1'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事カテゴリ２

	setCategory2 : function(value) {
		this.dict['category2'] = trim(value);
	},
	getCategory2 : function() {
		return this.dict['category2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事カテゴリ３

	setCategory3 : function(value) {
		this.dict['category3'] = trim(value);
	},
	getCategory3 : function() {
		return this.dict['category3'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//トップ画面ライン表示

	setOntopline : function(value) {
		this.dict['ontopline'] = trim(value);
	},
	getOntopline : function() {
		return this.dict['ontopline'];
	},


	// - - - - - - - - - - - - - - - - - - - - -
	//リンク区分

	setLink_class : function(value) {
		this.dict['link_class'] = trim(value);
	},
	getLink_class : function() {
		return this.dict['link_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//リンク先記事ID

	setLink_id : function(value) {
		this.dict['link_id'] = trim(value);
	},
	getLink_id : function() {
		return this.dict['link_id'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//地図使用

	setMap_class : function(value) {
		this.dict['map_class'] = trim(value);
	},
	getMap_class : function() {
		return this.dict['map_class'];
	},

	//緯度・経度
	getLatitude : function() {
		return this.dict['latitude'];
	},
	getLongitude : function() {
		return this.dict['longitude'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//登録者区分

	setOwner_class : function(value) {
		this.dict['owner_class'] = trim(value);
	},
	getOwner_class : function() {
		return this.dict['owner_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//登録者ＩＤ

	setOwnerid : function(value) {
		this.dict['ownerid'] = trim(value);
	},
	getOwnerid : function() {
		return this.dict['ownerid'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//登録者名

	setOwnername : function(value) {
		this.dict['ownername'] = trim(value);
	},
	getOwnername : function() {
		return this.dict['ownername'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//登録者表示

	setOwnerdisplayable : function(value) {
		this.dict['ownerdisplayable'] = trim(value);
	},
	getOwnerdisplayable : function() {
		return this.dict['ownerdisplayable'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//親記事ID

	setParent_id : function(value) {
		this.dict['parent_id'] = trim(value);
	},
	getParent_id : function() {
		return this.dict['parent_id'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事題名

	setHeadline : function(value) {
		this.dict['headline'] = trim(value);
	},
	getHeadline : function() {
		return this.dict['headline'];
	},	

	getHeadlineShort : function(size) {
		var str = this.dict['headline'];
		if ((str) && (str.length > size)) {
			return str.substring(0,size) + '...';
		}
		return str;
	},	

	// - - - - - - - - - - - - - - - - - - - - -
	//記事内容

	setContents : function(value) {
		this.dict['contents'] = trim(value);
	},
	getContents : function() {
		return this.dict['contents'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//外部リンク(URL)

	setUrl : function(value) {
		this.dict['url'] = trim(value);
	},
	getUrl : function() {
		return this.dict['url'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//写真1ファイル名

	setPhoto1 : function(value) {
		this.dict['photo1'] = trim(value);
	},
	getPhoto1 : function() {
		return this.dict['photo1'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//写真2ファイル名

	setPhoto2 : function(value) {
		this.dict['photo2'] = trim(value);
	},
	getPhoto2 : function() {
		return this.dict['photo2'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//写真3ファイル名

	setPhoto3 : function(value) {
		this.dict['photo3'] = trim(value);
	},
	getPhoto3 : function() {
		return this.dict['photo3'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//写真4ファイル名

	setPhoto4 : function(value) {
		this.dict['photo4'] = trim(value);
	},
	getPhoto4 : function() {
		return this.dict['photo4'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//最新参照日時

	setView_tstmp : function(value) {
		this.dict['view_tstmp'] = trim(value);
	},
	getView_tstmp : function() {
		return this.dict['view_tstmp'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//参照回数

	setView_count : function(value) {
		this.dict['view_count'] = trim(value);
	},
	getView_count : function() {
		return this.dict['view_count'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//検索条件リスト

	getConditionList : function() {
		return this.conditionList;
	},

	clearConditionList : function() {
		this.conditionList = [];
	},

	hasCondition : function() {
		return (this.conditionList) && (this.conditionList.length > 0);
	},

	pushCondition : function(condition) {

		//登録種別
		condition.setObject_type( this.dict['topic_class'] );

		//登録者コード
		condition.setObject_id( this.dict['id'] );

		this.conditionList.push(condition);
	},

	addCondition : function(type, value, value2) {

		var condition = new Condition();

		//検索条件種別
		condition.setType( type );

		//検索条件
		condition.setValue( value );
		condition.setValue2( value2 );

		this.pushCondition(condition);
	},

	searchCondition : function(type) {
		var ret = null;
		if ((this.conditionList) && (this.conditionList.length > 0)) {
			for(var idx=0;idx < this.conditionList.length;idx++) {
				var cond = this.conditionList[idx];
				if (cond.getType() == type) {
					if (!ret) {
						ret = [];
					}
					ret.push(cond);
				}
			}
		}
		return ret;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//スケジュールリスト

	getScheduleList : function() {
		return this.scheduleList;
	},

	clearScheduleList : function() {
		this.scheduleList = [];
	},

	hasSchedule : function() {
		return (this.scheduleList) && (this.scheduleList.length > 0);
	},

	pushSchedule : function(schedule) {

		//登録種別
		schedule.setObject_type( '1' ); //記事スケジュール

		//登録者コード
		schedule.setObject_id( this.dict['id'] );

		this.scheduleList.push(schedule);
	},

	addSchedule : function(display, type, date, start_time, end_time) {

		var schedule = new Schedule();

		//表示種別
		schedule.setDisplayable(display);

		//種別
		schedule.setType( type );

		//日付または曜日
		schedule.setDate( date );

		//開始時間
		schedule.setStart_time(start_time);

		//終了時間
		schedule.setEnd_time(end_time);

		this.pushSchedule(schedule);
	},


	// - - - - - - - - - - - - - - - - - - - - -
	//クーポン用

	setLimitDate : function(date) {

		switch( this.dict['category2'] ) {
		case "誕生日":

			//誕生日クーポン
			var clist = this.searchCondition("102");
			if (!clist) {
				return;
			}

			var cond = clist[0];
			var days = toInt(cond.getValue());
			if (!days) {
				days = 0;
			}

			var setdate = new Date();
			setdate.setTime(date.getTime() - (days * 86400000));	//日減算
			this.dict['start_tstmp'] = this._tstmpStringByDate( setdate );

			setdate.setTime(date.getTime() + (days * 86400000));	//日加算
			this.dict['end_tstmp'] = this._tstmpStringByDate( setdate );

			break;


		case "入会":
			//入会クーポン

			var clist = this.searchCondition("103");
			if (!clist) {
				return;
			}

			var cond = clist[0];
			var days = toInt(cond.getValue());

			var setdate = new Date();
			setdate.setTime(date.getTime() + (days * 86400000));	//日加算
			this.dict['end_tstmp'] = this._tstmpStringByDate( setdate );

			break;
		}

	},

	// - - - - - - - - - - - - - - - - - - - - -
	//バリデーションチェック

	validate : function() {

		var errmsg = "";

		//登録者区分
		if ( ! this.dict['owner_class'] ) {
			errmsg += "・登録者区分が設定されていません。\n";
		}

		//登録者ＩＤ
		if ( ! this.dict['ownerid'] ) {
			errmsg += "・登録者ＩＤが設定されていません。\n";
		}

		//有効日時
		if ( ! this.dict['start_tstmp'] ) {
			errmsg += "・有効期間(開始日時)が設定されていません。\n";
		}
		if ( ! this.dict['end_tstmp'] ) {
			errmsg += "・有効期間(終了日時)が設定されていません。\n";
		}

		//記事題名
		if ( ! this.dict['headline'] ) {
			errmsg += "・記事題名を入力してください。\n";
		}

		//記事内容
/*
		if ( ! this.dict['contents'] ) {
			errmsg += "・記事内容を入力してください。\n";
		}
*/

		//検索条件
		if ( this.conditionList.length > 0 ) {
			var idx = 0;
			for(idx=0;idx < this.conditionList.length;idx++) {
				errmsg += this.conditionList[idx].validate();
			}
		}

		//スケジュール
		if ( this.scheduleList.length > 0 ) {
			var idx = 0;
			for(idx=0;idx < this.scheduleList.length;idx++) {
				errmsg += this.scheduleList[idx].validate();
			}
		}

		return errmsg;
	},

	// - - - - - - - - - - - - - - - - - - - - -

	toJSON : function() {

		//検索条件
		if ( this.conditionList.length > 0 ) {
			var conArray = [];
			for(var idx=0;idx < this.conditionList.length;idx++) {
				conArray.push( this.conditionList[idx].getDict() );
			}

			this.dict['conditionList'] = {};
			this.dict['conditionList']['condition'] = conArray;
		} else {
			this.dict['conditionList'] = null;
		}

		//スケジュール
		if ( this.scheduleList.length > 0 ) {
			var scheArray = [];
			for(var idx=0;idx < this.scheduleList.length;idx++) {
				scheArray.push( this.scheduleList[idx].getDict() );
			}

			this.dict['scheduleList'] = {};
			this.dict['scheduleList']['schedule'] = scheArray;
		} else {
			this.dict['scheduleList'] = null;
		}

		return hashToJSON(this.dict);
    }

};

var TopicList = Class.create();

TopicList.prototype = {

	initialize  : function() {
		this.dict = {};
		this.array = [];
		this.totalrows = 0;
		this.reqparam = '';
    },

	// - - - - - - - - - - - - - - - - - - - - -
	//記事追加

	addTopic : function(topic) {

		var catTag1 = topic.getCategory1();
		if (!catTag1) {
			catTag1 = "_";
		}

		var catTag2 = topic.getCategory2();
		if (!catTag2) {
			catTag2 = "_";
		}

		var catDict1 = this.dict[catTag1];
		var catArray2 = null;

		if ( catDict1 ) {
			catArray2 = catDict1[catTag2];
		} else {
			catDict1 = new Object();
			this.dict[catTag1] = catDict1;
		}

		if (!catArray2) {
			catArray2 = new Array();
			catDict1[catTag2] = catArray2;
		}

		catArray2.push(topic);
		this.array.push(topic);
		//alert(topic.getStart_tstmp()+topic.getHeadline());
		//alert("add:"+topic.getHeadline());
	},

	merge : function(topiclist) {
		var list = topiclist.getTopicAll();
		if (list) {
			for(var idx = 0; idx < list.length ; idx++){
				this.addTopic( list[idx] );
			}
		}
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//リクエストパラメータ

	getReqParam : function() {
		return this.reqparam;
	},

	setReqParam : function(param) {
		this.reqparam = param;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事数

	getNumOfTopic : function() {
		return this.array.length;
	},

	getTotalRows : function() {
		return this.totalrows;
	},

	setTotalRows : function(num) {
		this.totalrows = num;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//記事取得

	getTopicAll : function() {
		return this.array;
	},

	getTopicAllwithSort : function() {
		this.array.sort(topic_start_tstmp_sort);
		return this.array;
	},

	getTopicIndex : function(idx) {
		if (idx < this.array.length) {
			return this.array[idx];
		}
		return null;
	},

	getTopic : function(catTag1, catTag2) {

		if (!catTag1) {
			catTag1 = "_";
		}

		if (!catTag2) {
			catTag2 = "_";
		}

//		alert(catTag1+"/"+catTag2);

		var ret = null;

		var tag1list = catTag1.split("|");
		var tag2list = catTag2.split("|");
		if ((tag1list) && (tag2list)) {

			for(var tag1idx=0;tag1idx < tag1list.length;tag1idx++) {
				var catDict1 = this.dict[tag1list[tag1idx]];
				if (catDict1) {
					for(var tag2idx=0;tag2idx < tag2list.length;tag2idx++) {
						var array = catDict1[tag2list[tag2idx]];
						if (array) {
							if (!ret) {
								ret = new Array();
							}
							ret = ret.concat(array);
						}

					}
				}
			}
		}

		if (ret) {
			ret.sort(topic_start_tstmp_sort);
		}

		return ret;

/*
		var catDict1 = this.dict[catTag1];
		var catArray2 = null;
		if (catDict1) {
			catArray2 = catDict1[catTag2];
			if (catArray2) {
				catArray2.sort(topic_start_tstmp_sort);
			}
		}

		return catArray2;
*/
	},

	getTopicC1 : function(catTag1) {

		var ret = null;

		if (!catTag1) {
			catTag1 = "_";
		}

		var tag1list = catTag1.split("|");
		if (tag1list) {
			for(var tag1idx=0;tag1idx < tag1list.length;tag1idx++) {
				var catDict1 = this.dict[tag1list[tag1idx]];
				if (catDict1) {
					if (!ret) {
						ret = new Array();
					}
					for(key in catDict1) {
						ret = ret.concat(catDict1[key]);
					}
				}
			}

			if (ret) {
				ret.sort(topic_start_tstmp_sort);
			}
		}

		return ret;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//カテゴリ取得

	getCategories1 : function() {

		return( getDictKeys( this.dict ) );
	},

	getCategories2 : function(catTag1) {

		if (!catTag1) {
			catTag1 = "_";
		}

		return( getDictKeys( this.dict[catTag1] ) );
	}
}


function topic_start_tstmp_sort(a, b) {

	var cat3a = a.getCategory3();
	var cat3b = b.getCategory3();

	if ((cat3a == 'おすすめランダム') && (cat3b != 'おすすめランダム')) {
		return 1;
	}
	else if ((cat3a != 'おすすめランダム') && (cat3b == 'おすすめランダム')) {
		return -1;
	}

	return compareDateTime( b.getStart_tstmp_y(), b.getStart_tstmp_m(), b.getStart_tstmp_d(), 
							b.getStart_tstmp_h(), b.getStart_tstmp_mm(), b.getStart_tstmp_s(),
							a.getStart_tstmp_y(), a.getStart_tstmp_m(), a.getStart_tstmp_d(), 
							a.getStart_tstmp_h(), a.getStart_tstmp_mm(), a.getStart_tstmp_s() );
}


function readTopic_wsse(id, param, wsse, callback) {

	var asynchronous = (callback != null);
	var reqparam = "json";
	var url = "/topic/content/" + id;
	var topic = null;

	if (param) {
		reqparam += "&" + param;
	}

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": asynchronous,
			"requestHeaders": ['X-WSSE', wsse,
							   'RequestPath', location.pathname],
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				var list = json.topicResponse.topicList.topic;
				if (! list ) {
					return;
				}

				topic = new Topic();
				topic.setDict( list[0] );

			},
			onComplete: function(request) {
				if (callback) {
					callback(topic);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return topic;
}

function readTopic(id, param, user, password, callback) {
	return readTopic_wsse(id, param, wsseHeader(user, password), callback);
}

function searchTopicHL(reqparam, callback) { 

	var asynchronous = (callback != null);

	//alert(reqparam);

	var numoftopic = 0;
	var topiclist = null;

	new Ajax.Request(
		"/topic/search/", 
		{
			"method": "get", 
			"parameters": reqparam + "&json&request_dummy=" + Math.random(), 
			"asynchronous": asynchronous,
			onSuccess: function(request) {
				//alert(request.responseText);
				var json; 
				eval("json="+replaceAll(request.responseText,"\n","\\n"));

				if (json.topicResponse.numoftopic) {
					numoftopic = parseInt(json.topicResponse.numoftopic);
				}
				//alert(numoftopic);

				var list = json.topicResponse.topicList.topic;
				if ( list ) {
					topiclist = new TopicList();
					topiclist.setTotalRows(numoftopic);
					topiclist.setReqParam(reqparam);

					for(var idx = 0; idx < list.length ; idx++){
						var topic = new Topic();
						topic.setDict( list[idx] );
						topiclist.addTopic( topic );
					}
				}
				
			},
			onComplete: function(request) {
				if (callback) {
					callback(numoftopic, topiclist);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	);

	return topiclist;
}


function _readTopicList(url, idparam, categorylist, noexpire, callback) { 

	var topiclist = null;
	var reqparam = "";
	var asynchronous = (callback != null);

	if (idparam) {
		reqparam += "&" + idparam;
	}

	if (noexpire) {
		reqparam += "&noexpire";
	}

	if (categorylist) {
		reqparam += "&categoryList=" + categorylist;
	}

//http://localhost/topic/content?user=test&noexpire&categoryList={今日は::0:3,天気::0:1,ニュース::2:5}&ie=MS932

//	alert(url);

	new Ajax.Request(
		url, 
		{
			"method": "get", 
			"parameters": reqparam + "&json&request_dummy=" + Math.random(), 
			"asynchronous": asynchronous,
			"requestHeaders": ['RequestPath', location.pathname],
			onSuccess: function(request) {
				//alert(request.responseText);
				var json; 
				eval("json="+replaceAll(request.responseText,"\n","\\n"));

				var list = json.topicResponse.topicList.topic;
				if ( list ) {

					topiclist = new TopicList();
					topiclist.setReqParam(reqparam);
					if (json.topicResponse.numoftopic) {
						topiclist.setTotalRows(parseInt(json.topicResponse.numoftopic));
					}

					for(var idx = 0; idx < list.length ; idx++) {
						var topic = new Topic();
						topic.setDict( list[idx] );
						topiclist.addTopic( topic );
					}
				}

			},
			onComplete: function(request) {
				if ( callback ) {
					callback(topiclist);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {/*alert(e.message);*/} 
		}
	);

	return topiclist;
}


function readTopicList(isHeadline, idparam, categorylist, noexpire, callback) { 

	var url = "/topic/";

	if (isHeadline) {
		url += "headline/"
	} else {
		url += "content/"
	}

	return _readTopicList(url, idparam, categorylist, noexpire, callback);
}


function readCouponList(isHeadline, idparam, callback) { 

	var url = "/topic/";

	if (isHeadline) {
		url += "couponheadline/"
	} else {
		url += "coupon/"
	}

	return _readTopicList(url, idparam, null, false, callback);
}

function readCoupon_wsse(id, param, wsse, callback) {

	var asynchronous = (callback != null);
	var reqparam = "json";
	var url = "/topic/coupon/" + id;
	//var url = "https://208.82.112.107/topic/coupon/" + id;
	var topic = null;

	if (param) {
		reqparam += "&" + param;
	}

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": asynchronous,
			"requestHeaders": ['X-WSSE', wsse,
							   'RequestPath', location.pathname],
			onSuccess: function(request) {
				alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				var list = json.topicResponse.topicList.topic;
				if (! list ) {
					return;
				}

				topic = new Topic();
				topic.setDict( list[0] );

			},
			onComplete: function(request) {
				if (callback) {
					callback(topic);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {/*alert(e.message);*/} 
		}
	); 

	return topic;
}

function readCoupon(id, param, user, password, callback) {
	return readTopic_wsse(id, param, wsseHeader(user, password), callback);
}


