var RB_TEST = false;
var display = '';
var year = '';
var months = '';
var Press_release_link = '';
var Enquiry = '';
var ProgrammePro = '';
var IssuesPro = '';
var ResultAnnouncement = '';
var SubPrice = '';
var PurchaseConfirm = '';
var NewBond = new Array();
var NewBond2 = new Array();
var spacing = (window.location.href.indexOf('/chi/') >= 0 ? '__': '___');
var ProspectusOnly = false;
var LatestIssue = true;

function setspacing(par1){
    spacing = par1;
}

function addNewIssue(par1,par2){
    year = par1;
    months = par2;
}

function addPressRelease(par1){
    Press_release_link = par1;
}
function addEnquiry(par1){
    Enquiry = par1;
}
function addProgrammePro(par1){
    ProgrammePro = par1;
}
function addIssuesPro(par1){
    IssuesPro = par1;
}
function addResult(par1) {
	ResultAnnouncement = par1;
}
function addSubPrice(par1) {
	SubPrice = par1;
}
function addPConfirmation(par1) {
	PurchaseConfirm = par1;
}
function addNewBond(par1,par2){
    NewBond[NewBond.length] = par1;
    NewBond2[NewBond2.length] = par2;
}
//######################### Latest issues
var lastYear ='';

function RB(root) {
	this.root = root;
	this.first = (LatestIssue == true);
	this.aIssues = new Array(); /* nodes of issues */
	this.subTitle = 'Subscription Period:'; /* subscription title */
	this.subBegin = ''; /* new issue subscription begin date */
	this.subEnd = ''; /* new issue subscription end date */
	this.rbId = -1;

    this.strYear = new Array();
    this.strDisplayYear = this.QueryString();
}

RB.prototype.QueryString = function() {
	var qs = new String(window.location);
    return (qs.indexOf('?') >= 0 ? qs.substring((qs.indexOf('?')) + 1): '');
}

function BondNode(code, remark) {
	this.code = code;
	this.remark = remark;
}

BondNode.prototype.toString = function() {
	var spc = '';
	var sp = '';
	var s = '<tr valign="top">' + "\r\n";
	if (this.code) {
		spc = '';
		sp = ' width="80%"';
		s += '<th nowrap="nowrap" align="left" width="20%">' + '<span style="color:white">' + spacing + '</span>' + this.code + '</th>' + "\r\n";
	} else {
		spc = '<span style="color:white">' + spacing + '</span>';
		sp = 'width="100%" colspan="2"';
	}
	s += '<td align="left"' + sp + '>' + spc + this.remark + '</td>' + "\n" + '</tr>' + "\r\n";
	return s;
}

function IssueNode(root, year, months, prlink, resultlink, subscription_price, Programme_prospectus, Issue_prospectus, Purchase_confirmation) {
	this.root = root;
	if (prlink != '') prlink = root + prlink;
	if (resultlink != '') resultlink = root + resultlink;
	if (subscription_price != '') subscription_price = root + subscription_price;
	if (Programme_prospectus != '') Programme_prospectus = root + Programme_prospectus;
	if (Issue_prospectus != '') Issue_prospectus = root + Issue_prospectus;
	if (Purchase_confirmation != '') Purchase_confirmation = root + Purchase_confirmation;
	this.year = year;
	this.months = months;
	this.aBonds = new Array();
    this.Programme_prospectus = Programme_prospectus;
    this.Issue_prospectus = Issue_prospectus;
	this.Purchase_confirmation = Purchase_confirmation;
	this.subscription_price = subscription_price;
	this.resultlink = resultlink;
	this.prlink = prlink;
}

/*
IssueNode.prototype.setTitle = function(prtitle, prostitle, resulttitle) {
	this.prtitle = prtitle;
	this.prostitle = prostitle;
	this.resulttitle = resulttitle;
}
*/


IssueNode.prototype.addBond = function(code, remark) {
	this.aBonds[this.aBonds.length] = new BondNode(code, remark);
}

IssueNode.prototype.toLink = function(Path_Img, path, url) {
	if (url == undefined || url == '') return '';
	var target = (url.toString().split('').reverse().join('').substring(0,4).toLowerCase() == 'fdp.' ? '_blank': '');
	if (path != '') url = path + '/' + url;
	if (url.indexOf('disclaimer.html') >= 0) {
		return '<img border="0" src="' + Path_Img + '" style="cursor:hand;" onclick="window.open(' + "'" + url + "','programmepro','height=370px,width=604px,directories=no,scrollbars=yes,resizable=no,status=no,toolbar=no,menubar=no,location=no');" + '" />' + "\n";
	} else {
		return '<a class="arial12" target="' + target + '" href="' + url + '">' + '<img border="0" src="'+ Path_Img + '" /></a>' + "\n";
	}
}

IssueNode.prototype.ProsOnly = function() {
	var r = '';
	r += '<table width="450" border="0" cellspacing="0" cellpadding="0" height="185">';
	r += '<tr>';
	r += '<td>';
	lastYear = this.year;
	r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="106" style="margin-left:15px;margin-top:0px;margin-bottom:15px;">';
	r += '  <tr>';
	r += '    <td height="141">';
var plink = BaseHref + 'ty/publication/disclaimer.html?pr=';
if (this.Programme_prospectus != undefined && this.Programme_prospectus != '') plink += this.Programme_prospectus;
plink += '&is=';
if (this.Issue_prospectus != undefined && this.Issue_prospectus != '') plink += this.Issue_prospectus;
	r += '<table border="0" valign="top" width="100%" cellpadding="0" cellspacing="0">';
	r += '<tr valign="top">';
	r += '<td width="97%" class="arial12" background="' + BaseHref + 'image/common/dots-full.gif"><a href="javascript:;" onclick="window.open(' + "'" + plink + "','','height=370px,width=604px,directories=no,scrollbars=yes,resizable=no,status=no,toolbar=no,menubar=no,location=no');" + '" style="background-color:white;"><span class="arial12boldred">' + this.months + '</span></a></td>';
	r += '<td width="3%"><a href="javascript:;" onclick="window.open(' + "'" + plink + "','','height=370px,width=604px,directories=no,scrollbars=yes,resizable=no,status=no,toolbar=no,menubar=no,location=no');" + '"><img src="' + BaseHref + 'image/common/i_view.gif" border="0" /></a></td>';
	r += '</tr>';
	r += '<tr><td colspan="2">&nbsp;</td></tr>';
	r += '</table>';

	r += '      <table bordercolor="#ffffff" bordercolorlight="#dddddd" bordercolordark="#ffffff" width="100%" border="1" cellspacing="0" cellpadding="0" height="59">';
	r += '        <tr>';
	r += '          <td>';
	r += '            <table width="100%" border="0" cellspacing="5" cellpadding="0">';
	for (var i = 0; i < this.aBonds.length; i++) {
		r += '              <tr>';
		r += '<td>' + this.aBonds[i] + '</td>';
		r += '              </tr>';
	}
	r += '            </table>';
	r += '         </td>';
	r += '        </tr>';
	r += '      </table>';
	r += '    </td>';
	r += '  </tr>';
	r += '</table>';
	r += '</td>';
	r += '</tr>';
	r += '</table>';
	return r;
}

IssueNode.prototype.toString = function() {
	var r = '';
	r += '<table width="450" border="0" cellspacing="0" cellpadding="0" height="185">';
	r += '<tr>';
	/*
	if(lastYear==this.year){
		r += '<td > <span style="font-size:16px;font-weight:bold;color:#000000">'+'</span>';
	}else{
		r += '<td > <span style="font-size:16px;font-weight:bold;color:#000000">'+this.year+'</span>';
	}
	*/
	r += '<td>';
	lastYear = this.year;
	r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="106" style="margin-left:15px;margin-top:0px;margin-bottom:15px;">';
	r += '  <tr>';
	//r += '    <td height="141"><span class="arial12boldred">' + this.year + ' ' + this.months + '</span>';
	r += '    <td height="141"><span class="arial12boldred">' + this.months + '</span>';
	r += '      <table bordercolor="#ffffff" bordercolorlight="#dddddd" bordercolordark="#ffffff" width="100%" border="1" cellspacing="0" cellpadding="0" height="59">';
	r += '        <tr>';
	r += '          <td>';
	r += '            <table width="100%" border="0" cellspacing="5" cellpadding="0">';
	r += '        <tr>';
	r += '          <td align="center" colspan="2" height="45">'+this.toLink(BaseHref+'image/section/btn_press_release_139.gif', '', this.prlink)+this.toLink(BaseHref+'image/section/btn_result_announcement_139.gif', '', this.resultlink)+this.toLink(BaseHref+'image/section/btn_subscription_price_139.gif', '', this.subscription_price)+'</td>';
	r += '        </tr>';
	for (var i = 0; i < this.aBonds.length; i++) {
		r += '              <tr>';
		r += '<td>' + this.aBonds[i] + '</td>';
		r += '              </tr>';
	}

	var category = (window.location.href.indexOf('retail-bond') == -1 ? 'retail-mbs': 'retail-bond');
	if (this.Programme_prospectus == undefined || this.Programme_prospectus == '') {
		this.Programme_prospectus = '';
	} else {

		// this.Programme_prospectus = BaseHref + 'ty/publication/disclaimer.html?pr=' + this.Programme_prospectus;
		this.Programme_prospectus = BaseHref + 'ty/publication/' + category + '/prospectus.html?' + this.year;
	}
	if (this.Issue_prospectus == undefined || this.Issue_prospectus == '') {
		this.Issue_prospectus = '';
	} else {
		// this.Issue_prospectus = BaseHref + 'ty/publication/disclaimer.html?is=' + this.Issue_prospectus;
		this.Issue_prospectus = BaseHref + 'ty/publication/' + category + '/prospectus.html?' + this.year;
	}
	if (this.Purchase_confirmation == '' || ProspectusOnly) {
		r += '          <td align="center" colspan="2" height="45">'+this.toLink(BaseHref+'image/section/btn_programme_prospectus_208.gif', '', this.Programme_prospectus)+this.toLink(BaseHref+'image/section/btn_issue_prospectus_208.gif', '', this.Issue_prospectus)+'</td>';
	} else {
		r += '          <td align="center" colspan="2" height="45">'+this.toLink(BaseHref+'image/section/btn_programme_prospectus_139.gif', '', this.Programme_prospectus)+this.toLink(BaseHref+'image/section/btn_issue_prospectus_139.gif', '', this.Issue_prospectus)+this.toLink(BaseHref+'image/section/btn_purchaser_confirmation_139.gif', '', this.Purchase_confirmation)+'</td>';
	}
	r += '              </tr>';
	r += '            </table>';
	r += '         </td>';
	r += '        </tr>';
	r += '      </table>';
	r += '    </td>';
	r += '  </tr>';
	r += '</table>';
	r += '</td>';
	r += '</tr>';
	r += '</table>';
	return r;
}

/*
RB.prototype.setSubscriptionTitle = function(str) {
	this.subTitle = str;
}

RB.prototype.setSubscriptionPeriod = function(begindate, enddate) {
	this.subBegin = begindate;
	this.subEnd = enddate;
}
*/


// add issue, return an id to add the list of bonds
RB.prototype.addIssue = function(year, months, par1, par2, par3, par4, par5, par6) {
	if (this.first && ProgrammePro == '') {
		addNewIssue(year, months);
		if (par1 != '') par1 = this.root + par1;
		if (par2 != '') par2 = this.root + par2;
		if (par3 != '') par3 = this.root + par3;
		if (par4 != '') par4 = this.root + par4;
		if (par5 != '') par5 = this.root + par5;
		if (par6 != '') par6 = this.root + par6;
		addPressRelease(par1);
		addProgrammePro(par4);
		addIssuesPro(par5);
		addResult(par2);
		addSubPrice(par3);
		addPConfirmation(par6);
		return this.rbId;
	} else {
		this.first = false;
	    //set default year.
		if (this.strDisplayYear == '') this.strDisplayYear = year;
		//store years for select.
		this.strYear[this.strYear.length] = year;
		if (year == this.strDisplayYear || RB_TEST) {
			var id = this.aIssues.length;
			this.aIssues[id] = new IssueNode(this.root, year, months, par1, par2, par3, par4, par5, par6);
			this.rbId = id;
			return id;
		}
	}
}

RB.prototype.setIssueTitle = function(prtitle, prostitle, resulttitle) {
	this.aIssues[this.aIssues.length - 1].setTitle(prtitle, prostitle, resulttitle);
}

// add a bond to an issue
RB.prototype.addBond = function(year, code, remark) {
	if (this.first) {
		addNewBond(code, remark);
	} else if (year == this.strDisplayYear || RB_TEST) {
		this.aIssues[this.rbId].addBond(code, remark);
	}
}

RB.prototype.displayNewIssue = function() {
	if (year == '') {
		var l = window.location.href;
		if (l.indexOf('/chi/') != -1) {
			document.writeln('<p>沒有最新發行</p>');
		} else {
			document.writeln('<p>No new offering currently</p>');
		}
		return;
	}

	var start = 0;
    var r = '';
    r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="185">';
    r += '<tr valign="top"><td>';
    r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="106" style="margin-left:15px;margin-top:0px;margin-bottom:0px;">';
    r += '  <tr>';
    r += '    <td height="141"><span class="arial12boldred">' + year + ' ' + months + '</span>';
    r += '      <table bordercolor="#ffffff" bordercolorlight="#dddddd" bordercolordark="#ffffff" width="100%" border="1" cellspacing="0" cellpadding="0" height="59">';
    r += '        <tr>';
    r += '          <td>';
    r += '            <table width="100%" border="0" cellspacing="5" cellpadding="0">';
    if (Enquiry != '' || Press_release_link != '') {
    	r += '          <tr><td align="center" colspan="2" height="45">'
    	if (Press_release_link != '') r += '<a href="' + Press_release_link + '" target="_blank"><img border="0" src="' + BaseHref + 'image/section/btn_press_release_208.gif" /></a>';
    	if (Enquiry != '') r += '<a target="_blank" href="' + Enquiry + '"><img border="0" src="' + BaseHref + 'image/section/btn_enquiry_hotlines_208.gif" /></a>';
    	r += '</td></tr>';
	}
    		for (var i = 0; i < NewBond.length; i++) {
    r += '              <tr>';
    			 r += '<td width=20%><b><span style="color:white">' + spacing + '</span>' + NewBond[i] + '</b></td>';
    			 r += '<td>' + NewBond2[i] + '</td>';
    r += '              </tr>';
    		};
    r += '          <tr><td align="center" colspan="2" height="45">';
	var category = (window.location.href.indexOf('retail-bond') == -1 ? 'retail-mbs': 'retail-bond');
	r += '<a href="' + BaseHref + 'ty/publication/' + category + '/prospectus.html"><img border="0" src="' + BaseHref + 'image/section/btn_programme_prospectus_208.gif"></a>';
	r += '<a href="' + BaseHref + 'ty/publication/' + category + '/prospectus.html"><img border="0" src="' + BaseHref + 'image/section/btn_issue_prospectus_208.gif"></a>';
var l = window.location.href;
if (l.indexOf('/chi/') != -1) {
    r += '   <p>';
    r += '本廣告不是債券的要約，也不是誘使提出獲取債券的要約的邀請。日期為二零零八年六月三日的債券的發行章程以及日期為二零零八年六月三日的發行計劃章程載有進一步關於香港按揭證券有限公司（“按揭證券公司”）、債券以及發行債券所依據的發行計劃的重要資料：閣下在作出是否投資的決定前務必細讀前述文件。本廣告由按揭證券公司根據『公司條例』第s38B(2)(e)款而發佈。';
    r += '   </p>';
} else {
    r += '   <p>';
    r += '   This is not an offer of Notes or an invitation to induce an offer to';
    r += '   acquire Notes. The issue prospectus dated 3 June 2008 for the Notes';
    r += '   and the programme prospectus dated 3 June 2008 contain further';
    r += '   important information about The Hong Kong Mortgage Corporation Limited';
    r += '   (the "HKMC"), the Notes and the Programme under which the Notes are';
    r += '   issued: you must read them before deciding whether to invest. This';
    r += '   advertisement is issued by the HKMC under s38B(2)(e)of the Companies';
    r += '   Ordinance.';
    r += '   </p>';
}
    r += '</td>';
    r += '              </tr>';
    r += '            </table>';
    r += '         </td>';
    r += '        </tr>';
    r += '      </table>';
    r += '    </td>';
    r += '  </tr>';
    r += '</table>';
    r += '    </td>';
    r += '  </tr>';
    r += '</table>';
	document.writeln(r);
}

RB.prototype.displayNewIssueProsOnly = function() {
	if (year == '') {
		var l = window.location.href;
		if (l.indexOf('/chi/') != -1) {
			document.writeln('<p>沒有最新發行</p>');
		} else {
			document.writeln('<p>No new offering currently</p>');
		}
		return;
	}

	var start = 0;
    var r = '';
    r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="185">';
    r += '<tr valign="top"><td>';
    r += '<table width="480" border="0" cellspacing="0" cellpadding="0" height="106" style="margin-left:15px;margin-top:0px;margin-bottom:0px;">';
    r += '  <tr>';
    r += '    <td height="141">';
var plink = BaseHref + 'ty/publication/disclaimer.html?pr=' + ProgrammePro + '&is=' + IssuesPro;
	r += '<table border="0" valign="top" width="100%" cellpadding="0" cellspacing="0">';
	r += '<tr>';
	r += '<td valign="top" colspan="2">&nbsp;</td>';
	r += '</tr>';
	r += '<tr valign="top">';
	r += '<td width="97%" class="arial12" background="' + BaseHref + 'image/common/dots-full.gif"><a href="javascript:;" onclick="window.open(' + "'" + plink + "','','height=370px,width=604px,directories=no,scrollbars=yes,resizable=no,status=no,toolbar=no,menubar=no,location=no');" + '" style="background-color:white;"><span class="arial12boldred">' + year + ' ' + months + '</span></a></td>';
	r += '<td width="3%"><a href="javascript:;" onclick="window.open(' + "'" + plink + "','','height=370px,width=604px,directories=no,scrollbars=yes,resizable=no,status=no,toolbar=no,menubar=no,location=no');" + '"><img src="' + BaseHref + 'image/common/i_view.gif" border="0" /></a></td>';
	r += '</tr>';
	r += '<tr>';
	r += '<td valign="top" colspan="2">&nbsp;</td>';
	r += '</tr>';
	r += '</table>';

    r += '      <table bordercolor="#ffffff" bordercolorlight="#dddddd" bordercolordark="#ffffff" width="100%" border="1" cellspacing="0" cellpadding="5" height="59">';
    r += '        <tr>';
    r += '          <td>';
    r += '            <table width="100%" border="0" cellspacing="5" cellpadding="0">';
    		for (var i = 0; i < NewBond.length; i++) {
    r += '              <tr>';
    			 r += '<td><b><span style="color:white">' + spacing + '</span>' + NewBond[i] + '</b></td>';
    			 r += '<td>' + NewBond2[i] + '</td>';
    r += '              </tr>';
    		};
/*
    r += '          <tr><td align="center" colspan="2" height="45">';
    var plink = BaseHref + 'ty/publication/disclaimer.html?pr=' + ProgrammePro + '&is=' + IssuesPro;
    r += "<img border=\"0\" src=\"" + BaseHref + "image/section/btn_programme_prospectus_208.gif\" onclick=\"window.open('" + plink + "','programmepro','');\" style=\"cursor:hand;\" />";
    r += "<img border=\"0\" src=\"" + BaseHref + "image/section/btn_issue_prospectus_208.gif\" onclick=\"window.open('" + plink + "','programmepro','');\" style=\"cursor:hand;\" />";
    r += '</td>';
    r += '              </tr>';
*/
    r += '            </table>';
var l = window.location.href;
if (l.indexOf('/chi/') != -1) {
    r += '   <p>';
    r += '本廣告不是債券的要約，也不是誘使提出獲取債券的要約的邀請。日期為二零零八年六月三日的債券的發行章程以及日期為二零零八年六月三日的發行計劃章程載有進一步關於香港按揭證券有限公司（“按揭證券公司”）、債券以及發行債券所依據的發行計劃的重要資料：閣下在作出是否投資的決定前務必細讀前述文件。本廣告由按揭證券公司根據『公司條例』第s38B(2)(e)款而發佈。';
    r += '   </p>';
} else {
    r += '   <p>';
    r += '   This is not an offer of Notes or an invitation to induce an offer to';
    r += '   acquire Notes. The issue prospectus dated 3 June 2008 for the Notes';
    r += '   and the programme prospectus dated 3 June 2008 contain further';
    r += '   important information about The Hong Kong Mortgage Corporation Limited';
    r += '   (the "HKMC"), the Notes and the Programme under which the Notes are';
    r += '   issued: you must read them before deciding whether to invest. This';
    r += '   advertisement is issued by the HKMC under s38B(2)(e)of the Companies';
    r += '   Ordinance.';
    r += '   </p>';
}
    r += '         </td>';
    r += '        </tr>';
    r += '      </table>';
    r += '    </td>';
    r += '  </tr>';
    r += '</table>';
    r += '    </td>';
    r += '  </tr>';
    r += '</table>';
	document.writeln(r);
}

RB.prototype.displayOldIssue = function(prev_str) {
	if (ProspectusOnly) {
		this.displayOldIssueProsOnly(prev_str);
	} else {
		this.displayNewIssue();
    	if (this.aIssues.length == 0) return;
		var start = 0; //;= ((this.subBegin == '' && this.subEnd != '') || (this.subBegin != '' && this.subEnd == '') ? 0: 1);
    	if (this.aIssues.length > start) {
			document.writeln('<br><br><br><p class="arial14bold">' + prev_str + '</p>');
			year = this.strDisplayYear;
			document.write('<p class="arial12bold">');
			this.displayOldIssueYears('', '');
			document.writeln('</p>');
			for (var i = start; i < this.aIssues.length; i++) document.writeln(this.aIssues[i]);
		}
	}
}

RB.prototype.displayOldIssueYears = function(header, url) {
	var s = '';
	var lastYear = '';
	for (var i = 0; i < this.strYear.length; i++) {
		if (lastYear == this.strYear[i]) continue;
		if (s != '') s += ' | ';
		if (s == '') s = header + ' ';
		if (year == this.strYear[i]) {
			s += '<span style="color:#8b2942;font-size:24px;font-weight:bold;">' + this.strYear[i] + '</span>';
		} else {
			s += '<a style="color:#8b2942;font-weight:normal;" href="' + url + '?' + this.strYear[i] + '">' + this.strYear[i] + '</a>';
		}
		lastYear = this.strYear[i];
	}
	document.write(s);
}

RB.prototype.displayOldIssueProsOnly = function(prev_str) {
	this.displayNewIssueProsOnly();
    if (this.aIssues.length == 0) return;
	var start = 0; //;= ((this.subBegin == '' && this.subEnd != '') || (this.subBegin != '' && this.subEnd == '') ? 0: 1);
    if (this.aIssues.length > start) {
		document.writeln('<br><br><br><p class="arial14bold">' + prev_str + '</p>');
		year = this.strDisplayYear;
		document.write('<p class="arial12bold">');
		this.displayOldIssueYears('', '');
		document.writeln('</p>');
		for (var i = start; i < this.aIssues.length; i++) document.writeln(this.aIssues[i].ProsOnly());
	}
}

/*
RB.prototype.displayMenu = function() {
	document.writeln('<form><select name="s" onchange="location=this.options[this.selectedIndex].value;">');
	for (var i = 0; i < this.aIssues.length; i++) {
		document.writeln('<option value="#rb_' + i + '">' + this.aIssues[i].title + '</option>');
	}
	document.writeln('</select></form>');
}
*/

