$(function() { 
  
  var part_meh1 = 'ma';
  var part_meh2 = 'ilto';
  var part_meh3 = ':';
  var part_meh4 = 's@';
  var part_meh5 = 'sale';
  var part_meh6 = 'rtp';
  var part_meh7 = 'mh.c';
  var part_meh8 = 'om';
  var part_meh9 = 'inf';
  var part_meh10 = 'o@';
  
  $('#top_menu_mail').html('<a style="display:inline;padding:0px;font-size:16px;" href="'+part_meh1+part_meh2+part_meh3+part_meh9+part_meh10+part_meh6+part_meh7+part_meh8+'" title="Click here to get in contact">'+part_meh9+part_meh10+part_meh6+part_meh7+part_meh8+'</a>');
  
  $(".footer_contactus").html('<a href="'+part_meh1+part_meh2+part_meh3+part_meh9+part_meh10+part_meh6+part_meh7+part_meh8+'" title="Click here to get in contact">'+part_meh9+part_meh10+part_meh6+part_meh7+part_meh8+'</a>');
  
  //close flash messageBox
  $('.close_flashmessage').click(function(){
    $(this).parent().slideUp();  
  });
  
  $('input[name=top-search-box]').autoComplete({
    ajax: '/search/live/'
  });
  
  $('#username').blur(function() {
    if($(this).val()!="")
    {
      $(this).addClass('remove_background');
    }else{
      $(this).removeClass('remove_background');
    }
  });
  
  $('#password').blur(function() {
    if($(this).val()!="")
    {
      $(this).addClass('remove_background');
    }else{
      $(this).removeClass('remove_background');
    }
  });
  
  $('#enquiry_button').click(function(){
    $('#enquiry_box').slideToggle('fast');
  });
  
  $('#enquiry_items .cart_item .delete_button').live('click',function(){
    $.ajax({
      type: "POST",
      url: "/enquiry/remove",
      data: "row_id="+$(this).attr('data-rowid'),
      success: function(msg){
        $('#enquiry_items').html(msg);
      }
    });
  });
  
  $('#product_enquiry_removeall').live('click',function(){
    $.ajax({
      type: "POST",
      url: "/enquiry/empty",
      success: function(msg){
        $('#enquiry_items').html(msg);
      }
    });
  });
  
  /* Popup for "Make enquiry about this product" link */
		
	$('#product_multi_enquiry').live('click',function (){
    $("#email_product_multi_popup").dialog('open');
  });
	
	$("#email_product_multi_popup").dialog({
		autoOpen: false,
		height: 450,
		width: 480,
		modal: true,
		buttons: {
			'Send equiry': function() {
			  if($('#email_product_multi_popup').find("input[name='email']").val().length!=0 || $('#email_product_multi_popup').find("input[name='telephone']").val().length!=0)
			  {
				  $.ajax({
            type: "POST",
            url: "/product/multi-enquiry",
            data: "name="+$('#email_product_multi_popup').find("input[name='name']").val()+"&email="+$('#email_product_multi_popup').find("input[name='email']").val()+"&telephone="+$('#email_product_multi_popup').find("input[name='telephone']").val()+"&details="+$('#email_product_multi_popup').find("textarea[name='details']").val(),
            success: function(return_string) {
              if(return_string=="win")
              {
                $("#flashmessage_data").text('Thankyou for your request, someone will be in contact with you as soon as possible');
                $("#flashmessage").slideDown();
              }else if(return_string=="no_items"){
                $("#flashmessage_data").text('There were no items in your enquiry list, please add some and try again');
                $("#flashmessage").slideDown();
              }else{
                $("#flashmessage_data").text('There was an error trying to send the message, please try again');
                $("#flashmessage").slideDown();
              }
              $('#enquiry_box').slideUp();
            }
          });
					$(this).dialog('close');
				}else{
				  $("#email_product_multi_error").text('Please fill in either a telephone number or an e-mail address');
				}
			}
		},
		Cancel: function() {
			$(this).dialog('close');
		}
	});
  
});
