$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
    var email = $("input#email").val();
    if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var firstname = $("input#firstname").val();
    if (firstname == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var lastname = $("input#lastname").val();
    if (lastname == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var newsletterId = $("input#newsletterId").val();
    if (newsletterId == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var sourceId = $("input#sourceId").val();
    if (sourceId == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var nl_type = $("input#nl_type").selected();
    if (nl_type == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
      var etest = validate_reg_email(email);
      if (etest == false) {
         $("label#email_error").show();
         $("input#email").focus();
         return false;
      }
		
		var dataString = 'firstname='+ firstname + '&lastname=' + lastname + '&nl_type=' + nl_type + '&newsletterId=' + newsletterId + '&sourceId=' + sourceId + 'email=' + email;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/cls.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thank You!</h2>")
        .append("<p>You will now receive an email from \"Ziff Davis Enterprise\" with a confirmation of this Newsletter Signup.<br/><br/>If you do not receive an email, please contact us at <a href=\"mailto:enewsletters@ziffdavisenterprise.com\">enewsletters<br/>@ziffdavisenterprise.com</a>.</p>")
        .hide()
        .fadeIn(1500, function() {
          
        });
      }
     });
    return false;
	});
});


// commentsInit: used to determine if the first call to pagination plugin has sent
var commentsInit=0;  

// used to set the number of items per page
// you must also change the code in mospaging.inc - topcount
var itemCount=7;

$(document).ready(function(){
  
   $("#comment_submit").click(function(event){
  
       //check for an invalid comment
       if ($("[name='repmsg']").val()==""){
           $("#comment_bad_comment").show();
           return;
       }

       $.getJSON("/index2.php?option=com_checkcaptcha&task=check_captcha&no_html=1&code=" + $("input[name='capid']").val() + "&icode=" + $("input[name='imageverify']").val(),
       function(data){
          
          if (data.status=="success"){
              submit_comment();
          }else{
              //alert('Invalid Verification Code.  Please try again!');
              $("#comment_bad_captcha").show();
              $("[name='captcha_image']").attr("src","/includes/showcap.php?capid=" + data.capid);
              $("input[name='capid']").attr("value",data.capid);
          }

        });

   });
 });
 
 
function submit_comment(){
		
    params = new Object();
    params.repto = $("input[name='repto']").val();
    params.pop = $("input[name='pop']").val();
    params.repmsg = $("[name='repmsg']").val();
    params.Itemid = $("input[name='Itemid']").val();
    params.id = $("input[name='id']").val();
    params.blog_topic_id = $("input[name='blog_topic_id']").val();
    params.url = $("input[name='url']").val();
    params.title = $("input[name='title']").val();
    params.yname = $("input[name='yname']").val();
    params.imageverify = $("input[name='imageverify']").val();
    params.capid = $("input[name='capid']").val();

    $.post("/index2.php?option=com_blogdisplay&task=sendreply&no_html=1",params,
       function(data){
          
          if (data.status=="success"){
              
              // get the first page
              commentPaginate(0, $("#Comments-Pagination"));
              
              // increment the item count
              var c = parseInt($("input[name='comments_count']").val()) + 1;
              $("input[name='comments_count']").val(c)
              
              // reset the pagination
              commentsInit=0;
              $("#Comments-Pagination").pagination($("input[name='comments_count']").val(), {
		              items_per_page:itemCount, 
		              callback:commentPaginate
	            });
              
              
              // reset the comment box
              $("[name='captcha_image']").attr("src","/includes/showcap.php?capid=" + data.capid);
              $("input[name='capid']").attr("value",data.capid);
              $("input[name='imageverify']").val('');
              $("input[name='yname']").val('');
              $("input[name='title']").val('');
              $("[name='repmsg']").val('');
        
              // show the new comment and set a timer to remove messages
              $(".comment-add-message").show();
              setTimeout('$(".comment-add-message").fadeOut("slow");$("#comment_bad_comment").fadeOut("fast");$("#comment_bad_captcha").fadeOut("fast");',5000);
              
          }else{
              $("[name='captcha_image']").attr("src","/includes/showcap.php?capid=" + data.capid);
              $("input[name='capid']").attr("value",data.capid);
              $("#comment_bad_captcha").show();
          }

        },"json");
 
 }
 

// Pagination for Comments
 function commentPaginate(new_page_index, pagination_container){
  
       /////////////////////////////////////////////////////
       // if this is the first time - do nothing
       // this event gets fired automatically on page load
       /////////////////////////////////////////////////////
       if (commentsInit==0){
           commentsInit=1;
           return;
       }

       $.getJSON("/index2.php?option=com_blogdisplay&task=get_comment_page&no_html=1&page=" +
                 new_page_index + 
                 "&page_count=" + itemCount + "&blog_topic_id=" + $("input[name='blog_topic_id']").val(),
                 
       function(data){
          
          if (data.status=="success"){
          
              // remove the current comments
              $('.usercomment').remove();
          
              for(var o in data.data){

                  var h = $("#comment-template").html();
                   
                  h=h.replace("[title]"  ,data.data[o].replytitle);
                  h=h.replace("[comment]",data.data[o].replytext);
                  h=h.replace("[author]" ,data.data[o].typename);
                  h=h.replace("[date]"   ,data.data[o].postdate);
 
                  $("#comment-template").before('<div id="usercomment" class="usercomment">' + h + '</div>');
              }
              
               // go to the top of the page
               location.href=location.href.replace("#comment_added","").replace("#comment_form","") + '#comment_added';
               console.log(location.href.replace("#comment_added","") + '#comment_added');
          
               // show the paginate box
               $("#Comments-Pagination").css("display","block");
               
               // hide the no comments box
               $("#postcommentnow").css("display","none");
              
          }

        });
        
        
      return false;
 }
 

 $(document).ready(function(){

   // implement the anchor to jump to the
   // leave a comment form.  This is done
   // in js because of the URL structure 
   // (which prevents an anchor fro working)
   $("#jump_to_comment").click(function(){
       location.href=location.href.replace("#comment_form","").replace("#comment_added","") + "#comment_form";
       return false;
       });
     
  // First Parameter: number of items
	// Second Parameter: options object
	$("#Comments-Pagination").pagination($("input[name='comments_count']").val(), {
		items_per_page:itemCount, 
		callback:commentPaginate
	});

})




