var quoteId = '';

function showSubCategoryList(questionCategory) {
    var id = questionCategory.options[questionCategory.selectedIndex].value;
    var url = "/subCategory.html?method=showCommunitySubCategoryList";
    var pars = '&categoryId=' + id + '&' + Date();
    url += pars;
    showSubCategory(url);
}

function showSubCategory(url) {
    var req = initXMLHttp();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            document.getElementById('subCategories').innerHTML = req.responseText;
        }
    }
    req.send(url);
}

function initXMLHttp() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
}

function goBack()
{
    history.go(-1);
}

function showAnswerPopUp(obj, offH, offV) {
    var p = getElementPosition(obj);
    var t = document.getElementById('popup');
    t.style.position = 'absolute';
    t.style.left = (p.left - offH + 8) + 'px';
    t.style.top = (p.top + offV) + 'px';
    t.style.display = 'block';
}

function getElementPosition(offsetTrail) {
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function displayMessage(id) {
    var obj = document.getElementById(id);
    obj.style.display = '';
    window.setTimeout(hideMessage, 6000);
}
function hideMessage() {
    var obj = document.getElementById('msg');
    obj.style.display = 'none';
}

function viewAnswerPop(id, obj, offH, offV) {
    var p = getElementPosition(obj);
    var t = document.getElementById(id);
    t.style.position = 'absolute';
    t.style.left = (p.left - offH + 8) + 'px';
    t.style.top = (p.top + offV) + 'px';
    t.style.display = 'block';
}

function checkNewTopic(communityId) {
    url = "/loginAction.html?method=checkNewTopic&groupId=" + communityId;
    url += '&' + Date();
    var myAjax = new Ajax.Updater('details1', url, {method: 'get',onComplete:checkNewTopicResponse
    });
}

function checkNewTopicResponse(request) {
    var communityId = document.getElementById('communityId').value;
    if (request.responseText.indexOf("failure") >= 0) {
        grayPop('/login.html', 'overlay', 'popup_content');
    } else if (request.responseText.indexOf("joined") >= 0) {
        showAnswerPopUp($('newtopic_btn'), 400, 30);
    } else {
        saveUserCommunityReply(communityId)
        showAnswerPopUp($('newtopic_btn'), 400, 30);
        //joinGroup();
    }
}

function topicReplyPopup(groupId) {
    url = "/loginAction.html?method=checkNewTopic&groupId="+groupId;
    url += '&' + Date();
    var myAjax = new Ajax.Updater('details1', url, {method: 'get',onComplete:checkTopicResponse
    });
}

function checkTopicResponse(request) {
    var comId = document.getElementById('comId').value;
    if (request.responseText.indexOf("failure") >= 0) {
        grayPop('/login.html', 'overlay', 'popup_content');
    } else if (request.responseText.indexOf("joined") >= 0) {
        viewAnswerPop('replyPopup',$('topicReply'), 450, 20);
    } else {
        saveCommunityReply(comId)
		viewAnswerPop('replyPopup',$('topicReply'), 450, 20);
        //replyOfTopic();
    }
}

function showQuotePopUp(id, elem) {
    quoteId = id;
    url = "/loginAction.html?method=checkCommentAuthor&topicCommentId=" + id;
    url += '&' + Date();
    var myAjax = new Ajax.Request(url, {method: 'get',onComplete:quoteReplyResponse
    });
}
function quoteReplyResponse(request) {
    if (request.responseText.indexOf("failure") >= 0) {
        grayPop('/login.html', 'overlay', 'popup_content');
    } else if (request.responseText.indexOf("joined") >= 0) {
        replyOfQuote();
    } else {
        viewAnswerPop('quotepopup', $('quote_img'+quoteId), 350, 20);
    }
}

function replyOfQuote() {
    alert('You cannot quote for your own comment!');
}

function saveQuote() {
    if (!validateFormElement('Quote Text', 'qDescription')) {
        return;
    }
    var url = "/quote.html?method=saveQuote&tcId="+quoteId+"&description="+escape($('qDescription').value)+"&"+Date();
    pars = "";
    url += pars;
    var myAjax = new Ajax.Request(url, {method: 'get',parameters: pars,onComplete:function(request) {
        $('quoteText' + quoteId).innerHTML = request.responseText;
        $('quotepopup').style.display = 'none';
        $('qDescription').value = '';
    }
    });
}

function validateUserMessage() {
    validateFormElement('Subject', 'subject');
    return validateFormElement('Message', 'message');
}

var answerId = '';

function checkAnswer(id) {
    answerId = id;
    var url = "/loginAction.html?method=checkForLogin" + '&' + Date();
    var myAjax = new Ajax.Updater('details1', url, {method: 'get',onComplete:checkAnswerResponse
    });
}

function checkAnswerResponse(request) {
    if (request.responseText.indexOf("success") >= 0) {
        viewAnswerPop('popup' + answerId, $('answerLink' + answerId), 10, 20);
    } else {
        grayPop('/login.html', 'overlay', 'popup_content');
    }
}

function checkAnswerOnQuestionDetailsPage(){
        var url = "/loginAction.html?method=checkForLogin"+'&'+Date();
        var myAjax = new Ajax.Updater('details1',url,{method: 'get',onComplete:checkAnswerResponseOnQuestionDetails
        });
}

function checkAnswerResponseOnQuestionDetails(request){
    if(request.responseText.indexOf("success") >= 0){
        showAnswerPopUp($('answer_img'),210,20);
    }else{
        grayPop('/login.html','overlay','popup_content');
    }
}

function saveCommunityReply(groupId){
    var url="/userCommunity.html?method=saveUserCommunity";
    var pars='&groupId='+groupId+'&'+Date();
    url+=pars;
    saveGroupReply(url);
}

function saveGroupReply(url){
    var req=initXMLHttp();
    req.open("GET",url,true);
    req.onreadystatechange=function(){
        if(req.readyState==4){
            if(req.responseText.indexOf("failure") >= 0){
               grayPop('/login.html','overlay','popup_content');
            }
        }
    }
    req.send(url);
}

