﻿// JavaScript Document
function checkEmptyField(fieldToCheck, fieldName) {
	var strForgotPattern = 'อ๊ะๆ ใส่ [fieldname] ด้วยจ้ะ!!!';
	var fieldId = '#'+fieldToCheck.attr('id');

	if ($(fieldId).val() == '') {
		alert(strForgotPattern.replace('[fieldname]', fieldName));
		$('html, body').scrollTo( $(fieldId), 800 );
		fieldToCheck.focus();
		return false;
	} else {
		return true;
	}
}

function validateCaptcha() {
    challengeField 	= $("input#recaptcha_challenge_field").val();
    responseField 	= $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "ajax/ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;
 
    if(html == "success")
    {
        $("#captchaStatus").html(" ");
        // Uncomment the following line in your application
        return true;
    }
    else
    {
        $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}

$(function () {
  	$.fck.config = {
		path: 'fckeditor/2.6.3/reply/',
		toolbar: 'JebanReply',
		height: '200px'
	};
	$(".fckUserReply").fck();

	$.fck.config = {
		path: 'fckeditor/2.6.3/message/',
		toolbar: 'JebanReply',
		height: '200px'
	};
	$(".fckPosterReply").fck();

	$("#submit").click(function () {
		if ($("#post_username").size() > 0) {
			if ($("#post_username").val().replace(/^\s+/, "").replace(/\s+$/, "") == "") {
				alert('อ๊ะๆ อย่าลืมใส่ ชื่อผู้ตอบ ด้วยนะ!');
				$("#post_username").focus();
				return false;
			}
		}
		if ($(".fckUserReply").size() == 0 && $(".fckPosterReply").size() == 0 && $("#txtMessage").size() > 0) {
			if ($("#txtMessage").val().replace(/^\s+/, "").replace(/\s+$/, "") == "") {
				alert('อ๊ะๆ อย่าลืมใส่ ข้อความ ด้วยนะ!');
				$("#txtMessage").focus();
				return false;
			}
		}
/*		
		if ($("#confirm_code").size() > 0) {
			if (hex_md5($("#confirm_code").val()) != $("#confirm_md5").val()) {
				alert('รหัสยืนยันจากรูปไม่ถูกต้อง!');
				$("#confirm_code").focus();
				return false;
			}
		}
*/
		if ($("#recaptcha_response_field").size() > 0) {
			if (checkEmptyField($("#recaptcha_response_field"), 'รหัสยืนยันจากรูป') == false) {
				$('html, body').scrollTo( $("#recaptcha_table"), 800 );
				return false;
			}
			
			if (validateCaptcha() == false) {
				alert('รหัสยืนยันจากรูปไม่ถูกต้อง!');
				$('html, body').scrollTo( $("#recaptcha_table"), 800 );
				return false;
			}
		}
		if ($(".fckUserReply").size() > 0 || $(".fckPosterReply").size() > 0) {
			//var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
			var oEditor = FCKeditorAPI.GetInstance('txtMessage');
			var oDOM = oEditor.EditorDocument;
			var strFCKEditorText = "";
			// If I.E.
			if (document.all) {
				strFCKEditorText = oDOM.body.innerText;
			} else {
				var r = oDOM.createRange();
				r.selectNodeContents(oDOM.body);
				strFCKEditorText = r.toString();
			}
			
			if (strFCKEditorText == '') {
				alert('ใส่ข้อความด้วยจ้าาา');
				$('#txtMessage').focus();
				return false;
			}
		}
		return true;
	});
	
	$(".imgThumb").each(function() {
		var button 	= $('#'+this.id), interval;
		var topicId = $("#topic_id").val();

		$.ajax_upload(button,{
			action: 'ajaxProxy.php?mode=uploadThumb&imageSize='+button.attr('title')+'&topicId='+topicId,
			name: 'myfile',
			onSubmit : function(file, ext){
				button.text('Uploading');
				
				this.disable();
				
				interval = window.setInterval(function(){
					var text = button.text();
					if (button.text().length < 13){
						button.text(button.text() + '.');					
					} else {
						button.text('Uploading');
					}
				}, 200);
			},
			onComplete: function(file, response){
				alert(response);

				button.text('Upload');
				button.removeClass('hover');
				
				window.clearInterval(interval);
				this.enable();
				$('#imgThumb_'+button.attr('title')).attr('src', 'userfiles/thumbs/topics/'+button.attr('title')+'/'+topicId+'.jpg');
			}
		});
	});
	
});
