﻿$(document).ready(function() {
    InitRatingStars();
});

function InitRatingStars() {
    if (!ratingsStars && document.getElementById("ratingboxspan")) ratingsStars = document.getElementById("ratingboxspan").childNodes;
    if (!ratingsStars) return;
    for (var i = 0; i < ratingsStars.length; i++) {
        var element = ratingsStars[i];
        if (element.tagName) {
            element.onmouseover = function() {
                FillRatingsStars(this);
            }
            element.onmouseout = function() {
                ratingBoxLayer.style.display = "none";
                ClearCurrentRating();
            }
            element.onclick = function() {
                ratingValue.value = (this.rating) ? this.rating : this.attributes.rating.value;
                ClearCurrentRating();
            }
        }        
        element = null;
    }
}

var ratingValue, ratingsStars, ratingBoxLayer;
var ratingsScore = "Not interesting,Somewhat interesting,Interesting,Very interesting,Excellent".split(',');
function FillRatingsStars(element) {
    if (!ratingValue) ratingValue = getObjFormField("rateDocumentForm", "ratingValue");
    if (!ratingsStars) ratingsStars = document.getElementById("ratingboxspan").childNodes;
    if (!ratingBoxLayer) ratingBoxLayer = document.getElementById("ratingBoxLayer");
    var rating = parseInt((element.rating)? element.rating : element.attributes.rating.value);
    ratingBoxLayer.style.left = element.offsetLeft - 20;
    ratingBoxLayer.style.top = element.offsetTop + element.offsetHeight + 20;
    ratingBoxLayer.innerHTML = ratingsScore[rating - 1];
    ratingBoxLayer.style.display = "block";

    var f = true;
    var j = 1;
    for (var i = 0; i < ratingsStars.length; i++) {
        if (ratingsStars[i].tagName) {
            ratingsStars[i].className = "itemRatingStar " + ((f) ? "itemRatingStarFilled" : "itemRatingStarWaiting");
            if (rating == j) {
                f = false;
            }
            j++;
        }
    }
    element = null;
}
function ClearCurrentRating() {
    var current = ratingValue.value;
    var j = 0;
    for (var i = 0; i < ratingsStars.length; i++) {
        if (ratingsStars[i].tagName) {
            ratingsStars[i].className = "itemRatingStar " + ((current == 0) ? "itemRatingStarWaiting" : (j < current) ? "itemRatingStarSaved" : "itemRatingStarEmpty");
            j++;
        }
    }
}
var ratingTitle, ratingComment;

function ratingSubmit() {
    if (!ratingValue) ratingValue = getObjFormField("rateDocumentForm", "ratingValue");
    if (!ratingTitle) ratingTitle = getObjFormField("rateDocumentForm", "ratingTitle");
    if (!ratingComment) ratingComment = getObjFormField("rateDocumentForm", "ratingComment");
    if (ratingValue.value == 0) {
        alert("Please give a rating to this document.\n(Click on the stars above).");
        return;
    }
    if (trim(ratingTitle.value) != "" && trim(ratingComment.value) == "") {
        alert("Please write your reaction.");
        ratingComment.focus();
        return;
    }
    if (trim(ratingTitle.value) == "" && trim(ratingComment.value) != "") {
        alert("Please give a title to your reaction.");
        ratingTitle.focus();
        return;
    }
    if (trim(ratingComment.value) == "") {
        //overlayIframe('/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingonly', 480, 200);
        onHideOverlay.add(closeSubmitIframe);

        overlayIframe('', 480, 200, "id", "submitFrame", "name", "submitFrame");
        var form = document.forms["rateDocumentForm"];
        form.action = "/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingonly";
        form.target = "submitFrame";
        form.submit();
    }
    else {
        overlayIframe('', 480, 400, "id", "submitFrame", "name", "submitFrame");
        var form = document.forms["rateDocumentForm"];
        form.action = "/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingcomment";
        form.target = "submitFrame";
        form.submit();
//        overlayIframe('/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingcomment&rating=' + ratingValue.value, 480, 400);
    }
}
function reactSubmit() {
    if (!ratingTitle) ratingTitle = getObjFormField("rateDocumentForm", "ratingTitle");
    if (!ratingComment) ratingComment = getObjFormField("rateDocumentForm", "ratingComment");

    var companyCheck = getObjFormField("rateDocumentForm", "reactDisplayCompany");
    var nameCheck    = getObjFormField("rateDocumentForm", "reactDisplayName");
    var nameText     = getObjFormField("rateDocumentForm", "reactDisplayedName");

    if (trim(ratingTitle.value) == "") {
        alert("Please give a title to your reaction.");
        ratingTitle.focus();
        return;
    }
    if (trim(ratingComment.value) == "") {
        alert("Please write your reaction.");
        ratingComment.focus();
        return;
    }

    if (companyCheck.checked && (!nameCheck.checked || trim(nameText.value) == "")) {
        alert("Please write your name.");
        nameText.focus();
        return;
    }

    overlayIframe('', 480, 400, "id", "submitFrame", "name", "submitFrame");
    var form = document.forms["rateDocumentForm"];
    form.action = "/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingcomment";
    form.target = "submitFrame";
    form.submit();
}
function ValidationReact() {
    onHideOverlay.add(closeSubmitIframe);

    overlayIframe('', 480, 200, "id", "submitFrame", "name", "submitFrame");
    var form = document.forms["rateDocumentForm"];
    form.action = "/common/shared/voice-peers/document-react/confirmation.aspx?type=ratingsubmit";
    form.target = "submitFrame";
    form.submit();
}
function closeSubmitIframe() {
    window.location.href = window.location.href.split('#')[0];
}
function getRatingValue() {
    return ratingValue.value;
}
function getReactionComment() {
    return "<i>" + ratingTitle.value + "</i><br />" + ratingComment.value;
}

if (document.location.hash == "#addcomments") {
    window.setTimeout("addComments(true)", 500);
}
function addComments(visible) {
    var div = document.getElementById("divAddComments");
    div.style.display = (visible)? "inline" : "none";

    //div = document.getElementById("divLinkAddComments");
    //div.style.display = "none";
}
function ratingLogin() {
    document.forms["rateLoginForm"].submit();
}
function checkBox(checkbox) {
    getObjFormField("rateDocumentForm", checkbox).checked = true;
}
function checkBoxCompany() {
    if (getObjFormField("rateDocumentForm", "reactDisplayCompany").checked) {
        getObjFormField("rateDocumentForm", "reactDisplayName").checked = true;
    }
}
function checkBoxName() {
    var company = getObjFormField("rateDocumentForm", "reactDisplayCompany");
    var name    = getObjFormField("rateDocumentForm", "reactDisplayName");
    if (company && !name.checked && company.checked) {
        name.checked = true;
    }
}

function ValidationWarning() {
    warningMessage = getObjFormField("warningModeratorForm", "warningMessage");
    if (warningMessage.value == "") {
        alert("Please write your comment.");
        warningMessage.focus();
        return false;
    }
    //return true;
    var objWindow = GetOpenerWindow();
    
    objWindow.window.hideOverlay();
}

function DeleteDefaultNotification() {
    defaultMessage = getObjFormField("warningModeratorForm", "warningMessage");
    if (defaultMessage.value == "Feel free to add a comment about your alert (it will remain confidential).") {
        defaultMessage.value = "";
    }
}

function DeleteDefaultTopicSuggestion() {
    defaultMessage = getObjFormField("suggestTopicForm", "topicText");
    if (defaultMessage.value == "Suggest your topic here") {
        defaultMessage.value = "";
    }
}

function ValidationSuggestTopic() {
    topicText = getObjFormField("suggestTopicForm", "topicText");
    if (trim(topicText.value) == "" || trim(topicText.value) == "Suggest your topic here") {
        alert("Please write your topic.");
        topicText.value = "";
        topicText.focus();
        return false;
    }
}

