// print.js

function printTimeLeft(strTime) {
	
	var finalString = strTime;
	var temp = new Array();

	if (strTime.match("0D ") != null) {
	    temp = strTime.split("0D ");
	    finalString = temp[1];
	}
	if (finalString.match("00H ") != null) {
	    temp = strTime.split("00H ");
	    finalString = temp[1];
	}
	if (finalString.match("00M ") != null) {
	    temp = strTime.split("00M ");
	    finalString = temp[1];
	}

	return finalString;					
}

function printResults(page){
	var htmlcode = null;
	htmlcode = printTableHeader();

	var distance = null;
	var title = null;
	var timeLeft = null;
	var price = null;
	
	for (var k = 0; k < 4; k++) {
		switch (searchCriteria[k]) {
		  case "Distance":
			distance = k;
		    break;
		  case "Title":
			title = k;
			break;
		  case "TimeLeftNew":
			timeLeft = k;
			break;
		  case "CurrentPrice":
			price = k;
			break;
		}
	}

	var start = page * 8;
	var end = start + 8;
	
	//for (var i = 0; i < sortedArray.length; i++) {
    for (var i = start; i < end; i++) {
		if (i < sortedArray.length) {
		    var dist = null;
			var time = printTimeLeft(sortedArray[i][timeLeft]);
			
			if(sortedArray[i][distance] == 100000) {
				dist = "N/A";
			}
			else {
				dist = sortedArray[i][distance];
			}
			
			htmlcode += printTableRow(sortedArray[i][8],
									  sortedArray[i][7],
									  sortedArray[i][title],
									  sortedArray[i][price],
									  sortedArray[i][10],
									  time,
									  dist,
									  sortedArray[i][4]);
	    }
	}
	htmlcode += printTableFooter(page);
	
	document.getElementById("res").innerHTML=htmlcode;
	showItemPins(page);

}

function printTableHeader() {
  var header = null;
  header =  "<form action=\"javascript:void(0)\"><table>";
  header += "<colgroup><col style=\"width:50px\"><col style=\"width:280px\"><col style=\"width:80px\"></colgroup><col style=\"width:100px\"><col style=\"width:80px\">";
  header += "<thead><tr id=head>";
  header += "<th><div id=\"table\" style=\"position:absolute; width:50px height:20px; left:5px; top:5px;\"> </div></th>";
  header += "<th><button id=table style=width:100% onClick=\"sortColumn('Title','CurrentPrice','Distance','TimeLeftNew')\">"; 
  
  header += getArrow("Title");
  
  header += "Item Title</button></th>";
  header += "<th><button id=table style=width:100% onClick=\"sortColumn('CurrentPrice','Title','Distance','TimeLeftNew')\">";
  
  header += getArrow("CurrentPrice");
  
  header += "Price</button></th>";
  header += "<th><button id=table style=width:100% onClick=\"sortColumn('TimeLeftNew','CurrentPrice','Distance','Title')\">"
 
  header += getArrow("TimeLeftNew");
  
  header += "Time Left</button></th>";
  header += "<th><button id=table style=width:100% onClick=\"sortColumn('Distance','CurrentPrice','Title','TimeLeftNew')\">"
 
  header += getArrow("Distance");
  
  header += "Distance</button></th>";
  header += "</tr></thead><tbody>";
  return header;
}

function printTableRow(picURL, link, titleName, currentPrice, currency, timeLeft, distance, id) {
   var row = null;
	  if (rowCount % 2 == 0) {
	    row = "<tr id=even>";
	  } else {
	    row = "<tr id=odd>";
	  }
  row += "<td><img src=\""+picURL+"\" width=50 height=50></td>";
  row += "<td><a href=\""+link+"\" target=\"_blank\">"+titleName+"</a></td>";
  row += "<td>"+currentPrice+" "+ currency +"</td>";
  row += "<td>"+timeLeft+"</td>";
 	  if (distance != "N/A") {
	    row += '<td><a href="javascript:myclick(' + id + ')">' + distance + ' m</a></td>';
	  } else {
	    row += '<td>' + distance + '</td>';
	  }

  row += "</tr>";
  rowCount++;
  return row;
}

function printTableFooter(currentPage) {
  var foot = "</tbody>";
  foot += "<tfoot><tr id=head><td id=foot colspan=\"5\">Page: "
  for (var i = 0; i < (sortedArray.length / 8); i++) {
    foot += "<button id=table onclick=\"printResults(" + i + ")\">";
	if (i == currentPage) {
		foot += "<u>";
		foot += i+1;
		foot += "</u>";
	} else {
		foot += i+1;
	}
    foot += "</button>";
  }
  foot += "</td></tr></foot>";
  foot += "</table></form>";
  return foot;
}