function disp_search_person_name(person_id,person_name){
    submitSearch(person_name, 'person');


// 	var encode_kperson = encodeURIComponent(person_name);
// 	var url = '/programlist/search.php?s=pl_star&person='
// 	+ person_id + '&kperson=' + encode_kperson;
// 	window.location.href = url;
	/*window.opener.location.href = url;*/
	/*window.close();*/
}

function open_program_list(){
	document.location.href = '/programlist/guide.php';
//	window.open('/programlist/guide.php','_blank');
}

var search_cats = Array('tv', 'bs', 'cs');

function submitSearch(text, mode, stype, schint) {
    var val = getcookie('bcmethod');
    if(!val) val = 'analog';
    var p = 'programlist';
    if(val == 'digital') p = 'digitalguide';

    var cookiepath  = "; path=/";

    if(!stype) stype = getselectedsearchcat();
    var date = new Date();
    date.setTime(date.getTime()+(300*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    if(schint) document.cookie = 's_chint='+encodeURI(schint)+cookiepath;
    document.cookie = 's_mode='+mode+expires+cookiepath;
    document.cookie = 's_text='+encodeURI(text)+expires+cookiepath;
    //2: document.cookie = 's_type='+encodeURI(stype)+cookiepath;
    document.cookie = 'type='+encodeURI(stype)+'; path=/';
    document.location.href = '/'+p+'/search.php';
}

function flip(el, pic) {
    el.oldpic       = el.src;
    el.onmouseover  = function() { el.src = pic }
    el.onmouseout   = function() { el.src = el.oldpic }
    el.onmouseover();
}

function getselectedsearchcat() {
    if(currseltype && currseltype != 'undefined') return currseltype;
    if(!document.getElementById('tv_btn')) {
        //2: var stype = getcookie('s_type');
        var stype = getcookie('type');
        //2: if(!stype || stype == 'undefined') stype = getcookie('type');
        if(!stype || stype == 'undefined') stype = 'tv';
        return stype;
    };
    //for(var i in search_cats)
    for(var i=0;i<elements.length;i++) if(document.getElementById(search_cats[i]+'_btn_sel').style.display != 'none') return search_cats[i];
    return 'tv';
}

var currseltype = getcookie('type');
function selectbutton(cat) {
    if(!cat) cat = currseltype;
    if(!cat || cat == 'undefined') cat = getcookie('type');
    if(!cat || cat == 'undefined') cat = 'tv';
    if(document.getElementById('tv_btn')) {
        //for(var i in search_cats) {
        for(var i=0;i<search_cats.length;i++) {
            document.getElementById(search_cats[i]+'_btn_sel').style.display = (search_cats[i] == cat) ? 'inline' : 'none';
            document.getElementById(search_cats[i]+'_btn').style.display     = (search_cats[i] == cat) ? 'none' : 'inline';
        }
    }
    if(el = document.getElementById('sbr'+cat)) el.checked = true;
    currseltype = cat;
}
//alert(getcookie('s_mode'));
var lasttext = (getcookie('s_mode') == 'category') ? '' : decodeURI(getcookie('s_text')).replace(/\+/g, ' ');
function syncinputs(s) {
    var txt = s ? s.value : lasttext;
    if(el = document.getElementById('sbitext')) el.value = txt;
    if(el = document.getElementById('input_search_kperson')) el.value = txt;
    if(el = document.getElementById('input_search_keyword')) el.value = txt;
    lasttext = txt;
}

function getcookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return '';
}

function get_station_cookie(){
	var value = 'snum1';
	if (document.cookie) {
		var cookies = document.cookie.split("; ");
		for (var i = 0; i < cookies.length; i++) {
			var str = cookies[i].split("=");
			if (str[0] == 'station_key') {
				value = unescape(str[1]);
				break;
			}
		}
	}else{
		set_station_cookie(value);
	}

	return value;
}

function set_station_cookie(pref){
	var period = 30;

	var nowtime = new Date().getTime();
	var clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * period));
	var expires = clear_time.toGMTString();
	document.cookie = "station_key=" + escape(pref) + "; expires=" + expires + "; path=/";
}

function disp_ctrl(id_name,mode){
	var obj = document.getElementById(id_name);
	if(obj){
		obj.style.display = mode;
	}
}

function init_station(){
	var key = get_station_cookie();
	if(key=='snum1'){
		push_station_snum_button(1);
	}else if(key=='snum2'){
		push_station_snum_button(2);
	}else{
		push_station_snum_button(1);
	}
}

function push_station_snum_button(n){
	if(n==1){
		set_station_cookie('snum1');
		disp_ctrl('push_chijouha_image_on','block');
		disp_ctrl('push_chijouha_image_off','none');
		disp_ctrl('push_bs_image_on','none');
		disp_ctrl('push_bs_image_off','block');
		var obj = document.getElementById('snum1');
		if(obj){
			obj.checked = true;
		}
		var obj = document.getElementById('snum2');
		if(obj){
			obj.checked = false;
		}
	}else{
		set_station_cookie('snum2');
		disp_ctrl('push_chijouha_image_on','none');
		disp_ctrl('push_chijouha_image_off','block');
		disp_ctrl('push_bs_image_on','block');
		disp_ctrl('push_bs_image_off','none');
		var obj = document.getElementById('snum1');
		if(obj){
			obj.checked = false;
		}
		var obj = document.getElementById('snum2');
		if(obj){
			obj.checked = true;
		}
	}
}

var pref_to_area_and_name = {
    'analog' : {
        'hokkaido':		{'area': 'TV05',	'name': '北海道'},
        'sapporo':      {'area': 'TV05',    'name': '北海道'},
        'asahikawa':    {'area': 'TV05',    'name': '北海道'},
        'muroran':      {'area': 'TV05',    'name': '北海道'},
        'hakodate':     {'area': 'TV05',    'name': '北海道'},
        'obihiro':      {'area': 'TV05',    'name': '北海道'},
        'kushiro':      {'area': 'TV05',    'name': '北海道'},
        'kitami':       {'area': 'TV05',    'name': '北海道'},
        'aomori':		{'area': 'TV07',	'name': '青森'},
        'iwate':		{'area': 'TV08',	'name': '岩手'},
        'akita':		{'area': 'TV09',	'name': '秋田'},
        'yamagata':		{'area': 'TV10',	'name': '山形'},
        'miyagi':		{'area': 'TV11',	'name': '宮城'},
        'fukushima':	{'area': 'TV12',	'name': '福島'},
        'tochigi':		{'area': 'TV02',	'name': '栃木'},
        'gunma':		{'area': 'TV02',	'name': '群馬'},
        'ibaraki':		{'area': 'TV02',	'name': '茨城'},
        'tokyo':		{'area': 'TV01',	'name': '東京'},
        'kanagawa':		{'area': 'TV01',	'name': '神奈川'},
        'saitama':		{'area': 'TV01',	'name': '埼玉'},
        'chiba':		{'area': 'TV01',	'name': '千葉'},
        'niigata':		{'area': 'TV13',	'name': '新潟'},
        'nagano':		{'area': 'TV14',	'name': '長野'},
        'yamanashi':	{'area': 'TV15',	'name': '山梨'},
        'shizuoka':		{'area': 'TV16',	'name': '静岡'},
        'ishikawa':		{'area': 'TV17',	'name': '石川'},
        'toyama':		{'area': 'TV18',	'name': '富山'},
        'fukui':		{'area': 'TV19',	'name': '福井'},
        'aichi':		{'area': 'TV04',	'name': '愛知'},
        'gifu':			{'area': 'TV04',	'name': '岐阜'},
        'mie':			{'area': 'TV04',	'name': '三重'},
        'osaka':		{'area': 'TV03',	'name': '大阪'},
        'hyogo':		{'area': 'TV03',	'name': '兵庫'},
        'kyoto':		{'area': 'TV03',	'name': '京都'},
        'wakayama':		{'area': 'TV03',	'name': '和歌山'},
        'nara':			{'area': 'TV03',	'name': '奈良'},
        'shiga':		{'area': 'TV03',	'name': '滋賀'},
        'okayama':		{'area': 'TV20',	'name': '岡山'},
        'kagawa':		{'area': 'TV20',	'name': '香川'},
        'tokushima':	{'area': 'TV21',	'name': '徳島'},
        'ehime':		{'area': 'TV22',	'name': '愛媛'},
        'kouchi':		{'area': 'TV23',	'name': '高知'},
        'hiroshima':	{'area': 'TV24',	'name': '広島'},
        'tottori':		{'area': 'TV25',	'name': '鳥取'},
        'shimane':		{'area': 'TV25',	'name': '島根'},
        'yamaguchi':	{'area': 'TV26',	'name': '山口'},
        'fukuoka':		{'area': 'TV06',	'name': '福岡'},
        'saga':			{'area': 'TV27',	'name': '佐賀'},
        'kumamoto':		{'area': 'TV28',	'name': '熊本'},
        'nagasaki':		{'area': 'TV29',	'name': '長崎'},
        'oita':			{'area': 'TV30',	'name': '大分'},
        'miyazaki':		{'area': 'TV31',	'name': '宮崎'},
        'kagoshima':	{'area': 'TV32',	'name': '鹿児島'},
        'okinawa':		{'area': 'TV33',	'name': '沖縄'}
    },
    'digital' : {
        'hokkaido':     {'area': 'TVSAPPORO',   'name': '札幌'},
        'sapporo':      {'area': 'TVSAPPORO',   'name': '札幌'},
        'asahikawa':    {'area': 'TVASAHIKAWA', 'name': '旭川'},
        'muroran':      {'area': 'TVMURORAN',   'name': '室蘭'},
        'hakodate':     {'area': 'TVHAKODATE',  'name': '函館'},
        'obihiro':      {'area': 'TVOBIHIRO',   'name': '帯広'},
        'kushiro':      {'area': 'TVKUSHIRO',   'name': '釧路'},
        'kitami':       {'area': 'TVKITAMI',    'name': '北見'},
        'aomori':       {'area': 'TVAOMORI',    'name': '青森'},
        'iwate':        {'area': 'TVIWATE',     'name': '岩手'},
        'akita':        {'area': 'TVAKITA',     'name': '秋田'},
        'yamagata':     {'area': 'TVYAMAGATA',  'name': '山形'},
        'miyagi':       {'area': 'TVMIYAGI',    'name': '宮城'},
        'fukushima':    {'area': 'TVFUKUSHIMA', 'name': '福島'},
        'tochigi':      {'area': 'TVTOCHIGI',   'name': '栃木'},
        'gunma':        {'area': 'TVGUNMA',     'name': '群馬'},
        'ibaraki':      {'area': 'TVIBARAKI',   'name': '茨城'},
        'tokyo':        {'area': 'TVTOKYO',     'name': '東京'},
        'kanagawa':     {'area': 'TVKANAGAWA',  'name': '神奈川'},
        'saitama':      {'area': 'TVSAITAMA',   'name': '埼玉'},
        'chiba':        {'area': 'TVCHIBA',     'name': '千葉'},
        'niigata':      {'area': 'TVNIIGATA',   'name': '新潟'},
        'nagano':       {'area': 'TVNAGANO',    'name': '長野'},
        'yamanashi':    {'area': 'TVYAMANASHI', 'name': '山梨'},
        'shizuoka':     {'area': 'TVSHIZUOKA',  'name': '静岡'},
        'ishikawa':     {'area': 'TVISHIKAWA',  'name': '石川'},
        'toyama':       {'area': 'TVTOYAMA',    'name': '富山'},
        'fukui':        {'area': 'TVFUKUI',     'name': '福井'},
        'aichi':        {'area': 'TVAICHI',     'name': '愛知'},
        'gifu':         {'area': 'TVGIFU',      'name': '岐阜'},
        'mie':          {'area': 'TVMIE',       'name': '三重'},
        'osaka':        {'area': 'TVOSAKA',     'name': '大阪'},
        'hyogo':        {'area': 'TVHYOGO',     'name': '兵庫'},
        'kyoto':        {'area': 'TVKYOTO',     'name': '京都'},
        'wakayama':     {'area': 'TVWAKAYAMA',  'name': '和歌山'},
        'nara':         {'area': 'TVNARA',      'name': '奈良'},
        'shiga':        {'area': 'TVSHIGA',     'name': '滋賀'},
        'okayama':      {'area': 'TVOKAYAMA',   'name': '岡山'},
        'kagawa':       {'area': 'TVKAGAWA',    'name': '香川'},
        'tokushima':    {'area': 'TVTOKUSHIMA', 'name': '徳島'},
        'ehime':        {'area': 'TVEHIME',     'name': '愛媛'},
        'kouchi':       {'area': 'TVKOUCHI',    'name': '高知'},
        'hiroshima':    {'area': 'TVHIROSHIMA', 'name': '広島'},
        'tottori':      {'area': 'TVTOTTORI',   'name': '鳥取'},
        'shimane':      {'area': 'TVSHIMANE',   'name': '島根'},
        'yamaguchi':    {'area': 'TVYAMAGUCHI', 'name': '山口'},
        'fukuoka':      {'area': 'TVFUKUOKA',   'name': '福岡'},
        'saga':         {'area': 'TVSAGA',      'name': '佐賀'},
        'kumamoto':     {'area': 'TVKUMAMOTO',  'name': '熊本'},
        'nagasaki':     {'area': 'TVNAGASAKI',  'name': '長崎'},
        'oita':         {'area': 'TVOITA',      'name': '大分'},
        'miyazaki':     {'area': 'TVMIYAZAKI',  'name': '宮崎'},
        'kagoshima':    {'area': 'TVKAGOSHIMA', 'name': '鹿児島'},
        'okinawa':      {'area': 'TVOKINAWA',   'name': '沖縄'}
    }
};

var horiday_list = {
	'20100101': '元日',
	'20100111': '成人の日',
	'20100211': '建国記念の日',
	'20100321': '春分の日',
	'20100322': '振替休日',
	'20100429': '昭和の日',
	'20100503': '憲法記念日',
	'20100504': 'みどりの日',
	'20100505': 'こどもの日',
	'20100719': '海の日',
	'20100920': '敬老の日',
	'20100923': '秋分の日',
	'20101012': '体育の日',
	'20101103': '文化の日',
	'20101123': '勤労感謝の日',
	'20101223': '天皇誕生日'
}

function get_area_cookie(){
	var value = 'tokyo';
	if (document.cookie) {
		var cookies = document.cookie.split("; ");
		for (var i = 0; i < cookies.length; i++) {
			var str = cookies[i].split("=");
			if (str[0] == 'area_pref_key') {
				value = unescape(str[1]);
				break;
			}
		}
	}else{
		set_area_cookie(value);
	}

	return value;
}

function set_area_cookie(pref){
	var period = 30;

	var nowtime = new Date().getTime();
	var clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * period));
	var expires = clear_time.toGMTString();
	document.cookie = "area_pref_key=" + escape(pref) + "; expires=" + expires + "; path=/";
}

function getbcmethod() {
    if(getcookie('bcmethod') == 'digital') return 'digital';
    return 'analog';
}

function tv_pref(pref,key){
	return pref_to_area_and_name[getbcmethod()][pref][key];
}

function tv_pref_area(){
	return tv_pref(get_area_cookie(),'area');
}

function tv_pref_name(){
	return tv_pref(get_area_cookie(),'name');
}

function tv_set_area(obj,id_name){
	set_area_cookie(obj.options[obj.options.selectedIndex].value);
	switch_panel(id_name);
	select_area_now_onair();
}

function tv_select_pref(pref){
	set_area_cookie(pref);
	opener.document.location = opener.document.URL;
}

function common_tmpl_pref(){
	var tmpl_name = 'tmpl_pref';
	var tmp_tmpl = document.getElementById(tmpl_name).innerHTML;

	var str = replace_tmpl_hash(tmp_tmpl,{
		'pref_name': tv_pref_name(),
		'area_code': tv_pref_area()
	});

	disp(tmpl_name,str);
}

function program_tmpl_day(){
	var tmpl_name = 'templ_day';
	var tmp_tmpl = document.getElementById(tmpl_name).innerHTML;

	var tmp_time = new Date();
	var now_time = tmp_time.getTime();
	var dweek = ['日','月','火','水','木','金','土'];
	var hweek = ['holiday','def','def','def','def','def','eve'];
	var str = '';
	for(var i=0; i<8; i++){
		tmp_time.setTime(now_time+1000*60*60*24*i);
		if(tmp_time.getHours()<5){
			tmp_time.setTime(now_time+1000*60*60*24*(i-1));
		}
		var year = tmp_time.getYear();
		if(year<2000){
			year = year+1900;
		}
		year = ''+year;
		var mon = '0'+(tmp_time.getMonth()+1);
		var day = '0'+tmp_time.getDate();
		var date_str = year+mon.substr(mon.length-2,2)+day.substr(day.length-2,2);
		str += replace_tmpl_hash(tmp_tmpl,{
			'li_class': hweek[horiday_list[date_str]?0:tmp_time.getDay()],
			'area_code': tv_pref_area(),
			'day_num': i,
			'wday': dweek[tmp_time.getDay()],
			'mon': tmp_time.getMonth()+1,
			'day': tmp_time.getDate()
		});
	}
	disp(tmpl_name,str);
}

function areaset_tmpl_select_pref(){
	var tmpl_name = 'tmpl_select_pref';
	var tmp_tmpl = document.getElementById(tmpl_name).innerHTML;

	var pref = pref_to_area_and_name[getbcmethod()];
	var str = '';

	for(var key in pref){
		str += replace_tmpl_hash(tmp_tmpl,{
			'value': key,
			'view_val': pref[key]['name'],
			'selected': (key==get_area_cookie() ? 'selected' : '')
		});

		if(key==get_area_cookie()){

		}
	}

	disp(tmpl_name,str);
}

function changebcmethod(newmethod) {
    document.cookie = 'bcmethod='+encodeURI(newmethod)+'; path=/';
}

function selectbcmethod() {

    var a = document.getElementById('ana_button');
    var d = document.getElementById('dig_button');

    var val = getcookie('bcmethod');

    if(!val) val = 'analog';

    if(val == 'analog') {
        a.src = '/digitalguide/img/chart/analog_sel.gif';
        d.src = '/digitalguide/img/chart/digital.gif';
        d.onmouseover =  function(e) { flip(this, '/digitalguide/img/chart/digital_over.gif'); }
        d.onclick = function(e) { changebcmethod('digital'); document.location = '/digitalguide/guide.php'; }
        d.style.cursor = 'pointer';
    }
    else if(val == 'digital') {
        a.src = '/digitalguide/img/chart/analog.gif';
        d.src = '/digitalguide/img/chart/digital_sel.gif';
        a.onmouseover =  function(e) { flip(this, '/digitalguide/img/chart/analog_over.gif'); }
        a.onclick = function(e) { changebcmethod('analog'); document.location = '/programlist/guide.php'; }
        a.style.cursor = 'pointer';
    }

}

function get_search_callback_func(id_name){
	var callback_func = init_defain;

	if(id_name=='pl_program'){
		callback_func = function(){
			common_tmpl_pref();
			program_tmpl_day();
            selectbcmethod();
			init_defain();
		}
	}else if(id_name=='pl_program_areaset'){
		callback_func = function(){
			common_tmpl_pref();
            selectbutton();
			areaset_tmpl_select_pref();
			init_defain();
		}
	}else if(id_name=='pl_genre'){
		callback_func = function(){
			common_tmpl_pref();
			init_defain();
		}
	}else if(id_name=='pl_genre_areaset'){
		callback_func = function(){
			common_tmpl_pref();
			areaset_tmpl_select_pref();
			init_defain();
		}
	}else if(id_name=='pl_keyword'){
		callback_func = function(){
			common_tmpl_pref();
			init_defain();
		}
	}else if(id_name=='pl_keyword_areaset'){
		callback_func = function(){
			common_tmpl_pref();
			areaset_tmpl_select_pref();
			init_defain();
		}
	}else if(id_name=='pl_star'){
		callback_func = function(){
			common_tmpl_pref();
			init_defain();
		}
	}else if(id_name=='pl_star_areaset'){
		callback_func = function(){
			common_tmpl_pref();
			areaset_tmpl_select_pref();
			init_defain();
		}
	}

	return callback_func;
}

var search_action_keyword = '';

function search_action(url){
	if(!url){
		url = make_post_data('search_action_form');
	}

	if(document.URL.match(/\/search\.html/)){
		disp_search_list(url);
	}else{
		location.href = '/search.html?'+url;
	}
}

function setPref(pref){
	if( !window.opener || window.opener.closed ){
		window.open( "/","newwtv" );
		set_area_cookie( pref );
	} else {
		tv_select_pref( pref );
	}
	window.close();
}

function setUrl(url){
	if( !window.opener || window.opener.closed ){
		window.open( url,"newwtv" );
	} else {
		window.opener.location.href=url;
	}
	window.close();
}

var global_focus_object_id = '';

function site_search_action(){
	var obj = document.getElementById('site_search_input_textbox');
	if(obj){
		var url = ('http://www.google.co.jp/search?'
			+'hl=ja&q=site%3Atelevision.co.jp+'
			+encodeURIComponent(obj.value)+'&safe=off'
		);
		window.open(url,'_blank');
	}
}

function blog_search_action(){
	var robj = document.getElementById('blog_search_input_radio');
	var title = '';
	if(robj){
		if(robj.checked){
			title = 'title';
		}
	}

	var obj = document.getElementById('blog_search_input_textbox');
	if(obj){
		var url = ('http://www.google.co.jp/search?'
			+'hl=ja&q=site%3Ablog.television.co.jp+'
			+encodeURIComponent(obj.value)+'&safe=off&as_occt='+title
		);
		window.open(url,'_blank');
	}
}

function tv_seatch_init(){
	var set_onfocus = function(id_name){
		var obj = document.getElementById(id_name);
		if(obj){
			obj.onfocus = function(){
				global_focus_object_id = id_name;
			}
			obj.form.onsubmit = function(){return false};
		}
	}
	set_onfocus('site_search_input_textbox');
	set_onfocus('blog_search_input_textbox');
	set_onfocus('input_search_keyword');
	set_onfocus('input_search_kperson');

	var set_onblur = function(id_name){
		var obj = document.getElementById(id_name);
		if(obj){
			obj.onblur = function(){
				if(obj.value){
					search_action_keyword = obj.value;
				}
			}
		}
	}
	set_onblur('input_search_keyword');
	set_onblur('input_search_kperson');

	document.onkeydown = function(evt){
		var key_code;
		if(document.all){
			key_code = event.keyCode;
		}else{
			key_code = evt.keyCode;
		}
		if(key_code==13){
			if(global_focus_object_id=='site_search_input_textbox'){
				site_search_action();
			}else if(global_focus_object_id=='blog_search_input_textbox'){
				blog_search_action();
			}else if(global_focus_object_id=='input_search_keyword'){
				submitSearch(document.getElementById('input_search_keyword').value, 'free');
			}else if(global_focus_object_id=='input_search_kperson'){
				submitSearch(document.getElementById('input_search_kperson').value, 'person');
			}
		}
	}
}

var switch_tv_buff1 = new Object();
var switch_tv_buff2 = new Object();

function switch_tv(id_name,path){
	var n = '';
	if(path.match(/_bs$/)){
		n = 1;
	}
	if(switch_tv_buff1[id_name]){
		if(n){
			disp(id_name,switch_tv_buff1[id_name]);
		}else{
			disp(id_name,switch_tv_buff2[id_name]);
		}
	}else{
		switch_tv_buff2[id_name] = inp(id_name);
		get_request(id_name,path+'/'+tv_pref_area()+'.html',
			function(){
				switch_tv_buff1[id_name] = inp(id_name);
			}
		);
	}
}

function init_defain(){
	init_station();
	tv_seatch_init();

	var set_keyword = function(id_name){
		var obj = document.getElementById(id_name);
		if(obj){
			if(id_name!='blog_search_input_textbox'){
				if(!obj.value){
					obj.value = search_action_keyword;
				}else{
					search_action_keyword = obj.value;
				}
			}
			obj.focus();
		}
	}
	set_keyword('blog_search_input_textbox');
	set_keyword('input_search_keyword');
	set_keyword('input_search_kperson');

    selectbutton();
}

function createTypeCookie(value,days) {
    var date = new Date();
    date.setTime(date.getTime()+(60*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = "type="+value+expires+"; path=/";
}
