function makeLinks(catid){
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		switch(links[i].className){
			case "results-1":
				links[i].onclick=function() {
					update_order(catid, 'productName', 'ASC');
					return false;
				}
				break
			case "results-2":
				links[i].onclick=function() {
					update_order(catid, 'productName', 'DESC');
					return false;
				}
				break
			case "results-3":
				links[i].onclick=function() {
					update_order(catid, 'productPrice', 'ASC');
					return false;
				}
				break
			case "results-4":
				links[i].onclick=function() {
					update_order(catid, 'productPrice', 'DESC');
					return false;
				}
				break
			case "results-5":
				links[i].onclick=function() {
					update_numresults('5', catid);
					return false;
				}
				break
			case "results-6":
				links[i].onclick=function() {
					update_numresults('10', catid);
					return false;
				}
				break
			case "results-7":
				links[i].onclick=function() {
					update_numresults('20', catid);
					return false;
				}
				break
			case "results-8":
				links[i].onclick=function() {
					update_numresults('50', catid);
					return false;
				}
				break
		}
	}
  if (!document.getElementsByClassName) return false;
  var links = document.getElementsByClassName("prodnavlinks");
  var length = links.length + 2;
  for (var i=2; i < length; i++) {
	  el = document.getElementById('prodnav-'+i);
	  var char = el.href.length;
	  var start = el.href.charAt(char-2);
	  el.href = 'javascript:update_results('+start+','+catid+')';
  }
}

var http_request = false;
function makePOSTRequest(url, parameters, type) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open(type, url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		//alert(http_request.responseText);
		result = http_request.responseText;
		document.getElementById('prod-cart').innerHTML = result;    
		Effect.Pulsate('cart-box', {duration:0.5});
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function detget(obj,id) {
  var poststr = "productUUID=" + encodeURI( document.getElementById("productUUID-"+id).value ) +
				"&quantity=" + encodeURI( document.getElementById("quantity-"+id).value ) +
				"&productName=" + encodeURI( document.getElementById("productName-"+id).value );
  makePOSTRequest('process-cart.php', poststr, 'POST');
}

function get(obj) {
  var poststr = "productUUID=" + encodeURI( document.getElementById("productUUID").value ) +
				"&quantity=" + encodeURI( document.getElementById("quantity").value ) +
				"&productName=" + encodeURI( document.getElementById("productName").value );
  makePOSTRequest('process-cart.php', poststr, 'POST');
}

function cartdelete(uuid) {
	var poststr = "productUUID=" + uuid + "&quantity=0";
	makePOSTRequest('process-cart.php', poststr, 'POST');
}

function deletefromcart(uuid,name) {
	Dialog.confirm("Are you sure you want to delete <strong>"+name+"</strong> from your shopping cart?", 
				  {windowParameters: {width:300}, okLabel: "Yes", cancelLabel: "No", 
				   buttonClass: "myButtonClass",
				   id: "myDialogId",
				   cancel:function(win) {},
				   ok:function(win) {cartdelete(uuid); return true;}
				  });
}

var dynamicContent_ajaxObjects = new Array(); 
var jsCache = new Array();
var enableCache = true; 

function ajax_loadContent(divId,pathToFile,catid)
{
  if(enableCache && jsCache[pathToFile]){
    document.getElementById(divId).innerHTML = jsCache[pathToFile];
    return;
  }
  
  var ajaxIndex = dynamicContent_ajaxObjects.length;
  document.getElementById(divId).innerHTML = '<img src="i/layout/progress.gif" /> Loading...';
  dynamicContent_ajaxObjects[ajaxIndex] = new sack();
  dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;

  dynamicContent_ajaxObjects[ajaxIndex].onCompletion = 
  function(){ ajax_showContent(divId,ajaxIndex,pathToFile,catid); };  

  dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
} 

function ajax_showContent(divId,ajaxIndex,pathToFile,catid)
{	
  	document.getElementById(divId).innerHTML =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[pathToFile] = 
    dynamicContent_ajaxObjects[ajaxIndex].response;
  }
  dynamicContent_ajaxObjects[ajaxIndex] = false;
}

function update_results(startpage,category){
	ajax_loadContent("product-list","ajax/update-productlist.php?startpage="+startpage+"&Category="+category,category);
	window.location.hash = "new";
}

function update_numresults(num,category){
	ajax_loadContent("product-list","ajax/update_numresults.php?startpage=0&Number="+num+"&Category="+category,category);
}

function update_order(category,field,order){
	ajax_loadContent("product-list","ajax/update_order.php?startpage=0&Field="+field+"&Category="+category+"&Order="+order,category);
	setTimeout("makeLinks("+category+")",300);
	//makeLinks(category);
}
