var poll = null;
var recipes_title = null;
var recipes_content = null;

function addPrintEvent(){
	var recipescontent = document.getElementById('recipescontent');
	if(recipescontent){
	recipes_title = recipescontent.getElementsByTagName('dt');
	recipes_content = recipescontent.getElementsByTagName('dd');

	var anchors = document.getElementsByTagName('a');
	for(var i=0; i<anchors.length; i++){
		if(anchors[i].className=='print'){
			anchors[i].onclick = function(){
				addClassNoPrint(recipes_content,'noprint content');
				addClassNoPrint(recipes_title,'noprint title');
				var parent = this.parentNode;
				var sib = parent.previousSibling;
				while(sib != null && sib.tagName != 'DT'){
					sib = sib.previousSibling;
				}
				
				var parentclass = get_Class(parent);
				var sibclass = get_Class(sib);

				if(parentclass.match(/noprint/)){
					updateClass(parent,'');
				}

				if(sibclass.match(/noprint/)){
					updateClass(sib,'');
				}
				window.print()
				return false;
			}
		}
	}
	}
}

function addClassNoPrint(objarray,str){
	for(var i=0; i<objarray.length; i++){
		if(objarray[i].parentNode.getAttribute('id') == 'recipescontent'){
			//console.log(i);
			updateClass(objarray[i],str)
		}
	}
}

function removeClassNoPrint(objarray){
	for(var i=0; i<objarray.length; i++){
		var classname = get_Class(objarray[i]);
		if(classname.match(/noprint/)){
			if(objarray[i].parentNode.getAttribute('id') == 'recipescontent'){
				updateClass(objarray[i],'')
			}
		}
	}
}

function updateClass(obj,val){
	obj.setAttribute('className',val);
	obj.setAttribute('class',val);
}

function get_Class(obj){
	return obj.getAttribute('className')?obj.getAttribute('className'):obj.getAttribute('class');
}

function addPollEvent(){
	poll = document.getElementById('poll');
	if(poll && poll.tagName=='FORM'){
		poll.onsubmit = function (){
			var weekfav = null;
			var inputs = this.getElementsByTagName('input');
			for(var i=0; i<inputs.length; i++){
				if(inputs[i].getAttribute('name')=='weekfav' && inputs[i].checked){
					weekfav = 'weekfav='+escape(encodeURI(inputs[i].getAttribute('value')));
				}
			}
			ajaxFunction(weekfav);
			return false;
		}
	}
}

function ajaxFunction(parameters)
{
if(parameters != null){

var xmlHttp;
try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    if (xmlHttp.overrideMimeType) {
	  // set type accordingly to anticipated content type
	  //http_request.overrideMimeType('text/xml');
	  xmlHttp.overrideMimeType('text/html');
    }
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function(){
	if(xmlHttp.readyState==4){
		poll.innerHTML = xmlHttp.responseText;
	}
  }

  xmlHttp.open('POST','index.php?action=ajax',true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", parameters.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(parameters);

}
}

addLoadEvent(addPrintEvent);
addLoadEvent(addPollEvent);

