// JavaScript Document
var staroptions = new Array(4);
staroptions[0] = new Option("-- All Star Ratings --","0");
staroptions[1] = new Option("3 Star","3");
staroptions[2] = new Option("4 Star","4");
staroptions[3] = new Option("5 Star","5");
var routeoptions = new Array();
routeoptions["none"] = new Option("-- All Directions --","none");
routeoptions["CQ/YC"] = new Option("Chongqing -> Yichang","CQ/YC");
routeoptions["CQ/JZ"] = new Option("Chongqing -> Jingzhou","CQ/JZ");
routeoptions["CQ/WH"] = new Option("Chongqing -> Wuhan","CQ/WH");
routeoptions["CQ/SH"] = new Option("Chongqing -> Shanghai","CQ/SH");
routeoptions["YC/CQ"] = new Option("Yichang -> Chongqing","YC/CQ");
routeoptions["JZ/CQ"] = new Option("Jingzhou -> Chongqing","JZ/CQ");
routeoptions["WH/CQ"] = new Option("Wuhan -> Chongqing","WH/CQ");
routeoptions["SH/CQ"] = new Option("Shanghai -> Chongqing","SH/CQ");

function defaultbind()
{
	      var objship = document.getElementById("DropDownListShips");
	      var objstar = document.getElementById("DropDownListStar");
	      var objdect = document.getElementById("DropDownListDirections");
	      objdect.options.length = 0;
	      objdect.add(routeoptions["none"]);
	      objdect.add(routeoptions["CQ/YC"]);
	      objdect.add(routeoptions["CQ/JZ"]);
	      objdect.add(routeoptions["CQ/WH"]);
	      objdect.add(routeoptions["CQ/SH"]);
	      objdect.add(routeoptions["YC/CQ"]);
	      objdect.add(routeoptions["JZ/CQ"]);
	      objdect.add(routeoptions["WH/CQ"]);
	      objdect.add(routeoptions["SH/CQ"]);
	      objstar.options.length = 0;
	      objstar.add(staroptions[0]);
	      objstar.add(staroptions[1]);
	      objstar.add(staroptions[2]);
	      objstar.add(staroptions[3]);
	      objship.options.length = 0;
		  objship.add(new Option("-- All Ships --","0"));
	      for(var i = 0; i < Ships.length;i++)
		  {
		       objship.add(new Option(Ships[i][1],Ships[i][0]));
		  }
		  objship.selectedIndex = 0;
		  objstar.selectedIndex = 0;
		  objdect.selectedIndex = 0;
}

function autoMatchShip(obj)
{
		  if(obj.selectedIndex == 0)
		  {
			  defaultbind();
		  }
		  else
		  {
			  bindstars();
		      bindroute();
		  }
}

function autoMatchStar(obj)
{
		  if(obj.selectedIndex == 0)
		  {
			  defaultbind();
		  }
		  else
		  {
			  bindships();
		      bindroute();
		  }
}

function autoMatchRoute(obj)
{
		  if(obj.selectedIndex == 0)
		  {
			  defaultbind();
		  }
		  else
		  {
			  bindships();
		      bindstars();
		  }
}

function bindships()
{
	       var objship = document.getElementById("DropDownListShips");
		   var objstar = document.getElementById("DropDownListStar");
		   var objdect = document.getElementById("DropDownListDirections");
		   var shipId = objship.options[objship.selectedIndex].value;
		   objship.options.length = 0;
		   objship.add(new Option("-- All Ships --","0"));
		   var starIndex = objstar.selectedIndex;
		   var star = -1;
		   if(starIndex > 0)
		      star = parseInt(objstar.options[starIndex].value);
		   var routeIndex = objdect.selectedIndex;
		   var selectRoute = null;
		   if(routeIndex > 0)
			   selectRoute = objdect.options[routeIndex].value;
		   var isnew,optionnum = 1;
		   for(var i = 0; i < Ships.length;i++)
		   {
			   isnew = false;
			   if(routeIndex > 0 && star > 0)
			   {
				   if((Ships[i][3] == selectRoute || Ships[i][4] == selectRoute) && parseInt(Ships[i][2]) == star)
			          isnew = true;
			   }
			   if(routeIndex > 0 && star < 0)
			   {
				   if(Ships[i][3] == selectRoute || Ships[i][4] == selectRoute)
			          isnew = true;
			   }
			   if(routeIndex == 0 && star > 0)
			   {
				   if(parseInt(Ships[i][2]) == star)
			          isnew = true;
			   }
			   if(routeIndex == 0 && star < 0)
			   {
			      isnew = true;
			   }
			   if(isnew == true)
			   {
				   objship.add(new Option(Ships[i][1],Ships[i][0]));
				   if(Ships[i][0] == shipId)
				      objship.selectedIndex = optionnum;
				   optionnum++;
			   }
		   }
}

function bindstars()
{
	      var objship = document.getElementById("DropDownListShips");
		  var objstar = document.getElementById("DropDownListStar");
		  var objdect = document.getElementById("DropDownListDirections");
		  var selectStar = objstar.options[objstar.selectedIndex].value;
		  objstar.options.length = 0;
		  var shipIndex = objship.selectedIndex;
		  var routeIndex = objdect.selectedIndex;
		  var shipId = 0;
		  var selectRoute = null;
		  if(shipIndex > 0)
		     shipId = parseInt(objship.options[shipIndex].value);
		  if(routeIndex > 0)
			  selectRoute = objdect.options[routeIndex].value;
		  objstar.add(staroptions[0]);
		  if(shipIndex == 0 && routeIndex == 0)
		  {
			  objstar.add(staroptions[1]);
			  objstar.add(staroptions[2]);
			  objstar.add(staroptions[3]);
		  }
		  var addIndex = -1;
		  if(shipIndex > 0 && routeIndex == 0)
		  {
			  for(var i = 0; i < Ships.length; i++)
			  {
				  if(parseInt(Ships[i][0]) == shipId)
				  {
					  addIndex = parseInt(Ships[i][2]) - 2;
					  objstar.add(staroptions[addIndex]);
					  break;
				  }
			  }
		  }
		  if(shipIndex == 0 && routeIndex > 0)
		  {
			  var isAdd = new Array(false,false,false);
			  for(var j = 0; j < Ships.length; j++)
			  {
				  if(Ships[j][3] == selectRoute || Ships[j][4] == selectRoute)
				  {
					  addIndex = parseInt(Ships[j][2]) - 2;
					  if(isAdd[addIndex - 1] == false)
					     objstar.add(staroptions[addIndex]);
					  isAdd[addIndex - 1] = true;
					  if(isAdd[0] == true && isAdd[1] == true && isAdd[2] == true)
					     break;
				  }
			  }
		  }
		  if(shipIndex > 0 && routeIndex > 0)
		  {
			  for(var k = 0; k < Ships.length; k++)
			  {
				  if(parseInt(Ships[k][0]) == shipId && (Ships[k][3] == selectRoute || Ships[k][4] == selectRoute))
				  {
					  addIndex = parseInt(Ships[k][2]) - 2;
					  objstar.add(staroptions[addIndex]);
					  break;
				  }
			  }
		  }
		  if(objstar.options.length > 2)
		     objstar.selectedIndex = 0;
		  else
		     objstar.selectedIndex = 1;
}
function bindroute()
{
	      var objship = document.getElementById("DropDownListShips");
		  var objstar = document.getElementById("DropDownListStar");
		  var objdect = document.getElementById("DropDownListDirections");
		  var selectRoute = objdect.options[objdect.selectedIndex].value;
	      objdect.options.length = 0;
		  objdect.add(routeoptions["none"]);
		  var shipIndex = objship.selectedIndex;
		  var starIndex = objstar.selectedIndex;
		  var shipId = 0;
		  var star = -1;
		  if(shipIndex > 0)
		     shipId = parseInt(objship.options[shipIndex].value);
		  if(starIndex > 0)
			  star = parseInt(objstar.options[starIndex].value);
		  var valueone = null,valuetwo = null;
		  var addIndex = null;
		  if(shipIndex > 0 && starIndex == 0)
		  {
			  for(var i = 0;i < Ships.length;i++)
			  {
				  if(parseInt(Ships[i][0]) == shipId)
				  {
					  addIndex = Ships[i][3];
					  objdect.add(routeoptions[addIndex]);
					  addIndex = Ships[i][4];
					  objdect.add(routeoptions[addIndex]);
					  break;
				  }
			  }
		  }
		  if(shipIndex > 0 && starIndex > 0)
		  {
			  for(var j = 0;j < Ships.length;j++)
			  {
				  if(parseInt(Ships[j][0]) == shipId && star == parseInt(Ships[j][2]))
				  {
					  addIndex = Ships[j][3];
					  objdect.add(routeoptions[addIndex]);
					  addIndex = Ships[j][4];
					  objdect.add(routeoptions[addIndex]);
					  break;
				  }
			  }
		  }
		  if(shipIndex == 0 && starIndex > 0)
		  {
			  var addRoute = '';
			  for(var k = 0;k < Ships.length;k++)
			  {
				  if(parseInt(Ships[k][2]) == star)
				  {
					  addIndex = Ships[k][3];
					  if(addRoute.indexOf(addIndex) == -1)
					  {
					     objdect.add(routeoptions[addIndex]);
						 addRoute = addRoute + addIndex;
					  }
					  addIndex = Ships[k][4];
					  if(addRoute.indexOf(addIndex) == -1)
					  {
					     objdect.add(routeoptions[addIndex]);
						 addRoute = addRoute + addIndex;
					  }
				  }
			  }
		  }
		  if(shipIndex == 0 && starIndex == 0)
		  {
			  objdect.add(routeoptions["CQ/YC"]);
			  objdect.add(routeoptions["CQ/JZ"]);
			  objdect.add(routeoptions["CQ/WH"]);
			  objdect.add(routeoptions["CQ/SH"]);
			  objdect.add(routeoptions["YC/CQ"]);
			  objdect.add(routeoptions["JZ/CQ"]);
			  objdect.add(routeoptions["WH/CQ"]);
			  objdect.add(routeoptions["SH/CQ"]);
		  }
		  for(var n = 0;n < objdect.options.length; n++)
		  {
			  if(objdect.options[n].value == selectRoute)
			  {
				  objdect.selectedIndex = n;
				  break;
			  }
		  }
}

function Contains(obj,input)
{
	      var isexists = false;
		  for(var i = 0;i < obj.options.length;i++)
		  {
			  if(obj.options[i].value == input)
			  {
				  isexists = true;
				  break;
			  }
		  }
		  return isexists;
}