var extra_arg ="";
// Recode Limit
var page_limit = USER_PAGE_LIMIT;
var rec_limit = USER_REC_LIMIT;
var PAGING_TOP ='';
var PAGING_BOTTOM ='';
function getDownLoadItem(eStatus,sorton, start)
{	
	//FOR INDICATIOR
	
	if(document.getElementById('divSearch'))
		document.getElementById('divSearch').style.display = '';
	//ENDS HERE	
	var extraArgu = "";
	var checkedArr = "";
	var getParamDecode = "";
	
	PAGING_TOP ='';
	
	PAGING_BOTTOM ='';
	
	if(eStatus == 'Showall'){
		stat = document.frmlist.stat.value;	
		if(stat == '1')	
			stat1 = 0;
		else	
			stat1 = 1;
		extraArgu += "&stat="+stat1;
		extraArgu += "&start="+start+"&rec_limit="+rec_limit;
	}
	if(eStatus == 'Search')
	{ 
		stat = document.frmlist.stat.value;	
		if(stat == '1')	
			stat1 = 0;
		else	
			stat1 = 1;
		
		extraArgu += "&stat="+stat1;
		extraArgu += '&search=true';
		
		var option = "doi.vTitle";
		stat = document.frmlist.stat.value;	
		extraArgu += "&start="+start+"&rec_limit="+rec_limit;	

		if(document.frmlist.keyword)
			extraArgu += "&keyword="+document.frmlist.keyword.value+"&option="+option;
		if(document.frmlist.iCategoryTypeId)
			extraArgu += "&iCategoryTypeId="+document.frmlist.iCategoryTypeId.value;
		if(document.frmlist.iCategoryId && document.frmlist.iCategoryId.value !="0")
			extraArgu += "&iCategoryId="+document.frmlist.iCategoryId.value;
		if(document.frmlist.FromPrice)
			extraArgu += "&FromPrice="+document.frmlist.FromPrice.value;
		if(document.frmlist.ToPrice)
			extraArgu += "&ToPrice="+document.frmlist.ToPrice.value;
		document.frmlist.keyword.value = '';
	}else{
		stat = document.frmlist.stat.value;	
		if(stat == '1')	
			stat1 = 0;
		else	
			stat1 = 1;
		extraArgu += "&stat="+stat1;
		extraArgu += "&start="+start+"&rec_limit="+rec_limit;
	}
	// FOR EXTRA PARAM	
	var file_type= document.frmlist.file_type.value;
	if(document.frmlist.mode)
	var mode= document.frmlist.mode.value;
	if(mode == 'AllItem')
	{
		var iCategoryId = document.frmlist.iCategoryId.value;
		extraArgu += "&iCategoryId="+ iCategoryId;
	}
	
	mainArgu = "&file_type="+file_type+"&mode="+mode+"&eStatus="+eStatus+extraArgu;
	var url = AJAX_URL+"getBestDownLoadItem.php?"+mainArgu
	//alert(url); 	
		document.getElementById('listing').style.display=''
		if (window.XMLHttpRequest)
	  	{
	  		http=new XMLHttpRequest()
	  		http.open("GET",url,true)
		  	http.onreadystatechange=dispalydata;
		  	http.send(null)
	  	}
		// code for IE
		else if (window.ActiveXObject)
	  	{
	  		http=new ActiveXObject("Microsoft.XMLHTTP")
	    	if (http)
	    	{  
	    		http.open("GET",url,true)
	    		http.onreadystatechange = dispalydata;
				http.send()
	    	}
	  	}
		/*if(mode == 'AllItem'){ alert(document.getElementById('divExtraItem').style.display);
			document.getElementById('divExtraItem').style.display ='none';
		}*/
		document.getElementById("tempval").innerHTML = url;
}   

function dispalydata() 
{ 
	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 mode = xmlDocument.getElementsByTagName('mode').item(0).firstChild.data;
			if(xmlDocument.getElementsByTagName('file_type').item(0).firstChild)
				var file_type = xmlDocument.getElementsByTagName('file_type').item(0).firstChild.data;
			if(xmlDocument.getElementsByTagName('categoryname').item(0).firstChild)
				var categoryname = xmlDocument.getElementsByTagName('categoryname').item(0).firstChild.data;
			if(xmlDocument.getElementsByTagName('estatus').item(0).firstChild)
				var eStatus = xmlDocument.getElementsByTagName('estatus').item(0).firstChild.data;
			//alert(totrec +','+ start+','+eStatus);
			setPaging(totrec, start,eStatus);
			
			var html;
			html = "";
			html = '<TABLE width="95%"  border="0" align="center" cellpadding="0" cellspacing="0" >'
			html +='<tr>';
			if(mode == "AllItem"){
				//html +='<tr>';
				html +='<td align="left" height="20" width="150" >';
				if(file_type == "music")
					html +='<img src="images/music-mp3-category.gif"></td>';	
				else
					html +='<img src="images/video-category.gif"></td>';
				html +='<td width="30%" align="left"><B>'+categoryname+'</B></td>';
				//html +=	'<tr><td>&nbsp;</td></tr>';
			}else if (mode == "Best"){
				//html +='<tr>';
				html +='<td align="left" height="20" width="20%" >';
				html +='<a id="lnlBest"  href="#" onclick="checkstatus(\''+file_type+'\', 0, \'Best\' )"><img id="imgBest" src="images/bestdowload-a.gif" border="0" /></a></td>';
				html +='<td width="20%"><a id="lnlFeatured" href="#" onclick="checkstatus(\''+file_type+'\', 0, \'Featured\')" ><img id="imgFeatured" src="images/featurelist-i.gif" border="0" /></a></td>';
				
				//html +='<span align="right" id="RECMSG_TOP"></SPAN>11<span align="right" id="PAGING-TOP"></span></td>';
				//html +='</tr>';
				/*if(document.getElementById('imgFeatured'))
					document.getElementById('imgFeatured').src= SITE_URL + "images/featurelist-i.gif"
				if(document.getElementById('imgBest'))
					document.getElementById('imgBest').src= SITE_URL + "images/bestdowload-a.gif"
				*/
			}else if (mode == "Featured"){
				html +='<td align="left" height="20" width="20%" >';
				html +='<a id="lnlBest"  href="#" onclick="checkstatus(\''+file_type+'\', 0, \'Best\' )"><img id="imgBest" src="images/bestdowload-i.gif" border="0"/></a></td>';
				html +='<td width="20%" ><a id="lnlFeatured" href="#" onclick="checkstatus(\''+file_type+'\', 0, \'Featured\')" ><img id="imgFeatured" src="images/featurelist-a.gif" border="0"/></a></td>';
				
				//html +='<span align="right" id="RECMSG_TOP"></SPAN>11<span align="right" id="PAGING-TOP">'+PAGING_TOP+'</span></td>';
				
				//html +='</tr>';
				/*document.getElementById('imgFeatured').src= SITE_URL + "images/featurelist-a.gif"
				document.getElementById('imgBest').src= SITE_URL + "images/bestdowload-i.gif"
				*/
			}
			html +='<td align="right">&nbsp;<span align="right" id="RECMSG_TOP"></SPAN><span align="right" id="PAGING-TOP">'+PAGING_TOP+'</span></td>';
			html +='</tr>';
			if(mode == "AllItem"){
				html +=	'<tr><td colspan="3">&nbsp;</td></tr>';
			}
			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;	
					if(xmlDocument.getElementsByTagName('description').item(x).firstChild)
						var description = xmlDocument.getElementsByTagName('description').item(x).firstChild.data;
					else
						var description = '';
					var price = xmlDocument.getElementsByTagName('price').item(x).firstChild.data;	
		 			var downloadlink = xmlDocument.getElementsByTagName('downloadlink').item(x).firstChild.data;	
					
					
					html +='<TR><td colspan="3">';
					html +='<TABLE width="100%"  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 class="wmatter2" valign="top" align="center" width="30%">';
							html += '<IMG align="center" class="image-border" SRC ="'+img+'" '+imgsize+' style="CURSOR:POINTER" onclick="viewdownloaddetail(\''+itemid+'\' , \''+img+'\');">';
							
							if(file_type == "music"){
								html += '<BR><BR><span title="Click to Play Song">' +PlaySong(downloadlink) + '</span>';
								//html += '<BR><BR><span id="spnPlayId_'+x+'"><img src="images/playsong.gif" align="bottom" style="CURSOR:POINTER" alt="Play Music" onclick="playSound(\''+downloadlink+'\','+x+','+no+')"></span>';
								//html += '<span style="display:none" id="spnStopPlayId_'+x+'"><img src="images/btn-stop.gif" align="bottom" style="CURSOR:POINTER" alt="Stop Music" onclick="stopSound('+x+')"></span>';
								//html += '&nbsp;&nbsp;<span style="display:''" id="spnPlayImg_'+x+'">'+ MusicPlayImg()+'</span>';
								
							}else
								html += '<BR><BR><img src="images/btn-playvideo.gif" align="bottom" style="CURSOR:POINTER" alt="Play Video" onclick="PlaySongVideo(\''+downloadlink+'\',\''+itemid+'\',\''+img+'\')">';
							
						html += '</TD>';
					
						html += '<TD valign="top" width="70%">';
						
									html += '<TABLE  border="0" width="100%">';
									html += '<TR valign="top">';	
										html += '<TD><B>'+title+'</B></TD>';
									html += '</TR>';
									html += '<TR valign="top">';
										html += '<TD>'+description+'&nbsp;&nbsp;...<Br><a href="javascript:viewdownloaddetail(\''+itemid+'\' , \''+img+'\');">More Details</a></TD>';
									html += '</TR>';
									html += '<TR valign="top" align="center">';
										html += '<TD align="left"><a href="javascript:StatusCart(\''+itemid+'\',\''+price+'\',\'add_cart\');"><img src="images/addtocart.gif" border="0"></a>&nbsp;<a href="#tblCart" onclick="javascript:StatusCart(\''+itemid+'\',\''+price+'\',\'view_cart\');"><img src="images/viewcart-btn.gif" border="0"></a>&nbsp;';
										/*if(file_type == "music"){
											html += '<span id="spnPlayId_'+x+'"><img src="images/playsong.gif" align="bottom" style="CURSOR:POINTER" alt="Play Music" onclick="playSound(\''+downloadlink+'\','+x+','+no+')"></span>';
											html += '<span style="display:none" id="spnStopPlayId_'+x+'"><img src="images/btn-stop.gif" align="bottom" style="CURSOR:POINTER" alt="Stop Music" onclick="stopSound('+x+')"></span>';
											html += '&nbsp;&nbsp;<span style="display:none" id="spnPlayImg_'+x+'">'+ MusicPlayImg()+'</span>';
										}else
											html += '<BR><BR><img src="images/btn-playvideo.gif" align="bottom" style="CURSOR:POINTER" alt="Play Video" onclick="PlayVideo(\''+downloadlink+'\')">';
										*/
										html += '</TD>';
									html += '</TR>';
									html += '</TABLE>';

						html += '</TD>';
					html += '</TR>';
					
					html += '</TABLE></TD></TR>';
					html += '<TR>';	
						html += '<TD width="10%" align="center" height="3"><img src="images/spacer.gif" height="1" weight="1"></TD>';
					html += '</TR>';
				}
			}else{
				html +='<TR>';
					html +='<TABLE width="97%" 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>';				
			}
			html += '</td><TR>';	
			html += '<TD colspan="3" align="right" height="3"><span id="PAGING-BOTTOM">'+PAGING_BOTTOM+'&nbsp;&nbsp;</span></TD>';
			html += '</TR>';
			html += '</TABLE>';
			//alert(html );
			///PAGING 
	
			//setPaging(totrec, start,eStatus);
		
			//FOR INDICATIOR
			if(document.getElementById('divSearch'))
				document.getElementById('divSearch').style.display='none';
	
			if(document.getElementById('divStatusBar'))
				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,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='#' onClick='getDownLoadItem(\""+type+"\",\"11\","+prev_loop+");return false;'>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='#' onClick='getDownLoadItem(\""+type+"\",\"11\","+start_loop+");return false; ' class='linksnar11'>"+start_loop+"</a>";
		}
		start_loop++;
	}

	if(start_loop<=tot_pages)
		page_string += "&nbsp;&nbsp;<a class='linksnar11' href='#' onClick='getDownLoadItem(\""+type+"\",\"11\","+start_loop+");return false; '>Next</a>&nbsp;&nbsp;";

	/*document.getElementById("PAGING-BOTTOM").innerHTML = page_string;
	if(document.getElementById("PAGING-TOP")){
		if(page_string !='')
			document.getElementById("PAGING-TOP").innerHTML = "Page: "+page_string;
			PAGING_TOP = "Page: "+page_string;
	}*/
	if(page_string !=''){
		PAGING_TOP = "Page: "+page_string;
		PAGING_BOTTOM = "Page: "+page_string;
	}
}
//ENDS HERE
//FUNCTION FOR CENTER POPUP AND EDIT RESPONSE
function checkstatus(type,iCategoryId, mode)
{  
	document.frmlist.iCategoryId.value = iCategoryId;
	document.frmlist.file_type.value = type;
	document.frmlist.mode.value = mode;
	//alert(mode + ' > ' + type + ' >' +  iCategoryId);
	getDownLoadItem();
}
function PlaySong(soundfilepath)
{
var id = 1;
//alert(soundfilepath)
var winContent ='';
/*
winContent += '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,47,0" WIDTH="25" HEIGHT="25" id="'+ id + '">'
winContent += '	<PARAM NAME=movie VALUE="'+ SITE_URL + 'wimpy_button.swf?theFile=' + soundfilepath + '">';
winContent += '	<PARAM NAME=quality VALUE=high>';
winContent += '	<PARAM NAME=bgcolor VALUE=#FFFFFF>';
winContent += '<param name=AUTOPLAY VALUE=true>'; 
winContent += '	<param name=wmode value=transparent>';
winContent += '	<EMBED src="'+ SITE_URL + 'wimpy_button.swf?theFile=' + soundfilepath + '" quality=high bgcolor=#FFFFFF  WIDTH="25" HEIGHT="25" NAME="' + id + '" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" />';
winContent += '	</OBJECT>';
*/

winContent += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,22,0" width="65" height="15" id="new_bt" align="middle">';
winContent += '<param name="allowScriptAccess" value="sameDomain" />';
winContent += '<param name="movie" value="'+SITE_URL +'small_bt.swf?theFile=' + soundfilepath + '">';
winContent += '<param name="quality" value="high" />';
winContent += '<param name="bgcolor" value="#990000" />';
winContent += '<param name="wmode" value="transparent" />';
winContent += '<embed src="'+SITE_URL +'small_bt.swf?theFile=' + soundfilepath + '&autoplay=no" quality="high" bgcolor="#990000" width="65" height="15" name="new_bt" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
winContent += '</object>';

return winContent;
//document.write(winContent); 
}
