/*
Page:           rating.js
Created:        Aug 2006
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
--------------------------------------------------------- */

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function file_get_contents(url) 
{
	var req = null;
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
		try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
			try { req = new XMLHttpRequest(); } catch(e) {}
		}
	}
	if (req == null) throw new Error('XMLHttpRequest not supported');

	req.open("GET", url, false);
	req.send(null);

	return req.responseText;
}

function rateRecipe(id,vote,infodiv) {
	var theDiv = document.getElementById(infodiv); // the UL
	theDiv.innerHTML = 'Updating...';
	xmlhttp.open('get', 'http://www.gourmandia.com/ajax/rater_bar.php?id='+id+'&vote='+vote);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function rateChef(id,vote,infodiv) {
	var theDiv = document.getElementById(infodiv); // the UL
	theDiv.innerHTML = 'Updating...';
	xmlhttp.open('get', 'http://www.gourmandia.com/ajax/rater_bar_chef.php?id='+id+'&vote='+vote);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function updateCart(id) {
	var theDiv = document.getElementById("cart"+id); // the UL
	theDiv.innerHTML = 'Updating...';
	xmlhttp.open('get', 'http://www.gourmandia.com/ajax/update_cart.php?id='+id);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function addFavorite(id,infoDiv) {
	var theDiv = document.getElementById(infoDiv); // the UL
	theDiv.innerHTML = 'Updating...';
	//xmlhttp.open('get', 'http://www.gourmandia.com/ajax/add_favorite.php?id='+id);
    	//xmlhttp.onreadystatechange = handleResponse;
   	// xmlhttp.send(null);
	res = file_get_contents('http://www.gourmandia.ca/ajax/add_favorite.php?id='+id);
	res = res.split('|');
	if (res.length > 1) { res = res[1]; } else { res = res[0]; }
	theDiv.innerHTML = res;
}


function recipeUpdate(id,rdiv,phpScript) {
   var theDiv = document.getElementById(rdiv); // the UL
	
    // switch UL with a loading div
    theDiv.innerHTML = '<div class="loading">Loading...</div>';

    xmlhttp.open('get', 'http://www.gourmandia.com/ajax/'+phpScript+'.php?id='+id);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function playVideo(id,rdiv) {
	var theDiv = document.getElementById(rdiv); // the UL
	
	// switch UL with a loading div
	theDiv.innerHTML = '<div class="loading">Loading...</div>';
	
    xmlhttp.open('get', 'http://www.gourmandia.com/ajax/play_video.php?id='+id);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function loadWorldMapContent(id,rdiv) {
	var theDiv = document.getElementById(rdiv); // the UL
	
	// switch UL with a loading div
	theDiv.innerHTML = '<div class="loading">Loading...</div>';
	
    xmlhttp.open('get', 'http://www.gourmandia.com/ajax/worldmap.php?id='+id);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function handleResponse() {

  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

