﻿var callBack
var spanId
var baseUrl = "/Request/?s=[S]&ss=[SS]&"
var showLoadingDiv=true
var newClass = 'error'
var newMemberIndex = 0
var newAnswerIndex = 0
var newArticleIndex = 0
var newUserCommentIndex = 1
var isMale 
var answerId
var highlight = true
var commentTypeId
var delimiter = ")#$(XX^-Z__zo0x"
var gender
var tempHolder

function spcAll(oURL){
    $('spellcheckDiv').style.display='';
    $('spellcheckFrm').src=oURL;
    
}
function showNewMembers(inc){
    if (inc=='-')
        newMemberIndex = (newMemberIndex - 4) % 20;
    else
        newMemberIndex = (newMemberIndex + 4) % 20;
    highlight = false
    showLoadingDiv=true
    callBack = 4;
    spanId='NewMembers'
    document.getElementById(spanId).style.display='none';
    tempURL = "f=NewMembers"
    tempURL = tempURL + "&i="+newMemberIndex;
    getResponse(baseUrl+tempURL);
}
function showNewAnswers(inc){
    if (inc=='-')
        newAnswerIndex = (newAnswerIndex - 2) % 20;
    else
        newAnswerIndex = (newAnswerIndex + 2) % 20;
    highlight = false
    showLoadingDiv=true
    callBack = 4;
    spanId='NewAnswers'
    document.getElementById(spanId).style.display='none';
    tempURL = "f=NewAnswers"
    tempURL = tempURL + "&i="+newAnswerIndex;
    getResponse(baseUrl+tempURL);
}
function showTopArticles(inc,cId,isRow,divId){
    if (inc=='-')
        newArticleIndex = (newArticleIndex - 2) % 6;
    else
        newArticleIndex = (newArticleIndex + 2) % 6;
    highlight = false
    showLoadingDiv=true
    callBack = 4;
    spanId=divId;
    document.getElementById(spanId).style.display='none';
    tempURL = "f=TopArticles"
    tempURL = tempURL + "&catId="+cId+"&isRow="+isRow+"&i="+newArticleIndex;
    getResponse(baseUrl+tempURL);
}
function RateQuestion(qId,rating){
    callBack = 5;
    highlight = false
    showLoadingDiv=true
    spanId='qRate'+qId
    tempURL = "f=QuestionRate"
    tempURL = tempURL + "&Id="+qId+"&r="+rating;
    getResponse(baseUrl+tempURL);
}
function RateArticle(aId,rating){
    callBack = 5;
    showLoadingDiv=true
    highlight = false
    spanId='aRate'+aId
    tempURL = "f=ArticleRate"
    tempURL = tempURL + "&Id="+aId+"&r="+rating;
    getResponse(baseUrl+tempURL);
}
function FFUser(uId,sep,a){
    callBack = 5;
    highlight = false
    showLoadingDiv=true
    spanId='action-'+uId+'-'+sep
    tempURL = "f=FriendRequest"
    tempURL = tempURL + "&uId="+uId+"&actionid="+a;
    getResponse(baseUrl+tempURL);
}
function FFUserProfile(uId,a){
    callBack = 5;
    highlight = false
    showLoadingDiv=true
    spanId='ffResponse'
    tempURL = "f=FriendRequestProfile"
    tempURL = tempURL + "&uId="+uId+"&actionid="+a;
    getResponse(baseUrl+tempURL);
}
function BlockUser(uId,sep,a){
    callBack = 5;
    showLoadingDiv=true
    highlight = false
    spanId='action-'+uId+'-'+sep
    tempURL = "f=BlockUser"
    tempURL = tempURL + "&uId="+uId+"&a="+a;
    getResponse(baseUrl+tempURL);
}
function SendQuestionToFriend(name,email,qId){
    callBack = 9;
    showLoadingDiv=true
    highlight = false
    spanId = 'EmailFriend'+qId
    tempURL = "f=EmailFriend"
    tempURL = tempURL + "&Id="+qId+"&n="+name+"&e="+email;
    getResponse(baseUrl+tempURL);
}
function SendArticleToFriend(name,email,aId){
    callBack = 9;
    showLoadingDiv=true
    highlight = false
    spanId = 'EmailFriend'+aId
    tempURL = "f=EmailFriendArticle"
    tempURL = tempURL + "&Id="+aId+"&n="+name+"&e="+email;
    getResponse(baseUrl+tempURL);
}
function SendFriendInvite(name,email){
    callBack = 9;
    showLoadingDiv=true
    highlight = false
    spanId = 'InviteFriend'
    tempURL = "f=InviteFriend"
    tempURL = tempURL + "&n="+name+"&e="+email;
    getResponse(baseUrl+tempURL);
}
function SendRealityToFriend(name,email,qId){
    callBack = 9;
    showLoadingDiv=true
    highlight = false
    spanId = 'EmailFriendReality'+qId
    tempURL = "f=EmailFriendReality"
    tempURL = tempURL + "&Id="+qId+"&n="+name+"&e="+email;
    getResponse(baseUrl+tempURL);
}
function BookmarkArticle(aId){
    callBack = 6;
    showLoadingDiv=true
    highlight = true
    spanId = 'BookmarkArticle'+aId
    tempURL = "f=NoteArticle"
    tempURL = tempURL + "&Id="+aId ;
    getResponse(baseUrl+tempURL);
}
function BookmarkQuestion(qId,el){
    callBack = 6;
    showLoadingDiv=true
    highlight = true
    spanId = 'BookmarkQuestion'+qId
    if (el)
        SlideCloseEls('Abuse'+qId,'EmailFriend'+qId)
    tempURL = "f=NoteQuestion"
    tempURL = tempURL + "&Id="+qId ;
    getResponse(baseUrl+tempURL);
}
function PreviewVideo(eid,c){
    var vurl = $(eid).value;
    if (vurl.indexOf('youtube.com')>0){
        $('PreviewVideo').style.display = '';
        callBack = 6;
        showLoadingDiv=true
        highlight = false
        spanId = 'PreviewVideo'+c
        tempURL = "f=GetVideoEmbed"
        tempURL = tempURL + "&v="+URLEncode(vurl);
        getResponse(baseUrl+tempURL);
    }
    else
        alert('Invalid YouTube video link!')
}
function ReportArticleAbuse(aId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'Abuse'+aId
    tempURL = "f=ReportArticleAbuse"
    tempURL = tempURL + "&Id="+aId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportProfile(uId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'ReportProfileRes'
    tempURL = "f=ReportProfile"
    tempURL = tempURL + "&Id="+uId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportQAbuse(qId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'Abuse'+qId
    tempURL = "f=ReportQAbuse"
    tempURL = tempURL + "&Id="+qId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportRAbuse(qId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'Abuse'+qId
    tempURL = "f=ReportRAbuse"
    tempURL = tempURL + "&Id="+qId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportCEAbuse(qId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'AbuseContest'+qId
    tempURL = "f=ReportCEbuse"
    tempURL = tempURL + "&Id="+qId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportAAbuse(aId,reason){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'ReportAns'+aId
    tempURL = "f=ReportAAbuse"
    tempURL = tempURL + "&Id="+aId+"&r="+reason;
    getResponse(baseUrl+tempURL);
}
function ReportCAbuse(cId){
    callBack = 6;
    showLoadingDiv=true
    highlight = false
    spanId = 'reportCommentDiv'+cId
    tempURL = "f=ReportCAbuse"
    tempURL = tempURL + "&Id="+cId;
    getResponse(baseUrl+tempURL);
}
function SubmitAnswerComment(aId){
    callBack = 7;
    showLoadingDiv=true
    spanId = 'AddCommentTd'+aId
    highlight = false
    answerId = aId
    commentTypeId = 1
    tempURL = "f=CommentAnswer"
    tempURL = tempURL + "&aId="+aId+"&c="+URLEncode($('comment'+aId).value);
    getResponse(baseUrl+tempURL);
}
function SubmitArticleComment(aId){
    callBack = 7;
    showLoadingDiv=true
    spanId = 'AddCommentTd'+aId
    highlight = false
    answerId = aId
    commentTypeId = 2
    tempURL = "f=CommentArticle"
    tempURL = tempURL + "&aId="+aId+"&c="+URLEncode($('comment'+aId).value);
    getResponse(baseUrl+tempURL);
}
function SubmitRealityComment(aId){
    callBack = 7;
    showLoadingDiv=true
    spanId = 'AddCommentTd' + aId
    highlight = false
    answerId = aId
    commentTypeId = 3
    tempURL = "f=CommentReality"
    tempURL = tempURL + "&aId="+aId+"&c="+URLEncode($('comment'+aId).value);
    getResponse(baseUrl+tempURL);
}
function RemoveRealityComment(aId,delid){
    if(confirm('Delete this comment?')){
        callBack = 6;
        showLoadingDiv=true
        spanId = 'Comments'+aId
        highlight = false
        tempURL = "f=RemoveRealityComment"
        tempURL = tempURL + "&aId="+aId+"&delid="+delid;
        getResponse(baseUrl+tempURL);
    }
}
function LoadComments(aId,typeId){
    callBack = 6;
    showLoadingDiv=true
    spanId = 'Comments'+aId
    highlight = false
    tempURL = "f=LoadComments"
    tempURL = tempURL + "&aId="+aId+"&typeId="+typeId;
    getResponse(baseUrl+tempURL);
}
function RateAnswer(aId,rating){
    callBack = 6;
    showLoadingDiv=true
    spanId = 'AnswerRating'+aId
    highlight = true
    tempURL = "f=RateAnswer"
    tempURL = tempURL + "&aId="+aId+"&r="+rating;
    getResponse(baseUrl+tempURL);
}
function showFeed(own,tid,feecount,feewidth,isf){
    callBack = 1;
    spanId = 'Site-Feed-Div'
    showLoadingDiv=false
    highlight = false
    tempURL = "f=ActivityFeed"
    tempURL = tempURL + "&own="+own+"&ch="+getChecked('feedtype')+"&fcount="+feecount+"&fwidth="+feewidth+"&isfriend="+isf;
    getResponse(baseUrl+tempURL);
}
function showUserInfo(uId,sep,showt,showa){
    callBack = 1;
    spanId = 'id-'+sep+'-'+uId
    showLoadingDiv=false
    highlight = false
    tempURL = "f=UserInfo"
    tempURL = tempURL + "&uId="+uId+"&sep="+sep+"&showt="+showt+"&showa="+showa;
    getResponse(baseUrl+tempURL);
}
function UpdateQAnon(el,id){
   
    var isanon
    if (el.src.indexOf('/images/chk-guy-off.gif')>0){
        el.src = '/images/chk-guy-on.gif';
        isanon = 1;
    }
    else{
        el.src = '/images/chk-guy-off.gif';
        isanon = 0;
    }
    
    callBack = 8;
    spanId = 'None'
    highlight = false
    tempURL = "f=QuestionAnonymous"
    tempURL = tempURL + "&Id="+id+"&a="+isanon;
    getResponse(baseUrl+tempURL);
}
function UpdateACMod(t,el,id){
    if (t==1)
        userModPost = 'Answer'
    else if (t==2)
        userModPost = 'Comment'
     
    if (el.src.indexOf('/images/chk-guy-off.gif')>0){
        el.src = '/images/chk-guy-on.gif'
        callBack = 8;
        spanId = 'None'
        highlight = false
        tempURL = "f=UserModApprove"
        tempURL = tempURL + "&Id="+id+"&typeid="+t;
        getResponse(baseUrl+tempURL);
    }
    else
        alert(userModPost+' is approved. If you want to remove, please click remove link!')
}
function SelectBestAnswer(el,aId,g){
    var imgArray = getElementsByClass("BestAnswerImage",null,null)
    var tdArray = getElementsByClass("BestAClass",null,null)
    highlight = false
    showLoadingDiv=true
    var makeCall = true
    if (g=='True'){
        if (el.src.indexOf('/images/chk-guy-off.gif')>0){
            for (x in imgArray){
               imgArray[x].src = '/images/chk-guy-off.gif' 
            }
            for (y in tdArray){
               tdArray[y].bgColor = 'white'
            }
            $('BestATd-'+aId).bgColor = '#FBFFD9'
            el.src = '/images/chk-guy-on.gif'
        }
        else
            makeCall = false
    }
    else{
        if (el.src.indexOf('/images/chk-girl-off.gif')>0){
            for (x in imgArray){
               imgArray[x].src = '/images/chk-girl-off.gif' 
            }
            for (y in tdArray){
               tdArray[y].bgColor = 'white' 
            }
            $('BestATd-'+aId).bgColor = '#FBFFD9'
            el.src = '/images/chk-girl-on.gif'
        }
        else
            makeCall = false
    }
    if (makeCall){
        callBack = 8;
        spanId = 'None'
        highlight = false
        tempURL = "f=BestAnswer"
        tempURL = tempURL + "&aId="+aId;
        getResponse(baseUrl+tempURL);
    }
}
function EnlargeImage(id,uId,genderVal,imgCount){
    callBack = 10;
    spanId = 'PicturesTab'
    gender=genderVal
    $(spanId+'Err').style.display='';
    showLoadingDiv=true
    highlight = false
    tempURL = "f=EnlargeImage"
    tempURL = tempURL + "&id="+id+"&userId="+uId+"&imgC="+imgCount;
    getResponse(baseUrl+tempURL);
}
function NextProfileQuestions(page,uId,genderVal){
    callBack = 1;
    spanId = 'questionsTab'
    gender=genderVal
    showLoadingDiv=true
    highlight = false
    tempURL = "f=ShowQuestions"
    tempURL = tempURL + "&page="+page+"&userId="+uId;
    getResponse(baseUrl+tempURL);
}
function NextProfileFriends(page,uId){
    callBack = 1;
    spanId = 'friendsTab'
    showLoadingDiv=true
    highlight = false
    tempURL = "f=ProfileFriends"
    tempURL = tempURL + "&page="+page+"&userId="+uId;
    getResponse(baseUrl+tempURL);
}
function NextProfileStories(page,uId,genderVal){
    callBack = 1;
    spanId = 'storiesTab'
    gender=genderVal
    showLoadingDiv=true
    highlight = false
    tempURL = "f=ShowStories"
    tempURL = tempURL + "&page="+page+"&userId="+uId;
    getResponse(baseUrl+tempURL);
}
function NextProfileAnswers(page,uId,genderVal){
    callBack = 1;
    spanId = 'answersTab'
    gender=genderVal
    showLoadingDiv=true
    highlight = false
    tempURL = "f=ShowAnswers"
    tempURL = tempURL + "&page="+page+"&userId="+uId;
    getResponse(baseUrl+tempURL);
}
function showBestAnswers(page,uId,genderVal){
    if ($('ba-btn').checked==false)
        NextProfileAnswers(page,uId,genderVal)
    else{
        callBack = 1;
        spanId = 'answersTab'
        gender=genderVal
        showLoadingDiv=true
        highlight = false
        tempURL = "f=ShowBestAnswers"
        tempURL = tempURL + "&page="+page+"&userId="+uId;
        getResponse(baseUrl+tempURL);
    }
}
function ShowProfileImages(uId,genderVal,imgCount){
    callBack = 10;
    spanId = 'PicturesTab'
    gender=genderVal
    $(spanId+'Err').style.display='';
    showLoadingDiv=true
    highlight = false
    tempURL = "f=ShowAllImages"
    tempURL = tempURL + "&userId="+uId+"&imgC="+imgCount;
    getResponse(baseUrl+tempURL);
}
function textAdClick(adVarId,tag,typeId){
    callBack = 8;
    spanId = ''
    highlight = false
    tempURL = "f=AdClick"
    tempURL = tempURL + "&advarId="+adVarId+"&tag="+URLEncode(tag)+"&type="+typeId;
    getResponse(baseUrl+tempURL);
}
function QuestionVote(checkedValue,pId){
    if (checkedValue != ''){
        callBack = 6;
        showLoadingDiv=true
        spanId = 'PollQuestionVote'
        highlight = true
        tempURL = "f=VoteQuestion"
        tempURL = tempURL + "&pId="+pId+"&r="+checkedValue;
        getResponse(baseUrl+tempURL);
        }
    else
        alert('Please select an answer to vote!')
}
function UserComments(uid,page){
    highlight = false
    showLoadingDiv=true
    callBack = 1;
    spanId='userComments'
    document.getElementById(spanId).style.display='none';
    tempURL = "f=UserComments"
    tempURL = tempURL + "&u="+uid+"&page="+page;
    getResponse(baseUrl+tempURL);
}
function RemoveComment(cid){
    if (confirm('Delete this comment?')){
    callBack = 6;
    showLoadingDiv=true
    spanId = 'userComments'
    highlight = false
    tempURL = "f=DeleteUserComment"
    tempURL = tempURL + "&c="+cid;
    getResponse(baseUrl+tempURL);
    }
}
function AddUserComment(uId){
    if ($('mynewcomment').className=='inputN2')
        alert('Please enter a comment!')
    else{
        callBack = 12;
        showLoadingDiv=true
        thisComment = URLEncode($('mynewcomment').value)
        spanId = 'userComments'
        highlight = false
        tempURL = "f=AddUserComment"
        tempURL = tempURL + "&u="+uId+"&comment="+thisComment;
        getResponse(baseUrl+tempURL);
    }
}
function changeRateOnOld(id){
    callBack = 6;
    showLoadingDiv=false
    spanId = 'rateonquestions'
    highlight = false
    tempURL = "f=RateOnQuestions"
    tempURL = tempURL + "&id="+id;
    getResponse(baseUrl+tempURL);
}
function SendMessage(id){
    callBack = 11;
    tempHolder = $('sndMessageDiv').innerHTML 
    $('sndMessageDiv').innerHTML = '<b>Sending...</b>'
    thisSubject = URLEncode($('subject').value)
    thisMessage = URLEncode($('message').value)
    highlight = false
    showLoadingDiv=true
    spanId='SendPrivateMessage'
    var post_data = "f=SendMessage&id="+id+"&message="+thisMessage+"&subject="+thisSubject;
    getPostResponse(post_data)
}






function getResponse(URL){
    xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null){
        alert("Browser does not support HTTP Request")
        return
    }
    
    URL = URL.replace("[S]",document.getElementById('SID').value)
    URL = URL.replace("[SS]",Math.round(Math.random() * 100000000))
    xmlHttp.open("GET",URL,true)
    xmlHttp.setRequestHeader("Pragma","cache=no")
    xmlHttp.setRequestHeader("Cache-Control","no-transform")
    xmlHttp.onreadystatechange=ResponseReady
    xmlHttp.send(null)
}
function getPostResponse(data){
    xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null){
        alert("Browser does not support HTTP Request")
        return
    }
    
    URL = baseUrl + 'a1=1'
    URL = URL.replace("[S]",document.getElementById('SID').value)
    URL = URL.replace("[SS]",Math.round(Math.random() * 100000000))
    var post_data = data;

    xmlHttp.open("POST",URL,true)
    xmlHttp.setRequestHeader("Method","POST "+URL+" HTTP/1.1")
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    xmlHttp.setRequestHeader("Pragma","cache=yes")
    xmlHttp.setRequestHeader("Cache-Control","no-transform")
    xmlHttp.onreadystatechange=ResponseReady
    xmlHttp.send(post_data)

}
function ResponseReady(){
    
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
        if(showLoadingDiv){
            if (callBack==10){
                responseArr = xmlHttp.responseText.split(delimiter)
                $(spanId+'Err').innerHTML = responseArr[0]
            }
            else
                document.getElementById("WaitingDiv").style.display='none'
        }
        switch(callBack){
            case 1:
                changeHtml();
                break
            case 2:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else{
                    changeHtml()
                    changeClass()}
                break
            case 3:
                changeHtmlEffectSlideDown();
                break
            case 4:
                changeHtmlEffectAppear();
                break
            case 5:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else{
                    makeItVisible()
                    changeHtml()}
                break
            case 6:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else
                    changeHtmlEffectAppear();
                break
            case 7:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else{
                    changeHtmlEffectAppear();
                    if (!(xmlHttp.responseText.indexOf('error.gif')>0))
                        LoadComments(answerId,commentTypeId)
                }
                break
            case 8:
                break
            case 9:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else
                    changeHtmlEffectAppear();
                break
            case 10:
                changeHtmlArr();
                break
            case 11:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else{
                    if (xmlHttp.responseText.indexOf('error.gif')<0)
                        $('sndMessageDiv').innerHTML = ''
                    else
                        $('sndMessageDiv').innerHTML = tempHolder
                    makeItVisible()
                    changeHtml()}
                break
            case 12:
                if(xmlHttp.responseText=="-1")
                    sendToSignup()
                else
                    responseArr = xmlHttp.responseText.split(delimiter)
                    changeHtmlArrWall();
                break
        }
    }
    else{
        if(showLoadingDiv){
            if (callBack==10)
                $(spanId+'Err').innerHTML = "<img align='right' src='/images/ajax-loader-"+gender+".gif'/>"
            else
                $("WaitingDiv").style.display='';
        }
    }
}
function GetXmlHttpObject(){
    var objXMLHttp=null
    if(window.XMLHttpRequest)
        objXMLHttp=new XMLHttpRequest()
    else if(window.ActiveXObject)
         objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    else
         objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
    return objXMLHttp;
}



function changeClass(){
    document.getElementById(spanId).className=newClass;
}
function changeHtml(){
    document.getElementById(spanId).innerHTML=xmlHttp.responseText;
    document.getElementById(spanId).style.display='';
}
function sendToSignup(){
    parent.location='/signup.html?current='+thisURL;
}
function changeHtmlEffectSlideDown(){
    new Effect.SlideDown(document.getElementById(spanId),{duration: 0.5})
    document.getElementById(spanId).innerHTML=xmlHttp.responseText;
}
function changeHtmlEffectAppear(){
    $(spanId).style.display='none'
    appear(spanId);
    if (highlight)
        highlightDiv(isMale);
    document.getElementById(spanId).innerHTML=xmlHttp.responseText;
}
function changeHtmlEffectAppear2(){
    $(spanId).style.display='none'
    appear(spanId);
    if (highlight)
        highlightDiv(isMale);
    document.getElementById(spanId).innerHTML=xmlHttp.responseText;
}
function makeItVisible(){
    document.getElementById(spanId).style.display='';
}
function closeUH(el){
    if (tempY+5<=findPosY(el) || tempX+5<=findPosX(el))
    {
        o3_showingsticky = 0;
    }
    return nd();
}
function changeHtmlArr(){
    document.getElementById(spanId).innerHTML=responseArr[1];
    document.getElementById(spanId).style.display='';
}
function changeHtmlArrWall(){
    document.getElementById(spanId).innerHTML=responseArr[1];
    if (responseArr[0]=='20091'){
        document.getElementById('AddNewCommentUser').style.display = 'none'
        //document.getElementById('mynewcomment').className='inputN2';
    }
}