/*
* @copyright Copyright (C) 2005-2006 TheGang http://www.the-gang.net
* @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
* @author Alejandro Pedraza
* @since Sciret 1.0
* @package Sciret
* @packager TheGang
*/

var onloadFunctions = new Array();

function triggerOnloadFunctions() {
    for (var i = 0; i < onloadFunctions.length; i++) {
        onloadFunctions[i]();
    }
}

function removeElement(elementId) {
    $(elementId).parentNode.removeChild($(elementId));
}

function openpopup() {
    var window1 = window.open('index.php?view=ManageArticles&selectArticles=1', 'Search', 'width=800,height=600,toolbar=no,scrollbars=yes,resizable=yes');
}

function isEnterKey(evt) {
    evt = (evt)? evt : (window.event)? window.event : '';
    var theKey;
    if (evt) {
        theKey = (evt.which)? evt.which : evt.keyCode;
    }

    return (theKey == 13);
}


function catchEnter(evt, execute) {
    if (isEnterKey(evt)) {
        execute();
    }
    return false;
}

function deleteArticle(articleId, articleTitle, confirmationMessage, referrerView) {
    if (!confirm(confirmationMessage + ' "' + articleTitle + '" ?')) {
        return;
    }

    location.href = 'index.php?action=DeleteArticle&id=' + articleId + '&referrerView=' + referrerView;
}

function transferId(articleID) {
    if (opener.document.add_article_form.related_articles.value == '') {
        old_value = new Array();
    } else {
    	old_value = opener.document.add_article_form.related_articles.value.split(', ');
    }
    old_value.push(articleID);
	opener.document.add_article_form.related_articles.value = old_value.join(', ');
}

// *********************************************************
// **                  AJAX FUNCTIONS                     **
// *********************************************************

function fade(elementId) {
    setTimeout('fade2(\'' + elementId + '\');', 2000);
}

function fade2(elementId) {
    new Effect.Fade(elementId, {duration: 1.0});
}

function showLoading(loadingId) {
    $(loadingId).style.visibility = 'visible';
}

function successAjax(loadingId) {
    $(loadingId).style.visibility = 'hidden';
}

function submitComment(loadingId) {
    var rate;
    var userName = encodeURIComponent(document.forms["commentAndRating"].elements["commentUserName"].value);
    var comment = encodeURIComponent(document.forms["commentAndRating"].elements["comment_box"].value);

    if (comment != '' && userName == '') {
        alert('Please enter your name to post a comment');
        return;
    }

    if (typeof document.forms["commentAndRating"].Rate == 'undefined') {
        rate = 0;
    } else {
        if (document.forms["commentAndRating"].Rate[0].checked) {
            rate = 1;
        } else if(document.forms["commentAndRating"].Rate[1].checked) {
            rate = 2;
        } else if(document.forms["commentAndRating"].Rate[2].checked) {
            rate = 3;
        } else if(document.forms["commentAndRating"].Rate[3].checked) {
            rate = 4;
        } else if(document.forms["commentAndRating"].Rate[4].checked) {
            rate = 5;
        }
    }

    new Ajax.Updater(   'commentsDiv',
                        'index.php?action=AddCommentAndRating&artId='+artId+'&loadingId='+loadingId,
                        {   method      : 'post',
                            parameters  : 'commentUserName='+userName+'&comment_box='+comment+'&Rate='+rate,
                            evalScripts : true,
                            onLoading   : function() {showLoading(loadingId);},
                            onComplete  : function() {successAjax(loadingId)}});
}

function publishComment(commentId, loadingId) {
    new Ajax.Updater(   'commentsDiv',
                        'index.php?action=PublishComment&artId='+artId+'&commentId='+commentId+'&loadingId='+loadingId,
                        {   method     : 'post',
                            evalScripts : true,
                            onLoading   : function() {showLoading(loadingId);},
                            onComplete  : function() {successAjax(loadingId)}});
}

function deleteComment(commentId, loadingId) {
    new Ajax.Updater(   'commentsDiv',
                        'index.php?action=DeleteComment&artId='+artId+'&commentId='+commentId+'&loadingId='+loadingId,
                        {   method     : 'post',
                            evalScripts : true,
                            onLoading   : function() {showLoading(loadingId);},
                            onComplete  : function() {successAjax(loadingId)}});
}

function publishArticle(loadingId, referrer, articleId) {
    articleId = articleId || artId;
    new Ajax.Request(   'index.php?action=PublishArticle&artId='+articleId+'&loadingId='+loadingId,
                        {   method     : 'post',
                            onLoading   : function() {showLoading(loadingId);},
                            onComplete  : function(response) {publishArticleCompleted(response, loadingId)}});
}

function publishArticleCompleted(responseObj, loadingId) {
    successAjax(loadingId);
    removeElement('publishButton');
    if (loadingId != 'publishDeleteArticleManageLoading') {
        removeElement('unpublishedNotice');
    }
    $('publishArticleStatusMessage').innerHTML = responseObj.responseText;
    fade('publishArticleStatusMessage');
}

function submitRelatedArticles(loadingId) {
    var relatedArticles = $('related_articles').value;

    new Ajax.Updater(   'relatedArticlesDiv',
                        'index.php?action=AddRelatedArticles&artId='+artId+'&loadingId='+loadingId,
                        {   method      : 'post',
                            parameters  : 'related_articles='+relatedArticles,
                            evalScripts : true,
                            onLoading   : function() {showLoading(loadingId)},
                            onComplete  : function() {successAjax(loadingId)}
                        });
}
