var totalCount = 6 

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	//if ( expires )
	//{
	expires = 1000 * 1000 * 60 * 60 * 24;
	//} 
	//alert('expires is '+expires);
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = eval('"'+name + "=" +escape( value ) + 
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" )+'"'
	);
}
	
	
// this function gets the cookie, if it exists
function Get_Cookie( name ) 
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function addCookies(name,id,photo){
	//alert("in addcookies");
	//alert(name)	;
	//alert(id)	;
	//alert(photo)	;
	//alert("in addcookies");
	flag = true;
	for(var index =1 ; index <= totalCount ; index++)
	{

		var storedId = Get_Cookie('petId_' + index)  
		//alert("storedId -->"+storedId);
		//alert("id -->"+id);
		if(storedId==id){
			flag = false;
			break;
		}				
	}			
	
	//alert(flag);
		
	if(flag){	
		
		//shiftCookies('7','8');		
		//shiftCookies('6','7');
		shiftCookies('5','6');		
		shiftCookies('4','5');						
		shiftCookies('3','4');		
		shiftCookies('2','3');
		shiftCookies('1','2');		
		
		//alert(Get_Cookie('name_2'));		
		
		//alert(Get_Cookie('name_1'));
		//alert(Get_Cookie('petId_1'));
		//alert(Get_Cookie('photoURL_1'));
		
		//alert(Get_Cookie('name_2'));
		Set_Cookie('name_1',name);
		Set_Cookie('petId_1',id);
		Set_Cookie('photoURL_1',photo);
		
		//alert(Get_Cookie('name_1'));
		//alert(Get_Cookie('petId_1'));
		//alert(Get_Cookie('photoURL_1'));
				
		
	}
	
}

function shiftCookies(from, to) {
	
	
	//alert(from + '-->' + to);
	
	var idKey = 'petId_' + from;
	var nameKey = 'name_' + from; 
	var photoKey = 'photoURL_' + from; 	
	
	//alert('idKey -- ' + idKey);
	//alert('nameKey -- ' + nameKey);
	//alert('photoKey -- ' + photoKey);
	
	
	
	var id = Get_Cookie(idKey);
	var name = Get_Cookie(nameKey);
	var photo = Get_Cookie(photoKey);
	
	
	//alert(id);
	//alert(name);
	//alert(photo);
	
	//alert("==========================================")	;
	
	idKey = 'petId_' + to;
	nameKey = 'name_' + to; 
	photoKey = 'photoURL_' + to; 
	
	//alert('idKey -- ' + idKey);
	//alert('nameKey -- ' + nameKey);
	//alert('photoKey -- ' + photoKey);
		
	Set_Cookie(idKey,id);
	Set_Cookie(nameKey,name);	
	Set_Cookie(photoKey,photo);	
	
	
	var id = Get_Cookie(idKey);
	var name = Get_Cookie(nameKey);
	var photo = Get_Cookie(photoKey);
	//alert(id);
	//alert(name);
	//alert(photo);
	
	
	
	/*Set_Cookie(('name_'+to),(Get_Cookie('name_'+from)));
	Set_Cookie('petId_'+to,Get_Cookie('petId_'+from));
	Set_Cookie('photoURL_'+to,Get_Cookie('photoURL_'+from));				*/
}		

function genarateData() 
{	
	
		//alert('in genarateData');
		
		var data = "";
		var id = "";
		var name = "";
		var photo = "";				
		
		for(var index =1 ; index <= totalCount ; index++)
		{
			
			//alert('index-->' + index);
			data = "";					
			var id = Get_Cookie('petId_'+index) ;
			//alert(id);
			if(id == null || id =='null' || id == ''){
				break;
			}
			name     = Get_Cookie('name_'+index) ;
			photoURL = Get_Cookie('photoURL_'+index) ;					
			
			data =  data+ "<a href='/iams/en_US/data_root/html/Angel/AdoptAPet_SearchDetail.jsp?jump=yes&petid=" + id + "'>";
			data =  data+ "<img src='"+photoURL+"'/>";
			data =  data+ "";
			data =  data+ name;
			data =  data+ "</a>";			
			(document.getElementById('pet_'+index)).innerHTML=data;
			//alert('for end');

		}
		//alert('OUT genarateData');	
			
}