// Need to keep these functions with JQuery
function writeError(field, message){$('#' + field +'_error').html("<span class=\"error\"><img src=\"/images/incomplete.png\"/> " + message + "</span>"); return false;}
function writeSuccess(field,message){$('#' + field +'_error').html("<span class=\"success\"><img src=\"/images/complete.png\"/> " + message + "</span>"); return true;}


function closeAllModalWindows(){
  $(".ui-dialog-content").each(function(){
  if ($(this).dialog !== undefined){
    $(this).dialog("close");   
  }
  });
} 

function displayLoginDialog(url, title){
  // displayAjaxDialogWithBackground(url, title, 320, 220);  
  $("#somedivforpopup").load(url).dialog({
    modal:true,
    title: title,
    minWidth: 320,
    minHeight: 220,
    width: 320,
    height: 285,    
    buttons: {
      "Fermer": function() { $(this).dialog("close"); }
    } 
  });  
}

function displayForgetPasswordDialog(url, title){
  // displayAjaxDialogWithBackground(url, title, 320, 220);
  $("#somedivforpopup").load(url).dialog({
    modal:true,
    title: title,
    minWidth: 320,
    minHeight: 220,
    width: 320,
    height: 320,    
    buttons: {
      "Fermer": function() { $(this).dialog("close"); }
    } 
  }); 
}

function popupVendorMessageSent(){
  $("#somedivforpopup").dialog("destroy");
  $("#somedivforpopup").load('/ad/send_confirmation_message').dialog({
    modal:true,
    title: 'Confirmation',
    width: 350,
    height: 200,
    buttons: {
      "Ok": function() { $(this).dialog("close"); }
    }   
  });
}

function popupVendorMessageError(){
  $("#somedivforpopup").dialog("destroy");
  $("#somedivforpopup").load('/ad/send_error_message').dialog({
    modal:true,
    title: 'Erreur',
    width: 350,
    height: 200,
    buttons: {
      "Ok": function() { $(this).dialog("close"); }
    }   
  });
}





function displaySubscriptionDialog(url, title){
  // displayAjaxDialogWithBackground(url, title, 660, 500);
  $("#somedivforpopup").dialog("destroy");
  $("#somedivforpopup").load(url).dialog({
    modal:true,
    title: title,
    minWidth: 500,
    minHeight: 400,
    width: 660,
    height: 700,
    buttons: {
      "Fermer": function() { $(this).dialog("close"); }
    }   
  });      
}

function validate_subscription_dialog() {
  
  var first_name = $("#vendor_first_name"),
  last_name = $("#vendor_last_name"),
  street = $('#vendor_street'),
  city = $('#vendor_city'),
  post_code = $('#vendor_post_code'),
  state = $('#vendor_state'),
  email = $("#vendor_email"),
  password = $("#vendor_password"),
  password_confirmation = $("#vendor_password_confirmation"),
  captcha = $('#captcha_value'),
  allFields = $([]).add(first_name).add(last_name).add(street).add(city).add(post_code).add(state).add(email).add(password).add(password_confirmation).add(captcha),
  tips = $(".validateTips");  

  var bValid = true;
  allFields.removeClass('ui-state-error');

  bValid = bValid && checkFieldLength(first_name,"Prénom",3,16);
  bValid = bValid && checkFieldLength(last_name,"Nom",3,16);
  bValid = bValid && checkFieldLength(street,"Adresse",3,16);
  bValid = bValid && checkFieldLength(city,"Ville",3,16);
  bValid = bValid && checkFieldLength(post_code,"Code postal",3,16);
  bValid = bValid && checkFieldLength(state,"Province",3,16);
  bValid = bValid && checkFieldLength(email,"E-mail",5,60);
  bValid = bValid && checkEmail();
  bValid = bValid && checkFieldLength(password,"Mot de passe",5,16);
  bValid = bValid && checkFieldLength(password_confirmation,"Confirmation du mot de passe",5,16);
          
  bValid = bValid && checkRegexp(first_name,/^[a-z]([0-9a-z_])+$/i,"Le prénom peut contenir des lettres a-z, 0-9, underscores, commençant par une lettre.");
  bValid = bValid && checkRegexp(last_name,/^[a-z]([0-9a-z_])+$/i,"Le nom peut contenir des lettres a-z, 0-9, underscores, commençant par une lettre.");
//  bValid = bValid && checkRegexp(street,/^[a-z]([0-9a-z_])+$/i,"L'adresse peut contenir des lettres a-z, 0-9, underscores, commençant par une lettre.");
  bValid = bValid && checkRegexp(city,/^[a-z]([0-9a-z_])+$/i,"La ville peut contenir des lettres a-z, 0-9, underscores, commençant par une lettre.");
          
          
   // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. support@gogloum.com");
  bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Le champs Mot de passe permet seulement les lettres ou les chiffres : a-z 0-9");
  bValid = bValid && checkRegexp(password_confirmation,/^([0-9a-zA-Z])+$/,"Le champs Confirmation du mot de passe permet seulement les lettres ou les chiffres : a-z 0-9");

  bValid = bValid && valid_password_match(password, password_confirmation);
  bValid = bValid && check_vendor_over_16();
  bValid = bValid && check_agree_terms();
  bValid = bValid && validateCaptcha();
              
  //$(this).dialog('close');
  return bValid
}





function displaySendEmailDialog(url, title)
{
 // displayAjaxDialogWithBackground(url, title, 400, 320);

  $("#somedivforpopup").load(url).dialog({
    modal:true,
    title: title,
    minWidth: 320,
    minHeight: 220,
    width: 520,
    height: 420,    
    buttons: {
      "Fermer": function() { $(this).dialog("close"); }
    } 
  });  
  
}

function validateSendMessageToVendor() {
  var bValid = true;
  var email = $("#send_email_form_email"),
      message = $('#send_email_form_message'),
      captcha = $('#captcha_value'),
      allFields = $([]).add(email).add(message).add(captcha),
      tips = $(".validateTips");        
      
  allFields.removeClass('ui-state-error');      

  bValid = bValid && checkFieldLength(email,"email",6,80);
  bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
  bValid = bValid && checkFieldLength(message,"message",2,1000); 
  bValid = bValid && validateCaptcha();
  return bValid;
}



// Validation Subscription


  function check_vendor_over_16(){
    if ($('#vendor_over_16').is(':checked')) {
      return true;
    }
      updateTips("Vous devez confirmer que vous êtes agé(e) de plus de 16 ans.");
      return false;
  }
  
  
  function check_agree_terms(){
    if ($('#vendor_terms').is(':checked')) {
      return true;
    }
    updateTips("Vous devez confirmer que vous acceptez les termes d'utilisations.");
    return false;
  }  


  function valid_password_match(password, confirmation){
    if (password.val() == confirmation.val()){
      return true;  
    }
    updateTips("La confirmation du mot de passe ne correspond pas au mot de passe.");
    return false;
  }
  

   function updateTips(t) {
    var tips = $(".validateTips"); 
      tips
        .text(t)
        .addClass('ui-state-highlight');
      setTimeout(function() {
        tips.removeClass('ui-state-highlight', 1500);
      }, 500);
    }

    function checkFieldLength(o,n,min,max) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass('ui-state-error');
        updateTips("La longueur de " + n + " doit être entre "+min+" et "+max+".");
        return false;
      } else {
        return true;
      }

    }

    function checkRegexp(o,regexp,n) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass('ui-state-error');
        updateTips(n);
        return false;
      } else {
        return true;
      }
    }


function validateCaptcha () {
  if ( $('#captcha_value').val() != "") {
    // Check if Captcha is valid
//    request = new Ajax.Request('/subscription/check_captcha?id=' + $('#captcha_id').val()+ '&text=' + $('#captcha_value').val(), {asynchronous:false, method: 'GET', evalScripts:false});

    var request = jQuery.ajax({ url: '/subscription/check_captcha?id=' + $('#captcha_id').val()+ '&text=' + $('#captcha_value').val(), async:false,  type:'GET'});
    if (valid_captcha){
      return writeSuccess('captcha', "");
    } else {
      return writeError('captcha','non valide'); 
    }
  } else {
    return writeError('captcha','non valide'); 
  }
}

function checkEmail () {
  if ( ($('#vendor_email').val() != "") && (checkRegexp($('#vendor_email'),/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. support@gogloum.com")) ) {
    // Check for an existing email
//    request = new Ajax.Request('/subscription/check_email?email=' + field, {asynchronous:false, method: 'GET', evalScripts:false});
       
    request = jQuery.ajax({ url: '/subscription/check_email?email=' + $('#vendor_email').val(), async:false,  type:'GET'});
    if (existing_email){
      return writeError('email','existe est déjà pris.'); 
    } else {
      return writeSuccess('email', "");
    }
  } else {
    return writeError('email',"n'est pas valide."); 
  }
}


