
function getPagination(totalPages, currentPage){
	var pagination = document.createElement("span"); //alert("3");
	pagination.setAttribute("id", "pagination"); //alert("4");
	pagination.setAttribute("class", "paging"); //alert("5");
	totalPages = parseInt(totalPages); //alert("6");
	currentPage = parseInt(currentPage); //alert("7");
	if(totalPages>1){
		for(i=1;i<=totalPages;i++){
			if(i==currentPage){
				newLink = document.createElement("span"); //alert("9");
			}else{
				var newLink = document.createElement("a"); //alert("12");
				newLink.setAttribute("href","javascript:retrieveListings('" + seller + "', '" + i + "');");
			}
			newLink.setAttribute("class","paging"); //alert("19");
			newLink.innerHTML = i; //alert("20");
			pagination.appendChild(newLink); //alert("21");
		}
		
	}
	return pagination; //alert("23");
}//END FUNCTION getPagination

function addListing(listing){
	var listingContainer = document.createElement("table");
	listingContainer.setAttribute("cellpadding","3");
	listingContainer.setAttribute("cellspacing","0");
	listingContainer.setAttribute("border","0");
	listingContainer.setAttribute("class","listingContainer");
	var tBody = document.createElement("tbody");
	listingContainer.appendChild(tBody);
	//alert("30");
	var firstRow = document.createElement("tr");
	var firstCell = document.createElement("td");
	firstCell.setAttribute("class","listingImage");
	var listingImg = document.createElement("img");
	listingImg.src="addons/sellersListings/servlets/thumb.php?w=100&h=90&image=/"+listing["listingImage"];
	listingImg.onclick=function(){
		window.location="index.php?a=2&b="+listing["listingID"];
	}
	//alert("39");
	firstCell.appendChild(listingImg);
	firstRow.appendChild(firstCell);
	tBody.appendChild(firstRow);
	//alert("43");
	var secondRow = document.createElement("tr"); //alert("44");
	var secondCell = document.createElement("td"); //alert("45");
	var myLink = document.createElement("a"); //alert("46");
	//alert("47");
	myLink.href="index.php?a=2&b="+listing["listingID"];
	myLink.innerHTML = listing["listingTitle"] + "<br />" + listing["listingPrice"];
	//alert("50");
	secondCell.appendChild(myLink);
	secondRow.appendChild(secondCell);
	tBody.appendChild(secondRow);
	//alert("54");
	if(document.getElementById("sellersAds")){
		document.getElementById("sellersAds").appendChild(listingContainer);
	}
}

function showListings(xmlDoc){
	var listings = xmlDoc.getElementsByTagName("listings")[0];
	if(document.getElementById("sellersAds")){
		document.getElementById("sellersAds").innerHTML = "";
	}
	for(i=0;i<listings.childNodes.length;i++){
		var listing = new Array();
		listing["listingID"] = xmlDoc.getElementsByTagName("listingID")[i].childNodes[0].nodeValue;
		listing["listingTitle"] = xmlDoc.getElementsByTagName("listingTitle")[i].childNodes[0].nodeValue;
		listing["listingPrice"] = xmlDoc.getElementsByTagName("listingPrice")[i].childNodes[0].nodeValue;
		listing["listingImage"] = xmlDoc.getElementsByTagName("listingImage")[i].childNodes[0].nodeValue;
		addListing(listing);
	}
	var pagination = getPagination(listings.getAttribute("totalPages"), listings.getAttribute("currentPage"));
	var clearDiv = document.createElement("div");
	clearDiv.style.clear="both";
	clearDiv.style.textAlign="right";
	clearDiv.appendChild(pagination);
	if(document.getElementById("sellersAds")){
		document.getElementById("sellersAds").appendChild(clearDiv);
	}
}

function retrieveListings(seller, page){
	rObj = new ajaxRequest("index.php");
	rObj.addParameter("a","ap");
	rObj.addParameter("addon","sellersListings");
	rObj.addParameter("page","servlet");
	rObj.addParameter("seller", seller);
	rObj.addParameter("p",page);
	rObj.handler = function(){
		if(rObj.httpRequest.readyState == 4){
			if(!rObj.httpRequest.responseXML){
				if(rObj.httpRequest.responseText){
					alert(rObj.httpRequest.responseText);
				}else{
					alert("No Response");
				} 
			}else{
				showListings(rObj.httpRequest.responseXML);
			}
		}
	};
	rObj.get();
}

function initializeSellersListings(){
	retrieveListings(seller, 1);
}
