// JavaScript Document
var dialogCreated = false;

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

$(function() {
	$("input[@name=posttype]").click(function() {
		if ($("#posttype_beautycheck:checked").size() > 0) {
			$("#txtBeautycheck").css('display', 'none');
		} else {
			$("#txtBeautycheck").css('display', 'block');
		}
		if ($("#posttype_postcomment:checked").size() > 0) {
			$("#txtComment").css('display', 'none');
		} else {
			$("#txtComment").css('display', 'block');
		}
	});
	
  	$.fck.config = {
		path: 'fckeditor/2.6.3/reply/',
		toolbar: 'JebanReply',
		height: '200px'
	};
	$(".fckComment").fck();
	$(".fckIntro").fck();

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

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

	var dateString = gup("date");
	if (dateString == '') { 
		qsDate = null; 
	} else {
		var dateStrings = dateString.split('-');
		qsDate = new Date(parseInt(dateStrings[0], 10),
						  parseInt(dateStrings[1], 10)-1,
						  parseInt(dateStrings[2], 10));
	}
	
	$('#calendar').datepicker({
		defaultDate: qsDate,
		gotoCurrent: false,
		changeMonth: false,
		changeYear: false,
		dayNamesMin: ['<img src="images/2009_weekdaySun.gif" alt="S" />',
					  '<img src="images/2009_weekdayMon.gif" alt="M" />',
					  '<img src="images/2009_weekdayTue.gif" alt="T" />',
					  '<img src="images/2009_weekdayWed.gif" alt="W" />',
					  '<img src="images/2009_weekdayThu.gif" alt="T" />',
					  '<img src="images/2009_weekdayFri.gif" alt="F" />',
					  '<img src="images/2009_weekdaySat.gif" alt="S" />'],
		prevText:'&lt;&lt;',
		nextText:'&gt;&gt;',
		dateFormat: ' yy-mm-dd',
		onSelect: function(dateText){
			var newURL		= 'beautycheck.php?date='+jQuery.trim(dateText);
			location.replace(newURL);
		},
		minDate: new Date(2009, 3 - 1, 16),
		maxDate: new Date()
	});
	
	$('#postMyBeautyCheck').click(function() {
		window.open('?mode=post', '_self');
		

	});

	$("#submit").click(function () {
		if ($("#subject").size() > 0) {
			if ($("#subject").val().replace(/^\s+/, "").replace(/\s+$/, "") == "") {
				alert('อ๊ะๆ อย่าลืมใส่หัวข้อ ตอบ ด้วยนะ!');
				$("#subject").focus();
				return false;
			}
		}
		if (($(".fckBeautyCheck").size() == 0) && ($("#txtBeautyCheck").size() > 0)) {
			if ($("#txtBeautyCheck").val().replace(/^\s+/, "").replace(/\s+$/, "") == "") {
				alert('อ๊ะๆ อย่าลืมใส่ ข้อความ ด้วยนะ!');
				$("#txtBeautyCheck").focus();
				return false;
			}
		}

		if ($(".fckBeautyCheck").size() > 0 ) {
			var oEditor = FCKeditorAPI.GetInstance('txtBeautyCheck');
			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('ใส่ข้อความด้วยจ้าาา');
				$('#txtBeautyCheck').focus();
				return false;
			}
		}
		
		if ($(".fckComment").size() == 0 && $("#txtComment").size() > 0) {
			if ($("#txtComment").val().replace(/^\s+/, "").replace(/\s+$/, "") == "") {
				alert('อ๊ะๆ อย่าลืมใส่ ข้อความ ด้วยนะ!');
				$("#txtComment").focus();
				return false;
			}
		}
	
		if ($(".fckComment").size() > 0 ) {
			//var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
			var oEditor = FCKeditorAPI.GetInstance('txtComment');
			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('ใส่ข้อความด้วยจ้าาา');
				$('#txtComment').focus();
				return false;
			}
		}
		return true;
	});
});
