//Gets run before form post ajax call
function showRequest(formData, jqForm, options){
    //$('div#error').hide();
    //$('div#comment_loader').show();
    $('body').css('cursor', 'wait');
    return true;
}

function showError(data){
    $("div#error").html("There was an error with the request");
    $("div#error").show();
    $('div#comment_loader').hide();
    $('body').css('cursor', 'default');
}

function showResponse(data){
   var currentHref = window.location.href;
   var d = new Date();
   if(data["result"] == "success"){
       /*
	if(data["anon"] == 1){
		$("div#error").html("Comment sucessfully submitted, but is waiting moderation.");
		$("div#error").show();
	} else {
		$("div#error").html("submitted");
		var currentHref = window.location.href;
		var d = new Date();
		window.location.href = currentHref.substr(0, currentHref.lastIndexOf("#")) + "?" + d.getTime().toString() + "#comments"; //goto to the comments section
	}
        */
       if(data["anon"] == 1){
		var msg = "Comment sucessfully submitted, but is waiting moderation.";
		
	} else {
		
                var msg = "Comment submitted";
	}
       
       window.location.href = currentHref.substr(0, currentHref.lastIndexOf("#")) + "?" + d.getTime().toString() + "&msg=" + msg + "#comments"; //goto to the comments section
   } else {
       /*
	$("div#error").html(data["reason"]);
	$("div#error").show();
        */
       var msg = data["reason"];
       window.location.href = currentHref.substr(0, currentHref.lastIndexOf("#")) + "?" + d.getTime().toString() + "&msg=" + msg + "#comments"; //goto to the comments section
   }
   
   $('div#comment_loader').hide();
   $('body').css('cursor', 'default');
   
}

$(document).ready(function() { 
    //$('div#captcha').load("/registration/get_captcha/");
        $("form#commentform").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function() {                               
                    var options = {
                        target: '#form_output',
                        beforeSubmit: showRequest,
                        success: showResponse,
                        url: '/registration/ajax_post_comment/',
                        dataType: 'json',
                        type: 'post',
                        error: showError
                    }
                    
                    $('form#commentform').ajaxSubmit(options);
                    return false;
		},
		messages: {
			password2: {
				required: " ",
				equalTo: "Please enter the same password as above"	
			},
			email: {
				required: " ",
				email: "Please enter a valid email address, example: you@yourdomain.com",
				remote: $.validator.format("{0} is already taken, please enter a different address.")	
			}
		},
		debug:true
	});

});
