$(document).ready(function() {
	//Alert Message when no tag name is entered for search.
	$('#searchTag').click(function() {
		if($('#searchTagName').val()=='') {
			jAlert(errSearchTerm, msgAlert);
			return false; }
		else return true;
	});
        $("#submitSearch").click(function(){
          if( jQuery.trim($('#queryTextSearch').val())=="" && jQuery.trim($('#searchDate').val())=="" && jQuery.trim($('#categoryID').val())=="" && jQuery.trim($('#synonymForSearch').val())=="" && jQuery.trim($('#numResultsSearch').val())=="" && jQuery.trim($('#popularitySearch').val())=="" && jQuery.trim($('#queryStatusSearch').val())=="") {
			   jAlert(msgSelectSearchCriteria);
			   return false;
		   }		
           return true; 
         });	
	//To add catalog tags on enter on text field.
	$('#tagName').on('keypress', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 13) {
			if($('#tagName').val()=='') 
				jAlert(errName, msgAlert);
			else {
				var name=$.trim($('#tagName').val());
				if(name!='') {
					$.ajax({
						type:'POST',
						//data:'tagName='+$.trim(name),
						data:{'tagName':$.trim(name)},
						url: GLOBAL_BASE_PATH + "/catalogtags/ajaxaddTags/",
						success: function(data) {
							if(data == 1)
								jNotifyShow(errTagNameExts, "common_message", "message_error");
							else {
								jNotifyShow(tagNameAdded, "common_message", "message_success");  
								$('#catalog-listing').html(data);
								$('#tagName').val('');
								$('#jsNoRecordMsg').attr('style','display:none');
								$('.catalog-txt').each(resizeInput);
							}
						}
					});
				}
			}		
		}  
    });
	//To add catalog tags on add tag button
	$('#addTag').click(function() {
		var name=$.trim($('#tagName').val());
		if(name!='' && (name!=$('#tagName').prop('defaultValue'))) {
			$.ajax({
				type:'POST',
				//data:'tagName='+$.trim(name),
				data:{'tagName':$.trim(name)},
				url: GLOBAL_BASE_PATH + "/catalogtags/ajaxaddTags/",
				success: function(data) {
					if(data == 1)
						jNotifyShow(errTagNameExts, "common_message", "message_error");
					else {
						jNotifyShow(tagNameAdded, "common_message", "message_success");  
						$('#catalog-listing').html(data);
						$('#tagName').val('');
						$('#jsNoRecordMsg').attr('style','display:none');
						$('.catalog-txt').each(resizeInput);
					}
				}
			});
		} else {						
			jAlert(errName, msgAlert);
		}						
	});											
	//To delete tags
	$(document).on('click','[task=deleteTag]',function() {
		var tagID = $(this).attr('tagid');
		jConfirm(msgDeleteItem,deleteText, function(r) {
			if(r==true) {
				 if(tagID > 0) {
					 $.ajax({
						type:'POST',
						data:'tagID='+tagID,
						url:GLOBAL_BASE_PATH + "/catalogtags/ajaxdeleteTags/",
						success:function(data) {
							$('#tag-'+tagID).remove();
							if($('li.tagItems').length == 0)  {
								$('#jsNoRecordMsg').attr('style','display:block');
								$('#catalog-listing').html('');
							}
							jNotifyShow(tagNameDelete, "common_message", "message_success");  
						}
					});
				} 
			}
		});
	});
	//To control the display of save and delete icons and add current class to focused input field.
	$(document).on("click", ".catalog-txt", function() {
		$(this).parent().siblings('[task=deleteTag]').addClass('hide').removeClass('delete-txt');
		$(this).parent().prev('.saveTag').removeClass('hide');
		$('.current').removeClass('current');
		$(this).closest('li').addClass('current');
	}); 
	//To control the display of current(li) save and delete icons and input field value.
	$('body').click(function (e) {
        if ($(".current").has(e.target).length === 0) {
			$('.current [task=deleteTag]').removeClass('hide');
            $('.current [task=deleteTag]').addClass('delete-txt');
			$('.current .saveTag').addClass('hide');
			
			if (($(".current .catalog-txt").val()=='') ||  $(".current .catalog-txt").val()!= $(".current .catalog-txt").attr('oldvalue')) {
				$(".current .catalog-txt").val($(".current .catalog-txt").attr('oldvalue'));
				$(".current .catalog-txt").attr('size', $(".current .catalog-txt").attr('oldvalue').length);
			}
			$('.current').removeClass('current');
        }
    });
	//To add catalog tags from Manage Search Terms page to selected languages.
	$('.addAsTag').click(function() {
		var name		=	$(this).closest('tr').children('td.queryText').text();
		var langid 		= 	'';
		var element 	= 	$(this);
		var link 		= 	$(this).closest('ul').find('.ListAll2');
		var ListAll 	= 	$(this).closest('ul').find(".ListAll2:checked");
		$.each(ListAll, function(key, element) {
			 langid +=   element.value+ ',';
		});
		 if(name!='' && langid!='') {
			data={'tagName':$.trim(name),"langid":langid};
			$.ajax({
			type:'POST',
			data:data,
			url: GLOBAL_BASE_PATH + "/catalogtags/ajaxaddTags/",
			success: function(data) {
				jNotifyShow(tagNameAdded, "common_message", "message_success");  
				location.reload();
			} 
			});
		}
	});
	//Language dropdrown for adding search terms as catalog tag.
	$('.langDropdownStart').click(function () {
		var queryid = $(this).attr('queryid');
        if ($(".ListLangSelection_"+queryid).is(':visible'))
            $(".ListLangSelection_"+queryid).slideUp(); 
        else {
            $(".ListLangSelection_"+queryid).slideDown();
        }
    });
	//To check all checkboxes(anguage) of a search term.
    $(document).on('click', ".check_lang li .checkall", function () { 
		 var link = $(this).closest('ul').find('.ListAll2');
		 var ListAllCount = link.size();
		 var ListAllCheckedCount = $(this).closest('ul').find(".ListAll2:checked").size();
		 if (parseInt(ListAllCount) != parseInt(ListAllCheckedCount)) {
                        link.prop('checked',true);
		 } else {
			link.prop('checked',false);
		 }
    });
});
$(document).mouseup(function (e) {
    var container = $(".check_lang");
    if (container.has(e.target).length === 0) {
        container.slideUp();
    }
});
//To save tag when save icon is clicked( Edit ).
function saveTags(thisvar) {					
	  var thistxtfield 	= 	thisvar.next('div.tagName').find('.catalog-txt');	
	  var name			=	$.trim(thistxtfield.val());
	  var tagID 		= 	thisvar.attr('tagid');
	  var oldvalue 		= 	thistxtfield.attr('oldvalue');
	
	 if(name!='' && name!=oldvalue) {								
        $.ajax({    							
			type: "POST",  
            url: GLOBAL_BASE_PATH + "/catalogtags/ajaxaddTags/",						
            data: {"tagName":name, "tagID":tagID},  
            success: function(data){
				if(data == 1) {
					jNotifyShow(errTagNameExts, "common_message", "message_error");						
					thistxtfield.val(oldvalue);											
				} else {
					thisvar.siblings('[task=deleteTag]').addClass('delete-txt');
					thisvar.addClass('hide');										
					jNotifyShow(tagNameUpdate, "common_message", "message_success");  
					thistxtfield.val(name);
					thistxtfield.attr('oldvalue',name);
					$('.current [task=deleteTag]').removeClass('hide').addClass('delete-txt');
				}
			}
		}); 
	}
	if(name == '') {
		thistxtfield.val(oldvalue);
		thistxtfield.attr('size', oldvalue.length);
	}
}
//Resize all input fields during page load
$('.catalog-txt').keypress(resizeInput).each(resizeInput);
function resizeInput() {  
	$(this).attr('size', $(this).val().length);
}