
var Xmlhttp;
var xmlDoc;
var CurrentType;
var CurrentpageIndex;
var PageSize=8;
var PreviousPage;
var NextPage;
var IsPaging;   
var PageCount;
var btncontrol;

var isIE=false;
if(document.all)
{
	isIE=true;
}
function createXmlHttp()
{
    var moz = (typeof document.implementation != 'undefined') && (typeof 
    document.implementation.createDocument != 'undefined');
    if (moz)
    {
	 Xmlhttp = new XMLHttpRequest();
    }
	else // for Ie
	{
		try
			{
				Xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch(e)
		{
			try
			{
				Xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				Xmlhttp = null;
			}
		}
	}  //end else;	
}// end createXmlhttp;


function getBindData(obj,Selectedlink)
{
		//document.getElementById('lblErrMsg').innerHTML='';
	document.getElementById('DivDetails').innerHTML='';
	document.getElementById('spanPageNo').innerHTML='0 of 0';
	
	var fnName;
	if(obj=='')
	{
		obj='Applications';
	}
	btncontrol=Selectedlink; 
	CurrentType=obj; 
	switch(obj)
	{
		case 'Applications': 
			fnName='getApplications';
			break;
		case 'Animations': 
			fnName='getAnimations';
			break;
		case 'Designs': 
			fnName='getDesigns';
			break;
		case 'iPhone': 
			fnName='getiPhone';
			break;
	}
	
	
	url="../pages/AjaxPage.aspx?From=" + fnName;
	
	createXmlHttp();
	if(Xmlhttp)
	{
		Xmlhttp.onreadystatechange =function ()
										{
											if(Xmlhttp.readyState==4)
											{
												if(Xmlhttp.status==200)
												{	
													displayDetails(fnName);
												}
												else 
												{
													alert("There was a problem retrieving data from the server." );
												}
											}
										}
		Xmlhttp.open("GET", url, true);
		//Xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		Xmlhttp.send(null);	
	
	}
		
		
	return false;	
}//end getText;


function displayDetails(fnName)
{
		
		var xmlData=Xmlhttp.responseXML.documentElement;
		var intinnerLoop;
		var intouterLoop;
		var baseIndex;
		var lastIndex;
		var NewTbl= document.createElement('Table');
		NewTbl.setAttribute('cellPadding',0);
		NewTbl.setAttribute('cellSpacing',0);
		
		var outerTbody= document.createElement('TBODY');
			
		if(xmlData)
		{
		
			// code for paging //
			//alert(xmlData.childNodes.length);	
			if(btncontrol==0)
			{
				CurrentpageIndex =0;
				baseIndex = 0;
				if(isIE==true)
				{
					lastIndex = xmlData.childNodes.length;
					
					if((xmlData.childNodes.length)>=8)
					{
						lastIndex = PageSize; //obj.childNodes.length;
					} 
					if(((xmlData.childNodes.length)%8)!=0)
					{
						PageCount = parseInt((xmlData.childNodes.length)/8) + 1;
					}
					else
					{
						PageCount = parseInt((xmlData.childNodes.length)/8);
					}
				}
				else
				{
					lastIndex = parseInt((xmlData.childNodes.length-1)/2);
					
					if(parseInt((xmlData.childNodes.length-1)/2)>=8)
					{
						lastIndex = PageSize; //obj.childNodes.length;
					} 
					if((parseInt((xmlData.childNodes.length-1)/2)%8)!=0)
					{
						PageCount = parseInt(parseInt((xmlData.childNodes.length-1)/2)/8) + 1;
					}
					else
					{
						PageCount = parseInt(parseInt((xmlData.childNodes.length-1)/2)/8);
					}
				
				}
			}
			
			
			
			if(btncontrol==1)            //for next button in paging
			{
				baseIndex = CurrentpageIndex * PageSize;
				lastIndex = baseIndex + PageSize;
				if(isIE==true)
				{
					if((xmlData.childNodes.length)< lastIndex)
					{
						lastIndex=xmlData.childNodes.length;
					}
				}
				else
				{
					if(parseInt((xmlData.childNodes.length-1)/2)< lastIndex)
					{
						lastIndex=parseInt((xmlData.childNodes.length-1)/2);
					}
				
				}
				
			}
				
			if(btncontrol==2)               //code for previous page          
			{
				baseIndex = CurrentpageIndex * PageSize;
				lastIndex = baseIndex + PageSize; 
			}
			
			
			if(CurrentpageIndex==0)
			{
				if(isIE==true)
				{
					document.getElementById('aPrev').className='greylinks';
				}
				else
				{
					document.getElementById('aPrev').className='greylinks';
				}
			}
			else
			{
				if(isIE==true)
				{
					document.getElementById('aPrev').className='orangelinks';
				}
				else
				{
					document.getElementById('aPrev').className='orangelinks';
				}
			}
			
			if(CurrentpageIndex==(PageCount-1))
			{
				if(isIE==true)
				{
					document.getElementById('aNext').className='greylinks';
				}
				else
				{
					document.getElementById('aNext').className='greylinks';
				}
			}
			else
			{
				if(isIE==true)
				{
					document.getElementById('aNext').className='orangelinks';
				}
				else
				{
					document.getElementById('aNext').className='orangelinks';
				}
			}
			// code for paging//
			
			var outerrow=document.createElement('TR');
			document.getElementById('spanPageNo').innerHTML=(CurrentpageIndex+1) + ' of ' + (PageCount)
			for(intouterLoop=baseIndex;intouterLoop<lastIndex;intouterLoop++)
			{
				
				if((intouterLoop%4)==0) 
				{
					outerrow=document.createElement('TR');
					outerTbody.appendChild(outerrow);
				}
				
				
					
				var outercontainer=document.createElement('TD');
				var innerTbl= document.createElement('Table');
				innerTbl.setAttribute('cellPadding',0);
				innerTbl.setAttribute('cellSpacing',0);
				if(fnName!='getiPhone')
				{
				innerTbl.style.width = "183px"
				innerTbl.style.height = "150px"
				}
				else
				{
					innerTbl.style.width = "183px";
					innerTbl.style.height = "260px";
				}
				
				var innerTbody= document.createElement('TBODY');
				var innerrow=document.createElement('TR');
				var innercontainer=document.createElement('TD');
				var imgAnchor=document.createElement('A');
				var imgApp=document.createElement('IMG');
				imgApp.style.border="0";
				if(fnName!='getiPhone')
				{
					innercontainer.style.height="80%";
					imgAnchor.target="_blank";
				}
				else
				{
					innercontainer.style.height="90%";
					innercontainer.style.verticalAlign = "middle";
				}
				innerTbl.className="bottomrightborder";
				
				
				if((intouterLoop+1)%8>4)
				{
					innerTbl.className="rightborder";
				}
				
				if((intouterLoop+1)%4==0)
				{
					innerTbl.className="bottomborder";
				}
				
				if((intouterLoop+1)%8==0)
				{
					innerTbl.className='noborder';
				}
				
				if((lastIndex-baseIndex)<=4)
				{
					innerTbl.className="rightborder";
					
					if(((intouterLoop+1)%4==0) || (intouterLoop==(lastIndex-1)))
					{
						innerTbl.className='noborder';
					}
				}
			
				//innercontainer.style.width = "135px"
				//innercontainer.style.height = "87px"
				innercontainer.align='center';
				//innercontainer.valign='top';
				imgApp.style.border="2px solid #CECCCC";
				if(fnName!='getiPhone')
				{
					innercontainer.valign='top';
					imgApp.style.width = "135px";
					imgApp.style.height = "87px";
				}
				else
				{
					innercontainer.valign='bottom';
					imgApp.style.width = "137px";
					imgApp.style.height = "209px";
				}
				if(isIE==true)
				{
					imgAnchor.href=document.createTextNode(xmlData.childNodes[intouterLoop].childNodes[2].text).nodeValue;
					imgApp.src= document.createTextNode(xmlData.childNodes[intouterLoop].childNodes[1].text).nodeValue;
				}
				else
				{
				
					imgAnchor.href=document.createTextNode(xmlData.childNodes[2*intouterLoop+1].childNodes[5].firstChild.nodeValue).nodeValue;
					imgApp.src= document.createTextNode(xmlData.childNodes[2*intouterLoop+1].childNodes[3].firstChild.nodeValue).nodeValue;
				}
				
				imgAnchor.appendChild(imgApp);
				innercontainer.appendChild(imgAnchor);
				innerrow.appendChild(innercontainer);
				innerTbody.appendChild(innerrow);
				
				innerrow=document.createElement('TR');
				innercontainer=document.createElement('TD');
				innercontainer.align='center';
				innercontainer.valign='middle';
				innercontainer.style.height = "20%";
				Anchor=document.createElement('A');
				if(fnName!='getiPhone')
				{
					Anchor.target="_blank";
				}
				
				Anchor.className="bluelinks1";
				if(isIE==true)
				{
					Anchor.href=document.createTextNode(xmlData.childNodes[intouterLoop].childNodes[2].text).nodeValue;
					Anchor.innerHTML= getString20(document.createTextNode(xmlData.childNodes[intouterLoop].childNodes[0].text).nodeValue);
				}
				else
				{
				
					Anchor.href=document.createTextNode(xmlData.childNodes[2*intouterLoop+1].childNodes[5].firstChild.nodeValue).nodeValue;
					Anchor.innerHTML= getString20(document.createTextNode(xmlData.childNodes[2*intouterLoop+1].childNodes[1].firstChild.nodeValue).nodeValue);
				}
				
				innercontainer.appendChild(Anchor);
				innerrow.appendChild(innercontainer);
				innerTbody.appendChild(innerrow);
				innerTbl.appendChild(innerTbody);
				outercontainer.appendChild(innerTbl);
				outerrow.appendChild(outercontainer);
				
							
			}
			
			//outerTbody.appendChild(outerrow);
			NewTbl.appendChild(outerTbody);
			
			
			document.getElementById('DivDetails').appendChild(NewTbl);
			
		}
		

}



function getNextPage()
{
	if (CurrentpageIndex < PageCount-1)        
	{
		CurrentpageIndex++;
		
		getBindData(CurrentType,1);
	}
	return false;
}
		
		
function getPreviousPage()
{
	if(CurrentpageIndex > 0)              
	{
		CurrentpageIndex--;
		getBindData(CurrentType,2);
	}
	return false;
}

function getString20(obj)
{
	if(obj.length>20)
	{
		return obj.substring(0,19) + '...';
	}
	else
	{
		return obj;
	}

}


