/*
Functions for wishlist
*/

var userWishCookie;
var userLastVisitCookie;
var dealid;
var orgDomain;
function wishList(id,userId,domainName)
{		
	cookiename = 'wishes';
	domainName1 = '"'+domainName+'"';
	userId1 = '"'+userId+'"';
	var usr;
	//alert('current - '+temp);
	if(userId != '' && userId != null)
	{		
		if(Get_Cookie(cookiename) != null && Get_Cookie(cookiename) != '')
		{
			cookieAllVal = Get_Cookie(cookiename); //get current cookie value with user Id in it
			ckVal = cookieAllVal.split('###');
			dbUsrId = ckVal[0];
			
			if(dbUsrId == userId) //if both ID are same, database ID and sent ID
			{
				cookieVal = ckVal[1];
			}
			else
			{				
				dealid = id;
				orgDomain = domainName;
				usr = 'userId='+userId+'&type=wishlist';
				getCookieStr(dealid,orgDomain,usr);
				return false;
			}
		}
		else
		{
			Set_Cookie(cookiename, userId+'###', 365,'/',domainName); //first time login and wishes cookie is not set			
			cookieVal = '';
		}
	}
	else
	{
		cookieVal = Get_Cookie(cookiename); //get current cookie value without user Id in it	
	}
	
	var arr = new Array();		
	if(cookieVal == ',')
	{
		cookieVal = '';	
	}
	
	if(cookieVal != null && cookieVal != '')
	{				
		arr = cookieVal.split(',');		
		//alert('Length '+arr.length);
		if(inArray(arr,id) == false)
		{
			if(arr.length == '')
			{
				cookieVal = id;	
			}
			else
			{
				cookieVal = cookieVal+','+id;		
			}
			$('#'+'wish'+id).removeClass("btn-add-wish-prf");
			$('#'+'wish'+id).addClass("btn-rmv-wish-prf");
			//document.getElementById('wish'+id).innerHTML = '<a href="javascript: void(0)" onclick="return removeWishList2("'+id+'","1","'+userId+'","'+domainName+'");">Remove From Wishlist</a>';
			document.getElementById('wish'+id).innerHTML = "<a href='javascript: void(0)' onclick='return removeWishList2("+id+",1,"+userId1+","+domainName1+");'>Remove From Wishlist</a>";
			//Set_Cookie('wishes'+checkdep, id, 365);
			//alert('id = '+temp);	
		}
		
		if(userId != '' && userId != null)
		{
			pass1 = 'userId='+userId+'&string1='+cookieVal+'&type=wishlist';
			passCookieData(pass1);
			Set_Cookie(cookiename, userId+'###'+cookieVal, 365,'/',domainName);
		}
		else
		{
			Set_Cookie(cookiename, cookieVal, 365,'/',domainName);
		}
	}
	else
	{	
		if(userId != '' && userId != null)
		{
			pass1 = 'userId='+userId+'&string1='+id+'&type=wishlist';
			passCookieData(pass1);
			Set_Cookie(cookiename, userId+'###'+id, 365,'/',domainName);
		}
		else
		{
			Set_Cookie(cookiename, id, 365,'/',domainName);
		}
			
		//alert('After Added = '+id);	
		$('#'+'wish'+id).removeClass("btn-add-wish-prf");
		$('#'+'wish'+id).addClass("btn-rmv-wish-prf");		
		//document.getElementById('wish'+id).innerHTML = '<a href="javascript: void(0)" onclick="return removeWishList2("'+id+'","1","'+userId+'","'+domainName+'");">Remove From Wishlist</a>';
		
		document.getElementById('wish'+id).innerHTML = "<a href='javascript: void(0)' onclick='return removeWishList2("+id+",1,"+userId1+","+domainName1+");'>Remove From Wishlist</a>";
	}
}

function removeWishList2(id,showResp,userId,domainName)
{
	userId1 = '"'+userId+'"';
	domainName1 = '"'+domainName+'"';
	
	if(showResp == '' || showResp==null)
		showResp = 0
	else
		showResp = 1;
	
	var cookiename;
	cookiename = 'wishes';
	
	if(userId != '' && userId != null)
	{
		cookieAllVal = Get_Cookie(cookiename); //get current cookie value with user Id in it
		ckVal = cookieAllVal.split('###');
		dbUsrId = ckVal[0];
		
		if(dbUsrId == userId) //if both ID are same, database ID and sent ID
		{
			cookieVal = ckVal[1];
			//alert(cookieVal);
		}
		else
		{
			//mechanism for removing with ajax	
			cookieVal = Get_Cookie(cookiename); //this is a temp fix			
		}
	}
	else
	{
		cookieVal = Get_Cookie(cookiename); //get current cookie value without user Id in it	
	}
	
	var arr = new Array();
	
	if(cookieVal != null)
	{				
		arr = cookieVal.split(',');
		
		if(inArray(arr,id) == true)
		{			 
			if(arr.length != 1)
			{
				if(id == arr[0])
				{
					replacestr = id+',';
				}
				else
				{
					replacestr = ','+id;
				}
				cookieVal = cookieVal.replace(replacestr,'');	
			}
			else
			{				
				cookieVal = cookieVal.replace(id,'');	
			}
			
			if(showResp == 0)
			{
				document.getElementById('wish'+id).innerHTML = '';			
			}
			else
			{
				$('#'+'wish'+id).removeClass("btn-rmv-wish-prf");
				$('#'+'wish'+id).addClass("btn-add-wish-prf");
				document.getElementById('wish'+id).innerHTML = '';											
				
				document.getElementById('wish'+id).innerHTML = "<a href='javascript: void(0)' onclick='return wishList("+id+","+userId1+","+domainName1+");'>Add to wishlist</a>";			
			}
			
			if(userId != '' && userId != null)
			{		
				pass1 = 'userId='+userId+'&string1='+cookieVal+'&type=wishlist';
				passCookieData(pass1);
				//Set_Cookie(cookiename,cookieVal, 365,'/',domainName);
				Set_Cookie(cookiename, userId+'###'+cookieVal, 365,'/',domainName); //for appending user id
			}
			else
			{
				Set_Cookie(cookiename, cookieVal, 365,'/',domainName);
			}
			
		}
		else
		{				
			cookieVal = cookieVal.replace(id,'');						
			document.getElementById('wish'+id).innerHTML = '';
			
			if(userId != '' && userId != null)
			{		
				pass1 = 'userId='+userId+'&string1='+cookieVal+'&type=wishlist';
				passCookieData(pass1);
				//Set_Cookie(cookiename, cookieVal, 365,'/',domainName);
				Set_Cookie(cookiename, userId+'###'+cookieVal, 365,'/',domainName); //for appending user id
			}
			else
			{
				Set_Cookie(cookiename, cookieVal, 365,'/',domainName);	
			}
			
			if(showResp == 0)
			{
				document.getElementById('wish'+id).innerHTML = '';			
			}
			else
			{
				$('#'+'wish'+id).removeClass("btn-rmv-wish-prf");
				$('#'+'wish'+id).addClass("btn-add-wish-prf");
				document.getElementById('wish'+id).innerHTML = '';
				//alert('kakk1');
							
				document.getElementById('wish'+id).innerHTML = "<a href='javascript: void(0)' onclick='return wishList("+id+","+userId1+","+domainName1+");'>Add to wishlist</a>";			

			}
			
		}
	}
	else
	{		
		if(userId != '' && userId != null)
		{
			pass1 = 'userId='+userId+'&string1='+id+'&type=wishlist';
			passCookieData(pass1);
			//Set_Cookie(cookiename, id, 365,'/',domainName);
			Set_Cookie(cookiename, userId+'###'+id, 365,'/',domainName); //for appending user id
		}
		else
		{
			Set_Cookie(cookiename, id, 365,'/',domainName);
		}
	}
}

function passCookieData(params)
{
	xmlHttp4 = null;
	xmlHttp4 = GetXmlHttpObject();	
	url= '/save-cookie.php?'+params;
	xmlHttp4.onreadystatechange= saveCookie
	xmlHttp4.open("GET",url,true)
	xmlHttp4.send(null);
}

function saveCookie()
{
	if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete")
	{		
		if(Trim(xmlHttp4.responseText) != 1)
		{
			//alert("Deal is added in database");
		}
		
		//pngfix();
		//closeLightBoxRev(AddRevDiv);
	}
}

function getCookieStr(dealid,orgDomain,params)
{	
	xmlHttp4 = null;
	xmlHttp4 = GetXmlHttpObject();	
	url= '/get-cookie.php?'+params;
	xmlHttp4.onreadystatechange= processCookie;
	xmlHttp4.open("GET",url,true);
	xmlHttp4.send(null);
}

function processCookie()
{
	if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete")
	{
		var cookieValue;
		var cookVal;		
		cookiename = 'wishes';
		cookieValue = Trim(xmlHttp4.responseText); //get current cookie value with user Id in it		
		cookVal = cookieValue.split('###');
		userId = cookVal[0];
		cookieVal = cookVal[1];
		var arr = new Array();		
		if(cookieVal == ',')
		{
			cookieVal = '';	
		}
		
		if(cookieVal != null && cookieVal != '')
		{				
			arr = cookieVal.split(',');		
			//alert('Length '+arr.length);
			if(inArray(arr,dealid) == false)
			{
				if(arr.length == '')
				{
					cookieVal = dealid;	
				}
				else
				{
					cookieVal = cookieVal+','+dealid;		
				}
				$('#'+'wish'+dealid).removeClass("btn-add-wish-prf");
				$('#'+'wish'+dealid).addClass("btn-rmv-wish-prf");
				orgDomain1 = '"'+orgDomain+'"';
				document.getElementById('wish'+dealid).innerHTML = "<a href='javascript: void(0)' onclick='return removeWishList2("+dealid+",1,"+userId+","+orgDomain1+");'>Remove From Wishlist</a>";
				//Set_Cookie('wishes'+checkdep, id, 365);
				//alert('id = '+temp);	
			}
			
			if(userId != '' && userId != null)
			{
				pass1 = 'userId='+userId+'&string1='+cookieVal+'&type=wishlist';
				passCookieData(pass1);
				Set_Cookie(cookiename, userId+'###'+cookieVal, 365,'/',orgDomain);
			}
			else
			{
				Set_Cookie(cookiename, cookieVal, 365);
			}
		}
		else
		{	
			if(userId != '' && userId != null)
			{
				pass1 = 'userId='+userId+'&string1='+dealid+'&type=wishlist';
				passCookieData(pass1);
				Set_Cookie(cookiename, userId+'###'+dealid, 365,'/',orgDomain);
			}
			else
			{
				Set_Cookie(cookiename, dealid, 365,'/',orgDomain);
			}
				
			//alert('After Added = '+id);	
			$('#'+'wish'+dealid).removeClass("btn-add-wish-prf");
			$('#'+'wish'+dealid).addClass("btn-rmv-wish-prf");		
			//document.getElementById('wish'+dealid).innerHTML = '<a href="javascript: void(0)" onclick="return removeWishList2("'+dealid+'","1","'+userId+'","'+orgDomain+'");">Remove From Wishlist</a>';	
			orgDomain1 = '"'+orgDomain+'"';
			document.getElementById('wish'+dealid).innerHTML = "<a href='javascript: void(0)' onclick='return removeWishList2("+dealid+",1,"+userId+","+orgDomain1+");'>Remove From Wishlist</a>";

		}
	}
}

function SetTabs1(param1,param2,param3)
{
	document.getElementById(param1).style.display = 'block';
	document.getElementById(param1+'1').className = 'active';
	
	document.getElementById(param2).style.display = 'none';
	document.getElementById(param2+'1').className = 'twotab';
	
	document.getElementById(param3).style.display = 'none';
	document.getElementById(param3+'1').className = 'threetab';

}
function SetTabs2(param1,param2,param3)
{
	document.getElementById(param1).style.display = 'none';
	document.getElementById(param1+'1').className = 'onetab';
	
	document.getElementById(param2).style.display = 'block';
	document.getElementById(param2+'1').className = 'active';
	
	document.getElementById(param3).style.display = 'none';
	document.getElementById(param3+'1').className = 'threetab';

}

function SetTabs3(param1,param2,param3)
{
	document.getElementById(param1).style.display = 'none';
	document.getElementById(param1+'1').className = 'onetab';
	
	document.getElementById(param2).style.display = 'none';
	document.getElementById(param2+'1').className = 'twotab';
	
	document.getElementById(param3).style.display = 'block';
	document.getElementById(param3+'1').className = 'active';

}

/*function removeWishList(id)
{
	var checkdep;
	if(Get_Cookie('addtocode') != '' && Get_Cookie('addtocode') != null)
	{
		checkdep = Trim(Get_Cookie('addtocode'));
	}
	else
	{
		checkdep = Get_Cookie('addtocode');
	}
	
	temp = Get_Cookie('wishes'+checkdep);	
	var arr = new Array();
	
	if(temp != null)
	{				
		arr = temp.split(',');
		//alert(arr.length);
		if(inArray(arr,id) == true)
		{
			for(i=0;i< arr.length;i++)
			{
				if(arr[i] != id)
				{
					if(i != (arr.length-1))
					{
						temp1 = arr[i]+',';
					}
					else
					{
						temp1 = arr[i];
					}
				}
			}
			//alert(temp1);
			if(arr.length != 1)
			{
				if(id == arr[0])
					temp = temp.replace(id+',','');	
				else
					temp = temp.replace(','+id,'');	
			}
			else
			{
				temp = temp.replace(id,'');	
			}
			//alert('After removed - '+temp);
			$('#'+'wish'+id).removeClass("btn-rmv-wish-prf");
			$('#'+'wish'+id).addClass("btn-add-wish-prf");
			document.getElementById('wish'+id).innerHTML = '';
			document.getElementById('wish'+id).innerHTML = '<a href="javascript: void(0)" onclick="return wishList('+id+');">Add to wishlist</a>';			
			//alert('id = '+temp);	
		}
		else if(temp != null)
		{
			temp = temp.replace(id,'');
			$('#'+'wish'+id).removeClass("btn-rmv-wish-prf");
			$('#'+'wish'+id).addClass("btn-add-wish-prf");
			document.getElementById('wish'+id).innerHTML = '';
			document.getElementById('wish'+id).innerHTML = '<a href="javascript: void(0)" onclick="return wishList('+id+');">Add to wishlist</a>';
			if(checkdep != '' && checkdep != null)
			{
				pass1 = 'userId='+checkdep+'&string1='+temp+'&type=wishlist';
				passCookieData(pass1);
			}
			Set_Cookie('wishes', temp, 365);
		}
	}
	
	if(temp != null)
	{
		if(checkdep != '' && checkdep != null)
		{
			pass1 = 'userId='+checkdep+'&string1='+temp+'&type=wishlist';
			passCookieData(pass1);
		}
		Set_Cookie('wishes'+checkdep, temp, 365);
	}
	else
	{
		if(checkdep != '' && checkdep != null)
		{
			pass1 = 'userId='+checkdep+'&string1='+id+'&type=wishlist';
			passCookieData(pass1);
		}
		Set_Cookie('wishes'+checkdep, id, 365);
	}
}
*/


