var isCatIdSet = false;
var catId = "";
var pageNum = "";
var pollId = "";
var rTemp = new Array;
var ansId = "";
var filterOption = "";

function getjmCatId(movieName) {
    //alert("getCatId..." + pathPrefix + movieName);
    
    catId = getQuerystring("catid", null);
    ansId = getQuerystring("ansid", null);
    pageNum = getQuerystring("curpagenum", null);
    filterOption = getQuerystring("filteroption", null);
    pollId = getQuerystring("pollid", null);
    
    //var userId = UserID;
    //alert("userId=" + UserID + ", catid=" + catId + ", ansId=" + ansId + ", pageNum=" + pageNum + ", filterOption=" + ", pollId=" + pollId);

    if (catId.length == 0 && filterOption.length == 0) {
        catId = "8";
    }
    else if(catId.length == 0 && filterOption.length > 0 ){
        catId = "-1";
    }

    if (pollId.length == 0) {
        pollId = "-1";

    }

    //if pollid not empty and pagenum is empty this should be the pollresultdetails...
    //if the page is .htm then get the poll id from the url
    if (window.location.href.toLowerCase().indexOf(".htm") > 0) {
        //the poll id comes after 'poll/' and until the next '/'
        var tUrl = window.location.href.toLowerCase();
        var sPos = tUrl.indexOf("/poll/");

        var nSPos = tUrl.lastIndexOf("/");
        //alert(sPos + "::" + nSPos + "::" + tUrl);
        var pId = tUrl.substr(sPos + 6, nSPos - (sPos + 6));
        //alert("Poll id=" + pId);
        pollId = pId;
    }
    //get the pagenum and catid by poll...
    //////////////////////////////////////////////////////////////////////////////////
    //make an AJAX request here...using jQuery
    if (pageNum.length == 0 && pollId.length > 0 && pollId != "-1") {


        $.get(pathPrefix + "/Poll/getPageNumberForPoll.aspx", { UserId: UserID, PollID: pollId }, function(data) {
            //data is returned as 'catId|pageNum'
        rTemp = data.split("|");
            setCatIdPageNum(rTemp, movieName);
        });



    }

    else {

        //////////////////////////////////////////////////////////////////////////////////

        try {
            //alert(rTemp + ":" + catId + ":" + pageNum + isCatIdSet);
            getFlashMovie(movieName).sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId, UserID);

            //getFlashMovie("Carousel").sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId);
            //only the construct below works on the server...dont know why!
            //document.getElementById("Carousel").sendCatId(catId, pathPrefix);//pathPrefix is there in the masterpage and is the server path
        }
        catch (e) {

            try {

                document.getElementById(movieName).sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId, UserID);

            }
            catch (ex) {

                alert("Error setting the Category ID.\n" + ex);
            }

        }
    }



}


//function to set the pageNum
function setCatIdPageNum(arrTemp, movieName) {
    catId = arrTemp[0]; pageNum = arrTemp[1];
    //alert("setting catId..." + arrTemp + ":" + catId + ":" + pageNum + isCatIdSet);
    try {
        
        getFlashMovie(movieName).sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId, UserID);

        //getFlashMovie("Carousel").sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId);
        //only the construct below works on the server...dont know why!
        //document.getElementById("Carousel").sendCatId(catId, pathPrefix);//pathPrefix is there in the masterpage and is the server path
    }
    catch (e) {

        try {

            document.getElementById(movieName).sendCatId(catId, pathPrefix, ansId, pageNum, filterOption, pollId, UserID);

        }
        catch (ex) {

            alert("Error setting the Category ID.\n" + ex);
        }

    }
    isCatIdSet = true;
    //alert("Set catId." + arrTemp + ":" + catId + ":" + pageNum + isCatIdSet);
    
}

//function for ajax calls...returns the response...

//function to rate poll..............
function ratejmPoll(pollId, rating, movieName) {
    //check login
    //alert("ratePoll(" + pollId + "," + rating + ")");
    if (UserID != null && UserID > 0) {
        //do the flag
        var url = pathPrefix + "/Poll/PollRating.aspx?UserId=" + UserID + "&PollID=" + pollId + "&Rt=" + rating;
        //var avRating;
        //make an AJAX request here...using jQuery

        $.get(pathPrefix + "/Poll/PollRating.aspx", { UserId: UserID, PollID: pollId, Rt: rating }, function(data) {
        //avRating = data;
            setjmRating(data, movieName);
        });
        
        ////////////////////////////////////////////////////////////////
    }
    else {

        LoginPopUp();

    }

}

function setjmRating(avRating, movieName) {
    //set the star to the average rating returned///////////////////
    try {
        //alert(catId);
        //getFlashMovie("Carousel").sendRating(avRating);
        getFlashMovie(movieName).sendRating(avRating);
        //only the construct below works on the server...dont know why!
        //document.getElementById("Carousel").sendCatId(catId, pathPrefix);//pathPrefix is there in the masterpage and is the server path
    }
    catch (e) {
        if (document.all) {
            //document.getElementById("Carousel").sendRating(avRating);
            document.getElementById(movieName).sendRating(avRating);
        }
        else {
            alert(e);
        }
    }
}

function getServerPath(movieName) {
    //alert("getting serverpath '" + pathPrefix + "'");
    try {
        //alert(catId);
        //getFlashMovie("Carousel").sendRating(avRating);
        getFlashMovie(movieName).setServerPath(pathPrefix);
        //only the construct below works on the server...dont know why!
        //document.getElementById("Carousel").sendCatId(catId, pathPrefix);//pathPrefix is there in the masterpage and is the server path
    }
    catch (e) {
        if (document.all) {
            //document.getElementById("Carousel").sendRating(avRating);
            document.getElementById(movieName).setServerPath(pathPrefix);
        }
        else {
            alert(e);
        }
    }
}





