﻿var resulturl;
function displaypro(xml,ordervalue) {
		if(typeof xml == "string"){
			if( window.DOMParser ){
        	   xml = (new DOMParser()).parseFromString(xml, 'text/xml');
           }else if( window.ActiveXObject && window.GetObject ) {
        	   var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
               dom.loadXML( xml );
               xml = dom;
           }
		 }
	     var html="";
		 var linkhtml="";
		 var prohtml='';
		 var recordcount =$(xml).find('productcount').text();
		 var numberPerPage =12;
		 var countnum=0;
		 var pagenum=1;
		  if (Math.floor(recordcount)%(numberPerPage)==0)
		  {
		     countnum=Math.floor((Math.floor(recordcount))/numberPerPage);
		  }
		  else
		  { 
		     countnum=Math.floor((Math.floor(recordcount))/numberPerPage)+1;
		  }
		 if (recordcount == "0")
		 {
		   html="<p align='center' style=\"padding-top:100px;\">There are currently no goods available for the criteria you selected.</p>";
		 }
		 else
		 {
			   $(xml).find('productlist').each(function(){   
					var $obj = $(this); 									 
					var index = $obj.attr("id");
					var proid = $obj.find("proid").text();
					var linkurl = $obj.find('prourl').text();
					var protitle = $obj.find('protitle').text();
					var reprice = $obj.find('prorprice').text();
					var price = $obj.find('proprice').text();
					var tmpimgpath = $obj.find('proimgpath').text();
					var imgpath="product_images.php?image_id="+tmpimgpath;
					var tmpsave = $obj.find('prosave').text();
					var save=tmpsave+".0%";
					pagenum= $obj.find("pronumpage").text();
			        prohtml+='<div class="prolist"><div class="prolistimg"><a href="'+linkurl+'" target="_blank"><img src="'+imgpath+'" border="0" width="180" height="135"/></a></div><div class="prolistdes"><div class="prolistname"><a href="'+linkurl+'" target="_blank">'+protitle+'</a></div><div class="prolistsave">Retail price: '+reprice+'</div><div class="prolistprice">Our Price: '+price+'</div><div class="prolistsave">Save: '+save+'</div></div></div>';										 														
				});
			 linkhtml=make_search_footer(countnum,numberPerPage,pagenum,recordcount,ordervalue);
			 var foothtml="<div class='propagination'>"+linkhtml+"</div>";
			 html=foothtml+prohtml+foothtml;
			 
		 }
		  $('#rowsContainer').empty();
		  $('#rowsContainer').append($(html));
}
 function display(){ 
   $('#rowsContainer').empty();
   var tmpsearchresult="<p align='center' style=\"padding-top:100px;\"><img src='images/loading-indicator-big-transparent.gif' border='0'/></p>";
    $('#rowsContainer').append($(tmpsearchresult)); 
	var temresult=resulturl;
   $.ajax({
   type: "get",
   url: temresult,
   success: function(xml){
   displaypro(xml);
  }
  });
  }
  function getfullurl(pagenum,ordervalue)
  {
	  var fullurl=document.location.href;
	  var tmpurlink=fullurl.substring(0,fullurl.lastIndexOf("_"))+"_"+pagenum+".html?order="+ordervalue+"";
      return tmpurlink;
  }
  function jump(pagenum,ordervalue)
  {
	  window.location.href=getfullurl(pagenum,ordervalue);
  }
 function make_search_footer(total_page,page_sum,numpage,records,ordervalue){ 
var html='';
 if(total_page>=1){
 var page =numpage;
 if(page<1)
 {
	 page=1;
 }
 if(page > total_page)
 {
   page=total_page;
 }
 var begin=parseInt(page)-4;
 var end=parseInt(page)+5;
 if (page <5)
 {
	 begin=1;
	 end=10;
 }
  if( parseInt(page)>parseInt(parseInt(total_page)-5)){
        begin = total_page-9;
        end = total_page;
    }
 if(begin<1){
        begin = 1;
    }
	if(end>=total_page){
        end = total_page;
    }
  html+=''+records+' Good(s) Found&nbsp;&nbsp;&nbsp;';
 if(numpage>1){
	 
   html+='<a href="javascript:jump('+(numpage-1)+','+ordervalue+');" style="text-decoration:none; ">Prev</a>&nbsp;';
 }
 for(var c=begin;c<=end;c++){
  if(c==numpage){
   html+='<a class="current">'+c+'</a>&nbsp;'; 
  }else{
   html+='<a href="javascript:jump('+c+','+ordervalue+');" style="text-decoration:none; ">'+c+'</a>&nbsp;';
  }
 }
 if( numpage<total_page ){
 html+='<a href="javascript:jump('+(parseInt(numpage)+1)+','+ordervalue+')" style="text-decoration:none;">Next</a>&nbsp;';
 }
 
 }
 return html;

}
		
