////////////////////////////////// Google Analytics ////////////////////////////////////////
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-21402070-4']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
////////////////////////////////// Google Analytics ////////////////////////////////////////

function findLines(prefecture_id) {
	if (prefecture_id > 0) {
		var ajax = new Ajax.Updater(
			'line_select',
			'/stations/ajax_line_select',
			{
				method: 'post',
				parameters: 'prefecture_id=' + prefecture_id
			}
		);
	} else {
		$('line_select').innerHTML = '';
	}
}

function findStations(line_cd) {
	if (line_cd != '') {
		var ajax = new Ajax.Updater(
			'station_select',
			'/stations/ajax_select',
			{
				method: 'post',
				parameters: 'line_cd=' + line_cd
			}
		);
	} else {
		$('station_select').innerHTML = '';
	}
}

function findCities(prefecture_id) {
	if (prefecture_id > 0) {
		var ajax = new Ajax.Updater(
			'city_select',
			'/cities/ajax_select',
			{
				method: 'post',
				parameters: 'prefecture_id=' + prefecture_id
			}
		);
	} else {
		$('city_select').innerHTML = '';
	}
}

function checkAllCategory(checked) {
	var elements = document.getElementsByName('categories[]');
	for (var i = 0; i < elements.length; i++) {
		elements[i].checked = checked;
	}
}

function checkSearchCondition() {
	if (document.getElementById('search_prefecture').selectedIndex == 0) {
		alert('都道府県を選択してください。');
		return false;
	}
	if (document.search_form.city_id == undefined || document.search_form.city_id.selectedIndex == 0) {
		alert('市区郡を選択してください。');
		return false;
	}
	var selectCategories = document.search_form['categories[]'];
	for (var i = 0; i < selectCategories.length; i++) {
		if (selectCategories[i].checked == true) {
			return true;
		}
	}
	alert('診療科目を選択してください。');
	return false;
}

function addBookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
}

function crawlAllSites(hospitalId, siteIds) {
	if (siteIds.length == 0) return;
	var row = siteIds.shift();
	var siteInfo = row.split(':');
	var category = siteInfo[0];
	var siteId = siteInfo[1];
	new Ajax.Request(
		'/hospitals_sites/ajax_crawl/' + hospitalId + '/' + siteId,
		{
			method: 'post',
			parameters: 'save_closed=' + (siteIds.length == 0 ? 1 : 0),
			onSuccess: function(request) {
				if (request.responseText != '') {
					$('site' + siteId).innerHTML = request.responseText;
					Element.show(category + '_sites');
					Element.show(category + '_sites_date');
					var element = $(category + '_sites_not_exist');
					if (element != undefined) {
						Element.hide(element);
					}
					siteCount[category]++;
				}
				if (siteIds.length > 0) {
					crawlAllSites(hospitalId, siteIds);
				} else {
					Element.hide('nurse_sites_loading');
					Element.hide('doctor_sites_loading');
					Element.show('nurse_sites_date');
					Element.show('doctor_sites_date');
					/*
					if (siteCount.nurse == 0) {
						Element.show('nurse_sites_not_exist');
					}
					if (siteCount.doctor == 0) {
						Element.show('doctor_sites_not_exist');
					}
					*/
				}
			}
		}
	);
}

function showMailBox() {
	var mailbox = document.getElementById('mailbox');
	var offset = Position.cumulativeOffset($('mailbox_open'));
	mailbox.style.left = offset.left + 'px';
	mailbox.style.top = offset.top + 'px';
	Element.show(mailbox);
}

function sendHospitalUrl(hospitalId) {
	var email = $F('mail_email');
	if (email == '') {
		alert('メールアドレスを入力してください。');
		return;
	}
	new Ajax.Request(
		'/hospitals/mail',
		{
			method: 'post',
			parameters: 'hospital_id=' + hospitalId + '&email=' + encodeURIComponent(email),
			onSuccess: function(request) {
				alert('URLを送信しました。');
				Element.hide(mailbox);
			},
			onFailure: function(request) {
				alert('送信に失敗しました。');
			},
			onException: function(request) {
				alert('送信に失敗しました。');
			}
		});
}

