///month list
var monthname = new Array;
monthname[1] = "January";
monthname[2] = "February";
monthname[3] = "March";
monthname[4] = "April";
monthname[5] = "May";
monthname[6] = "June";
monthname[7] = "July";
monthname[8] = "August";
monthname[9] = "September";
monthname[10] = "October";
monthname[11] = "November";
monthname[12] = "December";



function checkvaliddate(chkyear, chkmth, chkday) {
	var chkdate = chkyear + "" + chkmth  + "" + chkday;

	var validdate = new Date(chkyear, chkmth - 1, chkday ,0 ,0 ,0 ,0);
	var validmth = validdate.getMonth() + 1;
	if(validmth < 10) {
		validmth = "0" + validmth;
	}
	var validday = validdate.getDate();
	if(validday < 10) {
		validday = "0" + validday;
	}
	var fullvaliddate = validdate.getFullYear() + "" + validmth + "" + validday;
	if(fullvaliddate != chkdate) {
		return false;
	}
	return true;
}

function checktickettype(chkyear, chkmth, chkday) {
	var ediv = document.getElementById("errorlist");
	ediv.innerHTML =  "";
    	var pdiv = document.getElementById("methodhere");
    	pdiv.innerHTML = "<img src='images/shim.gif' border=0 width=1 height=42>";
	if(! checkvaliddate(chkyear, chkmth, chkday)) {
		errormsg = 'Please select a correct specific date.';
		ediv.innerHTML =  errormsg;
		return;
	}
	var tempary = eval("type" + "" + chkyear + "" + chkmth);
	if(tempary[Number(chkday)] == "") {
		errormsg = 'The date you selected falls before 3 January 2006. Please visit us as planned on the specific date for which you have already purchased tickets. Your ticket will be valid for admission to the park on the specific date which you have chosen.';
		ediv.innerHTML =  errormsg;
		return;
	}
	else if(chkyear + "" + chkmth + "" + chkday > "20060402") {
		errormsg = 'Please select a date on or before 2 April 2006.';
		ediv.innerHTML =  errormsg;
		return;
	}
	showmethodlayer(tempary[Number(chkday)], chkyear, chkmth, chkday);
	return;
}

function showmethodlayer(tickettype, chkyear, chkmth, chkday) {
    var pdiv = document.getElementById("methodhere");
    pdiv.innerHTML = "";
    var out = "";
    var htmlout = "";
    var headerout = "";
    if(tickettype == "R") {
    	headerout = "You have purchased tickets for <b>" + Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + "</b> which is a <b>Regular Day</b>.";
    	out = "You can change your tickets for similar value of non-date specific Regular Day Admission tickets that are valid for one visit during a 6-month period. Then you can visit us on most weekdays (<b>Mondays to Fridays</b>) during the 6-month validity period.<br><br>";
    	out += "The following calendar shows valid Regular Days:";
    	//out += "<table cellpadding=2 cellspacing=0 border=0><tr><td valign=top><div id=calchere>" + writecalendar(tickettype, chkyear, chkmth) + "</div></td><td valign=bottom align=right><table cellpadding=3 cellspacing=0 border=0><tr><td align=right width=32 height=20 style='background-image: url(images/popup/redcircle.gif);'><div id=calcdayhere>"+ showfirstday(tickettype, chkyear, chkmth) +"</div></td><td class=small> Regular Days</td></tr></table></td></tr></table><br>";
    	out += "<table cellpadding=2 cellspacing=0 border=0><tr><td valign=top><div id=calchere>" + writecalendar(tickettype, chkyear, chkmth) + "</div></td><td valign=bottom align=right><table cellpadding=3 cellspacing=0 border=0><tr><td align=right width=32 height=20 style='background-image: url(images/popup/redcircle.gif);'>&nbsp;</td><td class=small> Regular Days</td></tr></table></td></tr></table><br>";
    	out += "<b>How to change your tickets:</b><br><br>";
    	out += "<ul><li> Bring your unused tickets. If you have purchased tickets online and have not redeemed your tickets, please bring your confirmation number together with your credit card used to make the purchase.";
    	out += "<li> Go to the Hong Kong Disneyland Main Entrance Guest Relations Window, or Hong Kong Disneyland Ticket Express at MTR Hong Kong Station (Tung Chung Concourse) <b>between 3 January 2006 and 30 June 2006</b>.</ul>";
    	out += "<br>For more information, see <a href='ticketoption_faq.html'>FAQs</a>.";
    }
    else if(tickettype == "P") {
    	headerout = "You have purchased tickets for <b>" + Number(chkday) + " "+ monthname[Number(chkmth)] +" "+ chkyear + "</b> which is a <b>Peak Day</b>.";
    	out = "You can change your tickets for similar value of non-date specific Peak Day Admission tickets that are valid for one visit during 6-month period. Then you can visit us on most <b>Saturdays, Sundays and certain vacation periods</b> during the 6-month validity period.<br><br>";
    	out += "The following calendar shows valid Peak Days:";
    	//out += "<table cellpadding=2 cellspacing=0 border=0><tr><td valign=top><div id=calchere>" + writecalendar(tickettype, chkyear, chkmth) + "</div></td><td valign=bottom align=right><table cellpadding=3 cellspacing=0 border=0><tr><td align=right width=32 height=20 style='background-image: url(images/popup/redcircle.gif);'><div id=calcdayhere>"+ showfirstday(tickettype, chkyear, chkmth) +"</div></td><td class=small> Peak Days</td></tr></table></td></tr></table><br>";
    	out += "<table cellpadding=2 cellspacing=0 border=0><tr><td valign=top><div id=calchere>" + writecalendar(tickettype, chkyear, chkmth) + "</div></td><td valign=bottom align=right><table cellpadding=3 cellspacing=0 border=0><tr><td align=right width=32 height=20 style='background-image: url(images/popup/redcircle.gif);'>&nbsp;</td><td class=small> Peak Days</td></tr></table></td></tr></table><br>";
    	out += "<b>How to change your tickets:</b><br><br>";
    	out += "<ul><li> Bring your unused tickets. If you have purchased tickets online and have not redeemed your tickets, please bring your confirmation number together with your credit card used to make the purchase.";
    	out += "<li> Go to the Hong Kong Disneyland Main Entrance Guest Relations Window, or Hong Kong Disneyland Ticket Express at MTR Hong Kong Station (Tung Chung Concourse) <b>between 3 January 2006 and 30 June 2006</b>.</ul>";
    	out += "<br>For more information, see <a href='ticketoption_faq.html'>FAQs</a>.";
    }
    else if(tickettype == "S") {
    	if(chkyear == 2006 && chkmth == 1 && (chkday >= 28 || chkday <=31)) {
	    	headerout = "You have purchased tickets for <b>" + Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + "</b> which was a <b>Peak Day and is now a Special Day</b>.";
	    	headerout += " You can change your tickets of similar value for non-date specific Peak Day Admission tickets that are valid for one visit during 6-month period OR you can change your ticket for another Special Day.";
	    	out = "<b>Change your tickets for another Peak Day</b><br>You can change your tickets for similar value of non-date specific Peak Day Admission tickets that are valid for one visit during 6-month period. Then you can visit us on most Saturdays, Sundays and certain vacation periods during the 6-month validity period.";
	    	out += "<bR><br>The following calendar shows valid Peak Days:";
		out += "<table cellpadding=2 cellspacing=0 border=0><tr><td valign=top><div id=calchere>" + writecalendar('P', chkyear, chkmth) + "</div></td><td valign=bottom align=right><table cellpadding=3 cellspacing=0 border=0><tr><td align=right width=32 height=20 style='background-image: url(images/popup/redcircle.gif);'>&nbsp;</td><td class=small> Peak Days</td></tr></table></td></tr></table><br>";
		out += "<b>Change your tickets for another Special Day</b><br>You can change your tickets for similar value of date specific Special Day Admission tickets that are valid for one visit on your chosen date. Special Day tickets are valid only on the specified date and do not have 6-months validity.";
		out += "<br><br>Special Days from February  2006 to June 2006 include: ";
		out += "<br><table cellpadding=5 cellspacing=0 border=0><tr><td valign=top><li> April 14, 2006 (Friday)<li> April 15, 2006 (Saturday)<li> April 16, 2006 (Sunday)<li> April 17, 2006 (Monday)<li> April 18, 2006 (Tuesday)<li> April 19, 2006 (Wednesday)<li> April 20, 2006 (Thursday)<li> April 21, 2006 (Friday)<li> April 30, 2006 (Sunday)</td><td valign=top><li> May 1, 2006 (Monday)<li> May 2, 2006 (Tuesday)<li> May 3, 2006 (Wednesday)<li> May 4, 2006 (Thursday)<li> May 5, 2006 (Friday)<li> May 6, 2006 (Saturday)</td></tr></table>";
	    	out += "<br><br><b>How to change your tickets:</b><br><br>";
	    	out += "<ul><li> Bring your unused tickets. If you have purchased tickets online and have not redeemed your tickets, please bring your confirmation number together with your credit card used to make the purchase.";
	    	out += "<li> Go to the Hong Kong Disneyland Main Entrance Guest Relations Window, or Hong Kong Disneyland Ticket Express at MTR Hong Kong Station (Tung Chung Concourse) <b>between 3 January 2006 and 30 June 2006</b>.</ul>";	    	
	    	out += "<br><br>For more information, see <a href='ticketoption_faq.html'>FAQs</a>.";	
    	}
    	else {
	    	headerout = "You have purchased tickets for <b>" + Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + "</b> which was a <b>Peak Day and is now a Special Day</b>.";
	    	out = "Tickets for Special Days are valid only on the specified date and do not have 6-months validity.<br><br>You can visit us as planned on this day for which you have already purchased tickets. Your ticket will be valid for admission to the park on this day which you have chosen.";
	    	out += "<br><br>For more information, see <a href='ticketoption_faq.html'>FAQs</a>.";
	    	}
    }
    else if(tickettype == "N") {
    	headerout = "You have purchased tickets for <b>" + Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + "</b>.";
	out = "Tickets for "+ Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + " are valid only on this specified date and do not have 6-months validity.<br><br>";
	out += "You can visit us as planned on this day for which you have already purchased tickets.<Br><br>";
	//out += "Please note: tickets are no longer available for "+ Number(chkday) +" "+ monthname[Number(chkmth)] +" "+ chkyear + ".<br><br>";
	out += "For more information, see <a href='ticketoption_faq.html'>FAQs</a>.";
    }


    htmlout = "<table cellpadding=0 cellspacing=0 border=0 id=SaluteTable>";
    htmlout += "<Tr>";
    htmlout += "	<td valign=top width=5><img src='images/popup/table_tl.gif' width=5 height=5 border=0></td>";
    htmlout += "	<td valign=top style='border-top:1px solid #F5B62E;'><img src='images/shim.gif' width=1 height=1 border=0></td>";
    htmlout += "	<td valign=top width=5 align=right><img src='images/popup/table_tr.gif' width=5 height=5 border=0></td>";
    htmlout += "</tr>";
    htmlout += "</table>";
    htmlout += "<table cellpadding=0 cellspacing=0 border=0 width=580 id=SaluteTableMiddle>";
    htmlout += "<Tr>";
    htmlout += "	<td valign=top width=1><img src='images/shim.gif' width=1 height=1 border=0></td>";
    htmlout += "	<td valign=top><br>"+ headerout +"<br><br>" + out + "<br><br></td>";
    htmlout += "	<td valign=top width=1><img src='images/shim.gif' width=1 height=1 border=0></td>";
    htmlout += "</tr>";
    htmlout += "</table>";
    htmlout += "<table cellpadding=0 cellspacing=0 border=0 id=SaluteTableBottom>";
    htmlout += "<Tr>";
    htmlout += "	<td valign=top width=5><img src='images/popup/table_bl.gif' width=5 height=5 border=0></td>";
    htmlout += "	<td valign=top style='border-bottom:1px solid #F5B62E;'><img src='images/shim.gif' width=1 height=1 border=0></td>";
    htmlout += "	<td valign=top width=5 align=right><img src='images/popup/table_br.gif' width=5 height=5 border=0></td>";
    htmlout += "</tr>";
    htmlout += "</table>";

    pdiv.innerHTML = htmlout;
}

function writecalendar(tickettype, chkyear, chkmth) {
	var wkcolor = "#4E4D4C";
	var curdate   = new Date();
	var curFullYear = curdate.getFullYear();
	var curMonth = curdate.getMonth() + 1;
	if(curMonth < 10) {
		curMonth = "0" + curMonth;
	}
	if(Number(curFullYear + "" +  curMonth) > Number(chkyear + "" + chkmth)) {
		chkyear = curFullYear;
		chkmth = curMonth;
	}

	var setdate = new Date(chkyear, chkmth - 1, 1, 0, 0, 0, 0);
	var showwkday = setdate.getDay();
	var setmth = Number(chkmth);
	var setyear = Number(chkyear);
	var cfuncname = "chgmethodcal";
	if(onecal != 1) {
		cfuncname = "chgmethodindexcal";
	}	
	var prevmth = setmth - 1;
	var prevyear = setyear;
	var prevhtml = "";
	var showlastmth = curdate.getMonth();
	if(prevmth == 0) {
		prevmth = 12;
		prevyear = prevyear - 1;
	}
	if(prevmth < 10) {
		prevmth = "0" + prevmth;
	}
	if(showlastmth < 10) {
		showlastmth = "0" + showlastmth;
	}
	
	if(prevyear + "" + prevmth > curdate.getFullYear() + "" +  showlastmth) {
		prevhtml = "<a href=\"javascript:" + cfuncname + "('" + tickettype +"\','"+ prevyear +"','" + prevmth + "');\" class='small'>< previous</a>";
	}
	var nextmth = setmth + 1;
	var nextyear = setyear;
	var nexthtml = "";
	if(nextmth == 13) {
		nextmth = 1;
		nextyear = nextyear + 1;
	}
	if(nextmth < 10) {
		nextmth = "0" + nextmth;
	}
	if(nextyear + "" + nextmth <= lastmth) {
		nexthtml = "<a href=\"javascript:" + cfuncname + "('" + tickettype +"\','"+ nextyear +"','" + nextmth + "');\" class='small'>next ></a>";
	}
	var tempary = eval("type" + "" + chkyear + "" + chkmth);
	var out="";
	out += "<table cellpadding=0 cellspacing=0 border=0 width=275>";
	out += "<tr bgcolor='#FAF0AB'><Td valign=top><img src='images/popup/ts_white_tl_corn.gif' width=4 height=4 border=0></td>";
	out += "<td valign=top align=right><img src='images/popup/ts_white_tr_corn.gif' width=4 height=4 border=0></td></tr>";
	out += "</table>";
	out += "<table cellpadding=3 cellspacing=0 border=0 width=275>";
	out += "<tr bgcolor='#FAF0AB'>";
	out += "<td valign=top colspan=2>" + prevhtml + "</td>";
	out += "<td colspan=3 align=center><font color='#993235'><b>" + monthname[setmth] + " "  + chkyear + "</b></font></td>";
	out += "<td align=right valign=top colspan=2>" + nexthtml + "</td>";
	out += "</tr>";	
	//special case start
	//2006-07, regular period
	if(chkyear == "2006" && chkmth == "07" && tickettype == "R") {
    		out += "<tr bgcolor='#F9D074'><td colspan=7 align=center><font color='#cc0000'><b>There are no Regular Days in July.</b></font></td></tr>";
    		wkcolor = "#BBBBBB";
    	}
	//2006-08, regular period
	if(chkyear == "2006" && chkmth == "08" && tickettype == "R") {
    		out += "<tr bgcolor='#F9D074'><td colspan=7 align=center><font color='#cc0000'><b>There are no Regular Days in August.</b></font></td></tr>";
    		wkcolor = "#BBBBBB";
    	}
    	//special case end
	out += "<tr bgcolor='#FBF7C8'>";
	out += "	<td width=35 align=center><font color='" + wkcolor + "'>Sun</font></td>";
	out += "	<td width=34 align=center><font color='" + wkcolor + "'>Mon</font></td>";
	out += "	<td width=34 align=center><font color='" + wkcolor + "'>Tue</font></td>";
	out += "	<td width=34 align=center><font color='" + wkcolor + "'>Wed</font></td>";
	out += "	<td width=34 align=center><font color='" + wkcolor + "'>Thu</font></td>";
	out += "	<td width=34 align=center><font color='" + wkcolor + "'>Fri</font></td>";
	out += "	<td width=35 align=center><font color='" + wkcolor + "'>Sat</font></td>";
	out += "</tr></table>";
	out += "<table cellpadding=3 cellspacing=0 border=0 width=275 style='border:1px solid #F9D074; border-left:0px; border-top:0px'><tr>";
	//set start blank
	if(showwkday > 0) {
		for(i = 0; i < showwkday; i++) {
			out += "<td width=32 style='border:1px solid #F9D074; border-right:0px; border-bottom:0px'>&nbsp;</td>";
		}
	}
	//set day
	for(i = 1; i <= mthendday[setmth]; i++) {
		if(tempary[i] == tickettype) {
			out += "<td width=32 align=right style='border:1px solid #F9D074; border-right:0px; border-bottom:0px;background-image: url(images/popup/redcircle.gif);'>" + i + "</td>";
		}
		else {
			out += "<td width=32 align=right style='border:1px solid #F9D074; border-right:0px; border-bottom:0px'><font color='#BBBBBB'>" + i + "</font></td>";
		}
		showwkday++;
		if(showwkday > 6) {
			out += 	"</tr><tr>";
			showwkday = 0;
		}

	}
	//set end blank
	if(showwkday > 0) {
		for(i = showwkday; i <= 6; i++) {
			out += "<td width=32 align=right style='border:1px solid #F9D074; border-right:0px; border-bottom:0px'>&nbsp;</td>";
		}
	}
	out += "</tr></table>";
	out += "<table cellpadding=0 cellspacing=0 border=0 width=275>";
	out += "<tr bgcolor='#F9D074'><Td valign=bottom><img src='images/popup/ts_white_bl_corn.gif' width=4 height=4 border=0></td>";
	out += "<td valign=bottom align=right><img src='images/popup/ts_white_br_corn.gif' width=4 height=4 border=0></td></tr>";
	out += "</table>";
	return out;

}

function chgmethodcal(tickettype, chkyear, chkmth) {
	var pdiv = document.getElementById("calchere");
	pdiv.innerHTML = writecalendar(tickettype, chkyear, chkmth);
	//var pddiv = document.getElementById("calcdayhere");
	//pddiv.innerHTML = showfirstday(tickettype, chkyear, chkmth);
	//pddiv.innerHTML = "";
	return;
}

function chgmethodindexcal(tickettype, chkyear, chkmth) {
	var pdiv = document.getElementById(tickettype + "calchere");
	pdiv.innerHTML = writecalendar(tickettype, chkyear, chkmth);
	return;
}

function showfirstday(tickettype, chkyear, chkmth) {
	var firstday = 0;
	var tempary = eval("type" + "" + chkyear + "" + chkmth);
	for(i = 1; i <= mthendday[Number(chkmth)]; i++) {
		if(tempary[i] == tickettype) {
			if(firstday == 0) {
				firstday = i;
				i = mthendday[Number(chkmth)];
			}
		}
	}
	return firstday;
}