//feed template variable

var templateId1 ="";
var imageUrl1 = "";
var productId1 = "";
var currentPrice1 = "";
var vendername1 = "";
var shortdesc1 = "";
var longdesc1 = "";
var hreflink1 = "";


//first it will on click of tabs
function SetMeFB(currentTab, tab_type)
{
	//current tab the selected tab = li tab and tab_type = tab no 1 to 5
	for(var i=0; i < 6; i++)
	{
	    //tab0 to tab5 is li defined in postlogin.jsp
		var obj = E("tab"+i);
		
		
		if(obj)
			obj.className="";
	}
	
	//selected tab .classname=current
	currentTab.className = "current";
	
	//if my nags
	if(tab_type == 3)
	{
		setNagSortDropdown();
	}
	else if(tab_type == 1)
	{
		//my tags
		
		setTagSortDropdownFB();
	}
	else if(tab_type == 4)
	{
		setSavingSortDropdown(4);
	}
	else if(tab_type == 5)
	{
		setSavingSortDropdown(5);
	}
	else if(tab_type == 2)
	{
		//myhaggles
		setSavingSortDropdown(2);
	}
	

}


//option will set to filter
function setTagSortDropdownFB()
{

	//E("sortBy").setAttribute("tab_type", "1");
	//E("sortBy").options.length = 0
	var Item = new Option();
		Item.value =  "creation_date desc";
		Item.innerHTML = "Latest Tags";
		E("sortBy").appendChild(Item);
		Item = new Option();
		Item.value =  "Difference desc";
		Item.innerHTML = "Biggest Price Drops";
		E("sortBy").appendChild(Item);
		Item = new Option();
		Item.value =  "creation_date asc";
		Item.innerHTML = "Oldest First";
		E("sortBy").appendChild(Item);
		Item = new Option();
		Item.value =  "VendorName asc";
		Item.innerHTML = "By Store Name";
		E("sortBy").appendChild(Item);
		Item = new Option();
		Item.value =  "Expired desc";
		Item.innerHTML = "Item Expired";
		E("sortBy").appendChild(Item);
		E("filter_pannel").style.display = "inline";
		E("filterMsgRow").style.visibility = 'visible';
}

//
function authentiCallFB(mode,email,uid,basePath,facebookid)
{
if(mode==1)
setTagSortDropdownFB();
  if (!email || email.length == 0) {
		showlogin();
	} else
	if (typeof onloginFB === 'function') 
	{
		if(mode)
		{
			onloginFB(x54_toolbarid, email,mode,uid,basePath,facebookid);
		}
		else
		{  
			onloginFB(x54_toolbarid, email,uid,basePath,facebookid);
		}
	}
	
}



function onloginFB(tid, email, mode,uid,basePath,facebookid)
{
	var tag_type = E("sortBy").getAttribute("tab_type");
	var sortBy = E("sortBy").value;
	E("mode").value = "";
	if(mode)
	{ 
		E("mode").value = mode;
	}	
	
	
	if(E("mode").value == "undefined" || E("mode").value == "")
		mode = undefined;
	else
		mode = E("mode").value;
		
	if(tag_type == 1 || tag_type == 3 || tag_type == 6)
	{
	//alert(tag_type);
		E("filterMsgRow").style.visibility = 'visible';
		E("filterBy").innerHTML = '<strong>' + E("sortBy").options[E("sortBy").selectedIndex].innerHTML + '</strong>';		
		genrateTagFB(tid, email, sortBy, tag_type, mode,uid,basePath,facebookid);}	
	else if(tag_type == 4)
	{
		getSavingContent();
	}
	else if(tag_type == 5)
	{
		getMyProfileContent();
	}
	else if(tag_type == 2)
	{
		genrateTagFB(tid, email, sortBy, tag_type, mode,uid,basePath,facebookid);
	}	
	
}

function genrateTagFB(tid, email, sortBy, tag_type, mode,uid,basePath,facebookid)
{
	var Html = '';
	if(tag_type == 1)
	{
		//call mytag servlet through ajax by gettags
		var e2=document.getElementById("putPagingForTagAndHaggle")	
		var e=document.getElementById("puttag")	
		if(e==null || e2==null)
		{
			document.getElementById("tagsContent").innerHTML="<div align='right' style='margin-right:75px;' id='putPagingForTagAndHaggle'></div><div id='puttag'><div   style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;  \"></div></div>";
			e2=document.getElementById("putPagingForTagAndHaggle")	
			e=document.getElementById("puttag")
		}
		e2.innerHTML="";
		e.innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
		var v=ajaxCallForText(getBasePath()+"/myTagLimit",{op:'paging',toolbarid:tid,email:email,sortBy:sortBy,lower:0,facebook:'fb',uid:uid,basePath:basePath,facebookid:facebookid});	
		
		if(v=="0" || v==null || v=='undefined')
		{
			var mylist=document.getElementById("sortBy");
			var sortText=mylist.options[mylist.selectedIndex].text;		
			e.innerHTML="<div  style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;\"></div><center><strong>No Item available for "+sortText+"</strong></center>"
			e2.innerHTML="";
			return;
		}
		if(v.indexOf("session")!=-1)
		{
		
			location.reload();
			
			//e.innerHTML=v;
			//e2.innerHTML="";
			return;
		}	
		e2.innerHTML=v;	
		e.innerHTML=ajaxCallForText(getBasePath()+"/myTagLimit",{toolbarid:tid,email:email,sortBy:sortBy,lower:0,facebook:'fb',uid:uid,basePath:basePath,facebookid:facebookid});
		return;
		//mtags = getTags("mytag",{toolbarid:tid,email:email,sortBy:sortBy,lower:4});
		
		//if(mtags && mtags.length > 0)
		//{
			//var rowLimit = calculateRows(mtags.length);
			//Html = TagUI2(mtags,rowLimit);
		//}
		//else
			//Html = '<div  style="height:375px; width: 2px; margin: 0; padding:0; float: left;"></div><center><strong>No Item available!</strong></center>';
	}
	else if(tag_type == 3 && ((mode > 0 && sortBy ==-1) || ((!mode) && sortBy > -1)))
	{
	
		//getNags returns an array of nag object call mynag servlet through ajax by gettags
		document.getElementById("tagsContent").innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
		mtags = getNags(getBasePath()+"/mynag",{toolbarid:tid,email:email,sortBy:sortBy,mode:mode});
		
		if(mtags.session=='expired')
			location.reload();
		else if(mtags && mtags.length > 0)
		{
			var rowLimit = calculateRows(mtags.length);
			Html = NagUI(mtags,rowLimit);
		}
		else
			Html = '<div  style="height:375px; width: 2px; margin: 0; padding:0; float: left;"></div><center><strong>No Item available!</strong></center>';
	}
	else if(tag_type == 3 && ((!mode) && sortBy == -1) )
	{		
		//getnagsreturns an array of nag object call mynag servlet through ajax by gettags
		document.getElementById("tagsContent").innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
		mtags = getNags(getBasePath()+"/mynag",{toolbarid:tid,email:email,sortBy:sortBy,mode:-1});
		
		if(mtags.session=='expired')
			location.reload();	
		else if(mtags && mtags.length > 0)
		{
			
			//var rowLimit = calculateRows(mtags.length);
			Html = initNagUI(mtags);
		}
		else
			Html = '<div  style="height:375px; width: 2px; margin: 0; padding:0; float: left;"></div><center><strong>No Item available!</strong></center>';
	}
	else if(tag_type == 2)
	{
	
		
		var e2=document.getElementById("putPagingForTagAndHaggle")	
		var e=document.getElementById("puttag")	
		if(e==null || e2==null)
		{
			document.getElementById("tagsContent").innerHTML="<div align='right' style='margin-right:75px;' id='putPagingForTagAndHaggle'></div><div id='puttag'><div   style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;  \"></div></div>";
			e2=document.getElementById("putPagingForTagAndHaggle")	
			e=document.getElementById("puttag")
		}
		e2.innerHTML="";
		e.innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
		var v=ajaxCallForText(getBasePath()+"/myHaggleLimit",{op:'paging',toolbarid:tid,email:email,lower:0});	
		
		if(v=="0" || v==null || v=='undefined')
		{		
			e.innerHTML="<div  style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;\"></div><center><strong>No Item available</strong></center>"
			e2.innerHTML="";
			return;
		}	
		if(v.indexOf("session")!=-1)
		{
			location.reload();
			//e.innerHTML=v;
			//e2.innerHTML="";
			return;
		}
		e2.innerHTML=v;	
		e.innerHTML=ajaxCallForText(getBasePath()+"/myHaggleLimit",{toolbarid:tid,email:email,lower:0});
		return;
	
	   //getHaggle returns an array of nag object call myhaggle servlet through ajax by gettags
		//mtags = getHaggles("myhaggle",{toolbarid:tid});
		
		//if(mtags && mtags.length > 0)
		//{
		//	var rowLimit = calculateRows(mtags.length);
		//	Html = HaggleUI2(mtags,rowLimit);
	//	}
	//	else
		//	Html = '<div  style="height:375px; width: 2px; margin: 0; padding:0; float: left;"></div><center><strong>No Item available!</strong></center>';
	}
	
	document.getElementById("tagsContent").innerHTML = Html;
}


//pagination for newer

function pagingTagNewerFB(email,sortBy,total)
{
//alert("pagingTagNewerFB");
	var e=document.getElementById("puttag")	
	var ll=document.getElementById("lowerLimit")
	var ul=document.getElementById("upperLimit")
	upper=parseInt(ul.innerHTML)
	lower=parseInt(ll.innerHTML);
	//alert("old up"+upper);
	//alert("old low"+lower);
	if(lower > 1)
		lower=lower-12;
	else
		return false;	
	if(lower < 0)
		lower=1;
	e.innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
	e.innerHTML=ajaxCallForText(getBasePath()+"/myTagLimit",{email:email,sortBy:sortBy,lower:lower-1,facebook:'fb'});
	
	upper=lower+11;
	if(upper > total)
		upper=total
		
	//alert("new low"+lower);
	//alert("new up"+upper);
	ll.innerHTML=lower;
	ul.innerHTML=upper;
}

//older page for tags in facebook
function pagingTagOlderFB(email,sortBy,total)
{
	var e=document.getElementById("puttag")	
	var ll=document.getElementById("lowerLimit")
	var ul=document.getElementById("upperLimit")
	upper=parseInt(ul.innerHTML);
	lower=parseInt(ll.innerHTML);
	//alert("old low"+lower);
	//alert("old up"+upper);
	if(upper < total)
		lower=lower+12;
	else
		return false;	
	e.innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
	e.innerHTML=ajaxCallForText(getBasePath()+"/myTagLimit",{email:email,sortBy:sortBy,lower:lower-1,facebook:'fb'});
	upper=upper+12;
	if(upper >total)
		upper=total;
		
	//alert("new low"+lower);
	//alert("new up"+upper);
	ul.innerHTML=upper 	
	ll.innerHTML=lower;
	return;
	//window.location.href = document.URL;
}


function doChangeTagForLimitFB(el,tagid,currentPrice)
{
	currentPrice=parseFloat(currentPrice);
	if(currentPrice <= 1.0)
	{
		var msg = "<b>You can only Tag an item which has price greater than $1.0.</b>";
		setmsg(msg, "brown", tagid);
		E("tagValue" + tagid).focus();
		return false
	}
	var Cell = E(el + tagid);
	var offer = E("tagValue" + tagid).value;
	offer = String(offer).replace(/[^\d.-]*/gi, "");
	
	var decIndex=offer.indexOf(".");
	if(offer.indexOf(" ")!=-1 ||  offer.indexOf('+')!=-1 || offer.indexOf('-')!=-1)
	{
		msg = "<b>Tag price is invalid. There should be no space,+,- etc.</b>";
		setmsg(msg, "brown", tagid);
		E("tagValue" + tagid).focus();
		return false
	}
	if(decIndex!=-1)
	{		
		decIndex=decIndex+1;
		str=offer.substr(decIndex);		
		if(str.length > 3)
			{
				msg = "<b>Tag price is invalid. Please enter 3 digits after decimal</b>";
				setmsg(msg, "brown", tagid);
				E("tagValue" + tagid).focus();
				return false
			}
	}	
	
	var oprice = parseFloat(offer);
	tagPriceMin=currentPrice* 0.1;
	tagPriceMax = currentPrice * 0.99;
	//alert(isNaN(currentPrice));
	if (isNaN(oprice) ||   oprice < 1.0)
	{
		var msg = "<b>Please enter valid price.(Tag Price must be at least  $1.0) </b>";
		setmsg(msg, "brown", tagid);
		E("tagValue" + tagid).focus();
		return false
	}
	else if(oprice > currentPrice || oprice < tagPriceMin || oprice > tagPriceMax)
	{
		var msg = "<b>Please enter a price between 10% to 99% of the current price.</b>";
		setmsg(msg, "brown", tagid);
		E("tagValue" + tagid).focus();
		return false
	}
	else if(oprice >= currentPrice)
	{
		var msg = "<b>Offer price is invalid. It must be below the current price.</b>";
		setmsg(msg, "brown", tagid);
		E("tagValue" + tagid).focus();
		return false
	}
	else
	{
		//E("cellControl_"+ tagid).style.display = "none";
		//E("cellProgress_" + tagid).style.display = "block";
		var res = simpleCall(getBasePath()+"/tagupdate.jsp",{op:"change",tagid:tagid,offer:offer});
		if (res.status=="OK")
		{
			var email = authenticate();
			if(email)
			{
				var e=document.getElementById("puttag")	
				var sortBy = E("sortBy").value;				
				e.innerHTML=ajaxCallForText(getBasePath()+"/myTagLimit",{email:email,sortBy:sortBy,lower:0,facebook:'fb'});		
				
			}
		}
		else
		{
			alert("Could not change the offer price.\n" + res.message);
		}
	
	}
	
}
function mywishlistf(mode,email,facebookid,uid,basePath)
{//alert(" facebookid = "+facebookid+" basePath= "+basePath+" uid= "+uid)
    setWishListItems();
		var e2=document.getElementById("putPagingForTagAndHaggle")	
		var e=document.getElementById("puttag")	
		if(e==null || e2==null)
		{
			document.getElementById("tagsContent").innerHTML="<div align='right' style='margin-right:75px;' id='putPagingForTagAndHaggle'></div><div id='puttag'><div   style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;  \"></div></div>";
			e2=document.getElementById("putPagingForTagAndHaggle")	
			e=document.getElementById("puttag")
		}
		e2.innerHTML="";
		e.innerHTML='<center><img src="images/ajaxloader.gif" alt="Loading" /></center>';
		var v=ajaxCallForText(getBasePath()+"/myWishListLimit",{op:'paging',email:email,lower:0,facebook:'fb',facebookid:facebookid,uid:uid,basePath:basePath});	
		
		if(v=="0" || v==null || v=='undefined')
		{		
			e.innerHTML="<div  style=\"height:375px; width: 2px; margin: 0; padding:0; float: left;\"></div><center><strong>No Item available</strong></center>"
			e2.innerHTML="";
			return;
		}	
		if(v.indexOf("session")!=-1)
		{
			location.reload();
			return;
		}
		e2.innerHTML=v;	
		e.innerHTML=ajaxCallForText(getBasePath()+"/myWishListLimit",{email:email,lower:0,facebook:'fb',facebookid:facebookid,uid:uid,basePath:basePath});
		return;
}



function authentiCallF(email)
{
onlogin(x54_toolbarid, email);
}


function ondeleteFB(tagid)
{
	if(confirm("Are you sure you want to delete this Product from TAG list ?"))
	{
		dodeleteFB(tagid);
	}
}

function dodeleteFB(tagid)
{
	//E("cellControl_"+ tagid).style.display = "none";
	//E("cellProgress_" + tagid).style.display = "block";
	var res = simpleCall(getBasePath()+"/tagupdate.jsp",{op:"delete",tagid:tagid});
	if (res.status=="OK")
	{
		var sortBy = E("sortBy").value;
		checkMail();
	  // window.location.reload( true );
	   
		//authentiCall("1",sortBy);
	}
	else
	{
		alert("Could not delete the tag.\n" + res.message);
	}
}


function authentiCallPD(fid,infb,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink)
{  	
 	var me="";
	if (!me) 
      me = whoami();
    var email = me.email;		
	if (!email || email.length == 0)
	{	
		facebooklogin(fid,infb,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink)
		return false;
	}
	else
	{
		if(actionType == "wishlist")
		{
		   //added fbin variable for feed template should one time come		
		   	var fbin="fbin";
		 	//addToMyWishList(productId,currentPrice,fbin);	
		 	if(addToMyWishList(productId,currentPrice,fbin))
		 		feedTemplate(templateId,imageUrl,productId,currentPrice,vendername,shortdesc,longdesc,hreflink);	 	
		}		 	
		 else if(actionType == "mytags")
		 {   
			 currentDlg.showSharePopUP(productId,actionType);	
			 onTagClickWish(productId,0);
			// setCurrentTag(currentPrice,productId,url);
		 	 //authentiCall();
		 	// feedTemplate(templateId,imageUrl,productId,currentPrice,vendername,shortdesc,longdesc,hreflink);		 	 
		 }	
		 else if(actionType == "share")
		 currentDlg.showSharePopUP(productId,actionType);			 	
			
	}
	
}

//some time current price used for tag id
function checkAuthentiGlobal(facebookid,fbinPopup,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink)
{ 
	templateId1 = templateId;
    imageUrl1 = imageUrl;
    productId1 = productId;
    currentPrice1 = currentPrice;
    vendername1 = vendername;
    shortdesc1 = shortdesc;
    longdesc1 = longdesc;
    hreflink1 = hreflink;
    
	if(actionType=="wishlist")
	{		 	
	   authentiCallPD(facebookid,fbinPopup,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink);	
	    //feedTemplate(templateId,imageUrl,productId,currentPrice,vendername,shortdesc,longdesc);	
	}   
	else if(actionType=="mytags") 
	{
	  
		authentiCallPD(facebookid,fbinPopup,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink);
		//feedTemplate(templateId,imageUrl,productId,currentPrice,vendername,shortdesc,longdesc);
		//setCurrentTag(" + tag.TagId + "," + tag.ProductId + ",'"+URL+"');authentiCall();authentiCallPD('"+facebookid+"','"+fbinPopup+"');
	}	
	else if(actionType=="share") 
		authentiCallPD(facebookid,fbinPopup,productId,actionType,currentPrice,url,templateId,imageUrl,vendername,shortdesc,longdesc,hreflink);
	 
}

function feedTemplate(templateId, imageUrl, productId, currentPrice,vendername,shortdesc,longdesc,hreflink)
{ 
	if(FB.Facebook.apiClient.get_session()!=null)
	{
		   // alert(FB.Facebook.apiClient.get_session());
		    //var hreflink=getBasePath()+"/haggler_heaven_product.jsp?current_tab=5&reqstr="+productId;
		    var html='<b>Current Price: </b>$'+currentPrice+'<br />';
		        html += '<b>Vendor name: </b>'+vendername+'<br />';  
		        html += '<b style="color:red;">Short Description: '+shortdesc+'</b><br />';      
		        html += '<b>Long Description: </b>'+longdesc+'';
			var template_data = {"verb":'',"content":html, "noun":'', "body":'', "fullbody":'', "images":[{'src':imageUrl, 'href':hreflink}]};
			facebook_publish_feed_story(templateId, template_data);
			facebook_onload(true);
	}	
}

function addoffliceAccess(sessionkey,facebookid)
{
  var v=ajaxCallForText(getBasePath()+"/facebookuser",{op:'offlineAccess',fid:facebookid,sessionkey:sessionkey,email:''}); 
}




