// JavaScript Document

var MonthArray = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var DownStream = new Array("CQ/YC", "CQ/JZ", "CQ/WH","CQ/SH", "YC/JZ", "YC/WH", "YC/SH", "JZ/WH", "JZ/SH", "WH/SH");
var Abbreviations = new Array("CQ", "YC", "JZ", "WH", "SH");
var Original = new Array("Chongqing", "Yichang", "Jingzhou", "Wuhan", "Shanghai");
var today = new Date();
var thisday = today.getDate();
var thismonth = today.getMonth() + 1;
var thisyear = today.getFullYear();

function ShipChange(obj)
{
	      var objcode = document.getElementById("txt_Code");
		  var shipindex = parseInt(obj.options[obj.selectedIndex].value);
		  var streampos = objcode.value.lastIndexOf("U");
		  if(streampos == -1)
		       streampos = objcode.value.lastIndexOf("D");
		  var newcode = objcode.value.substring(streampos + 1);
		  var thisroute = null;
		  var backroute = null;
		  for(var i = 0; i < Ships.length;i++)
		  {
			  if(parseInt(Ships[i][0]) == shipindex)
			  {
				  thisroute = Ships[i][3];
				  backroute = Ships[i][4];
				  break;
			  }
		  }
		  var stream = GetStream(thisroute);
		  objcode.value = ShipsAbb[shipindex] + stream + newcode;
		  BindRoute(thisroute,backroute);
	      var objdep = document.getElementById("txt_Departure");
		  var objarr = document.getElementById("txt_Arrival");
		  objdep.value = '';
		  objarr.value = '';
		  BindRooms(shipindex,stream,null);
}

function BindRoute(droute,broute)
{
	      var objr = document.getElementById("rbl_Route");
		  objr.innerHTML = '<input id=\"rbl_Route_0\" type=\"radio\" name=\"rbl_Route\" value=\"'+droute+'\" checked=\"checked\" onclick=\"RouteClick(this);\" /><label for=\"rbl_Route_0\">'+ReplaceRoute(droute)+'</label><input id=\"rbl_Route_1\" type=\"radio\" name=\"rbl_Route\" value=\"'+broute+'\" onclick=\"RouteClick(this);\" /><label for=\"rbl_Route_1\">'+ReplaceRoute(broute)+'</label>';
}

function RouteClick(obj)
{
	      var objcode = document.getElementById("txt_Code");
		  var streampos = objcode.value.lastIndexOf("U");
		  if(streampos == -1)
		       streampos = objcode.value.lastIndexOf("D");
		  var newcode = objcode.value.substring(0,streampos);
		  var lastnum = objcode.value.substring(streampos+1);
		  var stream = GetStream(obj.value);
		  objcode.value = newcode + stream + lastnum;
		  var objship = document.getElementById("DropDownListShips");
		  var shipindex = parseInt(objship.options[objship.selectedIndex].value);
		  var objdep = document.getElementById("txt_Departure");
		  BindRooms(shipindex,stream,objdep.value);
		  var objarr = document.getElementById("txt_Arrival");
		  var dvalue = objdep.value;
		  var avalue = objarr.value;
		  if(dvalue.length == 0)
		  {
			  DefaultBind(thisyear,thismonth,'txt_Departure');
		  }
		  else
		  {
			  var inputdate = new Date(dvalue);
			  DefaultBind(inputdate.getFullYear(),inputdate.getMonth(),'txt_Departure');
		  }
		  objdep.value = avalue;
		  var alldates = ShipsDates[shipindex];
		  if(alldates.length > 0)
		  {
		     for(var i = 0; i < alldates.length;i++)
			 {
				 if(alldates[i] == avalue)
				 {
					 if(alldates[i + 1].type != "undefined")
					 {
						 objarr.value = alldates[i+1];
						 break;
					 }
				 }
			 }
		  }
}

function ReplaceRoute(input)
{
	      var result = input;
		  for(var i = 0; i < Abbreviations.length;i++)
		  {
			  result = result.replace(Abbreviations[i],Original[i]);
		  }
		  result = result.replace("/"," -&gt; ");
		  return result;
}

function BindDates(index,inputvalue,showfirst)
{
		  var datearr = ShipsDates[index];
		  var result = '<td class=\"daybox_css\">';
		  for(var i = 0; i < datearr.length;i++)
		  {
			  if(showfirst)
			  {
				  if(i % 2 == 0 && datearr[i] == inputvalue)
				  {
				      result = '<td class=\"today_css\" onclick=\"inputDate(\''+datearr[i];
				      result += '\',\''+datearr[i+1]+'\');\">';
				      break;
			      }
			  }
			  else
			  {
				  if(i % 2 == 1 && datearr[i] == inputvalue)
				  {
				      result = '<td class=\"today_css\" onclick=\"inputDate(\''+datearr[i];
				      result += '\',\''+datearr[i+1]+'\');\">';
				      break;
			      }
			  }
		  }
		  return result;
}

function showdatebox(obj)
{
	      if(obj.value == "SELECT A DATE")
		     obj.value = "CLOSE IT";
		  else
		     obj.value = "SELECT A DATE"
	      var objdep = document.getElementById("txt_Departure");
		  if(objdep.value.length == 0)
		  {
			  DefaultBind(thisyear,thismonth,'txt_Departure');
		  }
		  else
		  {
			  var inputdate = new Date(objdep.value);
			  DefaultBind(inputdate.getFullYear(),inputdate.getMonth(),'txt_Departure');
		  }
		  var x,y,offsetParent;
		  offsetParent = obj;
		  if(offsetParent)
		  {
		     x=0;
	         y=0;
	         while(offsetParent!=null && offsetParent.tagName.toUpperCase()!="BODY") {
		            x += offsetParent.offsetLeft;
		            y += offsetParent.offsetTop;
		            offsetParent=offsetParent.offsetParent;
	         }
			 x = x - 190;
			 y = y - 215;
		  }
		  var objbox = document.getElementById("MyCalendar");
		  objbox.style.left = x + "px";
		  objbox.style.top = y + "px";
		  if(objbox.style.display == "none")
		  {
		      objbox.style.display = "block";
		  }
		  else
		  {
			  objbox.style.display = "none";
		      return;
		  }
}

function DefaultBind(inputyear,inputmonth,objid)
{  
          var obj = document.getElementById("MyCalendar");
		  var objship = document.getElementById("DropDownListShips");
          var shipindex = parseInt(objship.options[objship.selectedIndex].value);
          var mydate = new Date(inputyear,inputmonth,1,0,0,0);
          var nextdate = new Date(inputyear,inputmonth + 1,1,0,0,0);
          var weekday = mydate.getDay();
          var monthnum = mydate.getMonth();
          var yearnum = mydate.getFullYear();
          var totaldays = Math.floor((nextdate - mydate) / 3600000 / 24);
          var previousm = monthnum - 1;
          var current = MonthArray[monthnum] + ' ' + yearnum.toString();
          var nextm = monthnum + 1;
          var myday = 1,newday = 0;
          var rows = Math.floor((totaldays + weekday) / 7);
          if((totaldays + weekday) % 7 > 0)rows = rows + 1;
          var MyHtml =  '<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"datebox_css\">';
          MyHtml += '<tr>';
          MyHtml += '<th scope=\"col\" title=\"LAST MONTH\"><span onclick=\"DefaultBind('+yearnum;
		  MyHtml += ','+previousm+',null);\">&lt;</span></th>';
          MyHtml += '<th colspan=\"5\" scope=\"col\">' + current + '</th>';
          MyHtml += '<th scope=\"col\" title=\"NEXT MONTH\"><span onclick=\"DefaultBind('+yearnum;
		  MyHtml += ','+nextm+',null);\">&gt;</span></th>';
          MyHtml += '</tr>';
          MyHtml += '<tr>';
          MyHtml += '<td class=\"weekbox_css\">Sun</td>';
          MyHtml += '<td class=\"weekbox_css\">Mon</td>';
          MyHtml += '<td class=\"weekbox_css\">Tue</td>';
          MyHtml += '<td class=\"weekbox_css\">Wed</td>';
          MyHtml += '<td class=\"weekbox_css\">Thu</td>';
          MyHtml += '<td class=\"weekbox_css\">Fri</td>';
          MyHtml += '<td class=\"weekbox_css\">Sat</td>';
          MyHtml += '</tr>';
		  var createdate = null;
		  var item1 = document.getElementById("rbl_Route_0");
          for(var i = 0;i < rows;i++)
          {
              MyHtml += '<tr>';
          	for(var j = 0; j < 7;j++)
          	{
          		newday = myday - weekday;
          	   if(newday > 0 && newday <= totaldays)
          	   {
				   createdate = yearnum.toString() + "/" + (monthnum + 1).toString() + "/" + newday.toString();
          		   MyHtml += BindDates(shipindex,createdate,item1.checked);
                   MyHtml += newday.toString()+'</td>';
          	   }
          	   else
          	   {
          	     MyHtml += '<td class=\"daybox_css\">&nbsp;</td>';
          	   }
          	   myday++;
          	}
              MyHtml += '</tr>';
          }
          MyHtml += '</table>';
          obj.innerHTML = MyHtml;
}

function inputDate(departure,arrival)
{
	      var objdep = document.getElementById("txt_Departure");
		  var objarr = document.getElementById("txt_Arrival");
		  objdep.value = departure;
		  objarr.value = arrival;
		  var item1 = document.getElementById("rbl_Route_0");
		  var item2 = document.getElementById("rbl_Route_1");
		  var theroute = null;
		  if(item1.checked == true)
		      theroute = item1.value;
		  if(item2.checked == true)
		      theroute = item2.value; 
		  var stream =  GetStream(theroute);
		  var objship = document.getElementById("DropDownListShips");
		  var shipindex = parseInt(objship.options[objship.selectedIndex].value);
		  BindRooms(shipindex,stream,departure);
		  var objbox = document.getElementById("MyCalendar");
		  objbox.style.display = "none";
		  document.getElementById("ButtonSelectDate").value = "SELECT A DATE";
}

function BindRooms(shipindex,route,departure)
{
	      var therooms = ShipsRoom[shipindex];
		  if(therooms.length > 0)
		  {
			  var obj = document.getElementById("ddl_RoomType");
			  obj.options.length = 0;
			  var isSeason = true;
			  var isDown = true;
			  if(departure > 0)
			  {
				  var k = 0;
			      var sdate = new Date(departure);
				  var inputmonth = sdate.getMonth() + 1;
				  var mymonth = therooms[1].split(",");
				  for(k = 0;k<mymonth.length;k++)
				  {
					  if(parseInt(mymonth[k]) == inputmonth)
					  {
						 isSeason = false;
						 break;
					  }
				  }
			  }
			  if(route == "U") isDown = false;
			  var showprice = -1;
			  if(isSeason == true &&isDown == true)
				  showprice = 5;
			  if(isSeason == true && isDown == false)
			      showprice = 6;
			  if(isSeason == false && isDown == false)
			      showprice = 3;
			  if(isSeason == false && isDown == true)
			      showprice = 2;
			  var opt_tv = null;
			  for(var i = 0; i < therooms.length;i+=7)
			  {
				  opt_tv = therooms[i] + " / " + therooms[i + showprice];
				  if(i == 0)
				    opt_tv =  opt_tv + " USD per person";
				  else
				    opt_tv =  opt_tv + " USD per suite";
				  obj.options.add(new Option(opt_tv,opt_tv));
			  }
		  }
}

function GetStream(thisroute)
{
	      var stream = "U";
		  for(var j = 0;j<DownStream.length;j++)
		  {
			  if(DownStream[j] == thisroute)
			  {
				  stream = "D";
				  break;
			  }
		  }
		  return stream;
}

function CheckInput()
{
	      var objdep = document.getElementById("txt_Departure");
		  if(objdep.value.length == 0)
		  {
			  alert("Please select a date!");
			  showdatebox(document.getElementById("ButtonSelectDate"));
			  return false;
		  }
		  var objname = document.getElementById("txt_Name");
		  if(objname.value.length == 0)
		  {
			  alert("Please enter your name!");
			  objname.focus();
			  return false;
		  }
		  var objemail = document.getElementById("txt_Email");
	      
		  if(objemail.value.length == 0)
		  {
			  alert("Please enter your e-mail address!");
			  objemail.focus();
			  return false;
		  }
		  else
		  {
			  var emailexp = /\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
			  if(!emailexp.test(objemail.value))
			  {
				  alert("Please enter the correct e-mail address!");
				  objemail.focus();
				  return false;
			  }
		  }
	      var obj = document.getElementById("cb_agreed");
		  if(obj.checked == false)
		  {
			  alert("Sorry, only when you read our Terms and Conditions and agree with it, you can continue to make a booking.")
			  obj.focus();
			  return false;
		  }
		  return true;
}

function shrule()
{
	      var obj = document.getElementById("therule");
	     if(obj.className == "rule_hide")
		    obj.className = "rule_show";
		 else
		    obj.className = "rule_hide";
}