﻿function UrlTest(path)
{
	var connection = null;
	try
	{
		connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			connection = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				connection = new window.XMLHttpRequest();
			}
			catch(e)
			{
					
			}
		}
	}
		
	if(connection != null)
	{
		connection.open("GET", path, false);
		connection.send(null);
		return connection.status != 404;
	}
}

function GenerateList()
{
	if(location.href.indexOf("eispage") == -1)
		return;
	
	//先清空
	$("#eispageSelector").empty();
		
	var testPath = (location.href.indexOf("vol") != -1) ? "../vol" : "vol";
	
	//1998.2為基數，是11期，先取得目前的期數
	var thisYear = (new Date()).getFullYear();
	var thisMonth = (new Date()).getMonth();
	var volCurrent = 11 + (thisYear - 1998) * 4 + Math.floor(thisMonth / 3);
	var foundMaxVol = false;
	var maxVol = 0;

	do
	{
		if(UrlTest(testPath + volCurrent))
		{
			if(!foundMaxVol)
			{
				foundMaxVol = true;
				maxVol = volCurrent;
				
				$("#eispageSelector").append($("<option></option>").val("../eispage.htm").html(volCurrent + "期"));
			}
			else
			{
				var optionName = volCurrent + "期";
				
				$("#eispageSelector").append($("<option></option>").val(testPath + volCurrent + "/eispage" + volCurrent + ".htm").html(optionName));
			}
		}
	}
	while(volCurrent-- >= 11)
	
	//1998.2為基數，是11期
	/*
	var vol = 11;
	var maxVol = 0;
	for(var year = 1998; year <= thisYear; year++)
		for(var month = 2; month <= 11; month += 3)
		{
			//最新的一期連結固定為../eispage.htm
			if(year == (new Date()).getFullYear() && month >= (new Date()).getMonth())
			{
				var optionName = vol + "期";
				$("#eispageSelector").append($("<option></option>").val("../eispage.htm").html(optionName));
				maxVol = vol;
				
				break;
			}

				if(UrlTest(testPath + vol + "/eispage" + vol + ".htm"))
				{
					var optionName = vol + "期";
					$("#eispageSelector").append($("<option></option>").val(testPath + vol + "/eispage" + vol + ".htm").html(optionName));
					maxVol = vol;
				}
			
			//if(year == (new Date()).getFullYear() && month > (new Date()).getMonth())
				//break;
				
			vol++;
		}
  */
  
		/*
		var queryStrings;
		var queryString;
    if(location.search.length > 1)
    {
    	var queryStrings = location.search.substr(1, location.search.length - 1).split('&');
    	var queryString = new Array();
    	for(var i = 0; i < queryStrings.length; i++)
    		queryString[queryStrings[i].split('=')[0]] = queryStrings[i].split('=')[1];
    		
		  $("#eispageSelector").val(queryString["vol"]);
    }
    */
    
    var path = location.pathname;
    if(path.indexOf("vol") != -1)
    {
    	var start = path.indexOf("vol") + 3;
    	var end = path.indexOf("/", start);
    	var myVol = path.substr(start, end - start);
    	
    	if(myVol == maxVol.toString())
    		$("#eispageSelector").val("../eispage.htm");
    	else
    		$("#eispageSelector").val("../vol" + myVol + "/eispage" + myVol + ".htm");

      //if(document.getElementById("HeadVolume") != null)
      	//document.getElementById('HeadVolume').innerHTML = 'ooo';
  	}
  	else
  	{
  		$("#eispageSelector").val("../eispage.htm");
  	}
    
    //如果是最新一期，目錄的連結不同
		//TODO: jquery
		//if(year == (new Date()).getFullYear() && month >= (new Date()).getMonth())
		if(document.getElementById('returnToIndex') != null)
		{
			if(parseInt(myVol, 10) == maxVol)
				document.getElementById('returnToIndex').href = "../eispage.htm";
			else
				document.getElementById('returnToIndex').href = "./eispage" + myVol + ".htm";
		}
}

function eisOnChanged(vol)
{
	//window.open("../vol" + vol + "/eispage" + vol + ".htm", "_self", "", "");
	/*var path = location.pathname;
    if(path.indexOf("vol") != -1)
    {
    	var start = path.indexOf("vol") + 3;
    	var end = path.indexOf("/", start);
    	var myVol = path.substr(start, end - start);
    	
    	if(myVol == maxVol.toString())
    		window.open("../eispage.htm", "_self", "", "");
      else
    		window.open("../vol" + myVol + "/eispage" + myVol + ".htm", "_self", "", "");

  	}
  	else
  	{
  		window.open("../eispage.htm", "_self", "", "");
  	}
  	*/
	window.open(vol, "_self", "", "");
}