$(document).ready(function() {
    $("#loader").remove();
    $(".inputWithDefaultValue").toggleVal();
    $("#commentForm").validate();
    $("#contactForm").validate();
    $("#subscriptionForm").validate();
    $("#formWithValidation").validate();
    //$(".questionMark").tooltip();
    $("#searchOwnCompanyByNameForm").validate();
    $("#searchOwnCompanyByAddressForm").validate();
    $('div.required').append('&nbsp;<span class="redAsterisk">*</span>&nbsp;');
    $('label.required').append('&nbsp;<span class="redAsterisk">*</span>&nbsp;');
    $('td.required').append('&nbsp;<span class="redAsterisk">*</span>&nbsp;');
    $('strong.required').append('&nbsp;<span class="redAsterisk">*</span>&nbsp;');
    $(".showOverlay").click(function() {
      scroll(0,0);
      $("#overlayBackground").fadeIn('slow');
      $(".contactForm").fadeIn('slow');
      return false;
    });
    $("#exitOverlay").click(function() {
      $("#overlayBackground").fadeOut('medium');
      $("#overlayForm").fadeOut('medium');
      return false;
    });
    
    $(".phoneNumberField").click(function() {
        $(this).css("text-decoration","none");
        $(this).removeClass("phoneNumberField");
        showPhoneNumber(this.id.substring(2));
    });
    
    $(".secondaryPhoneNumberField").click(function() {
        $(this).css("text-decoration","none");
        $(this).removeClass("phoneNumberField");
        showSecondaryPhoneNumber(this.id.substring(2));
    });
    
    $(".faxNumberField").click(function() {
        $(this).css("text-decoration","none");
        $(this).removeClass("phoneNumberField");
        showFaxNumber(this.id.substring(2));
    });
    
    $(".websiteField").click(function() {
        showWebsite(this.id.substring(2));
    });
    
    $(".getQuotationLink").mouseup(function() {
        showQuotationForm(this.id.substring(5));
    });
    
    $(".forgetPasswordLink").click(function() {
        $.get("/authenticate/showForgetPassword?page='"+window.location.href+"'",
          function(data) {
            $(".loginForm").html(data);
          }
        )
    });
    
    showPhoneNumber = function(id) {
      $.get("/getPhoneNumber/"+id, //this function must print the phonenumber when called with the id as a parameter
        function(data) {
          $("#ph"+id).html("<span class='noLink'>"+data+"</span>");
        }
      );
    };
    
    showSecondaryPhoneNumber = function(id) {
      $.get("/getSecondaryPhoneNumber/"+id, //this function must print the phonenumber when called with the id as a parameter
        function(data) {
          $('span[title=spn'+id+']').html("<span class='noLink'>"+data+"</span>");
        }
      );
    };
    
    showFaxNumber = function(id) {
      $.get("/getFaxNumber/"+id, //this function must print the phonenumber when called with the id as a parameter
        function(data) {
          $('span[title=fax'+id+']').html("<span class='noLink'>"+data+"</span>");
        }
      );
    };
    
    showWebsite = function(id) {
      $.get("/getWebsite/"+id, //this function must print the website when called with the id as a parameter
        function(data) {
          $('span[title=web'+id+']').html("<a rel='nofollow' target='_blank' href='http://"+data+"'>"+data+"</a></span>");
        }
      );
    };
    
    showQuotationForm = function(id){
      var pathArray = window.location.pathname.split( '/' );
      $.get("/getQuotation/"+id+"/"+pathArray[1], //this function must echo the view with the form
        function(data) {
          scroll(0,0);
          $('#getQuotation').html(data);
          $("#overlayBackground").fadeIn('slow');
          $("#overlayBackground").css('height','250%');
          $("#getQuotation").fadeIn('slow');
          $('label.required').append('&nbsp;<span class="redAsterisk">*</span>&nbsp;');
          $("#getOfferForm").validate();
        }
      );
    }
    
    $('#btnAdd').click(function() {
        var maxNum  = 15;
        var num     = $('.clonedInputVisible').length; // how many "duplicatable" input fields we currently have
        var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
        //alert(num);
 
        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#input' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem.attr('class', 'clonedInputVisible subscriptionFormField');

        // insert the new element after the last "duplicatable" input field
        //$('#input' + num).after(newElem);
 
        // business rule: you can only add 5 names
        if (newNum == maxNum) {
            $('#btnAdd').attr('style','display:none');
            $('#btnAdd').attr('value','max. ' + maxNum);
        }
    });
    
    $('#btnAddBrands').click(function() {
        var maxNum  = 15;
        var num     = $('.clonedInputVisibleBrands').length; // how many "duplicatable" input fields we currently have
        var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
        //alert(num);
 
        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#inputBrands' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem.attr('class', 'clonedInputVisibleBrands subscriptionFormField');

        // insert the new element after the last "duplicatable" input field
        //$('#input' + num).after(newElem);
 
        // business rule: you can only add 5 names
        if (newNum == maxNum) {
            $('#btnAddBrands').attr('style','display:none');
            $('#btnAddBrands').attr('value','max. ' + maxNum);
        }
    });
    
    $('#btnAddKeywords').click(function() {
        var maxNum  = 15;
        var num     = $('.clonedInputVisibleKeywords').length; // how many "duplicatable" input fields we currently have
        var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
        //alert(num);
 
        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#inputKeywords' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem.attr('class', 'clonedInputVisibleKeywords subscriptionFormField');

        // insert the new element after the last "duplicatable" input field
        //$('#input' + num).after(newElem);
 
        // business rule: you can only add 5 names
        if (newNum == maxNum) {
            $('#btnAddKeywords').attr('style','display:none');
            $('#btnAddKeywords').attr('value','max. ' + maxNum);
        }
    });
    
    $(".flip").click(function() {
      $(".loginForm").slideToggle("normal");
      return false;
    });
    
     
});

function loadSubmit() {
  scroll(0,0);
  var ProgressImage = document.getElementById('progress_image');
  $('.loader').attr('style','visibility:visible');
  $('#pageBox').attr('style','visibility:hidden');
  $('.adOnTop').attr('style','visibility:hidden');
  $('#google_ads_frame1').attr('style','visibility:hidden');
  setTimeout(function(){ProgressImage.src = ProgressImage.src},100);
  return true;

  }

function previousPage() {
  history.go(-1);  
}


