$(document).ready(function () { 
  $('.linkContacto').click(function (e) { 
    e.preventDefault();
    modalContact();
  });
});

function modalContact(){
//, onOpen: contact.open, onShow: contact.show,onClose: contact.close 
  Recaptcha.create("6LcoucgSAAAAAFJOqfHMAm9bkH3DtzlGtZufzqxn","recaptchaMail",{ lang : 'es',   theme : 'white'  });
      
   $("#contacto").modal({ 
    autoResize:true,close: false, overlayId: 'contactModalOverlay', containerId: 'contactModalContainer' 
   });
   
   $("#ContactViewForm").submit(function(){
     $("#cErr").hide();
     if(!$("#ContactName").val()){
        addError('Escribe tu nombre',$("#ContactName"));
        return false;
     }
     if(!$("#ContactMail").val()||!validateEmail($("#ContactMail").val())){
        addError('Escribe un mail valido',$("#ContactMail"));
        return false;
     }
     if(!$("#ContactMessage").val()){
        addError('Escribe un mensaje',$("#ContactMail"));
     }
     $("#ContactViewForm").hide();
     $("#cLoading").show();
     $.ajax({
        type:'POST',
        data: $("#ContactViewForm").serialize(),
        url: contactURL,
        dataType:'json',
        success:function(data){
         if (typeof data == "undefined" || !data){
           $("#cLoading").css({"background":"none","width":"auto",});
           $("#cLoading").html("<h3 style='color:#fff'>Tu mensaje fue enviado!</h3>");
           _gaq.push(['_trackEvent', 'contacto', 'terminado']);
           return;
         }
            if (typeof data.captcha !== "undefined"){
                $("#cLoading").hide();
                $("#ContactViewForm").show();
                addError(data.captcha,$("#recaptcha_response_field"));
                Recaptcha.reload();
                return;
            }
            if (typeof data.mail !== "undefined"){
              $("#cLoading").hide();
              $("#ContactViewForm").show();
              addError(data.mail,$("#ContactMail"));
              Recaptcha.reload();
              return;
          }
            if (typeof data.message !== "undefined"){
              $("#cLoading").hide();
              $("#ContactViewForm").show();
              addError(data.message,$("#ContactMessage"));
              Recaptcha.reload();
              return;
          }
           $("#cLoading").css({"background":"none","width":"auto",});
           $("#cLoading").html("<h3 style='color:#fff'>Tu mensaje fue enviado!</h3>");
           _gaq.push(['_trackEvent', 'contacto', 'terminado']);

        }
     }); 
     return false;

   });
   
      
}

function addError(msg,foc){
    $("#cErr").html(msg).fadeIn();
    foc.focus();
}

function validateEmail(email) {
  var at = email.lastIndexOf("@");

  // Make sure the at (@) sybmol exists and  
  // it is not the first or last character
  if (at < 1 || (at + 1) === email.length)
      return false;

  // Make sure there aren't multiple periods together
  if (/(\.{2,})/.test(email))
      return false;

  // Break up the local and domain portions
  var local = email.substring(0, at);
  var domain = email.substring(at + 1);

  // Check lengths
  if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
      return false;

  // Make sure local and domain don't start with or end with a period
  if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
      return false;

  // Check for quoted-string addresses
  // Since almost anything is allowed in a quoted-string address,
  // we're just going to let them go through
  if (!/^"(.+)"$/.test(local)) {
      // It's a dot-string address...check for valid characters
      if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
          return false;
  }

  // Make sure domain contains only valid characters and at least one period
  if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
      return false;   

  return true;
}

