var englishLanguageID = 1;
var arabicLanguageID = 18;
var arabicInArabic = "عربي";
var englishInEnglish = "English";
var arabicWords_moreComments = "المزيد من التعليقات";
var arabicWords_commentsAreModerated = "تخضع هذه التعليقات للمراقبة وبالتالي يجوز أن لا تُنشر على الفور";
var arabicWords_char2500Limit = "أقصاها 2500 حرف";
var arabicWords_postYourComments = "شارك بالنقاش";
var arabicWords_screenNamesAppearBlurb = "يظهر اسم المستخدم مع التعليق";
var arabicWords_enterScreenName = "اسم المستخدم";
var arabicWords_enterVcode = "أدخل الأرقام";
var arabicWords_whatsThis = "ما هذا";
var arabicWords_postComment = "أنشر تعليقك";
var arabicWords_numberOfCharactersUsed = "عدد الاحرف المستخدمة";
var arabicWords_yourCommentWasPosted = "لقد تم نشر تعليقك";	
var arabicWords_yourCommentWasNotPosted = "لم ينشر تعليقك بعد";
var arabicWords_error = "خطأ";
var arabicWords_commentsMustBeEntered = "لا تستوعب خانة التعليقات أكتر من 2500 حرف";
var arabicWords_screenNameRequired = "المرجو الإدلاء باسم المستخدم";
var englishWords_moreComments = "more comments";
var englishWords_commentsAreModerated = "Comments are moderated and may not appear immediately";
var englishWords_char2500Limit = "2500 character limit";
var englishWords_postYourComments = "Post your comments";
var englishWords_screenNamesAppearBlurb = "Screen names appear with your comment";
var englishWords_enterScreenName = "Enter screen name";
var englishWords_enterVcode = "Enter verification code";
var englishWords_whatsThis = "What is this";
var englishWords_postComment = "Post Comment";
var englishWords_numberOfCharactersUsed = "## of characters used";		
var englishWords_yourCommentWasPosted = "Your comment was posted";		
var englishWords_yourCommentWasNotPosted = "Your comment has NOT been posted";
var englishWords_error = "Error";
var englishWords_commentsMustBeEntered = "Comments must be between 1 and 2500 characters";
var englishWords_screenNameRequired = "ScreenName must be provided";

function KeepCount( textFieldID , countFieldID , maxCharInt){
	var textField = $("#" + textFieldID);
	var countField = $("#" + countFieldID);
	if ( textField.attr("value") )
	{
		var currTextCount = textField.attr("value").length;
		var diff = maxCharInt - currTextCount;
	
		// Need to check & enforce limit here also in case user pastes data
		if (diff < 0){
			countField.addClass( "warningDisabled" );
			textField.addClass( "warning" );
		}
		else {
			countField.removeClass( "warningDisabled" );
			textField.removeClass( "warning" );
		}
		countField.attr( "value" , currTextCount );
	}
	else
	{
		countField.attr( "value" , 0 );
	}
	return diff < 0;
}

function ajaxSubmitComment() {
	var commentLanguageField = $("#commentLanguage");
	var errorText = "";
	// content_id
	var contentIDField = $("#content_id");
	if ( !contentIDField.val() || contentIDField.val().length == 0 )
	{
		errorText += "\nThis is not a valid article";
	}
	var commentField = $("#comment");
	if ( commentField.val().length == 0 || KeepCount( "comment" , "commentTextCount", 2500) ){
		if ( commentLanguageField.val() == 18 )
		{
			errorText += "\n" + arabicWords_commentsMustBeEntered + ".";
		}
		else
		{
			errorText += "\n" + englishWords_commentsMustBeEntered + ".";
		}
	}
	// screenname
	var screenNameField = $("#screenname");
	if ( screenNameField.val().length == 0 )
	{
		if ( commentLanguageField.val() == 18 )
		{
			errorText += "\n" + arabicWords_screenNameRequired + ".";
		}
		else
		{
			errorText += "\n" + englishWords_screenNameRequired + ".";
		}
	}
	
	// vcode
	var vcodeField = $("#verification");
	// we validate the verification field on the back end instead of the front end during the post call.	

	if ( errorText.length == 0 )
	{
		$.post( 
			current_path_info() + "index.cfm?fa=ajaxPostComment",
			{
				screenName: screenNameField.val(),
				comment_text: commentField.val(),
				content_id: contentIDField.val(),
				info: vcodeField.val(),
				comment_language: commentLanguageField.val()
			},
			function(data) {
				var errorMessage;
				if ( data.errorMessage )
					errorMessage = data.errorMessage;
				else
					errorMessage = "undetermined";
				if ( data.status == 1 )
				{
					$("#postCommentForm").slideUp("normal", function() {	
						if ( commentLanguageField.val() == 18 ){
							$("#postCommentForm").before('<span><strong>' + arabicWords_yourCommentWasPosted + '!</strong> ' + arabicWords_commentsAreModerated);											
						} else {
							$("#postCommentForm").before('<span><strong>' + englishWords_yourCommentWasPosted + '!</strong> ' + englishWords_commentsAreModerated);											
						}
					});						
				} else {
					$("#postCommentForm").slideUp("normal", function() {				   
						if ( commentLanguageField.val() == 18 ){
							$("#postCommentForm").before('<span><strong>' + arabicWords_yourCommentWasNotPosted + '!</strong> ' + arabicWords_error + ': ' + errorMessage + '</span>');											
						} else {
							$("#postCommentForm").before('<span><strong>' + englishWords_yourCommentWasNotPosted + '!</strong> ' + englishWords_error + ': ' + errorMessage + '</span>');											
						}
					});						
				}
			},
			"json"
		);
	}
	else {
		alert( errorText );
	}
}

function showWhatIsVcode() {
	alert("Verification Code is to be implemented.");
}

// switchLanguage only supports English & Arabic
function switchLanguage() {
	var currentLanguageField = $("#commentLanguage");
	if ( currentLanguageField.val() == englishLanguageID ) {
		$("#postYourCommentsSpan").html( '<strong>' + arabicWords_postYourComments + '</strong> (' + arabicWords_char2500Limit + ' - ' + arabicWords_commentsAreModerated + ')' );
		$("#commentCharCounter").html( arabicWords_numberOfCharactersUsed + ':' );
		$("#screenNameLabel").html( arabicWords_enterScreenName + ':' );
		$("#screenNameBlurb").html( '(' + arabicWords_screenNamesAppearBlurb + ')' );
		$("#vcodeSpan").html( arabicWords_enterVcode + ':' );
		$("#whatIsVcode").html( '<a href="javascript:showWhatIsVcode();" title="what is the verification code?">' + arabicWords_whatsThis + '?</a>' );
		$("#submitButton").html( arabicWords_postComment );
		$("#switchLanguageButton").attr( "value" , englishInEnglish);
		currentLanguageField.attr("value" , arabicLanguageID );
	}
	else {
		$("#postYourCommentsSpan").html( '<strong>' + englishWords_postYourComments + '</strong> (' + englishWords_char2500Limit + ' - ' + englishWords_commentsAreModerated + ')' );
		$("#commentCharCounter").html( englishWords_numberOfCharactersUsed + ':' );
		$("#screenNameLabel").html( englishWords_enterScreenName + ':' );
		$("#screenNameBlurb").html( '(' + englishWords_screenNamesAppearBlurb + ')' );
		$("#vcodeSpan").html( englishWords_enterVcode + ':' );
		$("#whatIsVcode").html( '<a href="javascript:showWhatIsVcode();" title="what is the verification code?">' + englishWords_whatsThis + '?</a>' );
		$("#submitButton").html( englishWords_postComment );
		$("#switchLanguageButton").attr( "value" , arabicInArabic);
		currentLanguageField.attr("value" , englishLanguageID );
	}
}	


