function getXmlHttp() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	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;
			}
		}	
	}
	return xmlHttp;
}

function ge(elem) {
  return document.getElementById(elem);
}

function sort_category(category,sortBy,order) {
	var xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange=function() {
	    if(xmlHttp.readyState==4) {
			document.getElementById('notepad_large_body').innerHTML = xmlHttp.responseText;
			document.getElementById('category_loader').style.display = 'none';
			linkTableRows();
		} else {
			var divHeight = (document.getElementById('notepad_large_body').offsetHeight) - 70;
			document.getElementById('category_loader').style.height = divHeight + "px";
			document.getElementById('category_loader').style.display = 'block';
		}
	}
	var params = 'category=' + category + '&sortBy=' + sortBy + '&order=' + order;
	xmlHttp.open("POST","/_inc/sort.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function deleteRecipe(id) {
	$.ajax({
		type: "POST",
		url: "/my_account/delete_recipe.php",
		data: "id="+id,
		success: function() {
			$("#myAccountResult").html("<p>Recipe deleted.</p>");
			$("#row_"+id).fadeOut("slow");
			showResult();
		}
	});
}

function showResult() {
	$("#myAccountResult").slideDown("slow");
	$("#myAccountResult").animate({opacity: 1.0}, 3000)
	$("#myAccountResult").slideUp("slow");
}

function flagRecipe() {
	$('#flagBox').slideDown("slow");
}

function submitFlag(id) {
	var desc = $('#flagDesc').val();
	$.ajax({
		type: "POST",
		url: "/_inc/flag_recipe.php",
		data: "desc="+desc+"&id="+id,
		complete: function() {
			$('#flagBox').html("<p>Thank you</p>");
		}
	});
}


function postComment(recipe_id) {
	var name = document.getElementById('name').value;
	var comment = document.getElementById('comment_text').value;
	var captcha_code = document.getElementById('captcha_code').value;
	
	var xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange=function() {
	    if(xmlHttp.readyState==4) {
			document.getElementById('comments').innerHTML = xmlHttp.responseText;
			document.getElementById('captcha').src ='/_inc/securimage_show.php?' + Math.random();
			document.getElementById('comments_loader').style.display = 'none';
		} else {
			var divHeight = (document.getElementById('notepad_large_body_yellow').offsetHeight - 55);
			var width = document.getElementById('notepad_large_body_yellow').style.width;
			document.getElementById('comments_loader').style.height = divHeight + "px";
			document.getElementById('comments_loader').style.display = 'block';
		}
	}
	var params = 'name=' + name + '&comment=' + comment + '&captcha_code=' + captcha_code + '&recipe_id=' + recipe_id;
	xmlHttp.open("POST","/_inc/add_comment.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function linkTableRows() {
	var tr = getElementsByClass('row');
	for(var i = 0; i < tr.length; i++) {
		var a = tr[i].getElementsByTagName('a')[0];
		tr[i].setAttribute('href', a.href);
		tr[i].onclick = function(){window.location = this.getAttribute('href')};
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function remIng(id) {
	$("#ing_"+id).remove();
	$('#ings').attr("value",$('#ings').attr("value").replace(","+id,""));
	
	if ($("#ingredients > span:first").html() != null) {
		if ($("#ingredients > span:first").html().substr(0,2) == ", ") {
			$("#ingredients > span:first").html($("#ingredients > span:first").html().substr(2));
		}
	}
}

function clear() {
	$('#ings').attr("value","");
	$('#ingredients').html("");
}

function addContact() {
	var name = $("#name").attr("value");
	var email = $("#email").attr("value");
	var message = $("#message").val(); 
	
	if (name == "" || email == "" || message == "") {
		$("#addResult").html("<p>There was a problem processing the data. Please ensure you filled in all fields correctly.</p>");
		$("#addResult").slideDown("slow");
		$("#addResult").animate({opacity: 1.0}, 3000)
		$("#addResult").slideUp("slow");
		return false;
	} else {
		$.ajax({
			type: "POST",
			url: "/misc/sendcontact.php",
			data: "name="+name+"&email="+email+"&message="+message,
			success: function() {
					$("#addResult").html("<p>Thank you. We will respond to your query as soon as possible.</p>");
					$("#addResult").slideDown("slow");
			        $("#addResult").animate({opacity: 1.0}, 3000)
			        $("#addResult").slideUp("slow");
			}
		});
		return false;
	}
}


