//	for sorting
var ssLastSortOn = '';
var ssLastSortBy = '';

//Added to store ajax request in array to kill previous jquery request while requests are more than one
var xhrPool = [];

function sorting(searchFieldName, ssSortOn, ssSortBy, urlpath, updateDiv)
{	
	if(urlpath=='/content/mySettings' || urlpath.match('/individual/mySettings') || urlpath=='content/payList' )
	{
		var downImage =  '/images/downArrow.png';
		var downGrayImage = '/images/downArrowGray.png';
		var upImage = '/images/upArrow.png';
		var upGrayImage = '/images/upArrowGray.png';
	}
	else
	{
		var downImage = (urlpath.match('/content/segment') || urlpath.match('promotiongeneration') || urlpath.match('merchants.html') || urlpath.match('promotions.html')) ? '/images/down1.png' : '/images/down-arrow.gif';
		var downGrayImage = (urlpath.match('/content/segment') || urlpath.match('promotiongeneration') || urlpath.match('merchants.html') || urlpath.match('promotions.html')) ? '/images/down1-gray.png' : '/images/down-arrow-gray.gif';
		var upImage = (urlpath.match('/content/segment') || urlpath.match('promotiongeneration') || urlpath.match('merchants.html') || urlpath.match('promotions.html')) ? '/images/up1.png' : '/images/up-arrow.gif';
		var upGrayImage = (urlpath.match('/content/segment') || urlpath.match('promotiongeneration') || urlpath.match('merchants.html') || urlpath.match('promotions.html')) ? '/images/up1-gray.png' : '/images/up-arrow-gray.gif';
	}
	if(ssLastSortOn != '' && ssLastSortOn != ssSortOn && document.getElementById('img_' + ssLastSortOn + '_down') != null  && document.getElementById('img_' + ssLastSortOn + '_up') != null)
	{
		if(ssLastSortBy == 'desc')
			document.getElementById('img_' + ssLastSortOn + '_down').src = downImage;
		else
			document.getElementById('img_' + ssLastSortOn + '_up').src = upImage;
	}

	if(ssSortBy == 'desc')
	{
		if(document.getElementById('img_' + ssSortOn + '_down') != null && document.getElementById('img_' + ssSortOn + '_up') != null)
		{
			document.getElementById('img_' + ssSortOn + '_down').src = downGrayImage;
			document.getElementById('img_' + ssSortOn + '_up').src = upImage;
		}
	}
	else
	{
		if(document.getElementById('img_' + ssSortOn + '_down') != null && document.getElementById('img_' + ssSortOn + '_up') != null)
		{
			document.getElementById('img_' + ssSortOn + '_up').src = upGrayImage;
			document.getElementById('img_' + ssSortOn + '_down').src = downImage;
		}
	}
	
	ssLastSortOn 		= ssSortOn;
	ssLastSortBy 		= ssSortBy;

	var asFieldLen 		= searchFieldName.split('!');
	var ssSearchUrl 	= '';
	var ssActualUri		= (document.getElementById('actual_uri') != null) ? document.getElementById('actual_uri').value : '';

	for(var i=0 ; i<asFieldLen.length ; i++)
	{		
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';

		if(asFieldLen[i] == 'merchant_name' || asFieldLen[i] == 'merchant_keywords')
		{
			tbc = document.getElementById(asFieldLen[i]).value;
	  		tbc = encodeURIComponent(tbc);
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + tbc;
		}
		else
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;	
	}

	ssSearchUrl = (ssSortBy) ? (ssSearchUrl + '&ssSortBy=' + ssSortBy) : ssSearchUrl;
	ssSearchUrl = (ssSortOn) ? (ssSearchUrl + '&ssSortOn=' + ssSortOn) : ssSearchUrl;
	ssSearchUrl = ssSearchUrl + '&actual_uri=' + ssActualUri;

	if(document.getElementById('display_type'))
	{
		if(document.getElementById('display_type').value == 'promotion')
			searchFieldName = searchFieldName + '!id_merchant';
	}

	var snMerchantType	= '';
	var ssSearchType	= '';

	if(document.getElementById('showall') != null)
		var ssShowAll	= document.getElementById('showall').value;
	if(document.getElementById('search') != null)
		var ssSearch	= document.getElementById('search').value;

	if(document.getElementById('search_type') != null)
	ssSearchType = document.getElementById('search_type').value;
	var oMerchantType = document.getElementsByName('merchant_type');

	for(var i = 0; i < oMerchantType.length; i++) {
		if(oMerchantType[i].checked) {
			var snMerchantType = oMerchantType[i].value;
			break;
		}
	}

	if(ssSearchType == 'advance')
	{
		switch(parseInt(snMerchantType))
		{
			case 1:
				var snIdState		= document.getElementById('id_state').value;
				break;
			case 2:
			case 3:
				var snWithinKmMiles	= document.getElementById('within_km_miles').value;

				var smPostalCode	= ((document.getElementById('chkPostalRadius').checked == true && snMerchantType == 3) || snMerchantType == 2) ? document.getElementById('postal_code').value : '';
				var oKmMiles		= document.getElementsByName('km_miles');
				var snIdState		= document.getElementById('id_state').value;

				for(var i = 0; i < oKmMiles.length; i++) {
					if(oKmMiles[i].checked) {
						var snKmMiles = oKmMiles[i].value;
						break;
					}
				}
				break;
		}
	}

	if(ssSearchType)
	{
		ssSearchUrl = ssSearchUrl + '&search_type=' + ssSearchType;
	}

	if(snMerchantType == 2 || snMerchantType == 3)
		ssSearchUrl = ssSearchUrl + '&merchant_type=' + snMerchantType + '&within_km_miles=' + snWithinKmMiles + '&postal_code=' + smPostalCode + '&km_miles=' + snKmMiles + '&id_state=' + snIdState;
	else
		ssSearchUrl = ssSearchUrl + '&merchant_type=' + snMerchantType + '&id_state=' + snIdState;

	if(ssShowAll)
		ssSearchUrl = ssSearchUrl + '&showall=' + ssShowAll;

	if(ssSearch)
		ssSearchUrl = ssSearchUrl + '&search=' + ssSearch;


	$j=jQuery.noConflict();
		
	$j("#indicator").load(ajaxLoader());

	
	$j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message){	
	    	$j("#"+updateDiv).html(message);
	    },
		complete: function(){
			ajaxUnload();
		} 
	});
}

//	for searching
function searching(searchFieldName, urlpath, updateDiv, showType)
{
	if(document.getElementById('display_type'))
	{
		if(document.getElementById('display_type').value == 'promotion')
			searchFieldName = searchFieldName + '!id_merchant';
	}

	var snMerchantType	= '';
	var ssSearchType	= '';
	var ssShowAll		= '';
	var ssSearch		= '';
	var ssActualUri		= (document.getElementById('actual_uri') != null) ? document.getElementById('actual_uri').value : '';

	if(document.getElementById('showall') != null)
		ssShowAll	= document.getElementById('showall').value;

	if(document.getElementById('search') != null)
		ssSearch	= document.getElementById('search').value;

	if(document.getElementById('search_type') != null)
		ssSearchType = document.getElementById('search_type').value;

	var asFieldLen = searchFieldName.split('!');
	var ssSearchUrl = '';

	if(typeof showType != "undefined" && showType != 'search')
	{
		document.getElementById('id_merchant').value = '';
		obj = document.getElementById('id_segments');
		obj.value='';
	
		if(document.getElementById('id_scope'))
		{
			obj = document.getElementById('id_scope');
			obj.value='';
		}

		for(var i=0 ; i<asFieldLen.length ; i++)
		{
			if(i>1)
			{
				if(asFieldLen[i] == 'display_type')
					document.getElementById(asFieldLen[i]).value=document.getElementById(asFieldLen[i]).value;	
				else if(asFieldLen[i] == 'id_state')
					document.getElementById(asFieldLen[i]).value=0;
				else
					document.getElementById(asFieldLen[i]).value='';	
			}
		}
	}

	for(var i=0 ; i<asFieldLen.length ; i++)
	{
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';
		
		if(asFieldLen[i] == 'merchant_name' || asFieldLen[i] == 'merchant_keywords')
		{
			tbc = document.getElementById(asFieldLen[i]).value;
	  		tbc = encodeURIComponent(tbc);
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + tbc;
		}
		else
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;
	}

	if(ssSearchType == 'advance')
	{
		var oMerchantType = document.getElementsByName('merchant_type');

		for(var i = 0; i < oMerchantType.length; i++) {
			if(oMerchantType[i].checked) {
				var snMerchantType = oMerchantType[i].value;
				break;
			}
		}

		switch(parseInt(snMerchantType))
		{
			case 1:
				var snIdState		= document.getElementById('id_state').value;
				break;	
			case 2:
			case 3:
				var snWithinKmMiles	= document.getElementById('within_km_miles').value;

				var ssPostalRadius 	= (document.getElementById('chkPostalRadius').checked) ? true : false;	
				var smPostalCode	= ((document.getElementById('chkPostalRadius').checked == true && snMerchantType == 3) || snMerchantType == 2) ? document.getElementById('postal_code').value : '';
				var oKmMiles		= document.getElementsByName('km_miles');
				var snIdState		= document.getElementById('id_state').value;

				for(var i = 0; i < oKmMiles.length; i++) {
					if(oKmMiles[i].checked) {
						var snKmMiles = oKmMiles[i].value;
						break;
					}
				}
				break;
		}

		if(snMerchantType == 2 || snMerchantType == 3)
			ssSearchUrl = ssSearchUrl + '&within_km_miles=' + snWithinKmMiles + '&postal_code=' + smPostalCode + '&km_miles=' + snKmMiles + '&postal_radius=' + ssPostalRadius;
	}

	ssSearchUrl = ssSearchUrl + '&search_type=' + ssSearchType + '&showall=' + ssShowAll + '&search=' + ssSearch + '&merchant_type=' + snMerchantType + '&id_state=' + snIdState + '&actual_uri=' + ssActualUri;

	$j=jQuery.noConflict();
	$j("#indicator").load(ajaxLoader());

	var objAjax = $j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message){ 
	    	$j("#"+updateDiv).html(message);
	    },
		complete: function(){
			ajaxUnload();
		},
		beforeSend: function(jqXHR) {
			xhrPool.push(jqXHR);
			killRequest();
		}

	});
}

function setSearchSort(ssValue)
{
	document.getElementById('search_sort').value = ssValue;
}

// for showing loader image while sorting/searching
function ajaxLoader()
{
	var oIndicator = document.getElementById('indicator');
	if(oIndicator != null)
	{
		document.getElementById('indicator').style.display = '';
		if(navigator.appName == 'Microsoft Internet Explorer')
		   	document.getElementById('listing').style.filter = "alpha(opacity = 20)";
		else
			document.getElementById('listing').style.opacity = "0.1";
	
	}
}

// for hide loader image while sorting/searching is complete
function ajaxUnload()
{
	var oIndicator = document.getElementById('indicator');
	if(oIndicator != null)
	{
		document.getElementById('indicator').style.display = 'none';
		if(navigator.appName == 'Microsoft Internet Explorer')
			document.getElementById('listing').style.filter = "";
		else
			document.getElementById('listing').style.opacity = "1";
		
	}
}


// for Promotin generation Records per page
function recordsPerPage(searchFieldName, ssRecordPerPage, urlpath, updateDiv)
{
	var asFieldLen = searchFieldName.split('!');
	var ssSearchUrl = '';
	for(var i=0 ; i<asFieldLen.length ; i++)
	{
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';
		ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;
	}
		
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message) 
	    {	
	    	$j("#"+updateDiv).html(message);
	    } 
	});
}

// Add available promotion to selected promotion
function addPromotion(msg)
{
	var oElement = document.forms[0].elements;
	var snFlag = 0;

	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'promotionIds[]')
		{
			if(oElement[i].checked == true)
				snFlag++;
		}
	}	
	if(snFlag == 0)
	{
		alert(msg);
		return false;
	}

	document.getElementById('btnAddAll').value='y';
	document.affiliate.submit();
}	

// delete promotion for selected promotion
function deletePromotion(msg,delmsg)
{
   	var oElement = document.forms[0].elements;
	var snFlag = 0;

	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'promotionIds[]')
		{	
			if(oElement[i].checked == true)
				snFlag++;
		}
	}	
	if(snFlag == 0)
	{
		alert(msg);
		return false;
	}
	if(confirm(delmsg))  
	{
		document.getElementById('btnDeleteAll').value='y';
		document.affiliatePromotion.submit();
	}
}

function checkUncheckOne(frm,checkname,allcheckbox)
{
	var countElement = 0;
	var selectedElement = 0;
	
	if(typeof checkname == "undefined")
    {
	  var checkname='deleteIds[]';
    }

	 if(typeof allcheckbox =="undefined")
	 {
		var allcheckbox="deleteId"; 
	 }
	
	for ( x=0; x<frm.elements.length; x++ )
	{
		var e = frm.elements[x];
		if ( e.name == checkname )
		{
			countElement++;
			if ( e.checked ) selectedElement++;
		}
	}
	document.getElementById(allcheckbox).checked = countElement == selectedElement;
}

//Check Uncheck Multiple checkbox
function checkUncheckAll(frm,checkname,allcheckbox)
{
    if(typeof checkname == "undefined")
    {
	   var checkname='deleteIds[]';
    }
    if(typeof allcheckbox =="undefined")
    {
  	   var allcheckbox="deleteId"; 
    }
    for ( x=0; x<frm.elements.length; x++ )
    {
   	   var e = frm.elements[x];

 	   if ( e.name == checkname) e.checked = document.getElementById(allcheckbox).checked;
    }
}

// This Function is used for searching when enter on textbox in PromotionGeneration Page
function checkEvent(e, searchFieldName, urlpath, updateDiv)
{
	if(e.keyCode == 13)
	{
		var ssFlag = setPostalCodeFormat();

		if(ssFlag)
			searching(searchFieldName, urlpath, updateDiv);
	}
	//return false;
}

function trackLog(urlpath, updateDiv, searchFieldName, snIdMerchant, snIdAffiliate, snIdPromotion)
{
	var asFieldLen = searchFieldName.split('!');
	var ssSearchUrl = '';
	
	for(var i=0 ; i<asFieldLen.length ; i++)
	{
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';
		if(asFieldLen[i] == 'merchant_name' || asFieldLen[i] == 'merchant_keywords')
		{
			tbc = document.getElementById(asFieldLen[i]).value;
	  		tbc = encodeURIComponent(tbc);
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + tbc;
		}
		else
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;
	}

	ssSearchUrl = ssSearchUrl + "&IdMerchant="+snIdMerchant;
	if(typeof snIdAffiliate != "undefined")
		ssSearchUrl = ssSearchUrl + "&IdAffiliate="+snIdAffiliate;
	if(typeof snIdPromotion != "undefined")
		ssSearchUrl = ssSearchUrl + "&IdPromotion="+snIdPromotion;

	$j=jQuery.noConflict();

	$j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message) 
	    {	
	    	$j("#"+updateDiv).html(message);
	    } 
	});
}

function updateState(id, path, stateDiv)
{	
	ssSearchUrl = "&idCountry="+id;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
	    type: "POST", 
	    url: path, 
	    data: ssSearchUrl, 
	    success: function(message) 
	    {	
	    	$j("#"+stateDiv).html(message);
	    } 
	});
}

function postalCodeFormate(path, idCountry, postalCode, stateDiv)
{
	ssSearchUrl = "&id_country="+idCountry+"&postal_code="+postalCode;

	$j=jQuery.noConflict();

	$j.ajax(
	{
	    type: "POST",
	    url: path,
		async: false,
	    data: ssSearchUrl,
	    success: function(message)
	    {
	    	$j("#"+stateDiv).html(message);
	    }
	});
}

// Phase 4 Part 2 Changes
// Add available merchants to selected merchants
function addMerchant(msg)
{
	var oElement = document.forms[0].elements;
	var snFlag = 0;

	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'merchantIds[]')
		{
			if(oElement[i].checked == true)
				snFlag++;
		}
	}
	if(snFlag == 0)
	{
		alert(msg);
		return false;
	}

	document.getElementById('btnAddAll').value='y';
	document.affiliate.submit();
}

// delete merchant for selected merchant
function deleteMerchant(msg,delmsg)
{
   	var oElement = document.forms[0].elements;
	var snFlag = 0;

	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'merchantIds[]')
		{
			if(oElement[i].checked == true)
				snFlag++;
		}
	}
	if(snFlag == 0)
	{
		alert(msg);
		return false;
	}
	if(confirm(delmsg))
	{
		document.getElementById('btnDeleteAll').value='y';
		document.affiliatePromotion.submit();
	}
}

//This is used to add widget click 
function addWidgetClick(snIdWidget,ssUrlPath)
{
	var ssUrlPath		= ssUrlPath;
	var ssSearchUrl		= '&idWidget='+snIdWidget+'&addWidgetClick=yes';

	$j=jQuery.noConflict();

	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){	} 
	});
}

//This function is used for hot deals sorting
function hotSorting(searchFieldName, ssSortOn, ssSortBy, urlpath, updateDiv)
{	
	var downImage =  '/images/down1.png' ;
	var downGrayImage = '/images/down1-gray.png';
	var upImage = '/images/up1.png';
	var upGrayImage = '/images/up1-gray.png';

	if(ssLastSortOn != '' && ssLastSortOn != ssSortOn && document.getElementById('img_' + ssLastSortOn + '_down') != null  && document.getElementById('img_' + ssLastSortOn + '_up') != null)
	{
		if(ssLastSortBy == 'desc')
			document.getElementById('img_' + ssLastSortOn + '_down').src = downImage;
		else
			document.getElementById('img_' + ssLastSortOn + '_up').src = upImage;
	}

	if(ssSortBy == 'desc')
	{
		if(document.getElementById('img_' + ssSortOn + '_down') != null && document.getElementById('img_' + ssSortOn + '_up') != null)
		{
			document.getElementById('img_' + ssSortOn + '_down').src = downGrayImage;
			document.getElementById('img_' + ssSortOn + '_up').src = upImage;
		}
	}
	else
	{
		if(document.getElementById('img_' + ssSortOn + '_down') != null && document.getElementById('img_' + ssSortOn + '_up') != null)
		{
			document.getElementById('img_' + ssSortOn + '_up').src = upGrayImage;
			document.getElementById('img_' + ssSortOn + '_down').src = downImage;
		}
	}
	
	ssLastSortOn 		= ssSortOn;
	ssLastSortBy 		= ssSortBy;

	var asFieldLen 		= searchFieldName.split('!');
	var ssSearchUrl 	= '';
	var ssActualUri		= (document.getElementById('actual_uri') != null) ? document.getElementById('actual_uri').value : '';

	for(var i=0 ; i<asFieldLen.length ; i++)
	{		
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';

		if(asFieldLen[i] == 'merchant_name' || asFieldLen[i] == 'merchant_keywords')
		{
			tbc = document.getElementById(asFieldLen[i]).value;
	  		tbc = encodeURIComponent(tbc);
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + tbc;
		}
		else
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;	
	}

	ssSearchUrl = (ssSortBy) ? (ssSearchUrl + '&ssSortBy=' + ssSortBy) : ssSearchUrl;
	ssSearchUrl = (ssSortOn) ? (ssSearchUrl + '&ssSortOn=' + ssSortOn) : ssSearchUrl;
	ssSearchUrl = ssSearchUrl + '&actual_uri=' + ssActualUri;

	if(document.getElementById('display_type'))
	{
		if(document.getElementById('display_type').value == 'promotion')
			searchFieldName = searchFieldName + '!id_merchant';
	}

	var snMerchantType	= '';
	var ssSearchType	= '';

	if(document.getElementById('showall') != null)
		var ssShowAll	= document.getElementById('showall').value;
	if(document.getElementById('search') != null)
		var ssSearch	= document.getElementById('search').value;

	if(document.getElementById('search_type') != null)
	ssSearchType = document.getElementById('search_type').value;
	
	if(ssSearchType)
	{
		ssSearchUrl = ssSearchUrl + '&search_type=' + ssSearchType;
	}

	if(ssShowAll)
		ssSearchUrl = ssSearchUrl + '&showall=' + ssShowAll;

	if(ssSearch)
		ssSearchUrl = ssSearchUrl + '&search=' + ssSearch;


	$j=jQuery.noConflict();
		
	$j("#indicator").load(ajaxLoader());

	$j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message){	
	    	$j("#"+updateDiv).html(message);
	    },
		complete: function(){
			ajaxUnload();
		} 
	});
	
}

//This function is used for hot deals searching
function hotSearching(searchFieldName, urlpath, updateDiv, showType)
{
	if(document.getElementById('display_type'))
	{
		if(document.getElementById('display_type').value == 'promotion')
			searchFieldName = searchFieldName + '!id_merchant';
	}

	var snMerchantType	= '';
	var ssSearchType	= '';
	var ssShowAll		= '';
	var ssSearch		= '';
	var ssActualUri		= (document.getElementById('actual_uri') != null) ? document.getElementById('actual_uri').value : '';

	if(document.getElementById('showall') != null)
		ssShowAll	= document.getElementById('showall').value;

	if(document.getElementById('search') != null)
		ssSearch	= document.getElementById('search').value;

	if(document.getElementById('search_type') != null)
		ssSearchType = document.getElementById('search_type').value;

	var asFieldLen = searchFieldName.split('!');
	var ssSearchUrl = '';

	if(typeof showType != "undefined" && showType != 'search')
	{
		document.getElementById('id_merchant').value = '';
		obj = document.getElementById('id_segments');
		obj.value='';
	
		if(document.getElementById('id_scope'))
		{
			obj = document.getElementById('id_scope');
			obj.value='';
		}

		for(var i=0 ; i<asFieldLen.length ; i++)
		{
			if(i>1)
			{
				if(asFieldLen[i] == 'display_type')
					document.getElementById(asFieldLen[i]).value=document.getElementById(asFieldLen[i]).value;	
				else if(asFieldLen[i] == 'id_state')
					document.getElementById(asFieldLen[i]).value=0;
				else
					document.getElementById(asFieldLen[i]).value='';	
			}
		}
	}

	for(var i=0 ; i<asFieldLen.length ; i++)
	{
		if(i>0)
			ssSearchUrl = ssSearchUrl + '&';
		
		if(asFieldLen[i] == 'merchant_name' || asFieldLen[i] == 'merchant_keywords')
		{
			tbc = document.getElementById(asFieldLen[i]).value;
	  		tbc = encodeURIComponent(tbc);
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + tbc;
		}
		else
			ssSearchUrl = ssSearchUrl + asFieldLen[i] + '=' + document.getElementById(asFieldLen[i]).value;
	}

	ssSearchUrl = ssSearchUrl + '&search_type=' + ssSearchType + '&showall=' + ssShowAll + '&search=' + ssSearch + '&actual_uri=' + ssActualUri;

	$j=jQuery.noConflict();
	$j("#indicator").load(ajaxLoader());
	$j.ajax( 
	{ 
	    type: "POST", 
	    url: urlpath, 
	    data: ssSearchUrl, 
	    success: function(message){ 
	    	$j("#"+updateDiv).html(message);
	    },
		complete: function(){
			ajaxUnload();
		}
	});
}

function checkBoxValue(currentCheckbox,snId,ssUrlPath,snIdCountry,msg)
{	
	var ssMerchantPage = document.getElementById('ssMerchantPage').value;
	//alert(ssMerchantPage);
	var asCookie = document.cookie;
	var ssCookie = asCookie.split(';'); 
	var ssKey	 = ''; 
	for(i=1; i < ssCookie.length ; i++) 
	{
		if(ssCookie[i].match(/snCountMid/gi)) 
		{ 
			ssSplitCookie	= ssCookie[i].split('='); 
			ssKey			= ssSplitCookie[1].replace('snCountMid', ''); 
		}
	}
	
	var ssUrlPath	= ssUrlPath;
	var snFlag   = 0;
	if(document.getElementById('merchantIds_'+snId).checked == true)
	{
		if(ssKey > 9)
		{
			alert(msg);
			currentCheckbox.checked=false;
    		return false;
    	}	
		snFlag = 1;
	}	
				
	var ssSearchUrl	= '&snId='+snId+'&merchant_country='+snIdCountry+'&snFlag='+snFlag+'&ssMerchantPage='+ssMerchantPage;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ }
	});
}
// This Function is used for searching when enter on textbox in hot PromotionGeneration Page
function hotCheckEvent(e, searchFieldName, urlpath, updateDiv)
{
	if(e.keyCode == 13)
	{
			hotSearching(searchFieldName, urlpath, updateDiv);
	}
}

//This function is used to clear all selected hotdeal merchants
function doClearAllHotDealMerchants(ssConfirmMessage)
{
    if ( confirm(ssConfirmMessage) )
	{
		document.getElementById('flag').value=1;
		document.merchant.submit();
	}
}

// Add selected single record  for Individual merchant table
function addIndividualMerchnatId(snId,ssUrlPath)
{	
	snFlag = 0;
	if(document.getElementById('chk_merchants_'+snId).checked == true)
		snFlag = 1;
		
	var ssSearchUrl	= '&snId='+snId+'&snFlag='+snFlag;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ }
	});
}

// Add selected multiple records for Individual merchant table
/*
function addSeletedMerchantAjax(ssUrlPath,msg)
{
	var oElement = document.frmSettingPage['chk_merchants[]'];
	var snPriority = snIds = '';
	var snId = snFlag = 0;
	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'chk_merchants[]')
		{
			if(oElement[i].checked == true)
			{
				snPriority =  document.getElementById('anPriority_'+oElement[i].value).value;
				snIds = snIds + oElement[i].value+ '-' + snPriority + ',';
				snFlag = 1;
			}
			else
			{
				snIds = snIds + oElement[i].value + ',';
				snId++;	
			}
		}	
	}
		
	var ssSearchUrl	= '&snIds='+snIds+'&snFlag='+snFlag;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ }
	});
}
*/

function addSeletedMerchantAjax(ssUrlPath,msg)
{
	var oElement = document.frmSettingPage['chk_merchants[]'];
	var snPriority = snIds = '';
	var snId = snFlag = 0;
	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'chk_merchants[]')
		{
			if(oElement[i].checked == true)
			{
				snIds = snIds + oElement[i].value+ ',';
				snFlag = 1;
			}
			else
			{
				snIds = snIds + oElement[i].value + ',';
				snId++;	
			}
		}	
	}
		
	var ssSearchUrl	= '&snIds='+snIds+'&snFlag='+snFlag;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ }
	});
}


function setMySettingPageValue(currentCheckbox,snId,ssUrlPath,msg)
{	
	var asCookie = document.cookie;
	
	var ssCookie = asCookie.split(';'); 
	//alert(asCookie);
	var ssKey	 = ''; 
	var snFlag   = 0;
	for(i=1; i < ssCookie.length ; i++) 
	{
		if(ssCookie[i].match(/snCountMid/gi)) 
		{ 
			ssSplitCookie	= ssCookie[i].split('='); 
			ssKey			= ssSplitCookie[1].replace('snCountMid', ''); 
		}
	}
	
	if(document.getElementById('chk_merchants_'+snId).checked == true)
	{
		if(ssKey > 9)
		{
			alert(msg);
			currentCheckbox.checked=false;
    		return false;
    	}	
		snFlag = 1;
	}	
	
	var ssSearchUrl	= '&snId='+snId+'&snFlag='+snFlag;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ }
	});
}

//This function is used to clear all selected hotdeal merchants
function clearAllMySettingMerchants(ssConfirmMessage,msg,updateDiv)
{
	var oElement = document.frmSettingPage['chk_merchants[]'];
	var snPriority = snIds = '';
	var ssUrlPath = 'content/mySettings';
	var snId = snFlag = 0;
	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'chk_merchants[]')
		{
			if(oElement[i].checked == true)
			{
				snIds = snIds + oElement[i].value+ ',';
				snFlag = 1;
			}
		}	
	}
	if(snFlag == 1)
	{
		if(confirm(ssConfirmMessage))
		{
			var ssSearchUrl	= '&flag=1';
			$j=jQuery.noConflict();
			$j.ajax( 
			{ 
		    	type: "POST", 
		    	url: ssUrlPath, 
		    	data: ssSearchUrl, 
		    	 success: function(message){	
			    	$j("#"+updateDiv).html(message);
			    }
			});
		}
	}
	else
	{
		alert(msg);
		return false;
	}
}

//This function is used to clear all selected individual merchants from staff
function clearAllIndMySettingMerchants(ssConfirmMessage,msg,updateDiv,ssUrlPath)
{
	var oElement = document.frmSettingPage['chk_merchants[]'];
	var snPriority = snIds = '';

	var snId = snFlag = 0;
	for (var i=0; i < oElement.length; i++ )
	{
		if(oElement[i].name == 'chk_merchants[]')
		{
			if(oElement[i].checked == true)
			{
				snIds = snIds + oElement[i].value+ ',';
				snFlag = 1;
			}
		}	
	}
	if(snFlag == 1)
	{
		if(confirm(ssConfirmMessage))
		{
			var ssSearchUrl	= '&flag=1';
			$j=jQuery.noConflict();
			$j.ajax( 
			{ 
		    	type: "POST", 
		    	url: ssUrlPath, 
		    	data: ssSearchUrl, 
		    	 success: function(message){	
			    	$j("#"+updateDiv).html(message);
			    }
			});
		}
	}
	else
	{
		alert(msg);
		return false;
	}
}

function setIdCountryValue(snId,ssUrlPath,updateDiv)
{
	var ssSearchUrl	= '&snCountryId='+snId;
	$j=jQuery.noConflict();
	$j.ajax( 
	{ 
    	type: "POST", 
    	url: ssUrlPath, 
    	data: ssSearchUrl, 
    	success: function(message){ 
    		$j("#"+updateDiv).html(message);	
    	}
	});
}

//Added to kill previous jquery request while requests are more than one
function killRequest()
{
	if(xhrPool.length > 1)
	{
		var xhobj = xhrPool[0];
		if (typeof xhobj == 'object' && typeof xhrPool[1] == 'object')
		{
			xhobj.abort();
			xhrPool.shift();
		}

		/*
		$j=jQuery.noConflict();
		if ( $j.browser.mozilla && window.console != "undefined" )
			console.clear();
		*/
	}
}


