// JavaScript Document

function RefreshImage(valImageId) {
		var objImage = document.images[valImageId];
		if (objImage == undefined) {
			return;
		}
		var now = new Date();
		
		objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
	}


	$(document).ready(function(){
		$('#resetBut').click(function(){ 
				$("#fullname").attr("value", "");
				$("#email").attr("value", "");
				$("#mensahe").attr("value", "");	
				$("#captchacode").attr("value", "");	
				$("#fullname").focus()
				return false;
 	    });
		
		$('#clicker').click(function() {
			$('#forum').toggleClass('hideForum');
		});		
		
		$('#comm').click(function(){ 
			var name = $('input[name=fullname]');
			var email = $('input[name=email]');
			var comment = $('textarea[name=mensahe]');
			var captchacode = $('input[name=captchacode]');
			var xxx;
			
			//Simple validation to make sure user entered something
			//If error found, add hightlight class to the text field
			if (name.val()=='') {
				alert("Please enter your name");
				name.addClass('hightlight');
				name.focus();
				return false;
			} else name.removeClass('hightlight');
			
			if (email.val()=='') {
				alert("Please enter email address");				
				email.addClass('hightlight');
				email.focus();
				return false;
			} else email.removeClass('hightlight');
			
			if (comment.val()=='') {
				alert("At least say something about the article.");
				comment.addClass('hightlight');
				comment.focus();
				return false;
			} else comment.removeClass('hightlight');

			if (captchacode.val()=='') {
				alert("Ooops, you forgot about the security code");
				captchacode.addClass('hightlight');
				captchacode.focus();
				return false;
			} else captchacode.removeClass('hightlight');

/* THIS IS THE START OF THE CAPTCHA FUNCTION */
		$.getJSON("captcha.asp?validateCaptchaCode=" +  $("#captchacode").val() + "&format=json&jsoncallback=?", function(data){
			
		if (data.status == "1")
			{
				alert("Security code verified.");
				$('#entryForm').css("display", "none");
				$(postComments); 
			}
		else
			{
				RefreshImage("imgCaptcha");
				alert("Please key in the security code correctly!");
				captchacode.addClass('hightlight');
				$("#captchacode").focus()
				return false;
			}
//			console.log(xxx);
		});
/* THIS IS THE END OF THE CAPTCHA FUNCTION */
		
//				$("#msg1").empty();
//				$("#fullname").attr("disabled", true);
//				$("#email").attr("disabled", true);
//				$("#mensahe").attr("disabled", true);			
//				$("#comm").attr("disabled", true);	
//				$("#reset").attr("disabled", true);
				
//				$("#entryform").hide();	
//				$(postComments); 

		})	
	});

function postComments() { 
			$.ajax({
				type:"POST",
				url:"showComments.asp",
				contentType:"application/x-www-form-urlencoded",
				data: "artNo=" + $("#artNo").val() + "&fullname=" + encodeURIComponent($("#fullname").val()) + "&email=" + $("#email").val() + "&mensahe=" + encodeURIComponent($("#mensahe").val()) + "&chk=" + $("#chkfld").val(),
				async: false,
				success: function(msg) {
					$("#msg1").append(msg);
				}
			})
}
