$(function() {
  $("button[rel]").overlay({expose: '#000000'});
	$("a[rel]").overlay({expose: '#000000', closeOnClick: 'false'});
	
	$('#helpfulYes').bind('click', function(event){
    closeAll();
    showComments();
  });
  
	$('#helpfulNo').bind('click', function(event){
    closeAll();
    $('#helpful_buttons').hide();
    $('#helpfulFeedback').show();
  });
  
	$('#notHelpfulYes').bind('click', function(event){
    closeAll();
    showComments();
  });
  
	$('#notHelpfulNo').bind('click', function(event){
    closeAll();
    $('#helpful_buttons').hide();
    $('#unhelpfulFeedback').show();
  });
  
  
  $('#submit_comment').bind('click', function(event){
    event.stopPropagation();
    // SERVER: comment server call here
    $('#comment_form').hide();
    $('#commentReceived').show();
  });
  
  $('#customize_save').bind('click', function(event){
    event.stopPropagation();
    closeAll();
  })
  
  $('#customize_cancel').bind('click', function(event){
    event.stopPropagation();
    closeAll();
  })
  $('.print_now').click(function(event){
    window.print();
  });
  $('#email_save').bind('click', function(event){
    //TODO: Validate email
    event.stopPropagation();
    var emailAddress = $('.email_input').val();
    if(isValidEmailAddress(emailAddress)){
      closeAll();
    } else {
      alert('The email address is not valid. Please enter a valid email address.')
    }
  })
  $('#email_cancel').bind('click', function(event){
    event.stopPropagation();
    closeAll();
  });
  
  $(".email_select_all").bind('click', function(event){
    event.stopPropagation();
		var checked_status = this.checked;
		$("INPUT[type='checkbox']").attr('checked', true);
	});
	
});



function closeAll() { 
    $("button[rel]").each(function() { 
        $(this).overlay().close(); 
    }); 
}

function showComments(){
  $('#comment_form').show();
  $('#helpful_buttons').hide();
}

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}