// JavaScript Document
var clickBypass = false;

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: "tools/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 () {
	if( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) ||
 navigator.userAgent.match(/iPod/i)|| navigator.userAgent.match(/iPad/i)
 ){
 /* modify 23/8/2011 kiki */
}else
	{

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

    $.fck.config = {
        path: 'fckeditor/2.6.3/message/',
        toolbar: 'JebanReply',
        height: '130px',
        width: '600px'
		
    };
    $(".fckPosterReply").fck();
	}
    $("#cancel").click(function () {
        return false;
    });

    $("#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 ($("#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;
            }
        }
        clickBypass = true;
        event.stopPropagation();
        $('.replyMessage').unbind('click');

        return true;
    });
	$('img.lazy').lazyload({
        effect : "fadeIn",
        skip_invisible : false
    });
    $(".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');
            }
        });
    });

    $('#likeLink').click(function() {
        var id = $(this).attr('p'); 
 		 id = id.split(':');
 		 post_id=id[0];
		 topic_id=id[1];  
        $.get('tools/ajax/likePost.php', {
            'post_id' : post_id,
		    'topic_id': topic_id
        }, function(data) {
			 
			data = data.split('||'); 
			 
			if(data[0]!='')
				{
            $('#love-'+post_id).html(data[0]);
				}
			if(data[1]!='')
				{
			$('#loveit-'+post_id).html(data[1]);
				}
         });
    });
	

	$('.loveLink').click(function() {
        var post_id = $(this).attr('p');   
          $.get('tools/ajax/likePost.php', {
            'post_id' : post_id 
        }, function(data) {
				if(data !='') 
				{
              $('#love-'+post_id).html(data);
				}
  			 
        });
    });


    $('img[alt="Delete"]').css('cursor', 'pointer');
    $('img[alt="Delete"]').click(function() {
        var topicReplyDIV = $(this).parents('div.topicReply');
        var post_id = $(topicReplyDIV).attr('post_id');
        var sampleMessage = $.trim(strip_tags($(topicReplyDIV).find('.message').html())).replace('&nbsp;', ' ');

        if (confirm('ต้องการจะลบข้อความต่อไปนี้? \n\n\n"' + sampleMessage + '"') == true) {
            $.get(
                'tools/ajax/comment_remove.php',
                {
                    'post_id' : post_id
                },
                function(data) {
                    if (data == true) {
                        $(topicReplyDIV).remove();
                    } else {
                        alert(data);
                    }
                });
        }

        return false;
    });

 
 
 	$('#gotoComment').click(function(){
 		$.scrollTo('#comment_track', 900  );
 	});
	 
   /* $('.replyMessage').click(function() {
        if (clickBypass == false) {
            window.open('http://www.jeban.com/mgmt/impression.php?action=show&bannerId=476');
        }
    });
    $('.replyMessage2').click(function() {
        if (clickBypass == false) {
            window.open('http://www.jeban.com/mgmt/impression.php?action=show&bannerId=476');
        }
    });
    $('.replyMessage3').click(function() {
        if (clickBypass == false) {
            window.open('http://www.jeban.com/mgmt/impression.php?action=show&bannerId=476');
        }
    });
    $('.replyMessage').css('cursor','pointer');
    $('.replyMessage2').css('cursor','pointer');
    $('.replyMessage3').css('cursor','pointer');
	*/
	
});

function strip_tags(html){

    //PROCESS STRING
    if(arguments.length < 3) {
        html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
    } else {
        var allowed = arguments[1];
        var specified = eval("["+arguments[2]+"]");
        if(allowed){
            var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
            html=html.replace(new RegExp(regex, 'gi'), '');
        } else{
            var regex='</?(' + specified.join('|') + ')\b[^>]*>';
            html=html.replace(new RegExp(regex, 'gi'), '');
        }
    }

    //CHANGE NAME TO CLEAN JUST BECAUSE
    var clean_string = html;

    //RETURN THE CLEAN STRING
    return clean_string;
}

function editflea(editflea_id, editflea_e)
{
	
    if (confirm('ต้องการแจ้งข้อความต่อไปนี้เป็นเห็บ? \n\n')== true)
    {
        $.ajax({
            type: "POST",
            url: "flea.php",
            data: "id=" + editflea_id,
            beforesend: function() {
                $("#"+editflea_e).html("Please wait");
            },
            success: function(msg){
                $("#"+editflea_e).html(msg);
            }
        });
    }

 
	
//alert("#"+editflea_e);
}
 
