var global_userID = 0;
var global_moderator_level = 0;
function shocast(movieID, actorID, visitor_u, visitor_ismod, order, page)
{
    global_userID = visitor_u;
    global_moderator_level = visitor_ismod;
    var urlz = "/cast/xmlcast?movieID=" + movieID + "&actorID=" + actorID + "&order=" + order + "&page=" + page;
    loadXMLDoccast(urlz);
}
var reqcast;
function loadXMLDoccast(url) 
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqcast = new XMLHttpRequest();
        reqcast.onreadystatechange = processReqChangecast;
        reqcast.open("GET", url, true);
        reqcast.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqcast = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqcast)
        {
            reqcast.onreadystatechange = processReqChangecast;
            reqcast.open("GET", url, true);
            reqcast.send();
        }
    }
}

function processReqChangecast() 
{
    if (reqcast.readyState == 4)
    {
        if (reqcast.status == 200)
        {
            response = reqcast.responseXML.documentElement;
            var totalcasts = response.getElementsByTagName("totalcasts")[0].firstChild.data;
            var thismovieID = response.getElementsByTagName("thismovieID")[0].firstChild.data;
            var thisactorID = response.getElementsByTagName("thisactorID")[0].firstChild.data;
            var thisorder = response.getElementsByTagName("thisorder")[0].firstChild.data;
            var thispage = response.getElementsByTagName("thispage")[0].firstChild.data;
            var totalpages = response.getElementsByTagName("totalpages")[0].firstChild.data;
            if(totalcasts>=1)
            {
                result_string = '';
                thispage = Math.abs(thispage);
                totalpages = Math.abs(totalpages);
                var previouspage = Math.abs(thispage)-1;
                var nextpage = Math.abs(thispage)+1;
                if(totalpages>1)
                {
                    if(thispage>1)
                    {
                        var previousbuttonpage = '<a onclick="shocast(' + thismovieID + ', ' + thisactorID + ', ' + global_userID + ', ' + global_moderator_level + ', ' + "'" + thisorder + "'" + ', ' + previouspage + ')" style="cursor:pointer;">previous page</a> | ';
                    }
                    else
                    {
                        var previousbuttonpage = '';
                    }
                    if(thispage<totalpages)
                    {
                        var nextbuttonpage = ' | <a onclick="shocast(' + thismovieID + ', ' + thisactorID + ', ' + global_userID + ', ' + global_moderator_level + ', ' + "'" + thisorder + "'" + ', ' + nextpage + ')" style="cursor:pointer;">next page</a>';
                    }
                    else
                    {
                        var nextbuttonpage = '';
                    }
                    var paginationstring = '<div class="effect" style="font-size:80%;">' + previousbuttonpage + "page " + thispage + " of " + totalpages + nextbuttonpage + '</div>';
                }
                else
                {
                    paginationstring = '';
                }






                if(thismovieID==0)
                {
                    var thisleftcolumnname = "movie";
                }
                else
                {
                    var thisleftcolumnname = "actor";
                }
                result_string = result_string + '<div style="text-align:center;"><b>cast</b>:</div>' + paginationstring + '<table class="effect2"><tr><th style="width:25%;">&#160;</th><th style="width:25%;text-align:center;vertical-align:bottom;">' + thisleftcolumnname + '</th><th style="width:25%;text-align:center;vertical-align:bottom;">role</th><th style="width:25%;">&#160;</th><tr>';
                var count = 0;
                while(count < totalcasts)
                {
                    count++;
                    var this_castID_name = 'castID-' + count;
                    var this_movieID_name = 'movieID-' + count;
                    var this_actorID_name = 'actorID-' + count;
                    var this_role_name = 'role-' + count;
                    var this_rating_name = 'rating-' + count;
                    var this_votes_name = 'votes-' + count;
                    var this_time_name = 'time-' + count;
                    var this_userID_name = 'userID-' + count;
                    var this_actor_name_name = 'actor_name-' + count;
                    var this_movie_name_name = 'movie_name-' + count;
                    var this_movie_year_name = 'movie_year-' + count;
                    var this_castID = response.getElementsByTagName(this_castID_name)[0].firstChild.data;
                    var this_movieID = response.getElementsByTagName(this_movieID_name)[0].firstChild.data;
                    var this_actorID = response.getElementsByTagName(this_actorID_name)[0].firstChild.data;
                    var this_role = response.getElementsByTagName(this_role_name)[0].firstChild.data;
                    var this_rating = response.getElementsByTagName(this_rating_name)[0].firstChild.data;
                    var this_votes = response.getElementsByTagName(this_votes_name)[0].firstChild.data;
                    var this_time = response.getElementsByTagName(this_time_name)[0].firstChild.data;
                    var this_userID = response.getElementsByTagName(this_userID_name)[0].firstChild.data;
                    var this_actor_name = response.getElementsByTagName(this_actor_name_name)[0].firstChild.data;
                    var this_movie_name = response.getElementsByTagName(this_movie_name_name)[0].firstChild.data;
                    var this_movie_year = response.getElementsByTagName(this_movie_year_name)[0].firstChild.data;
                    var this_poster_publicname_name = 'publicname-' + this_userID;
                    var this_poster_publicname = response.getElementsByTagName(this_poster_publicname_name)[0].firstChild.data;
                    if(this_movie_year == '0')
                    {
                        var show_movie_year = '';
                    }
                    else
                    {
                        var show_movie_year = ' ' + this_movie_year;
                    }

                    if(thismovieID==0)
                    {
                        var this_movie_pic_name = 'movie_pic-' + this_movieID;
                        var this_movie_pic = response.getElementsByTagName(this_movie_pic_name)[0].firstChild.data;
                        var moviepicid = 'moviepic' + this_castID;
                        if(this_movie_pic=='none')
                        {
                            this_movie_pic='/common_files/pics/flobb.jpg';
                        }
                        var thisshitforleftside = '<table><tr><td style="width:40px;height:40px;text-align:center;vertical-align:middle;"><a href="/links_page?movie=' + this_movieID + '"><img src="' + this_movie_pic + '" id="' + moviepicid + '" style="width:40px;height:40px;" onload="resize_pic(40,40,' + "'" + moviepicid + "'" + ')" /></a></td><td style="text-align:left;vertical-align:middle;"><a href="/links_page?movie=' + this_movieID + '">' + this_movie_name + '</a> ' + show_movie_year + '</td></tr></table>';
                    }
                    else
                    {
                        var this_actor_pic_name = 'actor_pic-' + this_actorID;
                        var this_actor_pic = response.getElementsByTagName(this_actor_pic_name)[0].firstChild.data;
                        var actorpicid = 'actorpic' + this_castID;
                        if(this_actor_pic=='none')
                        {
                            this_actor_pic='/common_files/pics/noface.jpg';
                        }
                        var thisshitforleftside = '<table><tr><td style="width:40px;height:40px;text-align:center;vertical-align:middle;"><a href="/cast/element?actor=' + this_actorID + '"><img src="' + this_actor_pic + '" id="' + actorpicid + '" style="width:40px;height:40px;" onload="resize_pic(40,40,' + "'" + actorpicid + "'" + ')" /></a></td><td style="text-align:left;vertical-align:middle;"><a href="/cast/element?actor=' + this_actorID + '">' + this_actor_name + '</a></td></tr></table>';
                    }


                    if(this_userID==global_userID || global_moderator_level==1)
                    {
                        var deletebuttom =' <a onclick="erase_cast(' + thismovieID + ', ' + thisactorID + ', ' + global_userID + ', ' + global_moderator_level + ', ' + "'" + thisorder + "'" + ', ' + thispage + ', ' + this_castID + ', ' + "'" + escape(this_poster_publicname) + "'" + ')" style="cursor:pointer;color:red;">X</a>';
                    }
                    else
                    {
                        var deletebuttom ='';
                    }
                    if(this_userID == '25' || this_userID == '116475')
                    {
                        var itispostedby = 'listed ';
                    }
                    else
                    {
                        var itispostedby = 'listed by <a href="http://backstage.' + myhappydomain + '/userpage?u=' + this_userID + '">' + this_poster_publicname + '</a> ';
                    }
                    var postreblock = itispostedby + maketimus(this_time) + deletebuttom;
					
                    var votespanid = 'cast_votes_string_' + this_castID;
                    var votersblok = "<span id='" + votespanid + "'>" + makecastvotesstring(this_rating, this_votes, this_castID) + "</span>";

                    var thisdatarow = "<tr><td>" + votersblok + "</td><td style='text-align:left;vertical-align:middle;'>" + thisshitforleftside + "</td><td style='text-align:left;vertical-align:middle;'>" + this_role + "</td><td style='font-size:75%;'>" + postreblock + "</td></tr>";
                    result_string = result_string + thisdatarow;
                }
                result_string = result_string + "</tr></table>" + paginationstring;
                document.getElementById("cast").innerHTML = result_string;
            }
            else
            {
                document.getElementById("cast").innerHTML = '';
            }
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqcast.statusText);
        }
    }
}

function makecastvotesstring(rating, votes, castID)
{
    var thinger = 'cast';
    if(votes==1)
    {
        var votesS = '';
    }
    else
    {
        var votesS = 's';
    }
    var votesstring = "rating: " + rating + ", " + votes + " vote" + votesS + ".";
    votesstring = votesstring + " <span style='font-size:75%;'>vote: <a onclick='vote" + thinger + "(" + castID + ", 0)' style='cursor:pointer;'>0</a>, <a onclick='vote" + thinger + "(" + castID + ", 1)' style='cursor:pointer;'>1</a>, <a onclick='vote" + thinger + "(" + castID + ", 2)' style='cursor:pointer;'>2</a>, <a onclick='vote" + thinger + "(" + castID + ", 3)' style='cursor:pointer;'>3</a>, <a onclick='vote" + thinger + "(" + castID + ", 4)' style='cursor:pointer;'>4</a>, <a onclick='vote" + thinger + "(" + castID + ", 5)' style='cursor:pointer;'>5</a>, <a onclick='vote" + thinger + "(" + castID + ", 6)' style='cursor:pointer;'>6</a>, <a onclick='vote" + thinger + "(" + castID + ", 7)' style='cursor:pointer;'>7</a>, <a onclick='vote" + thinger + "(" + castID + ", 8)' style='cursor:pointer;'>8</a>, <a onclick='vote" + thinger + "(" + castID + ", 9)' style='cursor:pointer;'>9</a>, <a onclick='vote" + thinger + "(" + castID + ", 10)' style='cursor:pointer;'>10</a></span>";
    return votesstring;
}


function postcast(guestoruser, movieidz)
{
    var error = 0;
    actorID = document.getElementById("actorIDzz").value;
    role = document.getElementById("rolezz").value;
    actor_name = document.getElementById("actor_namezz").value;
    if (guestoruser == "guest")
    {
        error = 1;
        logintopostmessage = "please login to add a cast member.";
        alert(logintopostmessage);
    }
    else if (actorID == "0")
    {
        error = 1;
        alert("you must select an actor in the list");
    }
    else if (role == "")
    {
        error = 1;
        alert("you must enter a role for this cast member");
    }
    if (error == 0)
    {
        thispostforlog = "adding castmember <a href='cast/element?actor=" + actorID + "' target='_blank'>" + unescape(actor_name) + "</a> role: " + role + "<br />";
        document.getElementById("castpostlog").innerHTML = document.getElementById("castpostlog").innerHTML + thispostforlog;
        var urlz = "/cast/xmlpostcast?movie=" + movieidz + "&actorID=" + actorID + "&role=" + (encodeURIComponent(role));
        loadXMLDocpostcast(urlz);
    }
}
var reqpostcast;
function loadXMLDocpostcast(url) 
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqpostcast = new XMLHttpRequest();
        reqpostcast.onreadystatechange = processReqChangepostcast;
        reqpostcast.open("GET", url, true);
        reqpostcast.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqpostcast = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqpostcast)
        {
            reqpostcast.onreadystatechange = processReqChangepostcast;
            reqpostcast.open("GET", url, true);
            reqpostcast.send();
        }
    }
}

function processReqChangepostcast() 
{
    if (reqpostcast.readyState == 4)
    {
        if (reqpostcast.status == 200)
        {
            response = reqpostcast.responseXML.documentElement;
            var result = response.getElementsByTagName("result")[0].firstChild.data;
            var movieID = response.getElementsByTagName("thismovie")[0].firstChild.data;
            if(result=='ok')
            {
                var thispostforlog = 'cast member added<br />';
                document.getElementById("castpostlog").innerHTML = document.getElementById("castpostlog").innerHTML + thispostforlog;
                shocast(movieID, 0, global_userID, global_moderator_level, "time", "last");
            }
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqpostcast.statusText);
        }
    }
}



function lookforactor()
{
    var t=setTimeout("exec_lookforactor()",50);
}
function exec_lookforactor()
{
    var lookfor = document.getElementById("checkforactor").value;
    var lookforstring = 'actors with name matching "' + lookfor + '"';
    document.getElementById("showlookfor").innerHTML = lookforstring;
    var urlz = "/cast/xmllookfor?lookfor=" + lookfor;
    loadXMLDoclookfor(urlz);
}

var reqlookfor;
function loadXMLDoclookfor(url) 
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqlookfor = new XMLHttpRequest();
        reqlookfor.onreadystatechange = processReqChangelookfor;
        reqlookfor.open("GET", url, true);
        reqlookfor.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqlookfor = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqlookfor)
        {
            reqlookfor.onreadystatechange = processReqChangelookfor;
            reqlookfor.open("GET", url, true);
            reqlookfor.send();
        }
    }
}

function processReqChangelookfor() 
{
    if (reqlookfor.readyState == 4)
    {
        if (reqlookfor.status == 200)
        {
            response = reqlookfor.responseXML.documentElement;
            var totalsugg = response.getElementsByTagName("totalsugg")[0].firstChild.data;
            var coutem = 0;
            var suggestion_string = '';
            while(coutem < totalsugg)
            {
                coutem++;
                var this_actorID_name = 'actorID-' + coutem;
                var this_name_name = 'name-' + coutem;
                var this_birth_day_name = 'birth_day-' + coutem;
                var this_birth_month_name = 'birth_month-' + coutem;
                var this_birth_year_name = 'birth_year-' + coutem;
                var this_pic_name = 'pic-' + coutem;
                var this_actorID = response.getElementsByTagName(this_actorID_name)[0].firstChild.data;
                var this_name = response.getElementsByTagName(this_name_name)[0].firstChild.data;
                var this_birth_day = response.getElementsByTagName(this_birth_day_name)[0].firstChild.data;
                var this_birth_month = response.getElementsByTagName(this_birth_month_name)[0].firstChild.data;
                var this_birth_year = response.getElementsByTagName(this_birth_year_name)[0].firstChild.data;
                var this_pic = response.getElementsByTagName(this_pic_name)[0].firstChild.data;
                var this_picid = 'minipic_' + this_actorID;
                var thissuggestion = '<a onclick="select_actor(' + this_actorID + ', ' + "'" + escape(this_name) + "'" + ', ' + "'" + escape(this_pic) + "'" + ')" style="cursor:pointer;"><img src="' + this_pic + '" id="' + this_picid + '" style="width:40px;heigh:40px;" onload="resize_pic(40,40,' + "'" + this_picid + "'" + ')" /> ' + this_name + ' ' + this_birth_day + "/" + this_birth_month + "/" + this_birth_year + "</a><br />";
                suggestion_string = suggestion_string + thissuggestion;
            }
            if(totalsugg==0)
            {
                suggestion_string = 'no matching name found';
            }
            document.getElementById("showchoices").innerHTML = suggestion_string;
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqlookfor.statusText);
        }
    }
}

function select_actor(this_actorID, this_name, this_pic)
{
    this_pic = unescape(this_pic);
    document.getElementById("actorIDzz").value = this_actorID;
    document.getElementById("actor_namezz").value = this_name;
    var this_picid = 'averagepic_' + this_actorID;
    document.getElementById("showselectedactor").innerHTML = '<img src="' + this_pic + '" id="' + this_picid + '" style="width:120px;heigh:90px;" onload="resize_pic(60,90,' + "'" + this_picid + "'" + ')" /> <a href="cast/element?actor=' + this_actorID + '" target="_blank" style="font-size:150%;">' + unescape(this_name) + '</a>';
}



function votecast(castID, note)
{
    var votesbox = 'cast_votes_string_' + castID;
    document.getElementById(votesbox).innerHTML = '...Loading...';
    var urlz = "/cast/xmlvotecast?castID=" + castID + "&note=" + note;
    loadXMLDocvotecast(urlz);
}
var reqvotecast;
function loadXMLDocvotecast(url)
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqvotecast = new XMLHttpRequest();
        reqvotecast.onreadystatechange = processReqChangevotecast;
        reqvotecast.open("GET", url, true);
        reqvotecast.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqvotecast = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqvotecast)
        {
            reqvotecast.onreadystatechange = processReqChangevotecast;
            reqvotecast.open("GET", url, true);
            reqvotecast.send();
        }
    }
}
function processReqChangevotecast() 
{
    if (reqvotecast.readyState == 4)
    {
        if (reqvotecast.status == 200)
        {
            var response = reqvotecast.responseXML.documentElement;
            var result = response.getElementsByTagName("result")[0].firstChild.data;
            var rating = response.getElementsByTagName("rating")[0].firstChild.data;
            var votes = response.getElementsByTagName("votes")[0].firstChild.data;
            var castID = response.getElementsByTagName("castID")[0].firstChild.data;
            if(result=='ok')
            {
                var votesbox = 'cast_votes_string_' + castID;
                var redonevotesbox = makecastvotesstring(rating, votes, castID);
                document.getElementById(votesbox).innerHTML = redonevotesbox;
            }
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqvotecast.statusText);
        }
    }
}








function erase_cast(movieID, actorID, visitor_u, visitor_ismod, order, page, droppi, poster)
{
    global_userID = visitor_u;
    global_moderator_level = visitor_ismod;
    var urlz = "/cast/xmlerase_cast?droppi=" + droppi + "&movieID=" + movieID + "&actorID=" + actorID + "&order=" + order + "&page=" + page;
    var eraseconfmessage = 'are you sure you want to erase this post from: ' + poster;
    var r=confirm(eraseconfmessage);
    if(r==true)
    {
        loadXMLDocerase_cast(urlz);
    }
}
var reqerase_cast;
function loadXMLDocerase_cast(url)
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqerase_cast = new XMLHttpRequest();
        reqerase_cast.onreadystatechange = processReqChangeerase_cast;
        reqerase_cast.open("GET", url, true);
        reqerase_cast.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqerase_cast = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqerase_cast)
        {
            reqerase_cast.onreadystatechange = processReqChangeerase_cast;
            reqerase_cast.open("GET", url, true);
            reqerase_cast.send();
        }
    }
}
function processReqChangeerase_cast() 
{
    if (reqerase_cast.readyState == 4)
    {
        if (reqerase_cast.status == 200)
        {
            var response = reqerase_cast.responseXML.documentElement;
            var result = response.getElementsByTagName("result")[0].firstChild.data;
            var thispage = response.getElementsByTagName("page")[0].firstChild.data;
            var actorID = response.getElementsByTagName("actorID")[0].firstChild.data;
            var movieID = response.getElementsByTagName("movieID")[0].firstChild.data;
            var order = response.getElementsByTagName("order")[0].firstChild.data;
            if(result=='ok')
            {
                shocast(movieID, actorID, global_userID, global_moderator_level, order, thispage);
            }
            else if(result=='error')
            {
                var loginmessage = 'problems reaching database, please try reloading the page';
                alert(loginmessage);
            }
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqerase_cast.statusText);
        }
    }
}
