jQuery(document).ready(function() {     

  if($('#become-member .page .page-content ul li a')) {
    $('#become-member .page .page-content ul li p').slideToggle();
    $('#become-member .page .page-content ul li a').click(function(){ 
      $(this).parent().find('p').slideToggle();
      if($(this).hasClass('active')) {
        $(this).removeClass('active');
      } else {
        $(this).addClass('active');
      }
      return false;
    });
  }
  
  if($("#member_register_product_product_id")) {
    currentPack = $("#member_register_product_product_id").val('');   
  }
  var defaultText = "You could include the following information\r\n* Your scope of intervention\r\n* Your business hours and company holidays\r\n* Any membership of other networks\r\n* Any of your professional certificates, awards, etc.\r\n* Parking Opportunities\r\n";     
  
  if($("#member_en_description")) { 
    /*
      if($("#member_en_description").val() == "") {
        $("#member_en_description").val(defaultText);
      } 
    $("#member_en_description").click(function(){ 
      if($("#member_en_description").val().replace(/[\r\n]/gi, "") == defaultText.replace(/[\r\n]/gi, "")) {
        $("#member_en_description").val('');
      }
    });
    */
  }
  
  if($("#registration-step2")) {  
    $("#registration-step2").submit(function(){
    /*  
      if($("#member_en_short_description")){
        if($("#member_en_short_description").val().replace(/[\r\n]/gi, "") == defaultText.replace(/[\r\n]/gi, "")) {
          $("#member_en_short_description").val('');
        }
      }
      */
      if($("#member_url")) {
        if($("#member_url").val() == "http://") {
          $("#member_url").val("");
        }
      } 
    });
  }
  
  if($("#terms-and-conditions")){ 
    // if the function argument is given to overlay,
    // it is assumed to be the onBeforeLoad event listener    
      $("a[rel]").overlay({
        onBeforeLoad: function() {

          // grab wrapper element inside content
          var wrap = this.getOverlay().find(".contentWrap");

          // load the page specified in the trigger
          wrap.load(this.getTrigger().attr("href"));
        }
      });
      $("#member_price_payment-method_1").attr('disabled', 'disabled');
      $("#member_price_payment-method_2").attr('checked', 'checked');
  }
  /*
  if($("#member_payment-method_online")) {
    $("#member_payment-method_online").attr('disabled', 'disabled');
    $("#member_payment-method_other").attr('selected', 'selected');
  }
  */
  
  if($("#autocomplete_search_company_name")) {
    currentVal = $("#autocomplete_search_company_name").val();
    defaultValue = $("#autocomplete_search_company_name").val();
    if(currentVal == "") {
      $("#autocomplete_search_company_name").val(defaultValue);
    }     
    $("#autocomplete_search_company_name").focus(function(){
      if($("#autocomplete_search_company_name").val() == defaultValue) {
        $("#autocomplete_search_company_name").val('');
      }    
    });     
    $("#autocomplete_search_company_name").blur(function(){
      if($("#autocomplete_search_company_name").val() == '') {
        $("#autocomplete_search_company_name").val(defaultValue);
      }    
    });
    $("li.quick-search form").submit(function(){ 
      if($("#autocomplete_search_company_name").val() == defaultValue) {
        return false;
      }
    });
  }

  if($(".descriptions")){
    $(".descriptions textarea").each(function(){
      textareaId = $(this).attr('id');
      idComponents = textareaId.split('_');
      lang = idComponents[1]; 
      cptSpan = '#description_cpt_'+lang;
      fieldContent = $(this).val();
      $(cptSpan).html(fieldContent.length);
    });
    $(".descriptions textarea").keyup(function(){
      textareaId = $(this).attr('id');
      idComponents = textareaId.split('_');
      lang = idComponents[1]; 
      cptSpan = '#description_cpt_'+lang;
      fieldContent = $(this).val();
      currLength = fieldContent.length;
      if(currLength > 400){
        $(this).val(fieldContent.substr(0, 400));
        currLength = 400;
      }
      $(cptSpan).html(currLength);
    });
  }
}); 

function selectPack(packId, packName) {
  currentPack = $("#member_register_product_product_id").val();
  if(currentPack == packId) {
    $("#member_register_product_product_id").val('');
  } else {
    $("#member_register_product_product_id").val(packId);
    $("form.registration").submit();
  }
  return false;
}  

function ShowUploadedFile(fileObject, resp)
{                                         
  var fileName = fileObject.filePath; 
  var truePath = fileName.substr(0, fileName.lastIndexOf('/') + 1);
  $("#current-logo").attr("src", truePath+resp);
  $("#logo-loading").hide();
  $("#member_logo").val(resp);
  /*$("#member_logoQueue").hide();*/
  return;
}


function loadMemberPage(event, data){
  if($("#quick-search-image"))$("#quick-search-image").hide(); 
  if($("#ac_results")) {
    $("#autocomplete_member_company_name").attr('disabled', 'disabled'); 
    if(data[0].searchall == 1) {
      //gnu
    } else {
      document.location = data[0].url;    
    }
    //$("#ac_results ul").append('<li class="view-all"><a href="">View All</a></li>');
    // Adding a button "view all"
    return false;  
  }
}

function searchAll()
{ 
  $("li.quick-search form").submit();
  return false;
}

function cleanData(value, term)
{
  return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
}
