var http = getHTTPObject(); // Create the HTTP Object
var isPocessing = false;

if (document.images) {
	rateBar = new Image(12,28); 
	rateBar.src = "/img/r1.gif"; 
}

// Go get this HTTP Object thingy
function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

// Add recipe rating to database and show results on page
function handleRateHttpResponse() {
    var ratingBar = "";

    if (http.readyState == 4) {
        result = http.responseText;
        if (result != "") {
            rating = result.split(",");
            rating[0] = eval(rating[0]);
            for (i = 1; i <= rating[0]; i++) {
                ratingBar = ratingBar + "<img src=\"/img/r1.gif\" border=\"0\">";
            }
            for (i = eval(rating[0] + 1); i <= 10; i++) {
                ratingBar = ratingBar + "<img src=\"/img/r0.gif\" border=\"0\">";
            }
            document.getElementById("ratingBar").innerHTML = "Thanks For Rating!<br />"+ratingBar;
            document.getElementById("avgRating").innerHTML = rating[1];
            document.getElementById("ratingCount").innerHTML = rating[2]+"<br />Ratings";
        }
        isPocessing = false;
    }
}

// Add user's favorite recipe to database and show results on page
function handleAddFavHttpResponse() {
    if (http.readyState == 4) {
        result = http.responseText;
        if (result != "") {
            document.getElementById("favCount").innerHTML = "Favorites: "+result+" <img src=\"/img/fav_star.gif\" title=\""+result+" people have chosen this recipe as their favorite.\"/>";
            document.getElementById("favButton").innerHTML = "<div class=\"disabledButtonBox\">Added</div>";
        }
        isPocessing = false;
    }
}

// Remove user's favorite recipe from database and show results on page
function handleRemoveFavHttpResponse() {
    if (http.readyState == 4) {
        result = http.responseText;
        if (result != "") {
            document.getElementById("favCount").innerHTML = "Favorites: "+result+" <img src=\"/img/fav_star.gif\" title=\""+result+" people have chosen this recipe as their favorite.\"/>";
            document.getElementById("favButton").innerHTML = "<div class=\"disabledButtonBox\">Removed</div>";
        }
        isPocessing = false;
    }
}

// Subscribe user to database and show results on page
function handleSubscribeHttpResponse() {
    if (http.readyState == 4) {
        result = http.responseText;
        if (result != "") {
            document.getElementById("subscribe").innerHTML = "You have subscribed to "+result+"'s recipes.";
        }
        isPocessing = false;
    }
}

// Unsubscribe user from database and show results on page
function handleUnsubscribeHttpResponse() {
    if (http.readyState == 4) {
        result = http.responseText;
        if (result != "") {
            document.getElementById("subscribe").innerHTML = "You have unsubscribed from "+result+"'s recipes.";
        }
        isPocessing = false;
    }
}

// Send recipe to people
function handleSendRecipeHttpResponse() {
    if (http.readyState == 4) {
        isPocessing = false;
    }
}

// Submit the recipe rating
function submitRating(id, value) {
    if (!isPocessing && http) {
        http.open("GET", "/ajax/submit_rating.php?id="+escape(id)+"&r="+escape(value), true);
        http.onreadystatechange = handleRateHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}

// Set the recipe rating
function setRating(value) {
    for (i = 1; i <= value; i++) {
        document.getElementById("rb"+i).src = "/img/r1.gif";
    }
    for (i = (value+1); i <= 10; i++) {
        document.getElementById("rb"+i).src = "/img/r0.gif";
    }
    document.getElementById("ratingVal1").innerHTML = value;
    document.getElementById("ratingVal2").innerHTML = getRatingSubTitle(value);
}

// Get the rating subtitle
function getRatingSubTitle(value) {
    var subtitle = ""; 

    switch(value)
    {
    case 1:
      subtitle = "Epic Fail";
      break;
    case 2:
      subtitle = "Yucky";
      break;
    case 3:
      subtitle = "No Good";
      break;
    case 4:
      subtitle = "Tasteless";
      break;
    case 5:
      subtitle = "Edible";
      break;
    case 6:
      subtitle = "Not Bad";
      break;
    case 7:
      subtitle = "Looks Good";
      break;
    case 8:
      subtitle = "Yummy";
      break;
    case 9:
      subtitle = "Mouth Watering";
      break;
    case 10:
      subtitle = "Crazy Delicious!";
      break;
    default:
      subtitle = "Not Rated";
    }
    return subtitle;
}

// Add recipe to favorite
function addFavorite(id) {
    if (!isPocessing && http) {
        http.open("GET", "/ajax/submit_favorite.php?id="+escape(id), true);
        http.onreadystatechange = handleAddFavHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}

// Remove recipe from favorite
function removeFavorite(id) {
    if (!isPocessing && http) {
        http.open("GET", "/ajax/remove_favorite.php?id="+escape(id), true);
        http.onreadystatechange = handleRemoveFavHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}

// Subscribe to user's recipes
function subscribe(id) {
    if (!isPocessing && http) {
        http.open("GET", "/ajax/subscribe.php?id="+escape(id), true);
        http.onreadystatechange = handleSubscribeHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}

// Unsubscribe user's recipes
function unsubscribe(id) {
    if (!isPocessing && http) {
        http.open("GET", "/ajax/unsubscribe.php?id="+escape(id), true);
        http.onreadystatechange = handleUnsubscribeHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}

// Send recipe
function sendRecipe() {
    var emailList = document.getElementById("emailList").value;
    var message = document.getElementById("message").innerHTML;
    var user = document.getElementById("user").value;
    var erid = document.getElementById("erid").value;
    if (!isPocessing && http) {
        http.open("GET", "/ajax/send_recipe.php?e="+escape(emailList)+"&m="+escape(message)+"&u="+escape(user)+"&r="+escape(erid), true);
        http.onreadystatechange = handleSendRecipeHttpResponse;
        isPocessing = true;
        http.send(null);
    }
    document.getElementById("emailrecipe").innerHTML = "<div style=\"text-align: center; color: #385063;\"><h3>Recipe has been sent!</h3></div>";
}
