var selected=true;
var toRun = true;

jQuery.fn.FocusClear = function(){
  var clearPrevious='';
  this.focus(function() {
    if($(this).val()==$(this).attr('alt'))
    {
      clearPrevious=$(this).val();
      $(this).val('');
    }
  });

  this.blur(function() {
    if($(this).val()=='')
    {
      $(this).val(clearPrevious);
    }
  });
};

function checkImg(button)
{
  if($(button).parent("span").find("input").is(':checked'))
     $(button).parent("span").find("input").removeAttr("checked");
  else
    $(button).parent("span").find("input").attr("checked",true);

  showImg();
  
}
function showImg() {

  $(".emailOption").each(function(){
    if($(this).parent("span").find("input").is(':checked'))
      $(this).attr("src","/images/popupchecked.png")
    else
       $(this).attr("src","/images/popupunchecked.png")
   
  });
}

function CheckAll() {
   $(".emailOption").each(function(){
     if(selected)
       $(this).parent("span").find("input").removeAttr("checked");
     else
      $(this).parent("span").find("input").attr("checked",true);
     });
      
  showImg();

  selected=!selected;
}

function countDown(){
  $("#ContactsReceiver").css('margin-top', '200px').css("font-weight", "bold");
  setTimeout("closeDialog()", 3000);
}

function closeDialog() {
  $('#ContactsReceiver').dialog('close');
  $("#ContactsReceiver" ).html("");
  toRun = true;
}

function mailByProvider() {
  var mails = $("#SendEmails").serialize()
  $('#ContactsReceiver').html('<img class="dialogLoader" src="/images/loader.gif" />');
  $.post("/customer/information/sendEmailByProvider",
  { 'email': mails },
  function(data) {
    $("#ContactsReceiver").html(data.message);
    countDown();
  },"json");
}
//12345678912345678912345678912345678
function openPop() {
  if(!toRun)
    return;
  $("#ContactsReceiver").dialog({
    height: 600,
    width:675,
    closeText: ''
  });
  toRun = false;
  $("#ContactsReceiver").css('margin-top', '0px').css("font-weight", "none");
  $("#ContactsReceiver" ).html('<img class="dialogLoader" src="/images/loader.gif" />');
  $("#ContactsReceiver").dialog('open');
  $(".ui-icon-closethick").click(function(){
    toRun=true
    });
  $.post("/customer/information/retrieveContactsFromProvider",
  {email:$("#mailRE").val() , password:$("#passwordRE").val()},
  function(data) {
    $("#ContactsReceiver" ).html("");
    if(data.error == "") {
      str="<form id='SendEmails' name = 'SendEmails' \
                action='/customer/information/sendEmailByProvider/' \
                onsubmit='return false' > <table class='dialogT'>";
      str+="<tr><td><img src='/images/popupcheckall.png' onclick='CheckAll()'/>\
                  </td><td></td></tr>";
      j=0;
      $.each(data.contacts, function(i, item) {
        str+="<tr><td><span><img class='emailOption' src='/images/popupchecked.png' id='"+ j +"' onclick='checkImg(this)' style='cursor:pointer'; />\
              <input style='display:none;' id='mail" + j + "' name='email[" + (j++) + "]' \
                      type='checkbox' value='"+i+"' \
                       checked/></span></td> \
                  <td>"+data.contacts[i]+"</td><td>"+i+"</td></tr>";
      });
      str+="</table></form>";
      str+="<div id='mailByProvider'>\
                  <a style='cursor:pointer;' \
                    onclick='mailByProvider();' >Send</a>\
                </div>";
         
      str = str.replace('&amp;','&');
      $("#ContactsReceiver").html(str);
    }
    else {
      $("#ContactsReceiver").html(data.error);
      countDown();
      return 0;
    }
  },"json");
}

function closeSponsorDialog() {
  $('#messageDialog').dialog('close');
}

function sendSponsorship() {
  $.post("/customer/information/sendMailWithMsg/",
  {
    mails:$("#amiMailForm").serialize()
    },

  function(data)  {
    if(data.error=="")
    {
      $('#sponsorshipMsg').html("</br></br>All e-mails have been sent.");
    }
    else
    {
      $('#sponsorshipMsg').html("</br></br>The following e-mails have failed \
                                 to been delivered:</br></br>" + data.error);
    }
    $('#sponsorshipMsg').append("<div id='closeSponsorDialog'>\
              <a style='cursor:pointer;' \
                onclick='closeSponsorDialog();' >Close</a>\
            </div>");
  },"json");
  $('#customerMsg').text("");
}

function checkSponsorMsg() {
  $("#messageDialog").dialog({height: 400, width: 675,closeText: ''});
  $.post("/customer/information/checkSponsorMsg/",
  {
    msg:$("#amiMessage").val()
    },
  function(data) {
    $('#sponsorshipMsg').html(data.msg);
    $('#sponsorshipMsg').append('<div id="sendSponserMail">\
            <a style="cursor:pointer;" \
              onclick="sendSponsorship();" >Send</a>\
          </div>');
    $('#messageDialog').dialog('open');
  },"json");
}

