﻿
	var prefixURL = "http://www.cathaypacific.com/cpa/JSPs/checkURLExistence.jsp?url="
	var downloadURL = "";//"http://downloads.cathaypacific.com/cx/traffic/";
	var fileName = "";//"_cxtraffic_";
	var pdf = ".pdf";
	
	var recentDocYear = 0;
	var recentDocMonth = 0;
	var startYear = 2005;
	var lang = "en";
	
	var txt_download = "View";
	var txt_year = "Year";
	var txt_month = "Month";
	
	var selectedYear = "";
	var optionYearList = "";
	var optionMonthList = "";
	var d = new Date();
	
	var month=new Array(12);
	month[0]="Jan";
	month[1]="Feb";
	month[2]="Mar";
	month[3]="Apr";
	month[4]="May";
	month[5]="Jun";
	month[6]="Jul";
	month[7]="Aug";
	month[8]="Sep";
	month[9]="Oct";
	month[10]="Nov";
	month[11]="Dec";
	
	function fn_changeMonthLanguage(){
		month[0]="一月";
		month[1]="二月";
		month[2]="三月";
		month[3]="四月";
		month[4]="五月";
		month[5]="六月";
		month[6]="七月";
		month[7]="八月";
		month[8]="九月";
		month[9]="十月";
		month[10]="十一月";
		month[11]="十二月";
	}
	
	function fn_checkLanguage(){
		if(lang == "zh"){
			txt_download = "閱覽";
			txt_year = "年份";
			txt_month = "月份";
			fn_changeMonthLanguage();
		}else if(lang == "sc"){
			txt_download = "阅览";
			txt_year = "年份";
			txt_month = "月份";
			fn_changeMonthLanguage();
		}
		
	}
	
	function fn_getPDF(){
	
		var year = document.getElementById("yearList").value;
		var month = document.getElementById("monthList").value;
		
		var form = document.getElementById("investorRelation");
		var docURL = document.getElementById("docURL");
		var url = "";
		
		month++;
		
		url += downloadURL;
		url += year;
		
		if(month < 10){
			url += "0";
		}
		
		url += month ;
		url += fileName;
		url += lang;
		url += pdf;
	
		docURL.value = url;
		form.action = prefixURL;
		form.target = this;
		form.method = "post";
		/* SPIYGAB 20101122 skip the check file existence JSP if file exension is .pdf */
		if(url.split('.pdf').length>1){
			window.open(url);
			return false;
		}else{
			form.submit();
		}
		//window.open(url, '', 'toolbar=0,menubar=0,scrollbars=1,resizable=1');
		
	}
	
	function fn_updateMonthList(){
	
		var monthRange = 12;
		var updateMonthList = document.getElementById("monthList");
		
		selectedYear = document.getElementById("yearList").value;
		
		if(selectedYear == recentDocYear){
			monthRange = recentDocMonth;
		}
		
		updateMonthList.options.length = 0;
		
		for(var y = 0 ; y < monthRange; y++){
			updateMonthList.options[y] = new Option(month[y],y);
		}
		
	}
	
	function fn_getYearList(){
	
		optionYearList = "";
		optionYearList += "<span ><select id=\"yearList\" onChange=\"fn_updateMonthList()\"></span>";
		for(var i = recentDocYear; i >= startYear; i--){
			optionYearList +=("<option value=\""+ i+"\">" + i+"</option>");
		}
		optionYearList +=("</select>");
		
		document.write(optionYearList);
	}
	
	function fn_getMonthList(){
		
		optionMonthList = "";
		optionMonthList += "<select id=\"monthList\">";
		optionMonthList +=("</select>");
		
		document.write(optionMonthList);
		fn_updateMonthList();
	}
	
	function fn_getSearchButton(){
		var buttonString = "";
		
		buttonString += "<INPUT TYPE=BUTTON OnClick=\"fn_getPDF();\" VALUE=\"" + txt_download+ "\">";
		document.write(buttonString);
	}
	
	
	function fn_setLatestDoc(lastDocYear, lastDocMonth, language, input_documentURL, input_fileName){

		if(language != null){
			lang = language;
		}
		fn_checkLanguage();	
		
		if(lastDocYear == null){
			lastDocYear = d.getFullYear();
		}
		if(lastDocMonth == null){
			lastDocMonth = 12;
		}
		if(lastDocMonth > 12){
			lastDocMonth = 12;
		}
		if(input_documentURL != null){
			downloadURL = input_documentURL;
		}
		if(input_fileName != null){
			fileName = input_fileName;
		}
		recentDocYear = lastDocYear;
		recentDocMonth = lastDocMonth;
		
		//document.write("<TABLE width=\"10%\" cellspacing=\"0\" border=\"0\" cellpadding=\"0\"");
		document.write("<INPUT TYPE=\"HIDDEN\" NAME=\"docURL\" ID=\"docURL\">");
		document.write("<TABLE id=\"aboutUsInvestorRelation\">");
		document.write("<TR><TD>" + txt_year +"</TD><TD>"+ txt_month +"</TD><TD></TD></TR>");
		document.write("<TR>");
		
		document.write("<TD>");
		fn_getYearList();
		document.write("</TD>");
		
		document.write("<TD>");
		fn_getMonthList();
		document.write("</TD>");
		
		document.write("<TD>");
		fn_getSearchButton();
		document.write("</TD>");
		
		document.write("</TR>");
		document.write("</TABLE>");
	}
	
	
