// JavaScript Document
function submitFindForm(buttonType)
{
	document.merchantCouponSearchForm.buttonType.value = buttonType;
	document.merchantCouponSearchForm.submit();
}
function submitResult(pageNo)
{
	document.searchResultForm.pageNumber.value = pageNo;
	document.searchResultForm.chooseAction.value="page";
	document.searchResultForm.submit();
}
function submitRemarks()
{
	document.searchResultForm.buttonType.value="submitRemarks";
	document.searchResultForm.submit();
}
function deleteCoupon()
{
	if(confirm("Do you want to delete this coupon?"))
	{
		document.searchResultForm.buttonType.value="deleteCoupon";
		document.searchResultForm.submit();
	}
	else
	{
		HideContent('mmtbln');
	}
}
function submitCouponSummary(choiceType)
{
	document.couponSummaryForm.buttonType.value=choiceType;
	document.couponSummaryForm.submit();
}

var myGlobalHandlers = {
	onCreate: function(){
	showProcess();
	},
	
	onComplete: function() {
	if(Ajax.activeRequestCount == 0){
	hideProcess();
	}
	}
	};

var refineSearchType = "";	
function showProcess()
{
	if(refreshArea != "no")
	{
		document.getElementById('resultsProcessor').style.display='inline';
		document.getElementById('couponSearchResults').style.display='none';
	}	
}

function hideProcess()
{
	if(refreshArea != "no")
	{
		document.getElementById('resultsProcessor').style.display='none';
		document.getElementById('couponSearchResults').style.display='inline';
	}
}

//Loading Merchant Coupons when category changed
function loadCouponMerchantList()
{
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	refreshArea = "";
	getRefineSearchType();
	if(refineSearchType=='C')
	{
		var url = '/jsp/deals/coupon_merchant.jsp';
		var params = 'couponCatId='+catId;
		var myAjax = new Ajax.Updater(
			'couponMerchantList', 
			url, 
			{
				method: 'get', 
				parameters: params
			});
		merchantList = "";
	}
		
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
	window.scrollTo(0,0);
}

//Coupon merchant change
function loadCouponCategoryList()
{
	var catId = getCouponCategoryList();
	var sortOption = getCouponSortOption();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	refreshArea = "";
	getRefineSearchType();
	if(refineSearchType=='M')
	{
		var url = '/jsp/deals/coupon_category_list.jsp';
		var params = 'merchantList='+merchantList;
		var myAjax = new Ajax.Updater(
			'categoryList', 
			url, 
			{
				method: 'get', 
				parameters: params
			});
		catId = "";
	}
		
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
	window.scrollTo(0,0);
}

//Coupon sort
function loadMerchantCouponResults()
{
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	refreshArea = "";
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
}

//Coupon Pagination
function submitCouponResultForPage(pageNo)
{
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var sortOption = getCouponSortOption();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick+'&pageNumber='+pageNo;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
}

//Coupon go button
function couponSearchSave()
{
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	
	var searchProfileCheck = "N";
	if(document.merchantCouponSearchForm.searchProfileCheck.checked)
	{
		searchProfileCheck = "Y";
	}
	var searchProfileName = document.merchantCouponSearchForm.searchProfileName.value;
	
	var check=false;
	
	if(searchProfileCheck=="Y")
	{
		if(searchProfileName=="")
		{
			alert("Enter the Search Profile Name to Save");
			check=false;
		}
		else if(document.merchantCouponSearchForm.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.merchantCouponSearchForm.buttonType.value="saveSearch";
				document.merchantCouponSearchForm.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.merchantCouponSearchForm.buttonType.value="saveSearch";
		document.merchantCouponSearchForm.submit();
	}
	else if(keywords!="")
	{
		Ajax.Responders.register(myGlobalHandlers);
		var url = '/jsp/deals/merchant_coupon_search_results.jsp';
		var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
		var myAjax = new Ajax.Updater(
			'couponSearchResults', 
			url, 
			{
				method: 'get', 
				parameters: params
			});
	}
}

function getCouponCategoryList()
{
	var catList = "";
	if(document.merchantCouponSearchForm.categoryId != null)
	{
		for(var i=0; i < document.merchantCouponSearchForm.categoryId.length; i++)
		{
			if(document.merchantCouponSearchForm.categoryId[i].checked)
			{
				if(catList.length>0)
					catList = catList +","+ document.merchantCouponSearchForm.categoryId[i].value;
				else
					catList = catList + document.merchantCouponSearchForm.categoryId[i].value;
			}
		}
	}
	return catList;
}

function getCouponSortOption()
{
	var sortOption = document.merchantCouponSearchForm.couponSortOption[document.merchantCouponSearchForm.couponSortOption.selectedIndex].value;
	return sortOption;
}

function getCouponMoreMerchantList()
{
	var merchantList = "";
	if(document.merchantCouponSearchForm.merchantId != null)
	{
		for(var i=0; i < document.merchantCouponSearchForm.merchantId.length; i++)
		{
			if(document.merchantCouponSearchForm.merchantId[i].checked)
			{
				if(merchantList.length>0)
					merchantList = merchantList +","+ document.merchantCouponSearchForm.merchantId[i].value;
				else
					merchantList = merchantList + document.merchantCouponSearchForm.merchantId[i].value;
			}
		}
	}
	return merchantList;
}

function getAdminPickCheck()
{
	var adminCheck = "N";
	if(document.merchantCouponSearchForm.infozealPickedCode.checked)
	{
		adminCheck = "Y";
	}
	return adminCheck;
}

function getKeywords()
{
	var keywords = document.merchantCouponSearchForm.searchKeywords.value;
	return keywords;
}

function getRefineSearchType()
{
	for (var i=0; i < document.merchantCouponSearchForm.refineSearchType.length; i++)
	{
		if (document.merchantCouponSearchForm.refineSearchType[i].checked)
		{
			refineSearchType = document.merchantCouponSearchForm.refineSearchType[i].value;
		}
	}
}

//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.searchResultForm.buttonType.value=buttonType;
		document.searchResultForm.listId.value=listId;
		document.searchResultForm.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;
		}
		});
	}
}

//Merchant More link
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 clearCategoryList()
{
	for(var i=0; i < document.merchantCouponSearchForm.categoryId.length; i++)
	{
		if(document.merchantCouponSearchForm.categoryId[i].checked)
		{
			document.merchantCouponSearchForm.categoryId[i].checked=false;
		}
	}
	loadCouponMerchantList();	
}

function clearMerchantList()
{
	for(var i=0; i < document.merchantCouponSearchForm.merchantId.length; i++)
	{
		if(document.merchantCouponSearchForm.merchantId[i].checked)
		{
			document.merchantCouponSearchForm.merchantId[i].checked=false;
		}
	}
	loadCouponCategoryList();
}

function resetCouponSearchForm()
{
	resetForm();
	
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	refreshArea = "";
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
}

//Refine type submit
function submitRefineSearch()
{
	for(var i=0; i < document.merchantCouponSearchForm.categoryId.length; i++)
	{
		if(document.merchantCouponSearchForm.categoryId[i].checked)
		{
			document.merchantCouponSearchForm.categoryId[i].checked=false;
		}
	}
	
	for(var i=0; i < document.merchantCouponSearchForm.merchantId.length; i++)
	{
		if(document.merchantCouponSearchForm.merchantId[i].checked)
		{
			document.merchantCouponSearchForm.merchantId[i].checked=false;
		}
	}
	getRefineSearchType();
	
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	
	if(refineSearchType=="C")
	{
		var url = '/jsp/deals/coupon_refine_category_list.jsp';
		var params = 'couponCatId='+catId+'&merchantList='+merchantList;
		var myAjax = new Ajax.Updater(
			'refineTypeArea', 
			url, 
			{
				method: 'get', 
				parameters: params
			});
	}
	else if(refineSearchType=="M")
	{
		var url = '/jsp/deals/coupon_refine_merchant_list.jsp';
		var params = 'couponCatId='+catId+'&merchantList='+merchantList;
		var myAjax = new Ajax.Updater(
			'refineTypeArea', 
			url, 
			{
				method: 'get', 
				parameters: params
			});
	}
	
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
}

//Click on merchat id
function merchantIdClickSearch(mercId)
{
	resetForm();
	
	document.getElementById(mercId).checked = true;
	
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
	window.scrollTo(0,0);
}

//Click on Category Id
function categoryIdClickSearch(catId)
{
	resetForm();
	
	document.getElementById(catId).checked = true;
	
	var sortOption = getCouponSortOption();
	var catId = getCouponCategoryList();
	var merchantList = getCouponMoreMerchantList();
	var adminPick = getAdminPickCheck();
	var keywords = getKeywords();
	
	Ajax.Responders.register(myGlobalHandlers);
	var url = '/jsp/deals/merchant_coupon_search_results.jsp';
	var params = 'categoryId='+catId+'&merchantId='+merchantList+'&sortOption='+sortOption+'&keywords='+keywords+'&adminPick='+adminPick;
	var myAjax = new Ajax.Updater(
		'couponSearchResults', 
		url, 
		{
			method: 'get', 
			parameters: params
		});
	window.scrollTo(0,0);
}

function resetForm()
{
	document.merchantCouponSearchForm.couponSortOption.value = "P";
	for(var i=0; i < document.merchantCouponSearchForm.categoryId.length; i++)
	{
		if(document.merchantCouponSearchForm.categoryId[i].checked)
		{
			document.merchantCouponSearchForm.categoryId[i].checked=false;
		}
	}
	
	for(var i=0; i < document.merchantCouponSearchForm.merchantId.length; i++)
	{
		if(document.merchantCouponSearchForm.merchantId[i].checked)
		{
			document.merchantCouponSearchForm.merchantId[i].checked=false;
		}
	}
	
	if(document.merchantCouponSearchForm.infozealPickedCode.checked)
	{
		document.merchantCouponSearchForm.infozealPickedCode.checked = false;
	}
	document.merchantCouponSearchForm.searchKeywords.value = "";
}

//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>";
	}
	});	
}