//CREATED BY HIMS MISTRY ON 24 TH MAY 2006 AT 6:00 PM
//download listing 
var page_limit = USER_PAGE_LIMIT;
var rec_limit = USER_REC_LIMIT;

var extra_argu = "";
var type ="";

function checkstatus(type,iCategoryId,start,keyword)
{

	//FOR INDICATIOR
	document.getElementById('divStatusBar').style.display=''
	//ENDS HERE	
	
	
	// Disble Best Download (vishal)
	document.getElementById('divBestDownLoad').style.display = 'none';
	

	if(type !="")
		extra_argu += "&type="+type;
	if(iCategoryId !="")
		extra_argu += "&iCategoryId="+iCategoryId;
	
	if(keyword !="")
		extr = "&keyword="+keyword;
	else
		extr = "";

	var url = AJAX_URL+"getDownloadList.php?"+extra_argu+"&rec_limit="+rec_limit+"&start="+start+extr;
	
	//CODE FOR MOZILLA
	if (window.XMLHttpRequest)
  	{
  		http=new XMLHttpRequest()
  		http.open("GET",url,true)
	  	http.onreadystatechange=handleHttpResponse_status
	  	http.send(null)
  	}
	// code for IE
	else if (window.ActiveXObject)
  	{
  		http=new ActiveXObject("Microsoft.XMLHTTP")
    	if (http)
    	{
    		http.open("GET",url,true)
    		http.onreadystatechange=handleHttpResponse_status
   			http.send()
    	}
  	}
}

function handleHttpResponse_status() 
{ 
	if (http.readyState == 4) 
	{	
      	isWorking = false;
		
		//alert(http.responseText); 
		
		if (http.responseText.indexOf('invalid') == -1) 
		{
			
			var xmlDocument = http.responseXML;
			
			var no = xmlDocument.getElementsByTagName('itemid').length;
			var totrec = xmlDocument.getElementsByTagName('totrec').item(0).firstChild.data;
			var start = xmlDocument.getElementsByTagName('start').item(0).firstChild.data;
			var recmsg = xmlDocument.getElementsByTagName('recmsg').item(0).firstChild.data;
			var var_limit = xmlDocument.getElementsByTagName('var_limit').item(0).firstChild.data;
			var tot = xmlDocument.getElementsByTagName('tot').item(0).firstChild.data;
			var categoryname = xmlDocument.getElementsByTagName('categoryname').item(0).firstChild.data;
			
			var icategoryid = xmlDocument.getElementsByTagName('icategoryid').item(0).firstChild.data;
			var type = xmlDocument.getElementsByTagName('type').item(0).firstChild.data;
			

			var html;
			html = "";
			html +='<tr>';
			html +='<td align="left" height="20" width="20%" colspan="4">';
				if(type == "music")
					html +='<img src="images/music-mp3-category.gif">&nbsp;&nbsp;<B>'+categoryname+'</B>';	
				else
					html +='<img src="images/video-category.gif">&nbsp;&nbsp;<B>'+categoryname+'</B>';	
			html +=	'<td align="left">&nbsp;</td>';
			
			
			if(parseInt(no)>0)
			{
				//LOOP
				for(x=0;x<no; x++)
				{
				var itemid = xmlDocument.getElementsByTagName('itemid').item(x).firstChild.data;
				
				var img = xmlDocument.getElementsByTagName('img').item(x).firstChild.data;
				var imgsize = xmlDocument.getElementsByTagName('imgsize').item(x).firstChild.data;
				
				var title = xmlDocument.getElementsByTagName('title').item(x).firstChild.data;	
				var description = xmlDocument.getElementsByTagName('description').item(x).firstChild.data;
				var price = xmlDocument.getElementsByTagName('price').item(x).firstChild.data;	
		 		var downloadlink = xmlDocument.getElementsByTagName('downloadlink').item(x).firstChild.data;	
				
				
				html +='<TR>';
					html +='<Br><br><TABLE width="95%"  border="0" class="table-border3" align="center" cellpadding="3" cellspacing="3" >';
					
					html += '<TR>';	
						html += '<TD width="140" align="center valign="bottom" height="25"><B>Price : $'+price+'</TD>';
						html += '<TD align="right"><a href="#"><!--img alt="Download" src="images/download.gif" border="0"></a--></TD>';
					html += '</TR>';
					
					
					html += '<TR>';
						html += '<TD valign="top" align="center" width="20%">';
							html += '<IMG align="center" class="image-border" SRC ='+img+' '+imgsize+' style="CURSOR:POINTER" onclick="viewdownloaddetail(\''+itemid+'\' , \''+img+'\');">';
						html += '</TD>';
					
						html += '<TD valign="top" width="80%">';
						
									html += '<TABLE  border="0" width="100%">';
									html += '<TR valign="top">';	
										html += '<TD><B>'+title+'</B></TD>';
									html += '</TR>';
									html += '</TABLE>';
									
									html += '<TABLE border="0" height="50" width="100%">';
									html += '<TR valign="top">';
										html += '<TD>'+description+'&nbsp;&nbsp;...<Br><a href="javascript:viewdownloaddetail(\''+itemid+'\' , \''+img+'\');">More Details</a></TD>';
									html += '</TR>';
									html += '</TABLE>';
								
									html += '<TABLE border="0" width="100%">';
									html += '<TR valign="top">';
										html += '<TD align="left"><a href="javascript:StatusCart(\''+itemid+'\',\''+price+'\',\'add_cart\');"><img src="images/addtocart.gif" border="0"></a>&nbsp;<a href="javascript:StatusCart(\''+itemid+'\',\''+price+'\',\'view_cart\');;"><img src="images/viewcart-btn.gif" border="0"></a>&nbsp;<a href="'+downloadlink+'"><img src="images/playsong.gif" border="0"></a></TD>';
										html += '<TD align="right">&nbsp;</TD>';
									html += '</TR>';
									html += '</TABLE>';
					 
						html += '</TD>';
				html += '</TR>';
				
				html += '</TABLE></TD></TR>';
				}
			}
			else
			{

					html +='<TR>';
					html +='<br><br><TABLE width="100%" border="0" class="table-border3" align="center" cellpadding="0" cellspacing="1" >';
					html += '<TR>';	
						html += '<TD width="10%" align="center" height="40"><B>No Downloads!</B></TD>';
					html += '</TR>';
					html += '</TABLE>';
					html += '</TR>';				
			
			}
			
			///PAGING 
			
			setPaging(totrec, start,icategoryid,type);
			document.getElementById("RECMSG_TOP").innerHTML = recmsg;
			//PAGING ENDS
			
			//FOR INDICATIOR
			//document.getElementById('ind').style.display='none'
			document.getElementById('divStatusBar').style.display='none'
			//ENDS HERE	
			
			document.getElementById("listing").innerHTML = html;		
			isWorking = false;
		}
	}			
			
}

//FUNCTION FOR PAGING 

var varmsg = "";
var start;
var var_limit = ''
var page_string = "";
var start_loop;
	
// Top Paging link & Bottom Pagin link set from this function
function setPaging(tot_rec,start,icategoryid,type)
{

	if(tot_rec == "")	tot_rec = 0;
	page_string = "";
	extra = "";
	num_limit = 0;
	last_rec = 0;
	if(start != '0'){
       num_limit = (start-1)*rec_limit;
	   var_limit = " LIMIT "+num_limit+" ,"+rec_limit; 
   }else var_limit = " LIMIT 0,"+rec_limit;

	if(start == '0')		{
		start = '1';
	}
	document.frmlist.start.value = start;
	tot_pages = Math.ceil(tot_rec/rec_limit) ; 
	loop_limit = ((page_limit > tot_pages) ? tot_pages : page_limit) ;
	start_loop = Math.floor(start/page_limit);
	
	if(start_loop != (start/page_limit))
		start_loop = start_loop * page_limit+1;
	else
		start_loop = (start_loop-1) * page_limit+1;
		
	if(start_loop>page_limit)
	{
		prev_loop = start_loop - 1;
		page_string += "&nbsp;&nbsp;<a class='linksnar11' href='javascript:checkstatus(\""+type+"\","+icategoryid+","+prev_loop+",\"\");' >Prev</a>&nbsp;&nbsp;";
	}	

	for(loop=1 ; loop<=loop_limit ; loop++)
	{
		if(start_loop>tot_pages) break;
		if(parseInt(start_loop) == parseInt(start))
		{
			page_string += "<span class='amar16'> "+start_loop+"</span> ";
		}	
		else{
			page_string +=" <a href='javascript:checkstatus(\""+type+"\","+icategoryid+","+start_loop+",\"\");'  class='linksnar11'>"+start_loop+"</a>";
		}
		start_loop++;
	}

	if(start_loop<=tot_pages)
		page_string += "&nbsp;&nbsp;<a class='linksnar11' href='javascript:checkstatus(\""+type+"\","+icategoryid+","+start_loop+",\"\");' >Next</a>&nbsp;&nbsp;";
	
	document.getElementById("PAGING-TOP").innerHTML = page_string;
	//document.getElementById("PAGING-BOTTOM").innerHTML = page_string;
	//return var_limit;
}
//ENDS HERE
//FUNCTION FOR CENTER POPUP AND EDIT RESPONSE
function viewdownloaddetail(iItemId,img) 
{	
	var w = 500;
	var h = 400;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var page = "downloaddetail.php?iItemId="+iItemId+"&img="+img;
	windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
	+ "scrollbars=no,menubars=no,toolbars=no,resizable=no ,status=no";
	window.open(page, "Popup", windowprops);

}//ENDS HERE