$(document).ready(function() {
  /////////////////////////////////////////////////////////////// 
  //                                    CONTACT FORM & VALIDATION
  ///////////////////////////////////////////////////////////////
  $('.validateresponse').hide();
  $('.clear').click(function(){
    $('.validateresponse').hide();
    $('.required, .email').css({'border' : ''});
  });
  $('.send').click(function() {
    // VARIABLES
    var formid = $('#form'),
        regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
        validated = true,
        counter = 0,
        pathname = window.location.pathname,
        error = $('.validateresponse', formid).fadeIn(500, function(){$(this).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25);});

    // INPUT EMAIL ADDRESS VALIDATION
    $('input.email', formid).each(function(){
      if (!regEmail.test($('input.email', formid).val())){
        validated = false;
        $(error).text('Valid email address required')
        $('input.email', formid).css({'border-color' : '#A31018'});
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });

    // SELECT BOX VALIDATION. FIRST OPTION MUST HAVE 'SELECT' VALUE
    $('select.required', formid).each(function(){
      if ($('option:selected', this).val() == "Select"){
        validated = false;
        $(error).text("Please select your contact method");
        $('select.required', formid).css({'border-color' : '#A31018'});
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });
	
    // INPUT REQUIRED VALIDATION
    $('.required', formid).css({'border-color' : '#AAAAAA #DDDDDD #DDDDDD #AAAAAA'});
    $('.required', formid).each(function(){
      if ($(this).val().length == 0){
        $(this).css({'border-color' : '#A31018'});
        if (!counter){
          validated = false;
          counter++;
          var field = $(this).prev('label').text();
          $(error).text("Please complete your " + field);
          $(this).focus();
        }
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });

    /*
    // DOUBLE EMAIL CONFIRMATION
    if($(".email", formid).val() != $(".confirmemail", formid).val() && validated){
      validated = false;
      $(".email", formid).focus();
      $(error).text("Email address' must match");
      $(".email", formid).css({'border-color' : '#A31018'});
      $(".confirmemail", formid).css({'border-color' : '#A31018'});
    }
    */

    // SUCCESS FUNCTION
    if(validated){
      $(".validateresponse").hide();
      $.ajax({
        type: "POST",
        url: pathname,
        data: $('#form').serialize(),
        success: function() {
          $('#form').html("<div id='message'></div>");
          $('#message').html("<h2>Thank You!</h2><p class='tag'><small>Your submission has been recieved</small></p>")
          .append("<p>Thank you for showing interest in our services</p><p>One of our project managers shall contact you as soon as possible, meanwhile you are welcome to call us free of charge on <span class='orange'>08000 842 527</span></p><p>You are welcome to write to us with your project details and requirements: <span class='orange'>support@1stnetmarketing.co.uk</span></p>")
          .hide()
          .fadeIn(1500, function() {
            $('#message');
          });
        }
      });
    }
  return false;
  });
  
  /////////////////////////////////////////////////////////////// 
  //                                              BLOG VALIDATION
  ///////////////////////////////////////////////////////////////
  $('.validateresponse').hide();
  $('.comment_send').click(function() {
    // VARIABLES
    var formid = $('#comment-form'),
        regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
        validated = true,
        counter = 0,
        error = $('.validateresponse', formid).fadeIn(500, function(){$(this).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25);});

    // INPUT EMAIL ADDRESS VALIDATION
    $('.email', formid).each(function(){
      if (!regEmail.test($('.email', formid).val())){
        validated = false;
        $(error).text('Valid email address required')
        $('.email', formid).css({'border-color' : '#A31018'});
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });

    // INPUT REQUIRED VALIDATION
    $('.namerequired', formid).css({'border-color' : '#AAAAAA #DDDDDD #DDDDDD #AAAAAA'});
    $('.namerequired', formid).each(function(){
      if ($(this).val().length == 0){
        $(this).css({'border-color' : '#A31018'});
        if (!counter){
          validated = false;
          counter++;
          var field = $(this).prev('label').text();
          $(error).text("Please complete your " + field);
          $(this).focus();
        }
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });
    
    $('.commentsrequired', formid).css({'border-color' : '#AAAAAA #DDDDDD #DDDDDD #AAAAAA'});
    $('.commentsrequired', formid).each(function(){
      if ($(this).val().length == 0){
        $(this).css({'border-color' : '#A31018'});
        if (!counter){
          validated = false;
          counter++;
          var field = $(this).prev('label').text();
          $(error).text("Please complete your " + field);
          $(this).focus();
        }
      }
      else {        
        $(this).css({'border-color' : ''})
      }
    });

    // SUCCESS FUNCTION
    if(validated){
      $(".validateresponse").hide();
      $.ajax({
        type: "POST",
        url: "http://1stnetmarketing.co.uk/wp-comments-post.php",
        data: $('#comment-form').serialize(),
        success: function() {
          $('#comment-form').html("<div id='message'></div>");
          $('#message').html("<h2>Thank You!</h2>")
          .append("<p>Thanks for your comments</p>")
          .hide()
          .fadeIn(1500, function() {
            $('#message');
          });
        }
      });
    }
  return false;
  });
//////////////////////////////////////////// END DOCUMENT READY
});
