// JavaScript for Search-List@chisan-kaihatu.com

//{{{ get GET value

function getReqGET()
{
	var hash = {};
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			hash[key] = val;
		}
	}

	return hash;
}

//}}}
//{{{ sort DATA

function sort(int)
{
	var param = '';
	var sortLabel = new Array(
			'sort_value',
			'sort_area',
			'sort_update',
			'sort_age',
			'sort_image'
		);
	var hash = {};
	hash = getReqGET();

	// sort option
	if (isNaN(hash['sort'])) {
		hash['sort'] = eval(int);
	}else{
		var sort_option = int;
		if (eval(hash['sort']) == int) {
			sort_option = (eval(hash['sort']) + 10) % 20;
		}
		hash['sort'] = sort_option;
	}

	for (var key in hash) {
		param += key + '=' + hash[key] + '&';
	}
	
	// CSS SET
	for (var i=0; i<sortLabel.length; i++) {
		try{
			if (eval(int) == i+1) {
				document.getElementById(sortLabel[i]).className = 'search_menu-on';
			}else{
				document.getElemntById(sortLabel[i]).className = '';
			}
		}catch(e){
		}
	}

	// location.search
	param = '?' + param;
	window.location.search = param;
}

//}}}
//{{{ limit change
function limitChange(Obj)
{
	var param = '';
	var hash = {};
	hash = getReqGET();
	hash['limit'] = eval(Obj.value);

	// window.location.search
	for (key in hash) {
		param += key + '=' + hash[key] + '&';
	}

	param = '?' + param;
	window.location.search = param;
}
//}}}
//{{{ addWhere
function addWhere(type)
{
	var param = '';
	var typeLabel = new Array(
			'type_apart',
			'type_mansion',
			'type_house',
			'type_ground',
			'type_office',
			'type_shop',
			'type_shoped'
		);
	var hash = {};
	hash = getReqGET();

	// type OPTION SET
	hash['type'] = type;

	// window.location.search
	for (key in hash) {
		param += key + '=' + hash[key] + '&';
	}

	for (var i=0; i<typeLabel.length; i++) {
		try{
			if (typeLabel[i] == 'type_' + type) {
				document.getElementById(typeLabel[i]).className = 'search_menu-on';
			}else{
				document.getElemntById(typeLabel[i]).className = '';
			}
		}catch(e){
		}
	}

	param = '?' + param;
	window.location.search = param;
}
//}}}

// ----------------------------------------------------------------
// 予算で探す
// ----------------------------------------------------------------

var estType = new Array();
var estBudget = new Array();

//{{{ values set

	/* estType list */
	estType['rent'] = {};
	estType['rent'] = {
			'apart'		 : 'アパート',
			'mansion'	 : 'マンション',
			'house'		 : '一戸建て',
			'office'	 : '事務所',
			'shop'		 : '店舗',
			'shoped'	 : '店舗付物件'
		};
	estType['sale'] = {};
	estType['sale'] = {
			'mansion'	 : 'マンション',
			'house'		 : '一戸建て',
			'ground'	 : '土地'
		};
	
	/* estBudget list */
	estBudget['rent'] = new Array();
	estBudget['rent'][0] = '選択してください';
	estBudget['rent'][1] = '４万円以下';
	estBudget['rent'][2] = '４〜６万円';
	estBudget['rent'][3] = '６〜８万円';
	estBudget['rent'][4] = '８〜１０万円';
	estBudget['rent'][5] = '１０万円以上';

	estBudget['sale'] = new Array();
	estBudget['sale'][0] = '選択してください';
	estBudget['sale'][1] = '５００万円以下';
	estBudget['sale'][2] = '５００〜１，０００万円';
	estBudget['sale'][3] = '１，０００〜２，０００万円';
	estBudget['sale'][4] = '２，０００〜５，０００万円';
	estBudget['sale'][5] = '５，０００〜１０，０００〜万円';
	estBudget['sale'][6] = '１０，０００万円以上';

//}}}
//{{{ init()

/* INITIALIZE CHILD-OPTIONS FOR WINDOW OPEN */
function init()
{
	var html = "";
	html += "<select name='type' id='type' onchange='createChildOptions(this.form)'>";
	html += "<option value=''>選択してください</option>";
	html += "</select>";
	$('typeField').innerHTML = html;

	html = '';
	html += "<select name='range' id='range'>";
	html += "<option value=''>選択してください</option>";
	html += "</select>";
	html += "　<input type='submit' value='検索'>";
	$('budgetField').innerHTML = html;
}

// }}}
// {{{ createChildOptions()

function createChildOptions(frmObj)
{
	var ptObj = frmObj.elements['deal'].options;
	var ptObjLen = ptObj.length;
	var html = '';

	/* setType */
	for (var i=0; i<ptObjLen; i++) {
		if (ptObj[i].selected>0) {
			var selDeal = ptObj[i].value;
			if (selDeal == 'rent' || selDeal == 'sale') {
				var estTypeLen = estType[selDeal].length;
				html = "<select name='type' id='type'>";
				for (key in estType[selDeal]) {
					html += "<option value='" + key +"'>"+estType[selDeal][key]+"</option>";
				}
				/*
				for (var j=0; j<estTypeLen; j++) {
					html += "<option value='" + j +"'>"+estType[ptObj[i].value][j]+"</option>";
				}
				*/
				html += '</select>';
				$('typeField').innerHTML = html;
			}
		}
	}

	/* setBudget */
	for (var i=0; i<ptObjLen; i++) {
		if (ptObj[i].selected>0) {
			var selDeal = ptObj[i].value;
			if (selDeal == 'rent' || selDeal == 'sale') {
				var estBudgetLen = estBudget[selDeal].length;
				html = "<select name='range' id='range'>";
				for (var j=0; j<estBudgetLen; j++) {
					html += "<option value='" + j +"'>"+estBudget[selDeal][j]+"</option>";
				}
				html += '</select>';
				html += "　<input type='submit' value='検索'>";
				$('budgetField').innerHTML = html;
			}
		}
	}
}

//}}}

