
function checkCompare()
{
	var count = parseInt(document.getElementById('compare_count').innerHTML);
	if (count > 5)
	{
		alert("You may only select up to five products at a time for comparision.");
		return false;
	}
	else
	{
		if (count < 2)
		{
			alert("Please select two or more products for comparision.");
			return false;
		}
		else
		{
			location.href='compare.php';
		}
	}

}


function emptyDiv (divToClear){
	var i;
	while (i=divToClear.childNodes[0]){
		if (i.nodeType == 1 || i.nodeType == 3){
			divToClear.removeChild(i);
		}
	}
}


function createRequest() {
	var request = null;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}

	if (request == null) {
		alert("Error creating request object!");
	} else {
		return request;
	}
}

var request = createRequest();
var request1 = createRequest();
var request2 = createRequest();

function doPage(page,count,type,sort,rev)
{
	emptyDiv(document.getElementById('product_display'));
	var url = "/includes/ajax_getproducts.php?count="+count+"&page="+page+"&type="+type+"&sort="+sort+"&rev="+rev+"&dummy="+new Date().getTime();
	//alert(url);
	request.open("GET", url, true);
	request.onreadystatechange = updateProducts;
	request.send(null);


}


function doAll(type,sort,rev)
{
	emptyDiv(document.getElementById('product_display'));
	var url = "/includes/ajax_getproducts.php?all=1&type="+type+"&sort="+sort+"&rev="+rev+"dummy="+new Date().getTime();
	//alert(url);
	request.open("GET", url, true);
	request.onreadystatechange = updateProducts;
	request.send(null);


}


function updateProducts()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var ids = new Array();
			var pics = new Array();
			var models = new Array();
			var comps = new Array();
			var compnames = new Array();
			var heights = new Array();
			var widths = new Array();
			var prices = new Array();
			var cats = new Array();
			var count = 0;
			var all = 0;
			var sort = "";
			var rev = "";
			var headrev = "";
			var product_count = 0;
			var page_num = 1;
			var limit = 10;
			var type = "";
			var xmlDoc = request.responseXML;
			var x = xmlDoc.getElementsByTagName("product");

			for (i=0; i<x.length;i++)
			{
				for (j=0;j<x[i].childNodes.length;j++)
				{
					if (x[i].childNodes[j].nodeType != 1) continue;

					if(x[i].childNodes[j].nodeName == 'id')
					{
						ids[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'pic')
					{
						pics[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'comp')
					{
						comps[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'compname')
					{
						compnames[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'model')
					{
						models[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'count')
					{
						product_count = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'type')
					{
						type = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'height')
					{
						heights[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'width')
					{
						widths[count] = x[i].childNodes[j].firstChild.nodeValue;

					}
					if(x[i].childNodes[j].nodeName == 'page')
					{
						page_num = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'limit')
					{
						limit = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'price')
					{
						prices[count] = x[i].childNodes[j].firstChild.nodeValue;

					}
					if(x[i].childNodes[j].nodeName == 'cat')
					{
						if (x[i].childNodes[j].firstChild)
						{
							cats[count] = x[i].childNodes[j].firstChild.nodeValue;
						}
						else
						{
							cats[count] = "";
						}
					}
					if(x[i].childNodes[j].nodeName == 'all')
					{
						all = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'sort')
					{
						sort = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'rev')
					{
						if (x[i].childNodes[j].firstChild)
						{
							rev = x[i].childNodes[j].firstChild.nodeValue;
						}
						else
						{
							rev = "";
						}

						count += 1;
					}

				}
			}

			var table = document.createElement("table");
			table.setAttribute('cellpadding', '3');
			table.setAttribute('cellspacing', '1');
			table.setAttribute('id', 'product_table');
			table.setAttribute('border', '0');
			var tbody = document.createElement('tbody');
			var headrow = document.createElement('tr');

			var headcella = document.createElement('td');
			headcella.innerHTML="";

			var headcellb = document.createElement('td');
			if (sort == "model" && rev != "1")
			{
				headrev = "1";
			}
			else
			{
				headrev = "0";
			}
			headcellb.innerHTML="<strong><a href='javascript:;' onclick='doPage("+page_num+","+limit+",\""+type+"\",\"model\",\""+headrev+"\")'>Model</a></strong>";
			var headcellc = document.createElement('td');
			if (sort == "company" && rev != "1")
			{
				headrev = "1";
			}
			else
			{
				headrev = "0";
			}
			headcellc.innerHTML="<strong><a href='javascript:;' onclick='doPage("+page_num+","+limit+",\""+type+"\",\"company\",\""+headrev+"\")'>Company</a></strong>";
			var headcelld = document.createElement('td');
			if (sort == "price" && rev != "1")
			{
				headrev = "1";
			}
			else
			{
				headrev = "0";
			}
			headcelld.innerHTML="<strong><a href='javascript:;' onclick='doPage("+page_num+","+limit+",\""+type+"\",\"price\",\""+headrev+"\")'>U.S. MSRP</a></strong>";
			if (sort == "category" && rev != "1")
			{
				headrev = "1";
			}
			else
			{
				headrev = "0";
			}
			var headcelle = document.createElement('td');
			headcelle.innerHTML="<strong><a href='javascript:;' onclick='doPage("+page_num+","+limit+",\""+type+"\",\"category\",\""+headrev+"\")'>Category</a></strong>";
			var headcellf = document.createElement('td');
			headcellf.innerHTML="<strong>Compare</strong>";
			headrow.appendChild(headcella);
			headrow.appendChild(headcellb);
			headrow.appendChild(headcellc);
			headrow.appendChild(headcelld);
			headrow.appendChild(headcelle);
			headrow.appendChild(headcellf);
			tbody.appendChild(headrow);

			for (i = 0; i < count; i++)
			{
				var row = document.createElement('tr');
				//row.setAttribute('onmouseover',"style.backgroundColor='#999999';")
				//row.setAttribute('onmouseout',"style.backgroundColor='#FFFFFF';")

				var cell = document.createElement('td');
				if (pics[i] != "N/A")
				{
					var piclink = document.createElement('a');
					piclink.setAttribute('href',"/view_product.php?id="+ids[i]+"&type="+type);
					piclink.innerHTML="<img src='http://www.bicyclebuyersguide.com/uploads/company_"+comps[i]+"/"+pics[i]+"' height='"+heights[i]+"' width='"+widths[i]+"' border='0px' />"
					cell.appendChild(piclink);
					row.appendChild(cell);
				}
				else
				{
					var na = document.createElement("span");
					na.innerHTML = "N/A";
					cell.appendChild(na);
					row.appendChild(cell);
				}

				var cell2 = document.createElement('td');

				var link = document.createElement("a");
				link.setAttribute("href","/view_product.php?id="+ids[i]+"&type="+type);
				link.innerHTML = models[i];
				cell2.appendChild(link);
				row.appendChild(cell2);

				var cell3 = document.createElement("td");
				cell3.innerHTML=compnames[i];
				row.appendChild(cell3);

				var cell5 = document.createElement("td");
				cell5.innerHTML="$"+prices[i];
				row.appendChild(cell5);

				var cell6 = document.createElement("td");
				cell6.innerHTML=cats[i];
				row.appendChild(cell6);

				var cell4 = document.createElement("td");
				cell4.setAttribute('style', 'width:50px; text-align:center;')
				cell4.innerHTML="<input type='checkbox' name='compare[]' value='"+ids[i]+"' />";
				row.appendChild(cell4);
				tbody.appendChild(row);
				table.appendChild(tbody);
			}

			var heading = document.createElement('p');
			heading.innerHTML="To compare products, check each product box then press the Compare button.<br />";
			heading.innerHTML+="For products on different pages, click <a href='javascript:;' onclick='doAll(\""+type+"\",\""+sort+"\",\""+rev+"\")'>show all</a>.";

			var button = document.createElement("input");
			button.setAttribute('type','submit');
			button.setAttribute('name','submit');
			button.setAttribute('value','Compare');
			button.setAttribute('onclick','return checkCompare(document.compare_form);');

			//Calculate pages
			var total_pages = Math.ceil(product_count / limit);
			var next_page = parseInt(page_num) + 1;
			var prev_page = parseInt(page_num) -1 ;

			if (all == 0)
			{
				if (total_pages < 1)
				{
					document.getElementById('product_display_prev').innerHTML="";
					document.getElementById('product_display_next').innerHTML="";
					document.getElementById('product_display_pages').innerHTML="No Products Found.";
				}
				else

				{
					document.getElementById('product_display').appendChild(heading);
					document.getElementById('product_display').appendChild(button);
					if (page_num == 1)
					{
						if (total_pages == 1)
						{
							document.getElementById('product_display_prev').innerHTML="";
							document.getElementById('product_display_next').innerHTML="";
						}
						else if (total_pages > 1)
						{
							document.getElementById('product_display_prev').innerHTML="";
							document.getElementById('product_display_next').innerHTML="<a href='javascript:;' onclick='doPage("+next_page+","+limit+",\""+type+"\",\""+sort+"\",\""+rev+"\")'>next &gt;</a>";
						}
					}
					else if (page_num > 1)
					{

						if (page_num >= total_pages)
						{
							document.getElementById('product_display_prev').innerHTML="<a href='javascript:;' onclick='doPage("+prev_page+","+limit+",\""+type+"\",\""+sort+"\",\""+rev+"\")'>&lt; prev</a>";
							document.getElementById('product_display_next').innerHTML="";
						}
						else
						{
							document.getElementById('product_display_prev').innerHTML="<a href='javascript:;' onclick='doPage("+prev_page+","+limit+",\""+type+"\",\""+sort+"\",\""+rev+"\")'>&lt; prev</a>";
							document.getElementById('product_display_next').innerHTML="<a href='javascript:;' onclick='doPage("+next_page+","+limit+",\""+type+"\",\""+sort+"\",\""+rev+"\")'>next &gt;</a>";
						}
					}


					document.getElementById('product_display_pages').innerHTML="Page "+page_num+" of "+total_pages+"<br /><a href='javascript:;' onclick='doAll(\""+type+"\",\""+sort+"\",\""+rev+"\")'>show all</a>";
				}
			}
			else
			{
				document.getElementById('product_display').appendChild(button);
				document.getElementById('product_display_pages').innerHTML="";
				document.getElementById('product_display_prev').innerHTML="";
				document.getElementById('product_display_next').innerHTML="";
			}
			document.getElementById('product_display').appendChild(table);
		}
	}
}




function doComps(type)
{
	document.getElementById('comp_dropdown').options.length = 0;
	var url = "/includes/ajax_companies.php?type="+type+"&dummy="+new Date().getTime();
	//alert(url);
	request1.open("GET", url, true);
	request1.onreadystatechange = updateCompanies;
	request1.send(null);

	doCats(type);
}

function doCats(type)
{
	$("#txt_cat").removeOption(/./);
	$("#txt_cat").ajaxAddOption("/includes/ajax_subcats.php?type="+type, false, false, function(){

		$.get("/includes/ajax_subcats.php?action=get_selected", function(data){
			$("#txt_cat").selectOptions(data);
		});

	});

}
function updateCompanies()
{
	if (request1.readyState == 4)
	{
		if (request1.status == 200)
		{
			var ids = new Array();
			var names = new Array();
			var selected = "";
			var count = 0;

			var xmlDoc = request1.responseXML;
			var x = xmlDoc.getElementsByTagName("companies");

			for (i=0; i<x.length;i++)
			{
				for (j=0;j<x[i].childNodes.length;j++)
				{
					if (x[i].childNodes[j].nodeType != 1) continue;

					if(x[i].childNodes[j].nodeName == 'id')
					{
						ids[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'name')
					{
						names[count] = x[i].childNodes[j].firstChild.nodeValue;
					}
					if(x[i].childNodes[j].nodeName == 'selected')
					{
						selected = x[i].childNodes[j].firstChild.nodeValue;
						count += 1;
					}

				}
			}

			var dropdown = document.getElementById('comp_dropdown')

			for (i = 0; i < count; i++)
			{
				var option = document.createElement('option');
				option.setAttribute('value',ids[i]);

				if (selected == ids[i])
				{
					option.setAttribute('selected','selected');
				}
				option.innerHTML = names[i];
				dropdown.appendChild(option);
			}
		}
	}
}function addCompare(product_id, type, clear, checked, object)
{
	var count_span = document.getElementById('compare_count');
	var total = parseInt(count_span.innerHTML);
	var setoff = 1;
	if (checked)
	{
		setoff = 0;
	}
	if (total >= 5 && setoff == 0)
	{
		alert("Sorry, you may only select up to 5 products at once for comparison.");
		object.checked = false;
	}
	else
	{
		var url = "/includes/ajax_compares.php?pid="+product_id+"&type="+type+"&clear="+clear+"&setoff="+setoff+"dummy="+new Date().getTime();
		//alert(url);
		request2.open("GET", url, true);
		request2.onreadystatechange = updateCompares;
		request2.send(null);
	}


}

function updateCompares()
{
	if (request2.readyState == 4)
	{
		if (request2.status == 200)
		{
			var total = 0;
			var total = request2.responseText;
			var count_span = document.getElementById('compare_count')

			count_span.innerHTML = total;
			//alert(total);
		}
	}
}
