function emailIsValid(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
 }
$(function(){
	/* Multi-delete feature */
	$('.multiselect.select-all').click(function(){
		$(this).each(function(index, element){
			$(element.rel + ' .checkbox.select:input').attr('checked', true);
		})
		return false;
	});
	$('.multiselect.select-none').click(function(){
		$(this).each(function(index, element){
			$(element.rel + ' .checkbox.select:input').attr('checked', false);
		})
		return false;
	});
	$('.delete-selected').click(function(){
		if (confirm("Are you sure you want to delete these elements?"))
		  $( this.rel + '-form' ).append($('<input/>').attr({type: 'hidden', name: 'task', value: 'delete'}));
		  $( this.rel + '-form' ).submit();
		return false;
	});
	/* End of multi-delete feature */
	/* Copying to the clipboard */
	$('.copy-to-clipboard').click(function(){
		$(this).each(function(index, element){
			$(element.rel).copy();
			alert("Copied to your clipboard.");
		});
		return false;
	});
	$('.select-all-on-click').click(function(){
		$(this).select();
		return false;
	});
	/* End ov copying to the clipboard */
	/* watch video page */
	$('#recipient-email').watch('value', function(){
		if (emailIsValid($(this).val())){
			$($(this).attr('rel')).css('visibility', 'visible');
		} else {
			$($(this).attr('rel')).css('visibility', 'hidden');
		}
	});
	$('#add-recipient').click(function(){
		var email = $('#recipient-email').val();
		if (!emailIsValid(email)){
			alert('Email is invalid!');
			return false;
		}
		if ($('#addressBookRecipients input[type=hidden][value="'+email+'"]').length > 0){
			alert('This email is already added');
			return false;
		}
			
		var deleteLink = $('<a/>').attr({href: '#', rel: '#'}).addClass('button-delete-small').append($('<img/>').attr('src', '/images/icons/delete.png').addClass('image-inline'));
		var hidden = $('<input/>').attr({type: 'hidden', value: email, name: 'recipient_emails[]'});
		var li = $('<li/>').text(email).append(deleteLink).append(hidden).addClass('clearfix');
		deleteLink.click(function(){
			li.remove();
		});
		$('#addressBookRecipients').append(li);
		$('#recipient-email').val('');
		return false;
	});
	$('#address-groups').change(function(){
		var groupId = $(this).val();
		if (groupId == null || groupId == '')
			return;
	 	if ($('#addressBookRecipients input[type=hidden][value='+groupId+']').length > 0)
			return;
		var text = $(this).find('option[selected]').text();
		var deleteLink = $('<a/>').attr({href: '#', rel: '#'}).addClass('button-delete-small').append($('<img/>').attr('src', '/images/icons/delete.png').addClass('image-inline'));
		var hidden = $('<input/>').attr({type: 'hidden', value: groupId, name: 'address_groups[]'});
		var li = $('<li/>').text(text).append(deleteLink).append(hidden).addClass('clearfix');
		li.append($('<span/>').text('Group'));
		deleteLink.click(function(){
			li.remove();
		});
		$('#addressBookRecipients').append(li);
		$('#address-groups').val('');
	});
	/* customization UI */
	handleImageChange = function(){
	  if (!$(this).val().match(/.*\.(jpg|jpeg|gif|png)$/i)){
	    alert("Only png, jpeg and gif formats are allowed.");
	    $(this).replaceWith($("<input>").attr({type: 'file', id: $(this).attr('id'), name: $(this).attr('name')}).change(handleImageChange));
	    return false;
	  } else {
	    
	  }
  }
  handleVideoChange = function(){
    var ext = $(this).val().replace(/.*\./, '');
	  if (!$(this).val().match(/.*\.(flv|avi|mpeg|mpg|wmv|swf|flv|rm|asf|3g2|3gp|mp4|mp4v|mpe|moov|mov|jpg|jpeg|gif|png)$/i)){
	    alert("Video extension '" + ext + "' is not supported");
	    $(this).replaceWith($("<input>").attr({type: 'file', id: $(this).attr('id'), name: $(this).attr('name')}).addClass('file').change(handleVideoChange));
	    return false;
	  } else {
	   
	  }
  }
	$('#brand_uploaded_data').change(handleImageChange);
	$('#brand_background_image').change(handleImageChange);
  $('input.file').change(handleVideoChange);
	  

	/* end of watch video page */
	
});
