function xmlhttpPost(qstr, collection, recipe_id, coll)
{
	var strURL = 'addtocoll.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
        if (self.xmlHttpReq.readyState == 4)
        {
            updatepage(self.xmlHttpReq.responseText, collection);
        }
    }
    self.xmlHttpReq.send(qstr);
}

function addto(recipe_id, collection, remove, coll)
{
    qstr = 'r=' + escape(recipe_id);
    qstr += '&c=' + escape(collection);
    if(remove) qstr += '&remove=1';
    if(coll) collection=recipe_id;
    xmlhttpPost(qstr, collection);
    return true;
}

function updatepage(str, collection){
    document.getElementById(collection).innerHTML = str;
    if(is_numeric(collection)) document.getElementById(collection).style.display='none';
}

function rate(recipe_id, rating)
{
    qstr = 'r=' + recipe_id;
    qstr += '&rating=' + rating;
    xmlhttpPost(qstr, 'ratingfeedback');
    return true;   
}

function is_numeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber; 
}

function doPopup() {
	  
	  if (!document.getElementsByTagName) return false; 
	  
	  var links = document.getElementsByTagName("a");
	  for (var i=0; i < links.length; i++) {
			if (links[i].className.match("popup")) {
				
				var SectionID = links[i].getAttribute("href").split("#")[1];
				var helpTextDiv = document.getElementById(SectionID);
				helpTextDiv.style.display = 'none';		
				
				links[i].onclick = function() {
					
					var popups = document.getElementsByTagName("a");
					for (var i=0; i < popups.length; i++) {
						if (popups[i].className.match("popup")) {
						
							var ThisPopupID = popups[i].getAttribute("href").split("#")[1];
							var SelectedPopupID = this.getAttribute("href").split("#")[1];
								
							if(ThisPopupID == SelectedPopupID){							
							}else{					
								UnselectedPopupDiv = document.getElementById(ThisPopupID);
								UnselectedPopupDiv.style.display = 'none';	
							}
						}
					}
						
					var SectionID = this.getAttribute("href").split("#")[1];
					var helpTextDiv = document.getElementById(SectionID);
					
					if(helpTextDiv.style.display == "block"){
						helpTextDiv.style.display = 'none';
					}else{
						/* display popup */
						helpTextDiv.style.zIndex = '10';
						helpTextDiv.style.display = 'block';
						helpTextDiv.style.position = 'absolute';
						helpTextDiv.style.left = '10%';
						helpTextDiv.focus();
					}
					
					helpTextDiv.onclick = function(){
						/* Hide popups */
						helpTextDiv.style.zIndex = '110';
						helpTextDiv.style.display = 'none';
						return false;
					}
					return false;
				}		  
			}
	  }
}


function closePopups(){
		var popups = document.getElementsByTagName("a");
		for (var i=0; i < popups.length; i++) {
			if (popups[i].className.match("popup")) {
			
				var ThisPopupID = popups[i].getAttribute("href").split("#")[1];	
				ThisPopup = document.getElementById(ThisPopupID);						
				if(ThisPopup.style.display == "block"){
						ThisPopup.style.display = 'none';
				}
			}
		}
}




function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


addLoadEvent(doPopup);
