
function setSearchType()
{
	var $form                  = new Object;
	$form.submit               = false;
	$form.searchtext           = encodeURIComponent(($("input[@name=textfield]").val()).replace(/　/g,' '));

	/* reset */
	$('#searchcondition').html('');

	if( $('#partCategoryDepth1').val() == searchCondition.prototype.facility.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/facility/keywordsearch/';
		$form.classid        = searchCondition.prototype.facility.class_id;
	}
	else if( $('#partCategoryDepth1').val() == searchCondition.prototype.tourism.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/tourism/keywordsearch/';
		$form.classid        = searchCondition.prototype.facility.class_id;
	}
	else if( $('#partCategoryDepth1').val() == searchCondition.prototype.blog.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/blog/keywordsearch/';
		$form.classid        = searchCondition.prototype.blog.class_id;
	}
	else if( $('#partCategoryDepth1').val() == searchCondition.prototype.link.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/link/keywordsearch/';
		$form.classid        = searchCondition.prototype.link.class_id;
	}
	else if( $('#partCategoryDepth1').val() == searchCondition.prototype.event.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/event/keywordsearch/';
		$form.classid        = searchCondition.prototype.event.class_id;
	}
	else if( $('#partCategoryDepth1').val() == searchCondition.prototype.topics.object_id )
	{
		$form.submit         = true;
		$form.action         = searchCondition.prototype.site.url + '/topics/keywordsearch/';
		$form.classid        = searchCondition.prototype.topics.class_id;
	}
	
	/* JKL.Calendar */
	if ($('#event_date_begin').val()) {
		if ( !checkDate($('#event_date_begin').val()) ) {
			alert("イベント開始日の日付フォーマットが異なっています。");
			return false;
		}
	}
	if ($('#event_date_end').val()) {
		if ( !checkDate($('#event_date_end').val()) ) {
			alert("イベント終了日の日付フォーマットが異なっています。");
			return false;
		}
	}
	
	if( $form.submit )
	{
		$action.searchSite.execute( $form );
	}
	else
	{
		$form.action = searchCondition.prototype.site.url + '/keywordsearch/';
		$action.searchSite.execute( $form );
	}


}

$action.searchSite = {
	execute : function( $form )
	{
		this.object = $form;
		this.setFormAction();
		this.submit();
	},
	getObject : function()
	{
		return this.object;
	},
	setFormAction : function()
	{
		$form      = this.getObject();
		$condition = ''

		$condition += $form.action + '?';

		if( $("input[@name=textfield]").val() != '検索する単語を入力してください。' )
		{
			$condition += 'keyword=' + $form.searchtext;
		}

		/* JKL.Calendar */
		if( $('#partCategoryDepth1').val() == searchCondition.prototype.event.object_id ) {
			
			if( $form.searchtext != '検索する単語を入力してください。' ) {
				$condition += "&";
			}
			
			if ($('#event_date_begin').val()) {
				$event_date_begin = Date.parse($('#event_date_begin').val()) / 1000;
				$condition += 'begin=' + $event_date_begin;
			}
			
			if ($('#event_date_end').val()) {
				$event_date_end = Date.parse($('#event_date_end').val()) / 1000;
				
				if ( $event_date_begin >= $event_date_end ) {
					$event_date_end = $event_date_begin;
				}
				
				if ($('#event_date_begin').val()) {
					$condition += "&";
				}
				$condition += 'end=' + $event_date_end;
			}
		}
		
		this.object.condition = $condition;
	},
	submit : function($condition)
	{
		location.href = this.object.condition;
	}
}


/* JKL.Calendar */
$(JKL_calendar);
function JKL_calendar() {
	$('#partCategoryDepth1').change(function(){
	    $searchCondition = new setSearchCondition( false );
	    
		if( $('#partCategoryDepth1').val() == searchCondition.prototype.event.object_id ) {
			// 開始日
			$('.searchPullDown dl').append('<dd id="event_date_begin_area"><label for="event_date_begin">開始日</label><input name="event_date_begin" id="event_date_begin" type="text" onClick="jkl_cal_first.write(); jkl_cal_end.hide();" onChange="jkl_cal_first.getFormValue(); jkl_cal_first.hide();" /></dd>');
			$('.searchPullDown').append('<div id="event_calendar_begin"></div>');
			jkl_cal_first = new JKL.Calendar("event_calendar_begin","searchSiteCondition","event_date_begin");

			// 終了日
			$('.searchPullDown dl').append('<dd id="event_date_end_area"><label for="event_date_end">終了日</label><input name="event_date_end" id="event_date_end" type="text" onClick="jkl_cal_end.write(); jkl_cal_first.hide();" onChange="jkl_cal_end.getFormValue(); jkl_cal_end.hide();" /></dd>');
			$('.searchPullDown').append('<div id="event_calendar_end"></div>');
			jkl_cal_end = new JKL.Calendar("event_calendar_end","searchSiteCondition","event_date_end");

//			$('div#searchArea .searchWrap').css( 'padding', '0px 10px 0px 10px' );
		} else {
			$('#event_date_begin_area').remove();
			$('#event_calendar_begin').remove();
			$('#event_date_end_area').remove();
			$('#event_calendar_end').remove();
//			$('div#searchArea .searchWrap').css( 'padding', '10px' ) ;
		}
	    return false;
	});
}

function checkDate(dateVal) {
    if ( !dateVal.match(/^\d{4}\/\d{1,2}\/\d{1,2}$/) ) {
        return false;
    }
	
	dateArray = dateVal.split("/");
	dateYear  = dateArray[0];
	dateMonth = dateArray[1] - 1;
	dateDay   = dateArray[2];
	
	if(dateMonth >= 0 && dateMonth <= 11 && dateDay >= 1 && dateDay <= 31){
	    var checkDate = new Date(dateYear, dateMonth, dateDay);
	    if( isNaN(checkDate) ) {
	        return false;
	    } else if( checkDate.getFullYear() == dateYear && checkDate.getMonth() == dateMonth && checkDate.getDate() == dateDay ){
	        return true;
	    } else {
	        return false;
	    }
	}else{
	    return false;
	}
}