// JavaScript Document
function displayMerchant(type)
{
	if(type=="more")
	{
		$("merchantDisplayArea").style.display="inline";
		$("displayMerchantLabel").innerHTML="<a href=\"#merchantDispLink\" onClick=\"displayMerchant('less')\">Less Merchants</a>";
	}
	else if(type=="less")
	{
		$("merchantDisplayArea").style.display="none";
		$("displayMerchantLabel").innerHTML="<a href=\"#merchantDispLink\" onClick=\"displayMerchant('more')\">More Merchants</a>";
	}
}

function displaySubCategory(categoryId,type)
{
	if(type=="expand")
	{
		$(categoryId).style.display="inline";
		$("displayCatLabel"+categoryId).innerHTML="<a href=\"#subCatDisplay\" onClick=\"displaySubCategory('"+categoryId+"','collaps')\"><img src=\"/images/expanded.png\" border=\"0\"></a>";
	}
	else if(type=="collaps")
	{
		$(categoryId).style.display="none";
		$("displayCatLabel"+categoryId).innerHTML="<a href=\"#subCatDisplay\" onClick=\"displaySubCategory('"+categoryId+"','expand')\"><img src=\"/images/collapsed.png\" border=\"0\"></a>";
	}
}

var refineSearchType = "";
var refreshArea = "";
function submitIndexSearchForm()
{
	document.onlineSearchForm.buttonType.value = "Find";
	document.onlineSearchForm.submit();
}
function submitRefineSearch()
{
	for(var i=0; i < document.onlineSearchForm.categoryId.length; i++)
	{
		if(document.onlineSearchForm.categoryId[i].checked)
		{
			document.onlineSearchForm.categoryId[i].checked=false;
		}
	}
	
	for(var i=0; i < document.onlineSearchForm.merchantId.length; i++)
	{
		if(document.onlineSearchForm.merchantId[i].checked)
		{
			document.onlineSearchForm.merchantId[i].checked=false;
		}
	}
	
	submitIndexSearchForm();
}

function sortResults()
{
	submitIndexSearchForm();
}

function loadMerchantList()
{
	submitIndexSearchForm();		
}
	
function loadCategoryList()
{
	submitIndexSearchForm();
}

function clearCategoryList()
{
	for(var i=0; i < document.onlineSearchForm.categoryId.length; i++)
	{
		if(document.onlineSearchForm.categoryId[i].checked)
		{
			document.onlineSearchForm.categoryId[i].checked=false;
		}
	}
	loadMerchantList();	
}

function clearMerchantList()
{
	for(var i=0; i < document.onlineSearchForm.merchantId.length; i++)
	{
		if(document.onlineSearchForm.merchantId[i].checked)
		{
			document.onlineSearchForm.merchantId[i].checked=false;
		}
	}
	loadCategoryList();
}

function checkSubCategories(catId, value) 
{
	var collection = document.getElementById(catId).getElementsByTagName('INPUT');
	for (var x=0; x<collection.length; x++) 
	{
		if (collection[x].type.toUpperCase()=='CHECKBOX')
			collection[x].checked = value;
	}
	loadMerchantList();
}

function submitItemSearch()
{
	submitIndexSearchForm();
}

function searchByPriceKeywords()
{
	var maxPrice = document.onlineSearchForm.maxPriceAmount.value;
	var keywords = document.onlineSearchForm.searchKeywords.value;
	var searchProfileCheck = getSearchProfileCheck();
	var searchProfileName = getSearchProfileName();
	var searchProfileId = document.onlineSearchForm.searchProfileId.value;
	var check=false;
	
	if(searchProfileCheck=="Y")
	{
		if(searchProfileName=="")
		{
			alert("Enter the Search Profile Name to Save");
			check=false;
		}
		else if(document.onlineSearchForm.logInCheck.value=="" && searchProfileName!="")
		{
			if(confirm("You have to login to save this search. 'OK' will send you to login page 'Cancel' will send you to search with out saving"))
			{
				document.onlineSearchForm.buttonType.value="saveDashBoard";
				document.onlineSearchForm.submit();
			}
			else
			{
				searchProfileName="";
			}				
		}
		else
			check = true;
	}
	else if(searchProfileName!="" && searchProfileCheck=="N")
	{
		alert("Please check the Checkbox to save this search");
		check = false;
	}
	
	if(check==true)
	{
		document.onlineSearchForm.buttonType.value="saveDashBoard";
		document.onlineSearchForm.submit();
	}
	else if(maxPrice!="" || keywords!="")
	{
		submitIndexSearchForm();
	}
}

function getSearchProfileCheck()
{
	var saveSearchCheck = "N";
	if(document.onlineSearchForm.searchProfileCheck.checked)
	{
		saveSearchCheck = "Y";
	}
	return saveSearchCheck;
}

function getSearchProfileName()
{
	var searchProfileName = document.onlineSearchForm.searchProfileName.value;
	return searchProfileName;
}

//Deal Search For reset method
function resetDealSearchForm()
{
	document.onlineSearchForm.maxPriceAmount.value = "";
	document.onlineSearchForm.searchKeywords.value = "";
	document.onlineSearchForm.searchProfileCheck.checked = false;
	document.onlineSearchForm.searchProfileName.value = "";
	document.onlineSearchForm.searchProfileId.value = "0";
	submitIndexSearchForm();
}

function submitResultForPage()
{
	document.searchResultForm.pageNumber.value = 0;
	document.searchResultForm.chooseAction.value = "page";
	document.searchResultForm.submit();
}

//Loading Merchant Coupons when category changed
function loadCouponMerchantList()
{
	document.merchantCouponSearchForm.buttonType.value = "Find";
	document.merchantCouponSearchForm.submit();
}

//Coupon sort & merchant change
function loadMerchantCouponResults()
{
	document.merchantCouponSearchForm.buttonType.value = "Find";
	document.merchantCouponSearchForm.submit();
}

//Coupon Pagination
function submitCouponResultForPage(pageNo)
{
	document.couponSearchResultForm.pageNumber.value = 0;
	document.couponSearchResultForm.chooseAction.value = "page";
	document.couponSearchResultForm.submit();
}

//Cooupon vote submit
function increaseCouponVote(listId,i)
{
	if(document.getElementById("couponvoted"+listId).value!="increase")
	{
		processCouponVote(listId,"increaseVote");
		document.getElementById("couponvoted"+listId).value="increase";
	}
	else
		alert("Sorry you have already voted this listing.");
}

function decreaseCouponVote(listId,i)
{
	if(document.getElementById("couponvoted"+listId).value!="decrease")
	{
		processCouponVote(listId,"decreaseVote");
		document.getElementById("couponvoted"+listId).value="decrease";
	}
	else
		alert("Sorry you have already voted this listing.");
}

function processCouponVote(listId,voteType)
{
	document.getElementById("couponvotemessage"+listId).innerHTML="";
	document.getElementById("couponprocessImage"+listId).style.display="inline";
	refreshArea = "no";
	var url="/votecoupon.deals";
	var requestParameters="couponId="+listId+"&voteType="+voteType;
	
	var ajax = new Ajax.Request(
	url, {
	method: "post",
	parameters: requestParameters,
	onFailure: reportAjaxError,
	onSuccess: function(request) {
	var obj = eval('(' + ajax.transport.responseText + ')');
	document.getElementById("couponvotemessage"+listId).innerHTML="<font color='green'><b>Thank you for voting</b></font>";
	document.getElementById("couponprocessImage"+listId).style.display="none";
	document.getElementById("couponVoteCount"+listId).innerHTML=obj.voteCount;
	}
	});	
}

//Coupon Saving
function saveCouponList(listId,loginCheck,buttonType)
{
	if(loginCheck=="")
	{
		document.couponSearchResultForm.buttonType.value=buttonType;
		document.couponSearchResultForm.listId.value=listId;
		document.couponSearchResultForm.submit();
	}
	else
	{
		var url="/savecoupon.deals";
		var requestParameters="couponId="+listId;
		refreshArea = "no";
		var ajax = new Ajax.Request(
		url, {
		method: "post",
		parameters: requestParameters,
		onFailure: reportAjaxError,
		onSuccess: function(request) {
		var obj = eval('(' + ajax.transport.responseText + ')');
		document.getElementById("saveCouponList"+listId).innerHTML=obj.saveMessage;
		}
		});
	}
}

//Click on Category Id
function categoryIdClickSearch(catId, subCatIds)
{	
	document.getElementById(catId).checked = true;
	
	var collection = document.getElementById(subCatIds).getElementsByTagName('INPUT');
	for (var x=0; x<collection.length; x++) 
	{
		if (collection[x].type.toUpperCase()=='CHECKBOX')
			collection[x].checked = true;
	}	
	
	submitIndexSearchForm();
}

function subCategoryIdClickSearch(catId)
{	
	document.getElementById(catId).checked = true;	
	submitIndexSearchForm();
}

//Click on merchat id
function merchantIdClickSearch(mercId)
{	
	document.getElementById(mercId).checked = true;
	submitIndexSearchForm();
}

//Process Coupon SCoring
function processCouponAdminOperation(listId,pickType)
{
	document.getElementById("adminmessage"+listId).innerHTML="";
	
	adminRemarks="";
	if(pickType=='R')
		adminRemarks=document.getElementById("adminRemarks"+listId).value;
	
	var url="/couponadminoperation.deals";
	
	var requestParameters="couponId="+listId+"&pickType="+pickType+"&adminRemarks="+adminRemarks;
	
	var ajax = new Ajax.Request(
	url, {
	method: "post",
	parameters: requestParameters,
	onFailure: reportAjaxError,
	onSuccess: function(request) {
	var obj = eval('(' + ajax.transport.responseText + ')');
		document.getElementById("adminmessage"+listId).innerHTML="<font color='green'><b>Process Completed</b></font>";
	}
	});	
}

function resetCouponSearchForm()
{
}

//Deal Admin pick search
function adminPickItemSearch()
{
	document.onlineSearchForm.infozealPicksCode.checked=true;
	submitIndexSearchForm();
}

//Banner Voting submission
function increaseBannerVote(listId,i)
{
	if(document.getElementById("bannervoted"+listId).value!="increase")
	{
		processBannerVote(listId,"increaseVote");
		document.getElementById("bannervoted"+listId).value="increase";
	}
	else
		alert("Sorry you have already voted this listing.");
}

function decreaseBannerVote(listId,i)
{
	if(document.getElementById("bannervoted"+listId).value!="decrease")
	{
		processBannerVote(listId,"decreaseVote");
		document.getElementById("bannervoted"+listId).value="decrease";
	}
	else
		alert("Sorry you have already voted this listing.");
}

function processBannerVote(listId,voteType)
{
	document.getElementById("bannervotemessage"+listId).innerHTML="";
	document.getElementById("bannerprocessImage"+listId).style.display="inline";
	refreshArea = "no";
	var url="/votebanner.deals";
	var requestParameters="bannerId="+listId+"&voteType="+voteType;
	
	var ajax = new Ajax.Request(
	url, {
	method: "post",
	parameters: requestParameters,
	onFailure: reportAjaxError,
	onSuccess: function(request) {
	var obj = eval('(' + ajax.transport.responseText + ')');
	document.getElementById("bannervotemessage"+listId).innerHTML="<font color='green'><b>Thank you for voting</b></font>";
	document.getElementById("bannerprocessImage"+listId).style.display="none";
	document.getElementById("bannerVoteCount"+listId).innerHTML=obj.voteCount;
	}
	});	
}

//Sales Saving
function saveSales(listId,loginCheck,buttonType,mode)
{
	if(loginCheck=="")
	{
		document.salesSearchResultForm.buttonType.value=buttonType;
		document.salesSearchResultForm.saveMode.value=mode;
		document.salesSearchResultForm.listId.value=listId;
		document.salesSearchResultForm.submit();
	}
	else
	{
		var url="/savesales.deals";
		var requestParameters="listId="+listId+"&saveZone="+mode;
		refreshArea = "no";
		var ajax = new Ajax.Request(
		url, {
		method: "post",
		parameters: requestParameters,
		onFailure: reportAjaxError,
		onSuccess: function(request) {
		var obj = eval('(' + ajax.transport.responseText + ')');
		document.getElementById("saveSale"+listId).innerHTML=obj.saveMessage;
		}
		});
	}
}