function ajaxFunction()
{
//function to create and return a XMPHTTP object
    var ajaxRequest; 
    ajaxRequest = false;
    try
    {
        // create Opera 8.0+/Firefox/Safari XMPHTTP object
        ajaxRequest = new XMLHttpRequest();
        return ajaxRequest;
    } 
    catch (e)
    {
        // try creating IE object if above fails
        try
        {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            return ajaxRequest;
        } 
        catch (e) 
        {
            try
            {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e)
            { 
                //browser doesn't support ajax
                return false;
            }
        }
    }
    return;
}

function updatebox(mode,doodleid)
{
    if(mode == 1)
    {
        document.getElementById("ajaxemailbox").innerHTML = "<form method=\"post\" action=\"?m=11&amp;doodle="+doodleid+"\"><div class=\"bluebox\"><a href=\"javascript:void(0)\" onclick=\"updatebox('2',"+doodleid+");\">Hide This <-</a></div><div class=\"emailbox\">Friend's Email: <input type=\"text\" name=\"email1\" value = \"\" /></div><div class=\"emailbox\">Your Email: <input type=\"text\" name=\"email2\" value = \"\" /></div><div class=\"emailbox\">Your Name: <input type=\"text\" name=\"name\" value = \"\" /></div><div class=\"emailbox\"><input type=\"submit\" value=\"Send Email\" alt=\"Send Email\"  /></div></form>";
    }
    else if(mode == 2)
    {
        document.getElementById("ajaxemailbox").innerHTML = "<div class=\"bluebox\"><a href=\"javascript:void(0)\" onclick=\"updatebox('1',"+doodleid+");\">Email Doodle to Friend -></a></div>";
    }
}

function hidecomments() {
document.getElementById("showcomments").innerHTML = "";	
}

function ajaxvariantlinks(mode,vp,sp,doodle,vardoodle,o)
{
    ajaxRequest = ajaxFunction();

    if (ajaxRequest == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    var url="http://www.ratemydoodle.com/index.php";

    if(mode == "2225")// change comments box to loading image before pulling new data
    {
        url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o+"&doodle="+doodle+"&commentspage="+vp;
        document.getElementById("showcomments").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }
    else if(mode == "2226")// change variation's comment box to loading image
    {
        url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o+"&vardoodle="+vardoodle+"&doodle="+doodle+"&variantpage="+vp+"&commentspage="+vp;
        document.getElementById("showvarcomments").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }
    else//change the variations links box to loading image
    {
	url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o+"&vardoodle="+vardoodle+"&doodle="+doodle+"&variantpage="+vp;
	document.getElementById("imgvarbox").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }

    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        { 
            if(mode == "2225")//update comments box with new data
            {
                document.getElementById("showcomments").innerHTML = ajaxRequest.responseText;
            }
            else if(mode == "2226")//update variations comments box with new data
            {
                document.getElementById("showvarcomments").innerHTML = ajaxRequest.responseText;
            }
            else //update the variations links box with new data
            {
                document.getElementById("imgvarbox").innerHTML = ajaxRequest.responseText;
            }
        }
    }
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function ajaxlistlinks(mode, sp, o)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("imgbox").innerHTML = "<img src=\"http://www.ratemydoodle.com/loadingimg2.gif\" border=\"0\" alt=\"Loading...\" /><br />Loading....";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("imgbox").innerHTML = ajaxRequest.responseText;
        }
    }
    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function propComment(mode, doodle, propVal, type1, indexVal) {
    ajaxRequest = ajaxFunction();
    var proppingTxt = "Propping..";
    
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    
    if ( propVal == -1 ) {
	  proppingTxt = "Dropping..";    
    } else {
	  proppingTxt = "Propping..";  
    }
    
    document.getElementById("propTxt"+indexVal).innerHTML = proppingTxt;
    document.getElementById("propButton"+indexVal).innerHTML = "";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("propTxt"+indexVal).innerHTML = ajaxRequest.responseText;
        }
    }
    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode+"&doodle="+doodle+"&type1="+type1+"&propVal="+propVal;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);	
}

function flagDoodle(mode, doodle, type1)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("flagBox").innerHTML = "<br /><br />Flagging..";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("flagBox").innerHTML = ajaxRequest.responseText;
        }
    }
    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode+"&doodle="+doodle+"&type1="+type1;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function addmyvideo(doodle)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("addmyvideo").innerHTML = "Adding...";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("addmyvideo").innerHTML = ajaxRequest.responseText;
        }
    }
    var url="http://www.ratemydoodle.com/addmyvideo.php";
    url=url+"?doodle="+doodle;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function reorderVideos(mode, direction, moveid)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    
    var titleVal = document.getElementById("videoTitle").value;
    var descVal = document.getElementById("videoDesc").value;
    
    document.getElementById("videoList").innerHTML = "<br /><br />reordering..";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("videoList").innerHTML = ajaxRequest.responseText;
            
            document.getElementById("videoTitle").value = titleVal;
            document.getElementById("videoDesc").value = descVal;
        }
    }
    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m=9123&dir="+direction+"&moveid="+moveid;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function showFlagged() {
  document.getElementById("flagImg").style.display = "none";
  document.getElementById("imgBox").style.display = "block";
}

function verifyEmail(mode)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("flagBox").innerHTML = "<br /><br />Sending Email...";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("flagBox").innerHTML = ajaxRequest.responseText;
        }
    }
    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function showtoprated(mode)
{
    if(mode == 1)//hide top rated links
    {
        document.getElementById("toprated").innerHTML = "<a  href=\"javascript:void(0)\" onclick=\"showtoprated('2229');\">Show Top 10</a>";
        return;
    }
    else if(mode == 2)//hide latest doodles links
    {
        document.getElementById("latestdoodles").innerHTML = "<a  href=\"javascript:void(0)\" onclick=\"showtoprated('2230');\">Show New Doodles</a>";
        return;
    }
    else if(mode == 3)//hide latest variations links
    {
        document.getElementById("latestvars").innerHTML = "<a  href=\"javascript:void(0)\" onclick=\"showtoprated('2231');\">Show New Variations</a>";
        return;
    }

    ajaxRequest=ajaxFunction();

    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    else if(mode == 2229)//change top rated box to loading image
    {
        document.getElementById("toprated").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }
    else if(mode == 2230)//change new doodles box to loading image
    {
        document.getElementById("latestdoodles").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }
    else if(mode == 2231)//change new variations box to loading image
    {
        document.getElementById("latestvars").innerHTML = "<img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....";
    }
    
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            if(mode == 2229)//update top rated box with requested data
            {
                document.getElementById("toprated").innerHTML = ajaxRequest.responseText;
            }
            else if(mode == 2230)//update new doodles box with requested data
            {
                document.getElementById("latestdoodles").innerHTML = ajaxRequest.responseText;
            }
            else if(mode == 2231)//update new variations box with requested data
            {
                document.getElementById("latestvars").innerHTML = ajaxRequest.responseText;
            }
        }
    }

    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function ajaxmaincontent(mode, sp, o, rating, lastdoodle)
{
    ajaxRequest=ajaxFunction();
    if(ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
//change main content to loading image after rating a doodle
    document.getElementById("doodlecontent").innerHTML = "<br /><br /><img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....<br /><br />";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            contentbuffer = ajaxRequest.responseText; //store the next doodle's data in a variable until the rating has been processed
            ajaxprocessrating('2228', sp, o, rating, lastdoodle);
            document.getElementById("doodlecontent").innerHTML = contentbuffer; //go ahead and update the doodlecontent div with the new doodle's data
        }
    }

    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o;
    url=url+"&rate="+rating+"&lastdoodle="+lastdoodle;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}


function ajaxprocessrating(mode, sp, o, rating, lastdoodle)
{
    ajaxRequest = ajaxFunction();
    if(ajaxRequest == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
// change the last rating box (shown after rating a doodle) to the loading image
    document.getElementById("lastrating").innerHTML = "<div class='smallnavbox'><span class='navtitle'>Your Rating</span></div><br /><img src='http://www.ratemydoodle.com/loadingimg2.gif' border='0' alt='Loading...' /><br />Loading....<br /><div class='smallnavbox2'>";
	
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("lastrating").innerHTML = ajaxRequest.responseText;    
        }
    }

    var url="http://www.ratemydoodle.com/index.php";
    url=url+"?m="+mode+"&cat=1&startpage="+sp+"&ob="+o;
    url=url+"&rate="+rating+"&lastdoodle="+lastdoodle;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function BuildAnimation(doodle, size)
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("gifContent").innerHTML = "<h3>Building Animated Gif...</h3><br /><img src=\"http://www.ratemydoodle.com/animated-cartoon/loading.gif\" border=\"0\" /><br /><br /><div class=\"OkBox\">Allow a minute or two for the image to be built. <br /><br />Right click & 'save target as' once the image is built.</div><br /><br />";
	//document.getElementById("damnIE").style.display = "block";
    
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("gifContent").innerHTML = ajaxRequest.responseText;
            //document.getElementById("damnIE").style.display = "none";
        }
    }
    var url="http://www.ratemydoodle.com/animated-cartoon/creategif.php";
    url=url+"?id="+doodle+"&Size="+size;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function addVideo()
{
    ajaxRequest = ajaxFunction();
    if (ajaxRequest == null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("vidContent").innerHTML = "<h3>Building Video...</h3><br /><img src=\"http://www.ratemydoodle.com/animated-cartoon/loading.gif\" border=\"0\" /><br /><br /><div class=\"OkBox\">Allow a minute or two for the video to be built.</div><br /><br />";
	//document.getElementById("damnIE").style.display = "block";
    
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("vidContent").innerHTML = ajaxRequest.responseText;
            //document.getElementById("damnIE").style.display = "none";
        }
    }
    var url="http://www.ratemydoodle.com/doodle-video/createvideo.php";
    //url=url+"?id="+doodle+"&Size="+size;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}
