

function commentCookieIsEnabled() {
    var cookie;

    setCookie_c("testCookie", "aa", 365);
    cookie = readCookie_c("testCookie");
    if(cookie == "aa") return true;

    return false;
}


function displayComment() {
    var iframe = document.getElementById("commentFrame");
    var actionType = "display";
	  var callingUrl = getCallingUrl();
	  var url = WEBTOOL_SERVER + "comment_controller.php";
	  var parm = "?callingUrl=" + callingUrl + "&actionType=" + actionType + "&abuseLimit=" + ABUSE_LIMIT;

	  iframe.src = url + parm;

}


function displayCommentPage(commPage) {
    var frm = document.commentFrm;

    frm.actionType.value = "displayCommentPage";
    frm.callingUrl.value = escape(document.location);
    frm.commPage.value = commPage;
    frm.submit();

}



function displayData() {
    displayComment();
}

function foundForbiddenChar(txtValue, isAlphaOnly) {

    var i;
    var exp;
    var rexp;
    var word;
    var letters = new Array('@', '#', '%', '^', '&', '*', '+', '=', '>', '<',
                    ':', ';', '{', '}', '(', ')', '[', ']', '|', '\\', '_',
                    '..', '.!', '!.', '.?', '?.', '??', '?!', '!?', '!!');

    var size = letters.length;



    txtValue = txtValue.toLowerCase();
    /*
    if(isAlphaOnly) {
        exp = "^[a-zA-Z\\s]*$";
        rexp = new RegExp(exp, "gi");
        if(rexp.test(txtValue)) {
           return false;
        }
        return true;
    }
    */


    for(i=0; i<size; i++) {
       word = letters[i];
       if(txtValue.indexOf(word) > -1) {
          return true;
       }
    }

    var sentences = txtValue.split('\n');
    size = sentences.length;
    exp = "\\b[\\?\\.\\!]\\b";
    rexp = new RegExp(exp, "gi");
    for(i=0; i<size; i++) {
       sentence = sentences[i];
       if(rexp.test(sentence)) return true;
    }


    return false;
}


function saveComment() {
    var frm = document.commentFrm;
    var value = "";
    var pf;
    value = frm.userName.value;

    frm.commentArea.value = TrimAll(frm.commentArea.value);

    if(value == "") {
       alert("Please enter a name.");
       frm.userName.focus();
       return;

    } else if(profanityExists(value)) {
       alert("Some in our community have misused the comments feature. To combat that, " + 
			 "we've created an inappropriate word list as well as restricting the use of " + 
             "some punctuation marks.   Please review your name.");
       return;

    } else if(foundForbiddenChar(value, true)) {
           alert("Warning.  Only letters are allowed in the name area.  No punctuation allowed.\n\nPlease revise your posting.");
           return;
    }

    if((frm.commentArea.value == "") || (frm.commentArea.value.indexOf("Enter your comment here") > -1)) {
       alert("Please enter a comment before saving.");
       frm.commentArea.focus();
       return;

    } else {
        value = frm.commentArea.value.toLowerCase();
        if(foundForbiddenChar(value, false)) {
           alert("Warning.  Only letters and numbers are allowed in the comment area with the exception of period, question mark, and exclamation mark which can appear at the end of a sentence.\n\nPlease revise your posting.");
           return;
        }

        if(profanityExists(value)) {
             alert("Some in our community have misused the comments feature. To combat that, " + 
			 "we've created an inappropriate word list as well as restricting the use of " + 
             "some punctuation marks.   Please review your comment.");
           return;
        }

    }

    if(frm.commentArea.value.length > 1900) {
	     alert("Please limit the comment to 1900 characters");
	     frm.commentArea.focus();
	     return;
  	}


    var realStoryObj = document.getElementById("realstory");
    var realStory = ( (realStoryObj != null) && ((typeof realStoryObj) != "undefined") ) ? realStoryObj.value : "";


    frm.callingUrl.value = escape(document.location);

    //frm.docTitle.value = (realStory != "") ? realStory : escape(document.title);
    if(realStory != "") {
        frm.docTitle.value = realStory;
    } else if(frm.commTitle.value != "") {
        frm.docTitle.value = frm.commTitle.value;
    } else {
        frm.docTitle.value = escape(document.title);
    }

    frm.actionType.value = "save";

    if(frm.password.value == "") {
       alert("Please enter the validation code.");
       return;
    }

    validateCaptcha(frm.password.value);


}




function reportAbusiveComment(id) {
    var cookieName = "webtoolCommentId" + id;
    var displayMsg = true;
    var value = "yes";
    var days = 3650;

    if(!commentCookieIsEnabled()) {
       alert("Cookie must be enabled");
       return;
    } else if(userReportedAbusiveComment(cookieName, displayMsg)) {
       return;
    }

	if(confirm(ABUSE_MSG)) {
	   var url = WEBTOOL_SERVER + "comment_controller.php";
	   var frm = document.commentFrm;
	   setCookie_c(cookieName,value,days);
	   frm.actionType.value = "reportAbusiveComment";
       frm.callingUrl.value = escape(document.location);
       frm.id.value = id;
	   frm.action = url;
	   frm.submit();
	}
}




function userReportedAbusiveComment(cookieName, displayMsg) {
    var value = readCookie_c(cookieName);
    var userReportedAbuse = true;

    if(value == null) {
       return false;

    } else if(displayMsg) {
       alert("You have already reported this comment as abusive.");
    }

    return true;
}



function setCookie_c(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

    var cookieValue = name + "=" + value + expires + "; path=/";
	document.cookie = cookieValue;
}


function readCookie_c(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	var i;

	for(i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie_c(name) {
	setCookie(name,"",-1);
}


function validateCaptcha(key) {
	var actionType = "validateCaptcha";
	var callingUrl = escape(document.location);
    var parm = "captchaKey=" + key + "&actionType=" + actionType + "&callingUrl=" + callingUrl;
	var url = WEBTOOL_SERVER + "comment_controller.php?" + parm;

    if(key == "") {
        alert("Please enter the validation code.");
        return;
    } if(!gReadyToProcess) {
        return;
    }


    //Set this global variable to false in case user clicks on the submit button.
    //This value will be reset to true in the captchaResponseHandler().
    gReadyToProcess =  false;

    HTTP = getHttpObject();
	HTTP.onreadystatechange = captchaResponseHandler;
    HTTP.open("GET", url, true);
	HTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	HTTP.send(null);

}

function captchaResponseHandler() {
	var rc;

	if (HTTP.readyState == 4) {
        if (HTTP.responseText != "") {
            rc = HTTP.responseText;
            HTTP = null;
            if (rc.indexOf("Error") > -1) {
                alert(rc);

            } else if(rc.indexOf("Failure") > -1) {
                alert("The validation code is incorrect or expired.  Please retype the code or refresh the screen.");

            } else if(rc.length == 11) {
                var frm = document.commentFrm;
                frm.callbackPassw.value = rc;
                frm.submit();
            }

        }
        HTTP = null;
        gReadyToProcess = true;
    }
}


