// リファラにこの文字列があるときにクッキーに格納する
CHECK_POINT_URI = new Array('/search/search.aspx', '/list/pref');
COOKIE_KEY_REFERRER = 'referrer';
var backlink_obj;

function main() {
	SetBackLinkObject();
	if (! backlink_obj) {
		return;
	}
	SetRerefer2Cookie();
	SetBackUri();
}

// 変更したいオブジェクトを取得する。
function SetBackLinkObject() {
	if (document.getElementById) {
		backlink_obj = document.getElementById('backlink');
	} else if (document.all) {
		backlink_obj = document.all('backlink');
	} else if (document.layers) {
		backlink_obj = document.layers['backlink'];
	}
}

function SetBackUri() {
	uri = GetCookie(COOKIE_KEY_REFERRER);
	if (uri) {
		 backlink_obj.href = uri;
	}
}

// リファラ文字列がマッチしたときにクッキーにセットする
function SetRerefer2Cookie() {
	for (i in CHECK_POINT_URI) {
    	if (document.referrer.indexOf(CHECK_POINT_URI[i]) != -1) {
    		SetCookie(COOKIE_KEY_REFERRER, document.referrer);
    		break;
    	}
	}
}

function SetCookie(key, value){
	document.cookie = key + "=" + escape(value);
}

// Get data from the cookie.
function GetCookie(Key) {
  var sValue = "";
  var s = document.cookie + ";";

  var iKeyHeadIndex = s.indexOf(Key + "=", 0);
  if (iKeyHeadIndex == -1){
    return sValue;
    exit;
  }

  s = s.substring(iKeyHeadIndex, s.length);

  var iValHeadIndex = s.indexOf("=", 0);
  var iValTailIndex = s.indexOf(";", 0);
  if (iValTailIndex <= iValHeadIndex){
    return sValue;
    exit;
  }

  var iValHeadIndex = s.indexOf("=", 0) + 1;
  var iValTailIndex = s.indexOf(";", iValHeadIndex);
  sValue = s.substring(iValHeadIndex, iValTailIndex);

  return unescape(sValue);
}


function openWindow1() {
  var option_str = get_open_option(500, 400);
  window.open("/st.htm","window1", option_str);
}

function openWindow2() {
  var option_str = get_open_option(500, 360);
  window.open("/ck.htm","window2", option_str);
}

function get_open_option(width, height) {
  var window_height = document.body.clientHeight;
  var window_width = document.body.clientWidth ;
  var default_opiton = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes';

  wintop = (window_height - height) / 2;
  winleft = (window_width - width) / 2;

  var option_str = default_opiton + ',' + 'width=' + width +
                   ', height=' + height + ', left=' + winleft + ', top=' + wintop;
  return option_str;
}

